/
syntaviz.py
422 lines (389 loc) · 17.3 KB
/
syntaviz.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
# Copyright 2018 Comcast Cable Communications Management, LLC
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import pdb
from flask import Flask, abort, render_template, url_for, request
import cluster_query
import pickle as cp
import numpy as np
import urllib
import json
import sys
import base64
from io import BytesIO
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
'''
This is a quick working prototype of a visualizer that would
facilitate the exploring of syntactic patterns of queries and
the statistical distribution of the actions currently taken for
these patterns.
Note: This server needs the following files:
1. '../data/all-queries-raw.txt': This is a tab delimited list
of original queries. It also contains
2. '../data/dependency_syntaxnet_jsonified': This is a tab
delimited list of tokenized queries, dependency parse in
two different formats, and the query ID (index to the original
query list).
3. '../data/qaction.pickle': It contains a dictionary named qaction
which returns the actions taken (values) for the queries (keys).
'''
inpfile = sys.argv[1]
outfile = sys.argv[2]
query2actionfile = sys.argv[3]
PORT=5678
if len(sys.argv) > 4:
PORT = int(sys.argv[4])
################## Load the pre-requisites ####################
print("Loading cluster data ...")
clust_head, queries, freq_list = cluster_query.cluster_counts_and_queries(
original_query_file=inpfile,
parsed_query_file=outfile,
get_freq=True)
clust = clust_head
tot_uniq = sum([clust[akey][0] for akey in clust])
tot_nonuniq = sum([freq_list[aqid] for akey in clust for aqid in clust[akey][2]])
print("Done clustering.")
print("Loading list of actions performed for each query ...")
qaction = cp.load(open(query2actionfile))
print("Done loading actions.")
###############################################################
# The SyntaViz server
app = Flask('SyntaViz')
@app.route('/keys/<string:key>')
@app.route('/keys//<int:st_idx>/<int:en_idx>')
@app.route('/keys/<string:key>/<int:st_idx>/<int:en_idx>')
def get_keys_json(key='', st_idx=0, en_idx=50):
'''
Make a list of keys
'''
key = urllib.unquote(urllib.unquote(key))
allkeys = []
try:
for i, akey, count in cluster_query.get_keys(clust, key, st_idx, en_idx):
if key:
akey = key + '|' + akey
allkeys.append((i, count, akey))
except KeyError:
print('Key Not Found:', key)
return abort(404)
return json.dumps(allkeys)
@app.route('/queries/<string:key>')
@app.route('/queries/<string:key>/<int:st_idx>/<int:en_idx>')
def get_queries_json(key='', st_idx=0, en_idx=50):
'''
Make a list of queries
'''
key = urllib.unquote(urllib.unquote(key))
clust = clust_head
allqueries = []
try:
for i, qid, aquery in cluster_query.get_queries(clust, key, queries, st_idx, en_idx):
allqueries.append((i, qid, aquery))
except:
print('Key Not Found:', key)
return abort(404)
return json.dumps(allqueries)
def get_action_hist(key, qaction):
'''
Returns the frequency of various actions taken (in response to
the queries of a key) as well as the list of actions
'''
action_hist = {}
clust = clust_head
for i, qid, aquery in cluster_query.get_queries(clust, key, queries, en_idx=float('inf')):
aquery = aquery.lower()
if aquery in qaction:
if qaction[aquery] in action_hist:
action_hist[qaction[aquery]] += 1
else:
action_hist[qaction[aquery]] = 1
return action_hist
def get_plot(adict):
'''
Plot the action dictionary
'''
img_format = \
'<img src="data:image/png;base64,{0}" style="width: 100%; height: auto;">'
count, labels = zip(*sorted([(adict[akey], akey) for akey in adict],
key=lambda x: -1 * x[0]))
# total is the number of queries having an action
# m is the number of different actions
m = len(count)
total = sum(count)
# Plot
if m > 30:
plt.figure(num=1, figsize=(12, 8), fontsize=24)
plt.clf()
plt.bar(np.arange(30), count[:30])
plt.xticks(np.arange(30) + 0.4, labels[:30], rotation='vertical', fontsize=24)
plt.xlabel('Name of Actions (Only top 30 among {0})'.format(m))
plt.ylabel('Count of Actions')
plt.title('Total unique queries containing an action (Including NA) = {0}'.format(total))
try:
plt.tight_layout()
except:
print("matplotlib error")
pass
# pdb.set_trace()
else:
plt.figure(num=1, figsize=(12, 8), fontsize=24)
plt.clf()
plt.bar(np.arange(m), count)
plt.xticks(np.arange(m) + 0.4, labels, rotation='vertical', fontsize=24)
plt.xlabel('Name of Actions (all)')
plt.ylabel('Count of Actions')
plt.title('Total unique queries containing an action (Including NA) = {0}'.format(total))
try:
plt.tight_layout()
except:
print("matplotlib error")
pass
# pdb.set_trace()
# Convert to html
figfile = BytesIO()
plt.savefig(figfile, format='png')
figfile.seek(0)
figfile_png = base64.b64encode(figfile.getvalue())
return img_format.format(figfile_png)
@app.route('/')
@app.route('/both')
def both():
'''
Show the page
'''
# Parsing arguments
key_k = urllib.unquote(urllib.unquote(request.args.get('key_k', '')))
st_idx_k = int(request.args.get('st_idx_k', 0))
en_idx_k = int(request.args.get('en_idx_k', 500))
st_idx_q = int(request.args.get('st_idx_q', 0))
en_idx_q = int(request.args.get('en_idx_q', 1000))
sort_key_by = int(request.args.get('sort_key_by', 0)) # sort by unique count(0), total count(1)
sort_query_by = int(request.args.get('sort_query_by', 0)) # sort by query frequency(0), or qid(1)
try:
# Build the list of keys
clust = clust_head
allkeys = []
for i, akey, count, nucount in cluster_query.get_keys( \
clust,
key_k,
st_idx_k,
en_idx_k,
freq_list=freq_list,
sortby=sort_key_by):
if key_k:
fullkey = key_k + '|' + akey
else:
fullkey = akey
encodedkey = urllib.quote(fullkey)
# Link to go to a specific cluster
keylink = url_for('both',
key_k=encodedkey,
st_idx_k=st_idx_k,
en_idx_k=en_idx_k,
st_idx_q=st_idx_q,
en_idx_q=en_idx_q,
sort_key_by=sort_key_by,
sort_query_by=sort_query_by)
allkeys.append((i, count, akey, keylink, nucount,
'{0:0.2f}'.format(float(count) / float(tot_uniq) * 100.),
'{0:0.2f}'.format(float(nucount) / float(tot_nonuniq) * 100.)))
except KeyError:
print('Key Not Found:', key_k)
return abort(404)
# Build the left pane navigations
navformat = '<a href="{0}">{1}</a>'
diff = en_idx_k - st_idx_k
if st_idx_k > 0:
left_prev_code = navformat.format(url_for('both',
key_k=key_k,
st_idx_k=max(0, st_idx_k - diff),
en_idx_k=st_idx_k,
st_idx_q=st_idx_q,
en_idx_q=en_idx_q,
sort_key_by=sort_key_by,
sort_query_by=sort_query_by), '<<')
else:
left_prev_code = '<<'
left_next_code = navformat.format(url_for('both',
key_k=key_k,
st_idx_k=en_idx_k,
en_idx_k=en_idx_k + diff,
st_idx_q=st_idx_q,
en_idx_q=en_idx_q,
sort_key_by=sort_key_by,
sort_query_by=sort_query_by), '>>')
# Build the breadcrumb
if not key_k:
currentkey = '< None >'
else:
if '|' in key_k:
currentkey = ''
spltkey = key_k.split('|')
for i in range(len(spltkey)):
encodedkey = urllib.quote('|'.join(spltkey[:i + 1]))
currentkey += '<a href="{1}">{0}|</a>'.format(spltkey[i], url_for('both',
key_k=encodedkey,
st_idx_k=0,
en_idx_k=en_idx_k - st_idx_k,
st_idx_q=0,
en_idx_q=en_idx_q - st_idx_q,
sort_key_by=sort_key_by,
sort_query_by=sort_query_by))
if currentkey[-1] == '|':
currentkey = currentkey[:-1]
else:
currentkey = key_k
# Build the links for sorting the keys
unique_link = url_for('both',
key_k=key_k,
st_idx_k=st_idx_k,
en_idx_k=en_idx_k,
st_idx_q=st_idx_q,
en_idx_q=en_idx_q,
sort_key_by=0,
sort_query_by=sort_query_by)
total_link = url_for('both',
key_k=key_k,
st_idx_k=st_idx_k,
en_idx_k=en_idx_k,
st_idx_q=st_idx_q,
en_idx_q=en_idx_q,
sort_key_by=1,
sort_query_by=sort_query_by)
# If no key is selected, just send out the root keys
if not key_k:
return render_template('fullpage.html',
total_count=tot_nonuniq,
uniq_count=tot_uniq,
left_prev_code=left_prev_code,
left_next_code=left_next_code,
allkeys=allkeys,
currentkey=currentkey,
header_unique_link=unique_link,
header_total_link=total_link)
try:
# Build the list of queries
allqueries = []
clust = clust_head
if sort_query_by == 0:
# sort by query frequency
frequency_list = freq_list
else:
# sort by qid
frequency_list = None
# Accumulate the queries
for i, qid, aquery in cluster_query.get_queries(clust,
key_k,
queries,
st_idx_q,
en_idx_q,
freq_list=frequency_list):
if aquery.lower() in qaction:
query_action = qaction[aquery.lower()]
else:
query_action = '[Not Found]'
allqueries.append((i,
qid,
aquery,
query_action,
freq_list[qid],
'{0:0.3f}'.format(float(freq_list[qid]) / tot_nonuniq * 100.)))
except KeyError:
print('Key Not Found:', key_k)
return abort(404)
# Build the right pane navigations
diff = en_idx_q - st_idx_q
if st_idx_q > 0:
right_prev_code = navformat.format(url_for('both',
key_k=key_k,
st_idx_k=st_idx_k,
en_idx_k=en_idx_k,
st_idx_q=max(0, st_idx_q - diff),
en_idx_q=st_idx_q,
sort_key_by=sort_key_by,
sort_query_by=sort_query_by), '<<')
else:
right_prev_code = '<<'
right_next_code = navformat.format(url_for('both',
key_k=key_k,
st_idx_k=st_idx_k,
en_idx_k=en_idx_k,
st_idx_q=en_idx_q,
en_idx_q=en_idx_q + diff,
sort_key_by=sort_key_by,
sort_query_by=sort_query_by), '>>')
# Build the links for sorting the queries
# By frequency
header_freq_link = url_for('both',
key_k=key_k,
st_idx_k=st_idx_k,
en_idx_k=en_idx_k,
st_idx_q=st_idx_q,
en_idx_q=en_idx_q,
sort_key_by=sort_key_by,
sort_query_by=0)
# By QID
header_qid_link = url_for('both',
key_k=key_k,
st_idx_k=st_idx_k,
en_idx_k=en_idx_k,
st_idx_q=st_idx_q,
en_idx_q=en_idx_q,
sort_key_by=sort_key_by,
sort_query_by=1)
# Calculate the cluster statistics
clust_stats = cluster_query.get_statistics(clust, key_k, freq_list)
# Build the visualization on the right pane
action_freq = get_action_hist(key_k, qaction)
image_src = get_plot(action_freq)
# Send all the data with visualization if there are queries
if len(action_freq.keys()) > 0:
# When the plot exists
return render_template('fullpage.html',
total_count=tot_nonuniq,
uniq_count=tot_uniq,
left_prev_code=left_prev_code,
left_next_code=left_next_code,
allkeys=allkeys,
currentkey=currentkey,
header_unique_link=unique_link,
header_total_link=total_link,
right_prev_code=right_prev_code,
right_next_code=right_next_code,
allqueries=allqueries,
clust_stats=clust_stats,
image_src=image_src,
header_freq_link=header_freq_link,
header_qid_link=header_qid_link)
else:
# When the plot does not exist
return render_template('fullpage.html',
total_count=tot_nonuniq,
uniq_count=tot_uniq,
left_prev_code=left_prev_code,
left_next_code=left_next_code,
allkeys=allkeys,
currentkey=currentkey,
header_unique_link=unique_link,
header_total_link=total_link,
right_prev_code=right_prev_code,
right_next_code=right_next_code,
allqueries=allqueries,
clust_stats=clust_stats,
header_freq_link=header_freq_link,
header_qid_link=header_qid_link)
# Run the server
if __name__ == '__main__':
app.debug = False
app.run(host='0.0.0.0', port=PORT)