-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.py
273 lines (224 loc) · 8.63 KB
/
server.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
import csv
from flask import Flask, render_template, make_response, request, session, redirect, url_for
import json
import networkx as nx
from networkanalysis.Analysis import Retrievor
from user_Feedback.recordUser import record_thread, error_thread, userQuestion
import PF
import time
from time import gmtime, strftime
from concept2design import node2design
app = Flask(__name__)
app.secret_key = ''
myRtr = Retrievor.UndirectedG('data/test6_37_28_nominus_updated_cleannameplace2', 'total_v3_csvneo4j', 'userdata')
with open('data/word2id_from_graph.json', encoding='utf8')as fp:
word_map = json.load(fp)
# print(word_map)
# sign up
@app.route('/signup')
def signup():
print("signup")
session['firstTimeVisit'] = True
user = request.args.get('email', '')
w = request.args.get('w', '')
session['user'] = user
session['w'] = w
print(w)
fusers = open('allusers.txt', mode='a')
fusers.write(user + '\n')
fusers.close()
return redirect('/')
# Main Page
@app.route('/')
def index():
print("index")
if 'user' in session:
print(session['w'])
print(session['user'])
w = session['w']
if int(w) <= 750:
print('moble')
return make_response(open('m-index.html').read())
else:
return make_response(open('index.html').read())
else:
return make_response(open('signup.html').read())
@app.route('/mobile')
def mobile():
return make_response(open('m-index.html').read())
# get text return nodes number
@app.route('/texttowid/<info>')
def texttowid(info):
print("textword: info: ", info)
info = json.loads(info)
searchtext = info['searchtext']
distance = 'None'
# --new
print("searchtext:", searchtext)
ipts = [word.strip() for word in searchtext.split(';')]
print("step1: texttowid: ipts:", ipts) # ['apple', 'banana']
try:
# wids = myRtr.input_ids(ipts)
wids, new_ipts = myRtr.input_ids_read_csv(word_map, ipts)
print("texttowid: ", wids)
except:
# record the word which can't be found. error
errthread = error_thread(myRtr.userSchema, myRtr.data_version, distance, session['user'], 'search', 'null',
ipts[0], 'null', 'null')
errthread.start()
raise ValueError("Input NOT Found")
# print(myRtr.G.node[13795])
labels = [myRtr.G.node[wid]['label'] for wid in wids]
print("labels: ", labels)
# labels = new_ipts
# record user activity
user = session['user']
rthread = record_thread(myRtr.userSchema, myRtr.data_version, distance, user, 'search', wids, labels, 1)
rthread.start()
response = json.dumps(wids[-1])
return make_response(response)
# base concept + additive concept ->chatGPT-> explanation+description
@app.route('/nodetodesign/<info>')
def nodetodesign(info):
info = json.loads(info)
node1_id = info['node1_id']
node2_id = info['node2_id']
design_text = node2design(node1_id, node2_id)
return make_response(design_text)
# search button, add one node
@app.route('/searchbutton/<info>')
def search(info):
print("info:", info)
info = json.loads(info)
print("search: info: ", info)
distance = info['tp']
print("search: distance: ", distance)
localG = myRtr.G.subgraph(set(info['currentnodes'] + info['query']))
print(localG.nodes())
allnodes = [
# 每个顶点的次数(degree)
{"wid": n, "label": localG.node[n]["label"], "N": localG.degree(n, weight="weight"), "n": localG.degree(n)} for
n in localG.nodes()]
alledges = [{"source": source, "target": target, 'dist': dist} for (source, target, dist) in
localG.edges(data=distance)]
sorted_paths = sorted(localG.edges(nbunch=info['query'], data=distance), key=lambda x: x[2])
add_paths = [path[:-1] for path in sorted_paths]
try:
bi = 0
bornnode = sorted_paths[bi][1]
while bornnode in info['query']:
bi += 1
bornnode = sorted_paths[bi][1]
except:
bornnode = None
dataset = {"allnodes": allnodes, "alledges": alledges, "paths": add_paths, 'bornnode': bornnode}
print(dataset)
response = json.dumps(dataset)
return make_response(response)
# find the nearest node of the current nodes to the query node
@app.route('/findnear/<info>')
def findnear(info):
info = json.loads(info)
query = info['query']
localG = myRtr.G.subgraph(set(info['currentnodes'] + [query]))
sorted_neighbors = sorted([(n, localG[query][n]['Fw']) for n in localG.neighbors(query)], key=lambda x: x[1])
try:
bornnode = sorted_neighbors[0][0]
except:
bornnode = None
response = json.dumps(bornnode)
return make_response(response)
# Generate clusters based on current nodes
@app.route('/generateClusters/<info>')
def generateClusters(info):
info = json.loads(info)
nodes = info['nodes']
method = info['method']
weight = info['weight']
# record clusters activities
user = session['user']
record_wid = sorted(nodes)
rthread = record_thread(myRtr.userSchema, myRtr.data_version, weight, user, 'generateClusters', [record_wid],
["Omit"], 1)
rthread.start()
if method == 'normalized':
k = info['k']
clusters = myRtr.cutgraph(nodes, k, weight=weight)
elif method == 'mcl':
r = info['r']
M, clusters = myRtr.mcl_cluster(nodes, r, weight=weight)
else:
raise TypeError('unknown clustering method')
# sort clusters by centrality
distance = info['distance']
clusters = myRtr.sort_clustersCentrality(clusters, distance)
response = json.dumps(clusters)
return make_response(response)
# query generator in the server
@app.route('/generator/<info>')
def generator(info):
info = json.loads(info)
distance = info['parameters']['parameters']['tp']
query_type = info['parameters']['generator']
info['parameters']['user'] = session['user']
if info['explorelocal'] == True:
info['localnodes'] = info['parameters']['parameters']['localnodes']
explorenodes, explorepaths, position = myRtr.my_Gen(**info['parameters'])
# record user activities
if info['explorelocal'] == False:
if len(explorepaths) > 0:
record_wids = [path['ids'] for path in explorepaths]
record_labels = [path['labels'] for path in explorepaths]
rthread = record_thread(myRtr.userSchema, myRtr.data_version, distance, session['user'], query_type,
record_wids, record_labels, position)
rthread.start()
if set(explorenodes).issubset(info['localnodes']):
response = json.dumps({'AddNew': False, 'paths': explorepaths, "position": position})
else:
localG = myRtr.G.subgraph(set(info['localnodes']) | set(explorenodes)) # local
allnodes = [
{"wid": n, "label": localG.node[n]["label"], "N": localG.degree(n, weight="weight"), "n": localG.degree(n)}
for n in localG.nodes()]
alledges = [{"source": source, "target": target, 'dist': dist} for (source, target, dist) in
localG.edges(data=distance)]
dataset = {'AddNew': True, "allnodes": allnodes, "alledges": alledges, "paths": explorepaths,
"position": position}
response = json.dumps(dataset)
return make_response(response)
# check first time visit
@app.route('/checkFirstTimevisit')
def checkFirstTimevisit():
if session['firstTimeVisit'] == True:
session['firstTimeVisit'] = False
info = True
else:
info = False
response = json.dumps(info)
return make_response(response)
# get NeighborLevel for a node
@app.route('/neighbor_level/<int:node>')
def neighbor_level(node):
response = my_localRtr.get_Neib_one(node, None)
response['disconnected'] = set(my_localRtr.G.nodes()).difference(response["AllNb"])
response.pop("AllNb")
for key, value in response.iteritems():
response[key] = list(value)
response = json.dumps(response)
return make_response(response)
# WordRank
# Get relevant word list and corresponding path list for a word
@app.route('/wordrank/<int:node>')
def wordrank(node):
response = my_localRtr.get_Rel_one(node, "Fw", None)
nodesandpaths = []
for n, p in response.iteritems():
path = p[1]
nodesandpaths.append([n, path])
response = json.dumps(nodesandpaths)
return make_response(response)
############ FEEDBACK DATA COLLECTION #####################
@app.route('/feedback')
def feedback():
return make_response(open('fb-completion.html').read())
if __name__ == '__main__':
app.run(host='0.0.0.0', threaded=True, port=5000)