def get_recommendations(user_id): data, items_length = prepare_data() name_algorithm, algorithm = randomize() trainset = data.build_full_trainset() algo = algorithm algo.fit(trainset) ratings = [] db = Database() places = db.findAllPlaces() formated_ratings = [] for i in range(1, (items_length) + 1): predict = algo.predict(user_id, i, r_ui=4) dictionary = { 'user': user_id, 'item': i, 'est_rating': float(predict.est), 'impossible': predict.details['was_impossible'] } ratings.append(dictionary) formated_ratings = create_dict(ratings, places, name_algorithm) return formated_ratings
def get(self, user_id=None): if not user_id: db = Database() result = db.findAllPlaces() return json.dumps(result) recommendations = get_recommendations(user_id) return recommendations
def get(self, user_id=None): db = Database() if not user_id: result = db.findAllUsers() return json.dumps(result) result = db.findUserById(user_id) return json.dumps(result)
def post(self): args = parser.parse_args() ratings = args.get('ratings') for rtUser in ratings: rtUserDict = ast.literal_eval(rtUser) db = Database() db.insertRating(rtUserDict['userId'], rtUserDict['placeId'], rtUserDict['rate']) return
def post(self): args = parser.parse_args() ratings = args.get('ratings') for rtUser in ratings: rtUserDict = ast.literal_eval(rtUser) db = Database() db.insertSugestion(rtUserDict['userId'], rtUserDict['placeId'], rtUserDict['rate_prevision'], rtUserDict['rate_user'], rtUserDict['algorithm']) return
def get_top_n_recommendations(user_id, n): data, items_length = prepare_data() # Modelo de recomendação trainset = data.build_full_trainset() algo = SVD() algo.fit(trainset) ratings_pre = [] ratings = [] for i in range(1, (items_length) + 1): predict = algo.predict(user_id, i, r_ui=4) if not predict.details['was_impossible']: dictionary = { 'user': user_id, 'item': i, 'rating': float(predict.est) } ratings_pre.append(dictionary) ratings_pre = sorted(ratings_pre, key=lambda x: x['rating'], reverse=True) for i in range(0, n): ratings.append(ratings_pre[i]) places = [] db = Database() for place in ratings: places.append(db.findByIdPlaces(place['item'])) listRecommendations = [] for i in range(0, n): dict_ = { 'userId': ratings[i]['user'], 'name': places[i]['name'], 'photoUrl': places[i]['photoUrl'], 'rate_prevision': round(ratings[i]['rating'], 2), 'rate_user': 0 } listRecommendations.append(dict_) return listRecommendations
def prepare_data(): db = Database() data_pre = db.findAllRatings() user_id = [] item_id = [] rating = [] for row in data_pre: user_id.append(row['user_id']) item_id.append(row['place_id']) rating.append(row['rate']) ratings_dict = {'itemID': item_id, 'userID': user_id, 'rating': rating} df = pd.DataFrame(ratings_dict) reader = Reader(rating_scale=(1, 5)) data = Dataset.load_from_df(df[['userID', 'itemID', 'rating']], reader) items_length = len(df['itemID'].unique()) return (data, items_length)
def get(self): db = Database() result = db.findAllRatings() return result
def post(self): args = parser.parse_args() db = Database() user = db.insertUser(args.email) return json.dumps(user)