def user_similarity(self): all_users = [u[2] for u in self.importer.import_user()] try: similar_users = self.user['similar_users'] except KeyError as exc: logger.log_error("""User with id {} doesn't have seimilar_users key.\n {}""".format(self.user['id'], exc)) return else: for uid, d in similar_users.items(): sim = d['similarity'] # productions that exist in User product but not in similar # user with 'uid' user id. diff = d['diff'] user = next(u for u in all_users if u['id'] == uid) # todo: user_products should be merge with pre_analyzed products in order # to have the similar products too user_products = user['products'] for product in user_products: aff = product['affinity'] product = next(p for p in self.all_products if p['id'] == product['id']) similarity = self.independent_product_similarity( self.user, aff, product, sim, diff) self.user_profile['user_similarity']['level_1'][ product['id']] = similarity for pid, d in product['sim_products'].items(): sim_ = d['similarity'] self.user_profile['user_similarity']['level_2'][ pid] = sim_ * similarity
def find_similar_products(self, product): all_products = self.importer.import_prouct() try: sim_products = next(p['similar_products'] for p in all_products if p['id'] == product['id']) except Exception as exc: logger.log_error(exc) else: return sim_products.items()
def create_client(self): try: config = { 'hosts': [('127.0.0.1', 3000)] } client = aerospike.client(config).connect() except Exception as e: logger.log_error("error: {0}".format(e)) raise else: return client
def get_latest_products(self, user): all_products = user['products'] for product in all_products: try: status_date = datetime.strptime(product['status_date'], self.time_format) except Exception as exc: logger.log_error(exc) else: if (datetime.now() - status_date) < timedelta(days=self.recent_date_number): yield product
def product_similarity(self, user=None): user = user or self.user self.all_products = [p[2] for p in self.importer.import_product()] # User product is contain {id, status, activity, status_date } user_products = user['products'] for product in user_products: try: similar_products = next(p['sim_products'] for p in self.all_products if p['id'] == product['id']) except StopIteration as exc: logger.log_error("""Product with id {} Error: {}. \n""".format(product['id'], exc)) else: for aff, pid in self.cal_similar_product_affinity(product, similar_products): self.user_profile['product_similarity'][pid] = aff