-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
113 lines (79 loc) · 2.97 KB
/
main.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
import threading
import time
import ParseData
from YelpFetch import YelpFetcher
from Model import Model
from firebase import FBData
def main():
model = Model(4)
# model.trainModel("official_train.txt")
#print (model.getPrediction("predictdata.txt"))
model.loadModel()
fb = FBData("-KqK2DFRvziwDxcMqXnm")
yelp = YelpFetcher(['breakfast_brunch', 'chinese', 'diners', 'hotdogs', 'hotpot', 'italian', 'japanese', 'korean', 'mongolian', 'pizza', 'steak', 'sushi', 'tradamerican', 'vegetarian'])
done=False
while not done:
time.sleep(1)
print ("waiting")
done=fb.is_done(2)
time.sleep(1)
print ("running")
userRestData, userPriceComp, userCatData, userDelivData = fb.start()
userArr=[]
#userArr contains [categoriesArr, price]
#userRestData firebaserestArry contains [categoriesArr, priceArr,rating, rating #]
#
print ("user rest data: ",userRestData)
userCatComp=[0.0 for x in range(len(userCatData[0]))]
cnt=0.0
for c in userCatData:
for i in range(len(c)):
userCatComp[i] += c[i]
cnt+=1.0
userCatComp = [x/cnt for x in userCatComp]
userArr = [userCatComp, userPriceComp]
yelpRestDict = {} #dict of restaurants to query key: id, value: vector [categoryArr, price, rating, rating #
yelpRestData = yelp.vectors_nearest(37.5737019, -122.3269701, True)
for i in range(len(userCatComp)):
x=userCatComp[i]
if x != 0 : #query restaurants for this category if not zero
yelpId = yelpRestData[i][0]["id"]
yelpRestDict[str(yelpId)] = yelpRestData[i][1]
#check yelp restaurants
restKeyList = list(yelpRestDict.keys())
fileStr=""
for k in restKeyList:
tmp = ParseData.combineUserAndYelp(userArr, yelpRestDict[k])
tmp = [str(x) for x in tmp]
fileStr +=",".join(tmp) +"\n"
f= open("official_predict.txt", "w")
f.write(fileStr)
f.close()
predictions = model.getPrediction("official_predict.txt")
retDict ={}
for i in range(len(restKeyList)):
retDict[restKeyList[i]] = predictions[i]
#check user restaurants:
fileStr=""
userRestDict={} # key: id, value: vector
for d in userRestData:
userRestDict[d[0]["id"]] = d[1]
userRestKeyList = list(userRestDict.keys())
for k in userRestKeyList:
tmp = ParseData.combineUserAndYelp(userArr, userRestDict[k])
tmp = [str(x) for x in tmp]
fileStr +=",".join(tmp) +"\n"
f= open("official_predict.txt", "w")
f.write(fileStr)
f.close()
predictions = model.getPrediction("official_predict.txt")
for i in range(len(userRestKeyList)):
retDict[userRestKeyList[i]] = predictions[i] +0.5
retList= sorted(retDict.items(), key=lambda value: value[1])
retList = [list(x) for x in retList]
for k in retList:
k[1]=str(k[1])
print (retList)
fb.push_update(retList)
if __name__ == "__main__":
main()