def test_of_user_data(): session = DBManager.get_session() user_list = session.query(User).all() assert len(user_list) == 200
def test_cb_recommend(): ''' Everytime client ask for recommendation will invoke this method, return a python object ''' db_path = "./test.sqlite" # If db created then connect, otherwise, load test data. if os.path.exists(db_path): DBManager.init_db(db_name='test', is_echo=False) else: DBManager.init_db(db_name='test', is_echo=False) Loader.load(file_path='./tests/food.json') Loader.load(file_path='./tests/user.json') Loader.load(file_path='./tests/review.json') user_id = 20 session = DBManager.get_session() food = pd.read_sql(session.query(Food).statement, session.bind).rename(columns={'id': 'food_id'}) review = pd.read_sql( session.query(UserRecommendationReview).filter( user_id == UserRecommendationReview.user_id).statement, session.bind) review['is_accept'] = np.where(review['is_accept'] == True, 1, 0) review = review.groupby([ 'food_id' ])['is_accept'].mean().reset_index().sort_values(by=["is_accept"], ascending=False) final_recommendation = _recommend_food(review, food) print(final_recommendation)
def recommend( self, user=None, *args, **kwargs ) -> 'Food[] , high recommendation prority in lower index ': ''' Everytime client ask for recommendation will invoke this method, return a python object ''' user_id = user.id session = DBManager.get_session() food = pd.read_sql(session.query(Food).statement, session.bind).rename(columns={'id': 'food_id'}) review = pd.read_sql( session.query(UserRecommendationReview).filter( user_id == UserRecommendationReview.user_id).statement, session.bind) review['is_accept'] = np.where(review['is_accept'] == True, 1, 0) review = review.groupby([ 'food_id' ])['is_accept'].mean().reset_index().sort_values(by=["is_accept"], ascending=False) final_recommendation = self._recommend_food(review, food) food_list = self._id_to_food_list(session, final_recommendation) return food_list
def test_of_food_data(): session = DBManager.get_session() food_list = session.query(Food).order_by(Food.id.asc()).all() assert len(food_list) == 295
def recommend(self, max_lenght=10, *args, **kwargs): session = DBManager.get_session() avaiable_foods = session.query(Food).all() random.seed(random.randint(0, 999)) if not avaiable_foods: return list() return [random.choice(avaiable_foods) for _ in range(max_lenght)]
def recommend(self, user=None): if self.first_time: self.first_time = False try: self.requirement = user.basal_metabolic_rate except: pass self.min_cal, self.max_cal = self.requirement * 0.15, self.requirement * 0.6 self.session = DBManager.get_session() self.foodlist = self.session.query(Food).filter( self.min_cal < Food.calories).filter( Food.calories < self.max_cal).order_by( Food.calories.asc()).all() self.target_cal = self.requirement * 4.8 else: # update a_dict_take_or_not, 第二次以後被呼叫才會計算 Last_review = self.session.query( UserRecommendationReview).order_by( UserRecommendationReview.created_datetime.desc()).first() food_taken_id = Last_review.food_id for food in self.list: if food.id not in self.a_dict_take_or_not: self.a_dict_take_or_not[food.id] = [0, 0] self.a_dict_take_or_not[food.id][0] += 1 if food.id == food_taken_id: cal = food.calories self.a_dict_take_or_not[food.id][1] += 1 break # update last_requirement = self.cal_requirement(self.time - 1) try: self.target_cal += cal - last_requirement except: self.target_cal += self.session.query(Food).filter( Food.id == food_taken_id).first().calories - last_requirement # 開始處理 requirement = self.cal_requirement(self.time) this_meal_target = int(self.target_cal / self.meals_left) r_list = self.recommend_food_with_possibility(this_meal_target, requirement, self.foodlist) self.meals_left -= 1 self.time += 1 self.list = r_list return r_list
def test_of_food_data(): session = DBManager.get_session() food_list = session.query(Food).order_by(Food.id.asc()).all() assert len(food_list) == 295 user_list = session.query(User).all() assert len(user_list) == 201 review_list = session.query(UserRecommendationReview).all() assert len(review_list) > 0
def prepare_recommedation_foods(self): try: self.requirement = user.basal_metabolic_rate except: pass self.min_cal, self.max_cal = self.requirement * 0.15, self.requirement * 0.6 self.session = DBManager.get_session() self.foodlist = self.session.query(Food).filter( self.min_cal < Food.calories).filter( Food.calories < self.max_cal).order_by( Food.calories.asc()).all() self.lose_one_kg_scedule()
def _login(self): session = DBManager.get_session() while True: print('\nRandom User List') for user in random.choices(session.query(User).all(), k=10): print(user) username = Cmd.get_input('Please enter user name to login') user = session.query(User).filter( User.name == username).one_or_none() if user is None: print('User not found !') else: self.application.login(user) break
def test_recommend(): ''' Everytime client ask for recommendation will invoke this method, return a python object ''' db_path = "./test.sqlite" # If db created then connect, otherwise, load test data. if os.path.exists(db_path): DBManager.init_db(db_name='test', is_echo=False) else: DBManager.init_db(db_name='test', is_echo=False) Loader.load(file_path='./tests/food.json') Loader.load(file_path='./tests/user.json') Loader.load(file_path='./tests/review.json') # Setup session and tables session = DBManager.get_session() food = pd.read_sql(session.query(Food).statement,session.bind).rename(columns={'id': 'food_id'}) user = pd.read_sql(session.query(User).statement,session.bind).rename(columns={'id': 'user_id'}) review = pd.read_sql(session.query(UserRecommendationReview).statement,session.bind) # Data processing & prepare review['is_accept'] = np.where(review['is_accept']==True, 1, -1) review = review.groupby(['user_id','food_id'])['is_accept'].sum().reset_index() rating_pivot = review.pivot(index='user_id',columns = 'food_id',values='is_accept').fillna(0) rating_df = rating_pivot.as_matrix() user_ratings_mean = np.mean(rating_df, axis = 1) r_demeaned = rating_df - user_ratings_mean.reshape(-1, 1) # Matrix factorization U, sigma, Vt = svds(r_demeaned, k = 50) sigma = np.diag(sigma) prediction_all= np.dot(np.dot(U, sigma), Vt) + user_ratings_mean.reshape(-1, 1) prediction_df = pd.DataFrame(prediction_all, columns = rating_pivot.columns) # Recommend top 10 food that the user hasn't been recommended to recommendation_list = recommend_food(prediction_df, 130, food, review, 10) print(recommendation_list) result = session.query(Food).filter(Food.id.in_(recommendation_list)).all() food_map = {food.id: food for food in result} food_list = [food_map[id] for id in recommendation_list] for food in food_list: food.show_detail()
def _find_similar_food(self, food_id, food, positive=1) -> 'food.id[]': try: count = CountVectorizer() count_matrix = count.fit_transform(food['name']) cosine_sim = cosine_similarity(count_matrix, count_matrix) if positive == 1: score_series = pd.Series( cosine_sim[food_id]).sort_values(ascending=False) else: # finds the least similar score_series = pd.Series( cosine_sim[food_id]).sort_values(ascending=True) similar_food = list(score_series.iloc[1:11].index) return similar_food except Exception as e: session = DBManager.get_session() random_food = random.choice(session.query(Food).all()) return list([random_food.id])
def recommend( self, user=None, *args, **kwargs ) -> 'Food[] , high recommendation prority in lower index ': ''' Everytime client ask for recommendation will invoke this method, return a python object ''' user_id = user.id session = DBManager.get_session() food = pd.read_sql(session.query(Food).statement, session.bind).rename(columns={'id': 'food_id'}) user = pd.read_sql(session.query(User).statement, session.bind).rename(columns={'id': 'user_id'}) review = pd.read_sql( session.query(UserRecommendationReview).statement, session.bind) # Data processing & prepare review['is_accept'] = np.where(review['is_accept'] == True, 1, -1) review = review.groupby(['user_id', 'food_id'])['is_accept'].sum().reset_index() rating_pivot = review.pivot(index='user_id', columns='food_id', values='is_accept').fillna(0) # Turn review to ratings matrix rating_df = rating_pivot.as_matrix() user_ratings_mean = np.mean(rating_df, axis=1) r_demeaned = rating_df - user_ratings_mean.reshape(-1, 1) # Matrix factorization U, sigma, Vt = svds(r_demeaned, k=10) sigma = np.diag(sigma) prediction_all = np.dot(np.dot(U, sigma), Vt) + user_ratings_mean.reshape(-1, 1) prediction_df = pd.DataFrame(prediction_all, columns=rating_pivot.columns) recommendation_list = self._recommend_for_user(prediction_df, user_id, food, review) food_list = self._id_to_food_list(session, recommendation_list) return food_list
def recommend(self, user=None, *args, **kwargs): if self.first_time: self.first_time = False try: self.requirement = user.basal_metabolic_rate except: pass self.min_cal, self.max_cal = self.requirement * 0.15, self.requirement * 0.6 self.session = DBManager.get_session() self.foodlist = self.session.query(Food).filter( self.min_cal < Food.calories).filter( Food.calories < self.max_cal).order_by( Food.calories.asc()).all() self.lose_one_kg_scedule() r_f = self.path.pop(0) return [r_f]
def food_name_pattern_dict(self) -> 'dict(pattern : count_time)': ''' Put all element of Food.name in a dict (self._pattern) and the value is the referenced time ''' if any(self._pattern): return self._pattern session = DBManager.get_session() foods = session.query(Food).all() for food in foods: name_seq = food.name.lower().split(' ') for pattern in name_seq: if pattern in [ '!', '@', '#', '$', '%', '^', '&', '*', '(', ')' ]: continue if pattern in self._pattern: self._pattern[pattern] += 1 else: self._pattern[pattern] = 1 return self._pattern
def test_proxy_run(): Loader.load(file_path=os.path.join(os.getcwd(), 'data', 'user.json')) Loader.load(file_path=os.path.join(os.getcwd(), 'data', 'food.json')) Loader.load(file_path=os.path.join(os.getcwd(), 'data', 'review.json')) session = DBManager.get_session() foods = session.query(Food).all() random_user_name = ''.join( [random.choice(string.ascii_lowercase) for _ in range(10)]) user = User(name=random_user_name, weight=70, height=170).save() application = Application(db_is_echo=False) user_proxy = UserProxyCollection.default_proxy( user=user, diet_schedule=DietScheduleCollection.defautl_schedule, application=application) pickup_food_times = 30 for _ in range(pickup_food_times): user_proxy.choice_food() user_proxy.report()
def __init__(self, db_name='db', *args, **kwrags): if db_name is None or not isinstance(db_name, str): raise Exception('please provide DB name to init service') self.db_name = db_name DBManager.init_db(db_name=db_name, is_echo=kwrags.get('db_is_echo', True)) self.session = DBManager.get_session()