-
Notifications
You must be signed in to change notification settings - Fork 1
/
routes.py
432 lines (340 loc) · 13.1 KB
/
routes.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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
from json import dumps
import json
import csv
import traceback
import codecs
from bottle import Bottle, run, request, response, static_file, json_dumps
from Search import SolrSearcher
from Search import TimeFunc
from Graph.Manager import SRLGraphManager, WordGraphManager, AugmentedWordGraphManager
import datetime
from datetime import timedelta
import pytz
from NLP.EMTerms import EMTerms
from NLP.NLPManager import NLPManager
from NLP.TermPOSIndex import TermPOSIndex
from idlelib.IOBinding import encoding
# from Graph.WordGraphManager import wg_manager
app = Bottle()
termPOS = TermPOSIndex()
#initialize EM Terms#
EMTerms()
# # Static Routes
# @get('/<filename:re:.*\.js>')
# def javascripts(filename):
# return static_file(filename, root='static/js')
# @get('/<filename:re:.*\.css>')
# def stylesheets(filename):
# return static_file(filename, root='static/css')
# @get('/<filename:re:.*\.(jpg|png|gif|ico)>')
# def images(filename):
# return static_file(filename, root='static/img')
# @get('/<filename:re:.*\.(eot|ttf|woff|svg)>')
# def fonts(filename):
# return static_file(filename, root='static/fonts')
@app.hook('after_request')
def enable_cors():
"""
You need to add some headers to each request.
Don't use the wildcard '*' for Access-Control-Allow-Origin in production.
"""
response.headers['Access-Control-Allow-Origin'] = '*'
response.headers['Access-Control-Allow-Methods'] = 'POST, OPTIONS, GET'
response.headers['Access-Control-Allow-Headers'] = 'Origin, Accept, Content-Type, X-Requested-With, X-CSRF-Token'
search = SolrSearcher.SolrSearcher('http://128.46.137.79:8983/solr/TwitterDB_casestudy/')
search_in = SolrSearcher.SolrSearcher('http://128.46.137.79:8983/solr/TwitterDB_1401/')
@app.route('/term_groups/<terms>', method = 'GET')
def query(terms):
terms = terms.split('&')
rst = termPOS.build_groups(terms)
response.content_type = 'application/json'
return json_dumps(rst, indent=2)
#incomplete
@app.route('/merge/<graphIDs>', method = 'GET')
def query(graphIDs):
ids = graphIDs.split('&')
return awg_manager.merge_topics(ids)
#subclusters from created graph
@app.route('/topics/<graphID>', method = 'GET')
def query(graphID):
response.content_type = 'application/json'
return wg_manager.topics(str(graphID))
#initial query...
@app.route('/srl', method="GET")
def query():
term = request.query.get("query")
graphID = request.query.get("graphID")
radius = request.query.get("radius")
center = request.query.get("center")
term = term.split(' ')
print("term, size: ", term, len(term), "; graphID: ", graphID, "; radius: ", radius, "; center: ", center)
#test
#term = ['shooting']
#radius = '1000'
#center = str('40.437829,-86.921049')
#graphID = "0"
geobounds = {'type' : 'circular', 'center' : center, 'radius' : radius}
#test
start_time = TimeFunc.time_func_solr_date_to_python_date('2014-01-21T00:00:00Z')
end_time = TimeFunc.time_func_solr_date_to_python_date('2014-01-23T00:00:00Z')
try:
results = srl_manager.retrieve(term, graphID, start_time, end_time, geobounds)
if len(results) == 0:
raise ValueError('No topics formed from retrieved tweets.')
except ValueError as err:
# print("No topics found in this area")
results = []
print(err.args)
print(traceback.format_exc())
except:
print("error retrieving topics")
results = []
print(traceback.format_exc())
response.content_type = 'application/json'
return dumps(results)
@app.route('/topicsRT', method="GET")
def query():
term = request.query.get("query")
graphID = request.query.get("graphID")
radius = request.query.get("radius")
center = request.query.get("center")
term = term.split(' ')
print("term, size: ", term, len(term), "; graphID: ", graphID, "; radius: ", radius, "; center: ", center)
#test
#term = ['shooting']
#radius = '1000'
#center = str('40.437829,-86.921049')
#graphID = "0"
geobounds = {'type' : 'circular', 'center' : center, 'radius' : radius}
#test
start_time = TimeFunc.time_func_solr_date_to_python_date('2014-01-21T00:00:00Z')
end_time = TimeFunc.time_func_solr_date_to_python_date('2014-01-23T00:00:00Z')
try:
results = awg_manager.retrieve(term, graphID, start_time, end_time, geobounds, realtime = False)
if len(results) == 0:
raise ValueError('No topics formed from retrieved tweets.')
except ValueError as err:
# print("No topics found in this area")
results = []
print(err.args)
print(traceback.format_exc())
except:
print("error retrieving topics")
results = []
print(traceback.format_exc())
response.content_type = 'application/json'
return dumps(results)
@app.route('/topics', method="GET")
def query():
term = request.query.get("query")
graphID = request.query.get("graphID")
radius = request.query.get("radius")
center = request.query.get("center")
if term != None:
term = term.split(' ')
else:
term = []
print("term, size: ", term, "; graphID: ", graphID, "; radius: ", radius, "; center: ", center)
#test
#term = ['shooting']
#radius = '1000'
#center = str('40.437829,-86.921049')
#graphID = "0"
geobounds = {'type' : 'circular', 'center' : center, 'radius' : radius}
#test
start_time = TimeFunc.time_func_solr_date_to_python_date('2014-01-21T00:00:00Z')
end_time = TimeFunc.time_func_solr_date_to_python_date('2014-01-23T00:00:00Z')
try:
results = awg_manager.retrieve(term, graphID, start_time, end_time, geobounds)
if len(results) == 0:
raise ValueError('No topics formed from retrieved tweets.')
except ValueError as err:
# print("No topics found in this area")
results = []
print(err.args)
print(traceback.format_exc())
except:
print("error retrieving topics")
results = []
print(traceback.format_exc())
response.content_type = 'application/json'
return dumps(results)
@app.route('/EMcategory', method="GET")
def query():
term = request.query.get("query")
graphID = request.query.get("graphID")
radius = request.query.get("radius")
center = request.query.get("center")
start_time = request.query.get("start_time")
end_time = request.query.get("end_time")
start_time = TimeFunc.time_func_solr_date_to_python_date(start_time)
end_time = TimeFunc.time_func_solr_date_to_python_date(end_time)
if term != None:
term = term.split(' ')
else:
term = []
print("term, size: ", term, "; graphID: ", graphID, "; radius: ", radius, "; center: ", center)
print("start_time: ", start_time, "; end_time: ", end_time)
geobounds = {'type' : 'circular', 'center' : center, 'radius' : radius}
# 41.855140, -88.373475;
# 37.625001, -83.902040
try:
# rst = search_in.search(False, term, start_time, end_time, str(38.5), str(-87.5), str(41.5), str(-85), 100000)
rst = search.search_radius(False, term, start_time, end_time, center, radius, rows=500000)
if len(rst) == 0:
raise ValueError('No topics formed from retrieved tweets.')
tweets = []
for t in rst:
tweet = {}
tweet['tweet_id'] = str(t['tweet_id'])
tweet['created_at'] = t['created_at']
tweet['geolocation'] = {}
tweet['geolocation']['lon'] = t['geolocation'].split(',')[1]
tweet['geolocation']['lat'] = t['geolocation'].split(',')[0]
tweet['text'] = t['tokens']
tweet['token_tags'] = t['token_tags']
#initialize tokens#
#this is required by emterms mode 1
# nlp = NLPManager()
# tokens = t['tokens'].split()
# token_tags = list(t['token_tags'])
#
# tweet['tokens'] = []
#
# tags = ['N', 'S', '^', 'Z', 'V', '#']
#
# for idx, val in enumerate(tokens):
# if token_tags[idx] in tags and val.lower() not in nlp.stop_list:
# if val.lower() not in tweet['tokens']:
# tweet['tokens'].append(val.lower())
#initialize tokens#
tweets.append(tweet)
print("num of tweets", len(tweets))
# return EMTerms().category_tweets_to_topics2(tweets)
return EMTerms().category_tweets_to_topics_vn_pair(tweets)
except ValueError as err:
# print("No topics found in this area")
results = []
print(err.args)
print(traceback.format_exc())
except:
print("error retrieving topics")
results = []
print(traceback.format_exc())
response.content_type = 'application/json'
return dumps(results)
@app.route('/EMcategory_cache', method="GET")
def query():
start_time = request.query.get("start_time")
end_time = request.query.get("end_time")
case_index = int(request.query.get("case_index"))
fname = ''
if case_index == 0:
fname = 'shooting.json'
elif case_index == 1:
fname = 'boston.json'
elif case_index == 2:
fname = 'sandy.json'
elif case_index == 4:
fname = 'keene.json'
with open(fname) as data_file:
rst = json.load(data_file)
rst_new = []
for t in rst['tweets']:
if t['created_at'] >= start_time and t['created_at'] < end_time:
rst_new.append(t)
print("return tweets;", start_time, end_time, len(rst_new))
return json.dumps({'tweets':rst_new})
@app.route('/cate_cluster', method="GET")
def query():
case_index = int(request.query.get("case_index"))
fname = ''
if case_index == 0:
fname = 'shooting.json'
elif case_index == 1:
fname = 'boston_cate_cluster.json'
elif case_index == 2:
fname = 'sandy.json'
elif case_index == 4:
fname = 'keene.json'
rst = {}
with open(fname) as data_file:
rst = json.load(data_file)
return json.dumps(rst)
@app.route('/search', method="POST")
def query():
start_time = TimeFunc.time_func_solr_date_to_python_date('2015-05-13T00:00:00Z')
end_time = TimeFunc.time_func_solr_date_to_python_date('2016-01-21T20:00:00Z')
term = request.forms.get("queryText")
if term is None:
return None
term = term.split()
results = search.search(False, term, start_time, end_time, str(25), str(-129), str(50), str(-60))
rst = []
for result in results:
rst.append(result)
response.content_type = "application/json"
return dumps(rst)
@app.get('/<filename:path>', method="GET")
def server_static(filename):
print(filename)
return static_file(filename, root='Static')
@app.route('/')
def index():
return static_file('Static/main.html', '')
@app.route('/map')
def index():
return static_file('Static/map_test/map_d3_test.html', '')
##########################################################################################
# cluster functions
@app.route('/getCluster', method="GET")
def query():
with open('cluster_tree.json') as data_file:
rst = json.load(data_file)
return json.dumps(rst)
#########################################################################################
from NLP import mytfidf
if __name__ == '__main__':
wg_manager = WordGraphManager.WordGraphManager(search)
srl_manager = SRLGraphManager.SRLGraphManager(search)
awg_manager = AugmentedWordGraphManager.AugmentedWordGraphManager(search)
run(app, host='localhost', port=9006, debug=True)
# rst = []
# rst = json.load(codecs.open('rnc_raw.json', 'r', 'utf-8-sig'))
# rst = rst['tweets']
#
# tweets = []
# for t in rst:
# tweet = {}
# tweet['tweet_id'] = str(t['tweet_id'])
#
# tweet['created_at'] = t['created_at']
# tweet['geolocation'] = {}
# tweet['geolocation']['lon'] = t['geolocation'].split(',')[1]
# tweet['geolocation']['lat'] = t['geolocation'].split(',')[0]
# tweet['text'] = t['tokens']
# tweet['token_tags'] = t['token_tags']
#
# tweet['keywords'] = ' '.join(mytfidf.getTweetKeywords(tweet['text'].split(' ')))
#
# tweets.append(tweet)
#
# print("num of tweets", len(tweets))
#
# # calculate TF-IDF value
# # tweets_text = [tweet['text'].split(' ') for tweet in tweets]
# tweets_text = [tweet['keywords'] for tweet in tweets]
# scores = mytfidf.tfidfWrapper(tweets_text)
#
# print('tf-idf finished1')
#
# for idx, t in enumerate(tweets):
# tweets[idx]['tfidf'] = scores[idx]
# tweets[idx].pop("keywords", None)
#
# print('tf-idf finished2')
#
# rst = EMTerms().category_tweets_to_topics_vn_pair(tweets)
#
# with open("rnc_post.json", "w") as text_file:
# text_file.write(rst)