コード例 #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
コード例 #2
0
    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
コード例 #3
0
    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)
コード例 #4
0
    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
コード例 #5
0
    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
コード例 #6
0
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
コード例 #7
0
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)
コード例 #8
0
 def get(self):
     db = Database()
     result = db.findAllRatings()
     return result
コード例 #9
0
 def post(self):
     args = parser.parse_args()
     db = Database()
     user = db.insertUser(args.email)
     return json.dumps(user)