-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuserpreference_update.py
164 lines (150 loc) · 6.9 KB
/
userpreference_update.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
import json
import ast
import numpy as np
from calculate_similarity_vectors import calculate_similarity_vectors
from read_from_cf_json import read_from_cf
def trackTopNaccuracy(InputPythonJson):
print("here")
likeList = InputPythonJson['like']['like']
historyList = InputPythonJson['like']['history']
if len(historyList) > 0:
with open(f"./TopNaccuracy/TopNaccuracy.json","r") as f:
top_json = json.load(f)
top_json = json.dumps(top_json,ensure_ascii = False)
top_json = ast.literal_eval(top_json)
if top_json == {}:
length_ = len(historyList)
iter_ = 1
for i in range(length_):
print(historyList[i]['rating'])
if historyList[i]['rating'] == int(historyList[i]['rating']):
top_json[str(iter_)] = {
"top1":1 if historyList[i]['place']==0 else 0,
"top6":1 if historyList[i]['place']<=5 else 0,
"top12":1 if historyList[i]['place']<=11 else 0
}
iter_+=1
else:
max_ = 0
for k in top_json.keys():
max_ = max(int(k),max_)
length_ = len(historyList)
for i in range(length_):
if historyList[i]['rating'] == int(historyList[i]['rating']):
top_json[str(max_+1)] = {
"top1":1 if historyList[i]['place']==0 else 0,
"top6":1 if historyList[i]['place']<=5 else 0,
"top12":1 if historyList[i]['place']<=11 else 0
}
max_ += 1
with open(f"./TopNaccuracy/TopNaccuracy.json", 'w', encoding='utf-8') as make_file:
json.dump(top_json, make_file, indent="\t",ensure_ascii = False)
def returnVectorJson(filename):
with open(f"./recipe_vector/{filename}.json","r") as f:
vector_json = json.load(f)
vector_json = json.dumps(vector_json,ensure_ascii = False)
vector_json = ast.literal_eval(vector_json)
return vector_json
def UpdateUserPreferrence(InputPythonJson):
likeList = InputPythonJson['like']['like']
historyList = InputPythonJson['like']['history']
scrapList = InputPythonJson['like']['scrap']
#
likeList_id = []
for i in likeList:
likeList_id.append(i['id'])
person_id = None
if len(likeList_id) >= 10:
json_ = read_from_cf()
max_ = json_['max_']
for k,v in json_.items():
if v == list(map(lambda x: str(x),likeList_id)):
person_id = k
#
for recipebefore in historyList:
if "id" not in recipebefore:
continue
if recipebefore['rating'] == int(recipebefore['rating']):
recipebefore['rating'] += 0.1
if recipebefore['rating'] != None and int(recipebefore['rating']) < 3:
hate_id = recipebefore['id']
vector_json = returnVectorJson(hate_id)
hatevector = np.array(vector_json[str(hate_id)])
for likerecipe in likeList:
like_id = likerecipe['id']
vector_json = returnVectorJson(like_id)
likevector = np.array(vector_json[str(like_id)])
if calculate_similarity_vectors(likevector,hatevector) > 0.85:
if calculate_similarity_vectors(likevector,hatevector) > 0.95 and int(likerecipe['rating'])-int(recipebefore['rating']) >= 2:
likerecipe['rating'] = abs(int(likerecipe['rating'])+int(recipebefore['rating']))//2
else:
likeList.remove(likerecipe)
if recipebefore['rating'] == None or int(recipebefore['rating']) >= 4:
recipe_like_id = recipebefore['id']
replace = False
for likerecipe in likeList: # already updated history recipe into likeList
if likerecipe['id'] == recipe_like_id:
replace = True
likerecipe['rating'] = int(recipebefore['rating'])
if replace == False: # most recent history recipe -> should be updated into likeList
done = False
for likerecipe in likeList:
like_id = likerecipe['id']
vector_json_already_in_list = returnVectorJson(like_id)
vector_json_compare = returnVectorJson(recipe_like_id)
vector_already_in_list = np.array(vector_json_already_in_list[str(like_id)])
vector_compare = np.array(vector_json_compare[str(recipe_like_id)])
if calculate_similarity_vectors(vector_already_in_list,vector_compare) > 0.90 and done == False:
done = True
likeList.remove(likerecipe)
likeList.append(recipebefore)
if done == False:
likeList.append(recipebefore)
length_ = len(historyList)
has_to_remove = []
if length_ > 5:
for i in range(0,length_-1):
for j in range(i+1,length_):
if "id" not in historyList[j]:
has_to_remove.append(historyList[j])
else:
ca = np.array(returnVectorJson(historyList[i]['id'])[str(historyList[i]['id'])])
cb = np.array(returnVectorJson(historyList[j]['id'])[str(historyList[j]['id'])])
if calculate_similarity_vectors(ca,cb) > 0.95:
has_to_remove.append(historyList[j])
#print(has_to_remove)
if len(has_to_remove) > 0:
for element in has_to_remove:
if element in historyList:
historyList.remove(element)
length_ = len(historyList)
if length_ > 100:
for reduce_iter in range(length_ - 100):
if historyList[reduce_iter] not in scrapList:
historyList.remove(historyList[reduce_iter])
#
json_ = read_from_cf()
likeList_id = []
print(person_id)
for i in likeList:
likeList_id.append(i['id'])
if person_id != None and len(likeList) >= 10:
json_[person_id] = list(map(lambda x: str(x),likeList_id))
elif person_id != None and len(likeList) < 10:
del json_[person_id]
elif person_id == None and len(likeList) >= 10:
json_['max_'] += 1
json_[json_['max_']] = list(map(lambda x: str(x),likeList_id))
print(json_)
with open(f'CF/cf.json', 'w', encoding='utf-8') as make_file:
json.dump(json_, make_file, indent="\t",ensure_ascii = False)
#
outputJson = {
"id":InputPythonJson['id'],
"like":{
"history":historyList,
"like":likeList,
"scrap":scrapList
}
}
return outputJson