| 20080 |
kshitij.so |
1 |
from walrus import *
|
|
|
2 |
from dtr.utils.utils import get_mongo_connection, REVERSE_SOURCE_MAP, CATEGORY_MAP, SUB_CATEGORY_MAP
|
|
|
3 |
import random
|
|
|
4 |
|
|
|
5 |
xstr = lambda s: s or ""
|
|
|
6 |
database = Database()
|
|
|
7 |
ac = database.autocomplete()
|
|
|
8 |
|
|
|
9 |
class Suggestion():
|
| 20089 |
kshitij.so |
10 |
def __init__(self, category_id, subCategoryId, suggestion, word, category_name):
|
| 20080 |
kshitij.so |
11 |
self.category_id = category_id
|
|
|
12 |
self.subCategoryId = subCategoryId
|
|
|
13 |
self.suggestion = suggestion
|
|
|
14 |
self.word = word
|
| 20089 |
kshitij.so |
15 |
self.category_name = category_name
|
| 20080 |
kshitij.so |
16 |
|
|
|
17 |
|
|
|
18 |
def filterSuggestions(categorySuggestion):
|
|
|
19 |
returnObj = []
|
|
|
20 |
for cat in ('Mobiles','Tablets'):
|
|
|
21 |
v = categorySuggestion.get(CATEGORY_MAP.get(cat))
|
|
|
22 |
if v is not None:
|
|
|
23 |
returnObj.append(v[0].__dict__)
|
|
|
24 |
select_range = 2 if len(v[1:]) > 2 else len(v[1:])
|
|
|
25 |
random_list = random.sample(set(v[1:]), select_range)
|
|
|
26 |
for random_suggestion in random_list:
|
|
|
27 |
returnObj.append(random_suggestion.__dict__)
|
|
|
28 |
categorySuggestion.pop(CATEGORY_MAP.get(cat))
|
|
|
29 |
for v in categorySuggestion.itervalues():
|
|
|
30 |
returnObj.append(v[0].__dict__)
|
|
|
31 |
select_range = 2 if len(v[1:]) > 2 else len(v[1:])
|
|
|
32 |
random_list = random.sample(set(v[1:]), select_range)
|
|
|
33 |
for random_suggestion in random_list:
|
|
|
34 |
returnObj.append(random_suggestion.__dict__)
|
|
|
35 |
return returnObj
|
|
|
36 |
|
|
|
37 |
def getSuggestions(search_text):
|
|
|
38 |
categorySuggestion = {}
|
|
|
39 |
results = list(ac.search(search_text))
|
| 20085 |
kshitij.so |
40 |
returnObj = []
|
| 20080 |
kshitij.so |
41 |
if len(results) > 10:
|
|
|
42 |
"""Lets group data"""
|
|
|
43 |
for i in results:
|
|
|
44 |
if i.get('subCategoryId'):
|
|
|
45 |
if not categorySuggestion.has_key(i.get('subCategoryId')):
|
| 20089 |
kshitij.so |
46 |
suggestion_obj_primary = Suggestion(i.get('category_id'),i.get('subCategoryId'),search_text, search_text, SUB_CATEGORY_MAP.get(i.get('subCategoryId')))
|
|
|
47 |
suggestion_obj = Suggestion(i.get('category_id'),i.get('subCategoryId'),i.get('title'), i.get('title'), None)
|
| 20080 |
kshitij.so |
48 |
categorySuggestion[i.get('subCategoryId')] = [suggestion_obj_primary,suggestion_obj]
|
|
|
49 |
else:
|
| 20089 |
kshitij.so |
50 |
suggestion_obj = Suggestion(i.get('category_id'),i.get('subCategoryId'),i.get('title'), i.get('title'), None)
|
| 20080 |
kshitij.so |
51 |
categorySuggestion.get(i.get('subCategoryId')).append(suggestion_obj)
|
|
|
52 |
else:
|
|
|
53 |
if not categorySuggestion.has_key(i.get('category_id')):
|
| 20089 |
kshitij.so |
54 |
suggestion_obj_primary = Suggestion(i.get('category_id'),i.get('subCategoryId'),search_text, search_text, CATEGORY_MAP.get(i.get('category_id')), None)
|
|
|
55 |
suggestion_obj = Suggestion(i.get('category_id'),i.get('subCategoryId'),i.get('title'), i.get('title'), None)
|
| 20080 |
kshitij.so |
56 |
categorySuggestion[i.get('category_id')] = [suggestion_obj_primary, suggestion_obj]
|
|
|
57 |
else:
|
|
|
58 |
suggestion_obj = Suggestion(i.get('category_id'),i.get('subCategoryId'),i.get('title'), i.get('title'))
|
|
|
59 |
categorySuggestion.get(i.get('category_id')).append(suggestion_obj)
|
|
|
60 |
|
|
|
61 |
returnObj = filterSuggestions(categorySuggestion)
|
|
|
62 |
|
|
|
63 |
else:
|
|
|
64 |
for i in results:
|
| 20089 |
kshitij.so |
65 |
returnObj.append({'suggestion':i.get('title'),'category_id':i.get('category_id'),'subCategoryId':i.get('subCategoryId'),'word':i.get('title'),'category_name':None})
|
| 20080 |
kshitij.so |
66 |
|
|
|
67 |
return returnObj
|
|
|
68 |
|
|
|
69 |
|
|
|
70 |
def main():
|
|
|
71 |
pass
|
|
|
72 |
|
|
|
73 |
if __name__ == "__main__":
|
|
|
74 |
main()
|
|
|
75 |
|