-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrecommendation.py
371 lines (311 loc) · 12.5 KB
/
recommendation.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
from functools import cached_property
from mock_service import ALL_ORDERS, RESTAURANTS, NEW_TOP_RATING_RESTAURANTS
class CostTracking:
def __init__(self, cost_type: str, orders_count: int) -> None:
"""
:param cost_type: type of cost bracket cheapest to costly (1,5)
:param orders_count: count of orders
"""
self.type = cost_type
self.no_of_orders = orders_count
class CuisineTracking:
def __init__(self, cuisine_type: str, orders_count: int) -> None:
"""
:param cuisine_type: type of Cuisine (SouthIndian, NorthIndian, Chinese, Continental)
:param orders_count: count of orders
"""
self.type = cuisine_type
self.no_of_orders = orders_count
class User:
def __init__(self, cost_tracking: list = [], cuisine_tracking: list = []) -> None:
"""
:param cost_tracking: list of user's costs tracking
:param cuisine_tracking: list of user's cuisines tracking
"""
self.costTracking = cost_tracking
self.cuisineTracking = cuisine_tracking
@cached_property
def primary_cuisines(self) -> list:
"""
user's primary cuisine based on no_of_orders
"""
return self.get_primary_type(self.cuisineTracking)
@property
def secondary_cuisines(self) -> list:
"""
user's secondary cuisines based on no_of_orders
"""
return [
cuisine.type
for cuisine in self.cuisineTracking
if cuisine.type not in self.primary_cuisines
]
@cached_property
def primary_cost_brackets(self) -> list:
"""
user's primary cost brackets based on no_of_orders
"""
return self.get_primary_type(self.costTracking)
@property
def secondary_cost_brackets(self) -> list:
"""
user's secondary cost brackets based on no_of_orders
"""
return [
cost_bracket.type
for cost_bracket in self.costTracking
if cost_bracket.type not in self.primary_cost_brackets
]
@staticmethod
def get_primary_type(records):
"""
return primary type based on count of orders
:param records: list of cuisines or cost bracket
:return: primary type
"""
primary = None
index = 0
max_no_of_orders = 0
while index < len(records):
if records[index].no_of_orders > max_no_of_orders:
primary = records[index]
max_no_of_orders = records[index].no_of_orders
index += 1
return [primary.type]
class UsersOrderService:
def __init__(self, user_id) -> None:
self.user_id = user_id
self.user_orders = []
self.cuisine_tracking = []
self.cost_tracking = []
def populate_user_orders(self):
"""
populates user's orders from all orders
"""
for val in ALL_ORDERS.values():
if val["userId"] == self.user_id:
self.user_orders.append(val)
return self
def populate_user_cuisines(self):
"""
populates user's cuisines tracking from user's orders
"""
if self.user_orders:
cuisines = {}
for v in self.user_orders:
if v["cuisine"] in cuisines.keys():
cuisines[v["cuisine"]] += 1
else:
cuisines.update({v["cuisine"]: 1})
self.cuisine_tracking = [
CuisineTracking(cuisine_type=cuisine_type, orders_count=orders_count)
for cuisine_type, orders_count in cuisines.items()
]
return self
def populate_user_cost_bracket(self):
"""
populates user's cost tracking from user's orders
"""
if self.user_orders:
cost_bracket = {}
for v in self.user_orders:
if v["costBracket"] in cost_bracket.keys():
cost_bracket[v["costBracket"]] += 1
else:
cost_bracket.update({v["costBracket"]: 1})
self.cost_tracking = [
CostTracking(cost_type=cost_type, orders_count=orders_count)
for cost_type, orders_count in cost_bracket.items()
]
return self
def get_user_details(self):
self.populate_user_orders().populate_user_cuisines().populate_user_cost_bracket()
return User(
cost_tracking=self.cost_tracking, cuisine_tracking=self.cuisine_tracking
)
class UserRestaurantRecommendation:
LOGIC_TYPES = [
"get_featured_restaurants",
"get_high_ratings_restaurants",
"get_new_created_restaurants",
"get_low_rating_restaurants",
"get_all_restaurants",
]
def __init__(self) -> None:
self.user = None
self.available_restaurants = {}
def get_restaurant_recommendations(
self, user_instance: User, available_restaurants: dict
) -> list:
"""
Get final recommendation ordered list of restaurants using set of
multiple logics
:param available_restaurants: list of available restaurants
:param user_instance: User instance
:return: list of restaurants
"""
self.user = user_instance
self.available_restaurants = available_restaurants
recommendations_priorities = [
getattr(UserRestaurantRecommendation, f"{logic}")(self)
for logic in self.LOGIC_TYPES
]
return self.get_ordered_unique_recommendation(recommendations_priorities)
def get_featured_restaurants(self) -> list:
"""
Get featured restaurants of primary cuisine and primary cost bracket.
If none, then all featured restaurants of primary cuisine, secondary cost
and secondary cuisine, primary cost
:return: list of restaurants
"""
recommendations = []
for restaurant_id, v in self.available_restaurants.items():
if all(
[
(v["cuisine"] in self.user.primary_cuisines),
(v["costBracket"] in self.user.primary_cost_brackets),
(v["isRecommended"]),
(int(restaurant_id) not in recommendations),
]
):
recommendations.append(int(restaurant_id))
if not recommendations:
recommendations = [[] for _ in range(2)]
for restaurant_id, v in self.available_restaurants.items():
if all(
[
(v["cuisine"] in self.user.primary_cuisines),
(v["costBracket"] in self.user.secondary_cost_brackets),
(v["isRecommended"]),
(int(restaurant_id) not in recommendations[0]),
]
):
recommendations[0].append(int(restaurant_id))
if all(
[
(v["cuisine"] in self.user.secondary_cuisines),
(v["costBracket"] in self.user.primary_cost_brackets),
(v["isRecommended"]),
(int(restaurant_id) not in recommendations[1]),
]
):
recommendations[1].append(int(restaurant_id))
return self.get_ordered_unique_recommendation(recommendations)
else:
return recommendations
def get_high_ratings_restaurants(self) -> list:
"""
Get restaurant using following logics in order:
- All restaurants of Primary cuisine, primary cost bracket with rating >= 4
- All restaurants of Primary cuisine, secondary cost bracket with rating >= 4.5
- All restaurants of secondary cuisine, primary cost bracket with rating >= 4.5
:return: list of restaurants
"""
logic_counts = 3
recommendations = [[] for _ in range(logic_counts)]
for restaurant_id, v in self.available_restaurants.items():
if all(
[
(v["cuisine"] in self.user.primary_cuisines),
(v["costBracket"] in self.user.primary_cost_brackets),
(v["rating"] >= 4),
(int(restaurant_id) not in recommendations[0]),
]
):
recommendations[0].append(int(restaurant_id))
if all(
[
(v["cuisine"] in self.user.primary_cuisines),
(v["costBracket"] in self.user.secondary_cost_brackets),
(v["rating"] >= 4.5),
(int(restaurant_id) not in recommendations[1]),
]
):
recommendations[1].append(int(restaurant_id))
if all(
[
(v["cuisine"] in self.user.secondary_cuisines),
(v["costBracket"] in self.user.primary_cost_brackets),
(v["rating"] >= 4.5),
(int(restaurant_id) not in recommendations[2]),
]
):
recommendations[2].append(int(restaurant_id))
return self.get_ordered_unique_recommendation(recommendations)
def get_new_created_restaurants(self) -> list:
"""
Get Top 4 newly created restaurants by rating
:return: list of restaurants
"""
recommendations = []
for restaurant in NEW_TOP_RATING_RESTAURANTS:
if restaurant["restaurantId"] not in recommendations:
recommendations.append(restaurant["restaurantId"])
return recommendations
def get_low_rating_restaurants(self) -> list:
"""
Get restaurant using following logics in order:
All restaurants of Primary cuisine, primary cost bracket with rating < 4
All restaurants of Primary cuisine, secondary cost bracket with rating < 4.5
All restaurants of secondary cuisine, primary cost bracket with rating < 4.5
:return: list of restaurants
"""
logic_counts = 3
recommendations = [[] for _ in range(logic_counts)]
for restaurant_id, v in self.available_restaurants.items():
if all(
[
(v["cuisine"] in self.user.primary_cuisines),
(v["costBracket"] in self.user.primary_cost_brackets),
(v["rating"] < 4.5),
(int(restaurant_id) not in recommendations[0]),
]
):
recommendations[0].append(int(restaurant_id))
if all(
[
(v["cuisine"] in self.user.primary_cuisines),
(v["costBracket"] in self.user.secondary_cost_brackets),
(v["rating"] < 4.5),
(int(restaurant_id) not in recommendations[1]),
]
):
recommendations[1].append(int(restaurant_id))
if all(
[
(v["cuisine"] in self.user.secondary_cuisines),
(v["costBracket"] in self.user.primary_cost_brackets),
(v["rating"] < 4.5),
(int(restaurant_id) not in recommendations[2]),
]
):
recommendations[2].append(int(restaurant_id))
return self.get_ordered_unique_recommendation(recommendations)
def get_all_restaurants(self) -> list:
"""
Get All restaurants of any cuisine, any cost bracket
:return: list of all restaurants
"""
recommendations = []
for restaurant_id, v in self.available_restaurants.items():
recommendations.append(int(restaurant_id))
return recommendations
@staticmethod
def get_ordered_unique_recommendation(recommendations: list) -> list:
"""
Spread list of lists into single list maintaining order of values
:param recommendations: List of restaurants
:return: list of restaurants
"""
ordered_priorities = []
for rec in recommendations:
for rest in rec:
if rest not in ordered_priorities:
ordered_priorities.append(rest)
return ordered_priorities
user = UsersOrderService(user_id=1).get_user_details()
user_recommendations = UserRestaurantRecommendation()
restaurants_recommendation = user_recommendations.get_restaurant_recommendations(
user_instance=user, available_restaurants=RESTAURANTS
)
print(restaurants_recommendation)