-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFlaskCon-4-19.py
339 lines (297 loc) · 13 KB
/
FlaskCon-4-19.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
# coding=utf-8
import json
from flask import Flask
from flask import request
from flask import redirect
from flask import jsonify
#from flask.ext.sqlalchemy import SQLAlchemy
import pymysql
import Init_SQL as ISQL
from Recommand_System import RSystem
import thulac
import snownlp
import jieba
import jieba.analyse as jae
from snownlp import sentiment
from snownlp import SnowNLP
import synonyms
import math
import time
import os
import threading
RS = RSystem()
user_name = ISQL.user_name
pw = ISQL.pw
# web server的搭建
# web server面对各个请求的回应
# 与数据库推荐结果列表的连接
#用/进行本机测试
app = Flask(__name__)
@app.route('/v1/backend/food/sync/user/add',methods = ['POST']) #/http://lcoalhost:8080/v1/backend/food/sync/user/add
def Receive_new_User(): #获得新注册的用户的信息并添加
try:
a = request.get_data()
dict1 = json.loads(a.decode('utf-8'))
json_data = {'error':0}
#进行对数据库增加一行的操作
user_id= dict1['id']
DB_conn = pymysql.connect(host = 'localhost',user = user_name,password = pw,db = 'gd', charset = 'utf8mb4',cursorclass = pymysql.cursors.DictCursor)
cursor = DB_conn.cursor()
sql = "Insert INTO UTs(User_ID) values(\'%s\')" %user_id
cursor.execute(sql)
cursor.execute("Insert INTO User_CT(User_ID) values(\'%s\')" %user_id)
DB_conn.commit()
DB_conn.rollback()
cursor.close()
DB_conn.close()
RS.Insert_NewUserID(user_id)
RS.User_Fav_Food_List[user_id] = set()
return jsonify(json_data)
except Exception as e:
json_data = {'error':str(e)}
return jsonify(json_data)
@app.route('/v1/backend/food/sync/user/edit-tag',methods = ['POST'])
def Edit_Tag(): #将用户新增加的标签加入到数据库中
try:
a = request.get_data()
dict2 = json.loads(a.decode('utf-8'))
user_id = dict2['id']
#这里可能要改回去 'name':'tag1'
tags_list = [x['name'] for x in dict2['tags']]
DB_conn = pymysql.connect(host = 'localhost',user = user_name,password = pw,db = 'gd', charset = 'utf8mb4',cursorclass = pymysql.cursors.DictCursor)
#将用户有所行动的标签添加进其向量中
for tag in tags_list :
cursor_2 = DB_conn.cursor()
sql = """
update UTs set %s = %s + 1 where User_ID = \'%s\'
"""%(tag,tag,user_id)
cursor_2.execute(sql)
DB_conn.commit()
cursor_2.close()
DB_conn.close()
json_data = {'error':0}
return jsonify(json_data)
except Exception as e:
json_data = {'error':str(e)}
return jsonify(json_data)
@app.route('/v1/backend/food/sync/user/add-favorite',methods = ['POST']) #address needed
def Favourite_Food(): #将用户新增加的喜爱食物加入到数据库中
try:
a = request.get_data()
dict3 = json.loads(a.decode('utf-8'))
user_id = dict3['id']
Fav_Food = dict3['food']
#用户喜欢的食物可用来增加其标签的权重
RS.Up_Tags_Weight(user_id,Fav_Food)
RS.User_Fav_Food_List[user_id].add(Fav_Food)
#将记录加入到用户-喜爱食物表User_FavFood中
DB_conn = pymysql.connect(host = 'localhost',user = user_name,password = pw,db = 'gd', charset = 'utf8mb4',cursorclass = pymysql.cursors.DictCursor)
W_cursor = DB_conn.cursor()
W_cursor.execute("Insert INTO User_FavFood(User_ID,Fav_Food) values(\'%s\',\'%s\')" %(user_id,Fav_Food))
DB_conn.commit()
json_data = {'error':0}
return jsonify(json_data)
except Exception as e:
json_data = {'error':str(e)}
return jsonify(json_data)
@app.route('/v1/backend/food/sync/user/delelte-favorite',methods = ['POST'])
def Delete_Fav_Food():
try:
a = request.get_data()
dict4 = json.loads(a.decode('utf-8'))
user_id = dict4['id']
Fav_Food = dict4['food']
#删除其增加的权重
RS.DOWN_Tags_Weight(user_id,Fav_Food)
# RS.User_Fav_Food_List[user_id].remove(Fav_Food)
DB_conn = pymysql.connect(host = 'localhost',user = user_name,password = pw,db = 'gd', charset = 'utf8mb4',cursorclass = pymysql.cursors.DictCursor)
D_curosr = DB_conn.cursor()
D_curosr.execute("delete from User_FavFood where User_ID = \'%s\' and Fav_Food = \'%s\'" %(user_id,Fav_Food))
DB_conn.commit()
D_curosr.close()
json_data = {'error':0}
return jsonify(json_data)
except Exception as e:
json_data = {'error':str(e)}
return jsonify(json_data)
@app.route('/v1/backend/food/sync/user/add-comment',methods = ['POST'])
def Added_Comment_Annalysic():
try:
DB_conn = pymysql.connect(host = 'localhost',user = user_name,password = pw,db = 'gd', charset = 'utf8mb4',cursorclass = pymysql.cursors.DictCursor)
a = request.get_data()
dict5 = json.loads(a.decode('utf-8'))
User_ID = dict5['id']
Comment_Food = dict5['food']
Score = dict5['rate']
if(Score < 2):
Score = 0
Comment = dict5['detail']
Comment_ID = dict5['commentId']
Return_Tags_List = list()
#put the info into the table Comment_Info
add_comment_cursor = DB_conn.cursor()
operation_sentence = "Insert INTO Comment_Info(Comm_ID,Context,User_ID,Score,Food_ID) values(\'%s\',\'%s\',\'%s\',%s,\'%s\')" %(Comment_ID,Comment,User_ID,Score,Comment_Food)
add_comment_cursor.execute(operation_sentence)
DB_conn.commit()
add_comment_cursor.close()
#修改用户评论次数表
Change_User_CT_cursor = DB_conn.cursor()
Change_User_CT_cursor.execute("update User_CT set Comment_Times = Comment_Times + 1 where User_ID = \'%s\'" %User_ID)
DB_conn.commit()
Change_User_CT_cursor.close()
#提取文本中的标签并加入到数据库里用户的标签向量与食物的标签向量中(标签向量增量要根据其给分而定)
tag_words = jae.extract_tags(Comment,topK = 10, withWeight = True, allowPOS = ())
Predict_Score = SnowNLP(Comment).sentiments
#将获得的标签与原标签表中的作对比,选出相近的作为对应的标签,相近度也要放进标签权重计算中
Change_FTs_Cursor = DB_conn.cursor()
Change_UTs_Cursor = DB_conn.cursor()
for test_word, weight in tag_words:
Rank_Dict = dict()
for origin_tag in ISQL.Tags_List:
Rank_Dict[origin_tag] = synonyms.compare(test_word,origin_tag,seg = True)
Sorted_Rank_Dict = sorted(Rank_Dict.items(),key = lambda d:d[1],reverse = True)
Most_Similiar_Tag = Sorted_Rank_Dict[0][0]
Return_Tags_List.append(Most_Similiar_Tag)
Similiar_Point = Sorted_Rank_Dict[0][1]
if(Score * Predict_Score*Similiar_Point * weight == 0):
Final_Set_Weight = 0
else:
Final_Set_Weight = Score * Predict_Score*Similiar_Point * weight
Change_FTs_Cursor.execute("select %s from FTs where Food_ID = \'%s\'" %(Most_Similiar_Tag,Comment_Food))
Pre_FWeight_Dict = Change_FTs_Cursor.fetchone()
Pre_FWeight = Pre_FWeight_Dict[Most_Similiar_Tag]
Change_FTs_Cursor.execute("Update FTs set %s = %s where Food_ID = \'%s\'" %(Most_Similiar_Tag,Pre_FWeight + Final_Set_Weight,Comment_Food))
DB_conn.commit()
Change_UTs_Cursor.execute("select %s from UTs where User_ID = \'%s\'" %(Most_Similiar_Tag,User_ID))
Pre_UWeight_Dict = Change_UTs_Cursor.fetchone()
Pre_UWeight = Pre_UWeight_Dict[Most_Similiar_Tag]
Change_UTs_Cursor.execute("Update UTs set %s = %s where User_ID = \'%s\'" %(Most_Similiar_Tag,Pre_UWeight + Final_Set_Weight, User_ID))
DB_conn.commit()
#修改食物平均分表
Get_Info_cursor = DB_conn.cursor()
Change_F_AveS_cursor = DB_conn.cursor()
Get_Info_cursor.execute("Select * from F_AveS where Food_ID = \'%s\'"%Comment_Food)
Food_Info_Row = Get_Info_cursor.fetchone()
Pre_Score = Food_Info_Row['Ave_Score'] * Food_Info_Row['times']
New_Score = Pre_Score + Score
New_Times = Food_Info_Row['times'] + 1
New_Ave_Score = New_Score / New_Times
Change_F_AveS_cursor.execute("Update F_AveS set Ave_Score = %s , times = %s where Food_ID = \'%s\'" %(New_Ave_Score,New_Times,Comment_Food))
DB_conn.commit()
Get_Info_cursor.close()
Change_F_AveS_cursor.close()
DB_conn.close()
#还要修改RS中的字典
RS.Food_AveScore[Comment_Food]['Times'] = New_Times
RS.Food_AveScore['AveScore'] = New_Ave_Score
Return_Tags_List = list(set(Return_Tags_List))
json_data = {'error':0,'tag':Return_Tags_List}
return jsonify(json_data)
except Exception as e:
json_data = {'error':str(e)}
return jsonify(json_data)
@app.route('/v1/backend/food/sync/user/delete-comment',methods = ['POST'])
def Delete_Comment():
try:
DB_conn = pymysql.connect(host = 'localhost',user = user_name,password = pw,db = 'gd', charset = 'utf8mb4',cursorclass = pymysql.cursors.DictCursor)
a = request.get_data()
dict7 = json.loads(a.decode('utf-8'))
User_ID = dict7['id']
Comment_ID = dict7['commentId']
Return_Tags_List = list()
#score comment food_id are needed to get in db
get_comment_info = DB_conn.cursor()
get_info_sentence = 'select Context,Score,Food_ID from Comment_Info where Comm_ID = \'%s\''%(Comment_ID)
get_comment_info.execute(get_info_sentence)
info = get_comment_info.fetchone()
Comment = info['Context']
Score = info['Score']
Food_ID = info['Food_ID']
get_comment_info.close()
#delete the comment_info in table Comment_Info
del_comment_cursor = DB_conn.cursor()
del_comment_cursor.execute('delete from Comment_Info where Comm_ID = \'%s\''%(Comment_ID))
DB_conn.commit()
del_comment_cursor.close()
Change_User_CT_cursor = DB_conn.cursor()
Change_User_CT_cursor.execute("update User_CT set Comment_Times = Comment_Times - 1 where User_ID = \'%s\'" %User_ID)
DB_conn.commit()
Change_User_CT_cursor.close()
#先提取其中的标签,再在UTs上去除这些标签(-1),且在FTs上也去除
tag_words = jae.extract_tags(Comment,topK = 10, withWeight = True, allowPOS = ())
Predict_Score = SnowNLP(Comment).sentiments
#最后减去该评论导致的食品评分变化
Change_FTs_Cursor = DB_conn.cursor()
Change_UTs_Cursor = DB_conn.cursor()
for test_word, weight in tag_words:
Rank_Dict = dict()
for origin_tag in ISQL.Tags_List:
Rank_Dict[origin_tag] = synonyms.compare(test_word,origin_tag,seg = True)
Sorted_Rank_Dict = sorted(Rank_Dict.items(),key = lambda d:d[1],reverse = True)
Most_Similiar_Tag = Sorted_Rank_Dict[0][0]
Return_Tags_List.append(Most_Similiar_Tag)
Similiar_Point = Sorted_Rank_Dict[0][1]
if(Score/5 * Predict_Score*Similiar_Point * weight == 0):
Final_Set_Weight = 0
else:
Final_Set_Weight = Score/5 * Predict_Score*Similiar_Point * weight
Change_FTs_Cursor.execute("select %s from FTs where Food_ID = \'%s\'" %(Most_Similiar_Tag,Food_ID))
Pre_FWeight_Dict = Change_FTs_Cursor.fetchone()
Pre_FWeight = Pre_FWeight_Dict[Most_Similiar_Tag]
Change_FTs_Cursor.execute("Update FTs set %s = %s where Food_ID = \'%s\'" %(Most_Similiar_Tag,Pre_FWeight - Final_Set_Weight,Food_ID))
DB_conn.commit()
Change_UTs_Cursor.execute("select %s from UTs where User_ID = \'%s\'" %(Most_Similiar_Tag,User_ID))
Pre_UWeight_Dict = Change_UTs_Cursor.fetchone()
Pre_UWeight = Pre_UWeight_Dict[Most_Similiar_Tag]
Change_UTs_Cursor.execute("Update UTs set %s = %s where User_ID = \'%s\'" %(Most_Similiar_Tag,Pre_UWeight - Final_Set_Weight, User_ID))
DB_conn.commit()
#食物平均分表的撤销
Get_Info_cursor = DB_conn.cursor()
Change_F_AveS_cursor = DB_conn.cursor()
Get_Info_cursor.execute("Select * from F_AveS where Food_ID = \'%s\'"%Food_ID)
Food_Info_Row = Get_Info_cursor.fetchone()
Pre_Score = Food_Info_Row['Ave_Score'] * Food_Info_Row['times']
New_Score = Pre_Score - Score
New_Times = Food_Info_Row['times'] - 1
if New_Times == 0:
New_Ave_Score = 0
else:
New_Ave_Score = New_Score / New_Times
Change_F_AveS_cursor.execute("Update F_AveS set Ave_Score = %s , times = %s where Food_ID = \'%s\'" %(New_Ave_Score,New_Times,Food_ID))
DB_conn.commit()
Get_Info_cursor.close()
Change_F_AveS_cursor.close()
DB_conn.close()
#还要修改RS中的字典
RS.Food_AveScore[Food_ID]['Times'] = New_Times
RS.Food_AveScore['AveScore'] = New_Ave_Score
json_data = {'error':0}
return jsonify(json_data)
except Exception as e:
json_data = {'error':str(e)}
return jsonify(json_data)
@app.route('/v1/backend/food/recommend/id',methods = ['POST'])
def Return_Recommand_Foods():
DB_conn = pymysql.connect(host = 'localhost',user = user_name,password = pw,db = 'gd', charset = 'utf8mb4',cursorclass = pymysql.cursors.DictCursor)
DB_conn.close()
a = request.get_data()
dict6 = json.loads(a.decode('utf-8'))
user_id = dict6['id']
#根据与其相似的用户及与其有过行为的物品相似度高的物品计算推荐物品列表并返回结果列表 result_list
result_list = RS.Recommand(user_id)
json_data = {'foods':result_list}
return jsonify(json_data)
def Cal_M():
seconds = 12 * 3600
while (True):
RS.Cal_ItemCF()
RS.Cal_UserCF()
time.sleep(seconds)
def Run_Server():
app.run(debug = False, threaded = True, port = 8088)
if __name__ == '__main__':
os.system("Init_SQL.py") #同个目录下的文件
t1 = threading.Thread(target = Cal_M)
t2 = threading.Thread(target = Run_Server)
t1.start()
t2.start()