Ejemplo n.º 1
0
def filter_res(customer, reviews, token):
    fav_res = get_fav_res(customer, reviews)
    res_top = restaurant_top(reviews, fav_res, token)
    filtered_res = []

    res_body = Restaurants.by_id(fav_res, token)
    menu_fav = res_body[Constants.DETAILS][Constants.SPECIALS]

    for provider in res_top:
        spec = provider[Constants.DETAILS][Constants.SPECIALS]
        for item in menu_fav:
            if spec.count(item):
                filtered_res.append(provider[Constants.ID])

    res_dic = score_mean(reviews, list(set(filtered_res)))
    sorted_top = sorted(res_dic, key=lambda item: res_dic[item], reverse=True)

    if len(sorted_top) == 0:
        return res_top

    rtopbody = []
    res_top = sorted_top[0:9]
    for r in res_top:
        rtopbody.append(Restaurants.by_id(r, token))
    return rtopbody
Ejemplo n.º 2
0
 def test_raise_exception(self):
     token = TestConstants.REST_RECOMM_TOKEN
     decoded = decode(token, Constants.SECRET)
     customer_id = decoded['_id']
     print(customer_id)
     #self.assertRaises(ValueError, final, customer_id, Reviews.all(token), Restaurants.all(token))
     self.assertRaises(ValueError, final, {"_id": '1234'},
                       Reviews.all(token), Restaurants.all(token))
Ejemplo n.º 3
0
    def test_recommendation_output(self):
        token = TestConstants.REST_RECOMM_TOKEN
        decoded = decode(token, Constants.SECRET)
        customer_id = decoded['_id']

        restaurant_id = final(Customers.by_id(customer_id, token),
                              Reviews.all(token), Restaurants.all(token))
        print(restaurant_id)
        self.assertEqual(TestConstants.REST_RECOMM_OUTPUT, restaurant_id)
Ejemplo n.º 4
0
    def test_recommendation_output(self):
        token = TestConstants.REST_FOOD_FOR_REST_TOKEN
        orders = Orders.all(token)
        restaurant_id =Restaurants.by_id("5ebcf11126e32517c46effff", token)
        self.assertEqual(TestConstants.REST_FOOD_FOR_REST_OUTPUT, final(restaurant_id, orders))


#127.0.0.1:5000/recommendations/food/5ebcf11126e32517c46effff
#token: eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJfaWQiOiI1ZWIxNmZkZjRhZmJmNjU0OTY2Y2I2OGQiLCJpYXQiOjE1ODgyMzc0NTZ9.OG3o5XPIDDGlyFusinKVN11w27b5JYCSwLMl9XhYHeI
 def test_recommendation_output1(self):
     token = TestConstants.SEARCH_BY_REST_TOKEN
     decoded = decode(token, Constants.SECRET)
     customer_id = decoded['_id']
     restaurant_prefix = "Ram"
     orders = Orders.all(token)
     reviews= Reviews.by_customer_id(customer_id, token)
     restaurants = Restaurants.all(token)
     self.assertEqual("5ebcf11126e32517c46effff",
     final(reviews, restaurants, orders, customer_id, restaurant_prefix, token)[0]["id"])
Ejemplo n.º 6
0
def main(customer_id, token):
    try:
        restaurants_id = final(Customers.by_id(customer_id, token),
                               Reviews.all(token), Restaurants.all(token))
        restaurants_data = map(lambda e: Restaurants.by_id(e, token),
                               restaurants_id)

    except Exception as error:
        answer = dict({
            Constants.SUCCESS: "false",
            Constants.ERROR: error.__str__()
        })
        return answer

    answer = dict({
        Constants.SUCCESS: "true",
        Constants.DATA: restaurants_data
    })
    return answer
Ejemplo n.º 7
0
def restaurant_top(reviews, fav_res, token):

    cluster = get_cluster(reviews, fav_res)
    restaurants = []
    for customer_id in cluster:
        for review in reviews:
            if review[Constants.REVIEWER_ID] == customer_id and review[
                    Constants.SCORE] > 0 and review[
                        Constants.PROVIDER_ID] != fav_res:
                restaurants.append(review[Constants.PROVIDER_ID])

    rset = list(set(restaurants))
    rset_body = []
    for r in rset:
        rset_body.append(Restaurants.by_id(r, token))

    return rset_body
Ejemplo n.º 8
0
def main(customer_id, restaurant_prefix, token):

    try:
        restaurants_data = final(Reviews.by_customer_id(customer_id, token),
                                 Restaurants.all(token), Orders.all(token),
                                 customer_id, restaurant_prefix, token)
    except Exception as error:
        answer = dict({
            Constants.SUCCESS: "false",
            Constants.ERROR: error.__str__()
        })
        return answer

    answer = dict({
        Constants.SUCCESS: "true",
        Constants.DATA: restaurants_data
    })
    return answer
Ejemplo n.º 9
0
def main(restaurant_id, token):
    try:
        orders = Orders.all(token)
        orders_with_userid = []
        for order in orders:
            if 'userId' in order:
                orders_with_userid.append(order)
        foods_ids = final(Restaurants.by_id(id=restaurant_id, token=token),
                          orders_with_userid)
        foods_data = map(lambda e: Foods.by_id(e, token), foods_ids)

    except Exception as error:
        answer = dict({
            Constants.SUCCESS: "false",
            Constants.ERROR: error.__str__()
        })
        return answer

    answer = dict({Constants.SUCCESS: "true", Constants.DATA: foods_data})
    return answer
Ejemplo n.º 10
0
def final(reviews, restaurants, orders, customer_id, restaurant_prefix, token):
    rest_array = []
    if len(customer_id) != Constants.OBJECT_ID_LENGTH:
        raise ValueError("the customer id must be 24 characters long")
    t = Trie()
    update_trie(t, restaurants, reviews, orders, customer_id, token, rest_array)

    rest_array = sorted(rest_array, key=lambda k: k[Constants.SCORE])
    final_result = []
    for rest in rest_array:
        name=get_restaurant_name(restaurants, rest[Constants.DICT_REST_ID], token)
        print(name)
        if name.startswith(restaurant_prefix):
            print(name)
            restaurant = Restaurants.by_id(rest[Constants.DICT_REST_ID], token)
            final_result.append(restaurant)

    if len(final_result) == 0:
        raise Exception("Recommendation starting with given prefix not found")
    return final_result