예제 #1
0
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