forked from neerajvashistha/pa-dude
-
Notifications
You must be signed in to change notification settings - Fork 0
/
service_prov_db_handle.py
261 lines (227 loc) · 7.51 KB
/
service_prov_db_handle.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
import json
import pymongo
import serv_decrp,sillybot
import phrase_extract
from pymongo import MongoClient
import re
import ast,math
from collections import defaultdict
def main():
load_JSON_into_Collection("services.json","service_type")
#loc_serv_list = queryServNameLocation(qryd_serv_prvd_list,"Khadi Machine")
#print(loc_serv_list)
#alist1 = queryServName("food","chinese")
#phraseExtracted = someFunctoFetchValue("phraseExtracted")
#print phraseExtracted
#phraseExtracted = ['manchurian','rice']
#command="Haa got you"
#print command
#phraseExtracted = phrase_extract.extract_phrase(command)
#loc_area="katraj"
#print(queryCollection(phraseExtracted,command,loc_area))
#intrmList = []
#intrmList = queryCollection(phraseExtracted,command,loc_area)
#strop = ""
#for i in range(len(intrmList)):
# for k,v in intrmList[i].items():
# strop += str(k)+ " : "+str(v) +"\n"
#print strop
dropCollection("services")
def load_JSON_into_Collection(filename,JSONobj):
'''
param @filename, filename of json file, note: pathname is not validated here for now
param @JSONobj, first JSON obj to be identified by system.
returns NULL
Load and insert JSON file to db.<collection>
'''
try:
page = open(filename, 'r')
parsed = json.loads(page.read())
collection = db[filename.split('.',1)[0]]
for item in parsed[JSONobj]:
collection.insert(item)
except Exception as e:
print("Error has occurred", e)
def queryServName(JSONobj_key1,JSONobj_key2):
'''
param @JSONobj_key1, service type identifier
param @JSONobj_key2, service sub type identifier
returns a list[] with phone, serv_loc, serv_name needs iter to get dict
returns all vendors/service providers not based on location, see also queryServName
'''
#cursor = db.services.find({"food.type":"chinese"},{"food.$":1,"_id":0})
#print JSONobj_key1+".type",JSONobj_key1+".$"
try:
cursor = db.services.find({JSONobj_key1+".type":JSONobj_key2},{JSONobj_key1+".$":1,"_id":0})
alist = []
index = 3
for document in cursor:
alist = document.get(JSONobj_key1)[0].get("serv_prvd")
#for i in range(len(alist)):
# for key,value in alist[i].items():
# print key," : ", value
return alist
except Exception as e:
print("Error has occurred", e)
def queryServNameLocation(alist, location,index_dict):
'''
param @alist, a list with refined vendors/service providers with name, phone, loc
param @location, str with location
return loc_serv_list, a location based vendors/service providers,
with name, phone, loc if @location exists, else returns @alist
'''
try:
#dict = {}
priceList=list()
loc_serv_list =[]
blist=[]
uniloc_serv_list=[]
price =0
for i in alist:
#print i
for k,v in i.items():
if v == location.lower():
loc_serv_list.append(str(i))#stringify the list for the purpose of finding unique list.
#print "\nhi",loc_serv_list
uniloc_serv_list = unique(loc_serv_list)
#print index_dict
#print uniloc_serv_list
compPriceList = []
priceList = []
if len(loc_serv_list) !=0:
for serv_ty,index in index_dict.items():
for i in range(len(uniloc_serv_list)):
for key, value in uniloc_serv_list[i].items():
if value == serv_ty:
priceList.append(lowFucn(uniloc_serv_list[i],index))
#print priceList
temp = priceList[0].get("serv_type")
#print temp
lowPri = 9999
for i in range(len(priceList)):
typz = priceList[i].get("serv_type")
#print typz
if typz in priceList[i].values():
price = priceList[i].get("price")
if price is u"-999":
priceList[i]['price']="Req"
blist.append(priceList[i])
if price<lowPri and temp == typz:
lowPri = price
blist.append(priceList[i])
else:
lowPri=9999
temp = typz
for i in range(len(blist)):
blist[i].pop("serv_type")
if len(uniloc_serv_list) == 0 or len(blist) == 0:
someLst = []
for i in alist:
someLst.append(str(i))# as unique() takes list have strings form dict only
uniqueList = unique(someLst)
#print uniqueList
for serv_ty,index in index_dict.items():
for i in range(len(uniqueList)):
for key, value in uniqueList[i].items():
if value == serv_ty:
priceList.append(lowFucn(uniqueList[i],index))
#print "hi",priceList
temp = priceList[0].get("serv_type")
#print temp
lowPri = 9999
for i in range(len(priceList)):
typz = priceList[i].get("serv_type")
#print typz
if typz in priceList[i].values():
price = priceList[i].get("price")
if price is u"9999":
priceList[i]['price']="Req"
blist.append(priceList[i])
if price<lowPri and temp == typz:
lowPri = price
blist.append(priceList[i])
else:
lowPri=9999
temp = typz
for i in range(len(blist)):
blist[i].pop("serv_type")
if len(blist)==0:
blist = ["Item/Service not avail"]
return [blist[0]]
#return loc_serv_list
except Exception as e:
print("Error has occurred", e)
def lowFucn(adict,alist):
price=0
for key,value in adict.items():
if key =="menuprice":
#print value[alist[0]]
for i in range(len(alist)):
if value[alist[i]]!=0:
price+=value[alist[i]]
else:
price = u"9999"
adict.pop("menuprice")
adict['price'] = price
return adict
def unique(seq):
seen = set()
seen_add = seen.add
alist = [x for x in seq if not (x in seen or seen_add(x))]
#print type(alist)
uniqueList = []
for i in range(len(alist)):
uniqueList.append(ast.literal_eval(alist[i]))
return uniqueList
def queryCollection(itemList,query,location):
'''
param @itemList, item could be any item decribed in serv_decrp like manchurian, or monitor or chinese
return serv_prvd_list, which could be a list of servie providers, or menu list
'''
i=0
serv_prvd_list = []
serv_prvd_list1 = []
index_dict = defaultdict(list)
if len(itemList) !=0:
for item in itemList:
#fetch values from serv_decrp.py for an inquired <item>.
boolItemExist,servItem,servDecrp,indexForMenuprice= serv_decrp.match_serv_menu(item)#change func match_serv_menu
#boolItemExist,servItem,servDecrp could be <true/false>,<food>,<chinese> or<true/false>,<chinese>,<menulist> or none.
if (boolItemExist is False):
#sillybot.load()
stri = sillybot.responds(query)
return stri
#return str("Did you mean "+" ".join(phrase_extract.extract_phrase(query)))
if (boolItemExist is True):
if isinstance(servDecrp, basestring):
#print "\nhe",serv_prvd_list1,indexForMenuprice
serv_prvd_list1 += queryServName(servItem,servDecrp)#change func queryServName
#serv_prvd_list1=[]
#print serv_prvd_list
index_dict[servDecrp].append(indexForMenuprice)
elif isinstance(servDecrp, list):
serv_prvd_list = servDecrp
#print serv_prvd_list1
#print [index_dict]
serv_prvd_list = queryServNameLocation(serv_prvd_list1,location,index_dict)
else:
serv_prvd_list = ["ok"]
return serv_prvd_list
def dropCollection(col_name):
'''
param @col_name, collection name
return NULL
drops collection
'''
try:
db.drop_collection(col_name)
except Exception as e:
print("Error has occurred", e)
client = MongoClient("mongodb://192.168.203.130:27027")
db = client.test
load_JSON_into_Collection("services.json","service_type")
if __name__ == "__main__":
client = MongoClient("mongodb://192.168.203.130:27027")
db = client.test
main()
db.close