forked from WheatonCS/Lexos
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lexos.py
733 lines (540 loc) · 30.6 KB
/
lexos.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
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
#!/usr/bin/python
# -*- coding: utf-8 -*-
import sys
import os
import chardet
import time
import re
from os import makedirs
from urllib import unquote
from flask import Flask, redirect, render_template, request, session, url_for, send_file
from models.ModelClasses import FileManager
import helpers.general_functions as general_functions
import helpers.session_functions as session_functions
import helpers.constants as constants
from os.path import join as pathjoin
# ------------
import numpy as np
app = Flask(__name__)
app.config['MAX_CONTENT_LENGTH'] = 4 * 1024 * 1024
@app.route("/", methods=["GET"]) # Tells Flask to load this function when someone is at '/'
def base():
"""
Page behavior for the base url ('/') of the site. Handles redirection to other pages.
Note: Returns a response object (often a render_template call) to flask and eventually
to the browser.
"""
if 'id' not in session:
session_functions.init() # Initialize the session if needed
return redirect(url_for('upload'))
@app.route("/reset", methods=["GET"]) # Tells Flask to load this function when someone is at '/reset'
def reset():
"""
Resets the session and initializes a new one every time the reset URL is used
(either manually or via the "Reset" button)
Note: Returns a response object (often a render_template call) to flask and eventually
to the browser.
"""
session_functions.reset() # Reset the session and session folder
session_functions.init() # Initialize the new session
return redirect(url_for('upload'))
@app.route("/upload", methods=["GET", "POST"]) # Tells Flask to load this function when someone is at '/upload'
def upload():
"""
Handles the functionality of the upload page. It uploads files to be used
in the current session.
Note: Returns a response object (often a render_template call) to flask and eventually
to the browser.
"""
if request.method == "GET":
return render_template('upload.html')
if 'X_FILENAME' in request.headers: # X_FILENAME is the flag to signify a file upload
# File upload through javascript
fileManager = session_functions.loadFileManager()
fileName = request.headers['X_FILENAME'] # Grab the filename, which will be UTF-8 percent-encoded (e.g. '%E7' instead of python's '\xe7')
if isinstance(fileName, unicode): # If the filename comes through as unicode
fileName = fileName.encode('ascii') # Convert to an ascii string
fileName = unquote(fileName).decode('utf-8') # Unquote using urllib's percent-encoding decoder (turns '%E7' into '\xe7'), then deocde it
# detect (and apply) the encoding type of the file's contents
# since chardet runs slow, initially detect (only) first 500 chars;
# if that fails, chardet entire file for a fuller test
try:
encodingDetect = chardet.detect(request.data[:500]) # Detect the encoding from the first 500 characters
encodingType = encodingDetect['encoding']
fileString = request.data.decode(encodingType) # Grab the file contents, which were encoded/decoded automatically into python's format
except:
encodingDetect = chardet.detect(request.data) # :( ... ok, detect the encoding from entire file
encodingType = encodingDetect['encoding']
fileString = request.data.decode(encodingType) # Grab the file contents, which were encoded/decoded automatically into python's format
fileManager.addFile(fileName, fileString) # Add the file to the FileManager
session_functions.saveFileManager(fileManager)
return 'success'
@app.route("/select", methods=["GET", "POST"]) # Tells Flask to load this function when someone is at '/select'
def select():
"""
Handles the functionality of the select page. Its primary role is to activate/deactivate
specific files depending on the user's input.
Note: Returns a response object (often a render_template call) to flask and eventually
to the browser.
"""
fileManager = session_functions.loadFileManager() # Usual loading of the FileManager
if request.method == "GET":
activePreviews = fileManager.getPreviewsOfActive()
inactivePreviews = fileManager.getPreviewsOfInactive()
return render_template('select.html', activeFiles=activePreviews, inactiveFiles=inactivePreviews)
if 'toggleFile' in request.headers:
# Catch-all for any POST request.
# On the select page, POSTs come from JavaScript AJAX XHRequests.
fileID = int(request.data)
fileManager.toggleFile(fileID) # Toggle the file from active to inactive or vice versa
elif 'setLabel' in request.headers:
newLabel = (request.headers['setLabel']).decode('utf-8')
fileID = int(request.data)
fileManager.files[fileID].label = newLabel
elif 'disableAll' in request.headers:
fileManager.disableAll()
elif 'selectAll' in request.headers:
fileManager.enableAll()
elif 'applyClassLabel' in request.headers:
fileManager.classifyActiveFiles()
elif 'deleteActive' in request.headers:
fileManager.deleteActiveFiles()
session_functions.saveFileManager(fileManager)
return '' # Return an empty string because you have to return something
@app.route("/scrub", methods=["GET", "POST"]) # Tells Flask to load this function when someone is at '/scrub'
def scrub():
"""
Handles the functionality of the scrub page. It scrubs the files depending on the
specifications chosen by the user, with an option to download the scrubbed files.
Note: Returns a response object (often a render_template call) to flask and eventually
to the browser.
"""
fileManager = session_functions.loadFileManager()
if request.method == "GET":
# "GET" request occurs when the page is first loaded.
if 'scrubbingoptions' not in session:
session['scrubbingoptions'] = constants.DEFAULT_SCRUB_OPTIONS
previews = fileManager.getPreviewsOfActive()
tagsPresent, DOEPresent = fileManager.checkActivesTags()
return render_template('scrub.html', previews=previews, haveTags=tagsPresent, haveDOE=DOEPresent)
if 'preview' in request.form or 'apply' in request.form:
#The 'Preview Scrubbing' or 'Apply Scrubbing' button is clicked on scrub.html.
session_functions.cacheAlterationFiles()
session_functions.cacheScrubOptions()
# saves changes only if 'Apply Scrubbing' button is clicked
savingChanges = True if 'apply' in request.form else False
previews = fileManager.scrubFiles(savingChanges=savingChanges)
tagsPresent, DOEPresent = fileManager.checkActivesTags()
if savingChanges:
session_functions.saveFileManager(fileManager)
return render_template('scrub.html', previews=previews, haveTags=tagsPresent, haveDOE=DOEPresent)
if 'download' in request.form:
# The 'Download Scrubbed Files' button is clicked on scrub.html.
# sends zipped files to downloads folder.
return fileManager.zipActiveFiles('scrubbed.zip')
@app.route("/cut", methods=["GET", "POST"]) # Tells Flask to load this function when someone is at '/cut'
def cut():
"""
Handles the functionality of the cut page. It cuts the files into various segments
depending on the specifications chosen by the user, and sends the text segments.
Note: Returns a response object (often a render_template call) to flask and eventually
to the browser.
"""
fileManager = session_functions.loadFileManager()
if request.method == "GET":
# "GET" request occurs when the page is first loaded.
if 'cuttingoptions' not in session:
session['cuttingoptions'] = constants.DEFAULT_CUT_OPTIONS
previews = fileManager.getPreviewsOfActive()
return render_template('cut.html', previews=previews, num_active_files=len(previews))
if 'preview' in request.form or 'apply' in request.form:
# The 'Preview Cuts' or 'Apply Cuts' button is clicked on cut.html.
session_functions.cacheCuttingOptions()
savingChanges = True if 'apply' in request.form else False # Saving changes only if apply in request form
previews = fileManager.cutFiles(savingChanges=savingChanges)
if savingChanges:
session_functions.saveFileManager(fileManager)
return render_template('cut.html', previews=previews, num_active_files=len(previews))
if 'downloadchunks' in request.form:
# The 'Download Segmented Files' button is clicked on cut.html
# sends zipped files to downloads folder
return fileManager.zipActiveFiles('cut_files.zip')
@app.route("/tokenizer", methods=["GET", "POST"]) # Tells Flask to load this function when someone is at '/tokenize'
def tokenizer():
"""
Handles the functionality on the tokenize page. It analyzes the texts to produce
and send various frequency matrices.
Note: Returns a response object (often a render_template call) to flask and eventually
to the browser.
"""
fileManager = session_functions.loadFileManager()
if request.method == "GET":
# "GET" request occurs when the page is first loaded.
if 'csvoptions' not in session:
session['csvoptions'] = constants.DEFAULT_CSV_OPTIONS
labels = fileManager.getActiveLabels()
return render_template('tokenizer.html', labels=labels)
if 'gen-csv' in request.form:
#The 'Generate and Visualize Matrix' button is clicked on tokenizer.html.
DocTermSparseMatrix, countMatrix = fileManager.generateCSVMatrix(roundDecimal=True)
countMatrix = zip(*countMatrix)
dtm = []
for row in xrange(1,len(countMatrix)):
dtm.append(list(countMatrix[row]))
matrixTitle = list(countMatrix[0])
matrixTitle[0] = "Token"
matrixTitle[0] = matrixTitle[0].encode("utf-8")
labels = fileManager.getActiveLabels()
session_functions.saveFileManager(fileManager)
return render_template('tokenizer.html', labels=labels, matrixData=dtm, matrixTitle=matrixTitle, matrixExist=True)
if 'get-csv' in request.form:
#The 'Download Matrix' button is clicked on tokenizer.html.
session_functions.cacheCSVOptions()
savePath, fileExtension = fileManager.generateCSV()
session_functions.saveFileManager(fileManager)
return send_file(savePath, attachment_filename="frequency_matrix"+fileExtension, as_attachment=True)
@app.route("/csvgenerator", methods=["GET", "POST"]) # Tells Flask to load this function when someone is at '/csvgenerator'
def csvgenerator():
"""
Handles the functionality on the csvgenerator page. It analyzes the texts to produce
and send various frequency matrices.
Note: Returns a response object (often a render_template call) to flask and eventually
to the browser.
"""
fileManager = session_functions.loadFileManager()
if request.method == "GET":
# "GET" request occurs when the page is first loaded.
if 'csvoptions' not in session:
session['csvoptions'] = constants.DEFAULT_CSV_OPTIONS
labels = fileManager.getActiveLabels()
return render_template('csvgenerator.html', labels=labels)
if 'get-csv' in request.form:
#The 'Generate and Download Matrix' button is clicked on csvgenerator.html.
session_functions.cacheCSVOptions()
savePath, fileExtension = fileManager.generateCSV()
session_functions.saveFileManager(fileManager)
return send_file(savePath, attachment_filename="frequency_matrix"+fileExtension, as_attachment=True)
@app.route("/hierarchy", methods=["GET", "POST"]) # Tells Flask to load this function when someone is at '/hierarchy'
def hierarchy():
"""
Handles the functionality on the hierarchy page. It analyzes the various texts and
displays a dendrogram.
Note: Returns a response object (often a render_template call) to flask and eventually
to the browser.
"""
fileManager = session_functions.loadFileManager()
ineq = '≤'.decode('utf-8')
if request.method == "GET":
# "GET" request occurs when the page is first loaded.
# if 'dendrogramoptions' not in session: # Default settings
# session['dendrogramoptions'] = constants.DEFAULT_DENDRO_OPTIONS
labels = fileManager.getActiveLabels()
thresholdOps={}
return render_template('hierarchy.html', labels=labels, thresholdOps=thresholdOps)
if 'dendro_download' in request.form:
# The 'Download Dendrogram' button is clicked on hierarchy.html.
# sends pdf file to downloads folder.
attachmentname = "den_"+request.form['title']+".pdf" if request.form['title'] != '' else 'dendrogram.pdf'
return send_file(pathjoin(session_functions.session_folder(),constants.RESULTS_FOLDER+"dendrogram.pdf"), attachment_filename=attachmentname, as_attachment=True)
if 'refreshThreshold' in request.form:
pdfPageNumber, score, inconsistentMax, maxclustMax, distanceMax, distanceMin, monocritMax, monocritMin, threshold = fileManager.generateDendrogram()
labels = fileManager.getActiveLabels()
inconsistentOp="0 " + ineq + " t " + ineq + " " + str(inconsistentMax)
maxclustOp= "2 " + ineq + " t " + ineq + " " + str(maxclustMax)
distanceOp= str(distanceMin) + " " + ineq + " t " + ineq + " " + str(distanceMax)
monocritOp= str(monocritMin) + " " + ineq + " t " + ineq + " " + str(monocritMax)
thresholdOps= {"inconsistent": inconsistentOp,"maxclust":maxclustOp,"distance":distanceOp,"monocrit":monocritOp}
return render_template('hierarchy.html', labels=labels, inconsistentMax=inconsistentMax, maxclustMax=maxclustMax, distanceMax=distanceMax, distanceMin=distanceMin, monocritMax=monocritMax, monocritMin=monocritMin, threshold=threshold, thresholdOps=thresholdOps, distanceList=distanceList)
if 'getdendro' in request.form:
#The 'Get Dendrogram' button is clicked on hierarchy.html.
pdfPageNumber, score, inconsistentMax, maxclustMax, distanceMax, distanceMin, monocritMax, monocritMin, threshold = fileManager.generateDendrogram()
session['dengenerated'] = True
labels = fileManager.getActiveLabels()
inconsistentOp="0 " + ineq + " t " + ineq + " " + str(inconsistentMax)
maxclustOp= "2 " + ineq + " t " + " " + str(maxclustMax)
distanceOp= str(distanceMin) + " " + ineq + " t " + ineq + " " + str(distanceMax)
monocritOp= str(monocritMin) + " " + ineq + " t " + ineq + " " + str(monocritMax)
thresholdOps= {"inconsistent": inconsistentOp,"maxclust":maxclustOp,"distance":distanceOp,"monocrit":monocritOp}
session_functions.saveFileManager(fileManager)
return render_template('hierarchy.html', labels=labels, pdfPageNumber=pdfPageNumber, score=score, inconsistentMax=inconsistentMax, maxclustMax=maxclustMax, distanceMax=distanceMax, distanceMin=distanceMin, monocritMax=monocritMax, monocritMin=monocritMin, threshold=threshold, thresholdOps=thresholdOps)
# @app.route("/dendrogram", methods=["GET", "POST"]) # Tells Flask to load this function when someone is at '/dendrogram'
# def dendrogram():
# """
# Handles the functionality on the dendrogram page. It analyzes the various texts and
# displays a dendrogram.
# Note: Returns a response object (often a render_template call) to flask and eventually
# to the browser.
# """
# fileManager = session_functions.loadFileManager()
# if request.method == "GET":
# # "GET" request occurs when the page is first loaded.
# # if 'dendrogramoptions' not in session: # Default settings
# # session['dendrogramoptions'] = constants.DEFAULT_DENDRO_OPTIONS
# labels = fileManager.getActiveLabels()
# return render_template('dendrogram.html', labels=labels)
# if 'dendro_download' in request.form:
# # The 'Download Dendrogram' button is clicked on dendrogram.html.
# # sends pdf file to downloads folder.
# attachmentname = "den_"+request.form['title']+".pdf" if request.form['title'] != '' else 'dendrogram.pdf'
# return send_file(pathjoin(session_functions.session_folder(),constants.RESULTS_FOLDER+"dendrogram.pdf"), attachment_filename=attachmentname, as_attachment=True)
# if 'getdendro' in request.form:
# #The 'Get Dendrogram' button is clicked on dendrogram.html.
# pdfPageNumber = fileManager.generateDendrogram()
# session['dengenerated'] = True
# labels = fileManager.getActiveLabels()
# return render_template('dendrogram.html', labels=labels, pdfPageNumber = pdfPageNumber)
@app.route("/dendrogramimage", methods=["GET", "POST"]) # Tells Flask to load this function when someone is at '/dendrogramimage'
def dendrogramimage():
"""
Reads the png image of the dendrogram and displays it on the web browser.
*dendrogramimage() linked to in analysis.html, displaying the dendrogram.png
Note: Returns a response object with the dendrogram png to flask and eventually to the browser.
"""
# dendrogramimage() is called in analysis.html, displaying the dendrogram.png (if session['dengenerated'] != False).
imagePath = pathjoin(session_functions.session_folder(), constants.RESULTS_FOLDER, constants.DENDROGRAM_FILENAME)
return send_file(imagePath)
@app.route("/rollingwindow", methods=["GET", "POST"]) # Tells Flask to load this function when someone is at '/rollingwindow'
def rollingwindow():
"""
Handles the functionality on the rollingwindow page. It analyzes the various
texts using a rolling window of analysis.
Note: Returns a response object (often a render_template call) to flask and eventually
to the browser.
"""
fileManager = session_functions.loadFileManager()
if request.method == "GET":
#"GET" request occurs when the page is first loaded.
labels = fileManager.getActiveLabels()
session['rwadatagenerated'] = False
#default legendlabels
legendLabels = [""]
return render_template('rwanalysis.html', labels=labels, legendLabels=legendLabels)
if request.method == "POST":
#"POST" request occurs when user hits submit (Get Graph) button
labels = fileManager.getActiveLabels()
dataPoints, dataList, graphTitle, xAxisLabel, yAxisLabel, legendLabels = fileManager.generateRWA()
session['rwadatagenerated'] = True
if 'get-RW-plot' in request.form:
#The 'Generate and Download Matrix' button is clicked on csvgenerator.html.
savePath, fileExtension = fileManager.generateRWmatrixPlot(dataPoints, legendLabels)
return send_file(savePath, attachment_filename="rollingwindow_matrix"+fileExtension, as_attachment=True)
if 'get-RW-data' in request.form:
#The 'Generate and Download Matrix' button is clicked on csvgenerator.html.
savePath, fileExtension = fileManager.generateRWmatrix(dataList)
return send_file(savePath, attachment_filename="rollingwindow_matrix"+fileExtension, as_attachment=True)
return render_template('rwanalysis.html', labels=labels,
data=dataPoints,
graphTitle=graphTitle,
xAxisLabel=xAxisLabel,
yAxisLabel=yAxisLabel,
legendLabels=legendLabels)
@app.route("/wordcloud", methods=["GET", "POST"]) # Tells Flask to load this function when someone is at '/wordcloud'
def wordcloud():
"""
Handles the functionality on the visualisation page -- a prototype for displaying
single word cloud graphs.
Note: Returns a response object (often a render_template call) to flask and eventually
to the browser.
"""
fileManager = session_functions.loadFileManager()
if request.method == "GET":
# "GET" request occurs when the page is first loaded.
labels = fileManager.getActiveLabels()
return render_template('wordcloud.html', labels=labels)
if request.method == "POST":
# "POST" request occur when html form is submitted (i.e. 'Get Dendrogram', 'Download...')
labels = fileManager.getActiveLabels()
JSONObj = fileManager.generateJSONForD3(mergedSet=True)
# Create a list of column values for the word count table
from operator import itemgetter
terms = sorted(JSONObj["children"], key=itemgetter('size'), reverse=True)
columnValues = []
for term in terms:
rows = [term["name"], term["size"]]
columnValues.append(rows)
return render_template('wordcloud.html', labels=labels, JSONObj=JSONObj, columnValues=columnValues)
@app.route("/multicloud", methods=["GET", "POST"]) # Tells Flask to load this function when someone is at '/multicloud'
def multicloud():
"""
Handles the functionality on the multicloud pages.
Note: Returns a response object (often a render_template call) to flask and eventually
to the browser.
"""
fileManager = session_functions.loadFileManager()
if 'multicloudoptions' not in session:
session['multicloudoptions'] = constants.DEFAULT_MC_OPTIONS
folderPath = pathjoin(session_functions.session_folder(), constants.RESULTS_FOLDER)
if (not os.path.isdir(folderPath)):
makedirs(folderPath)
malletPath = pathjoin(folderPath, "topicFile")
if request.method == 'GET':
# 'GET' request occurs when the page is first loaded.
labels = fileManager.getActiveLabels()
return render_template('multicloud.html', jsonStr="", labels=labels)
if request.method == "POST":
# 'POST' request occur when html form is submitted (i.e. 'Get Graphs', 'Download...')
labels = fileManager.getActiveLabels()
JSONObj = fileManager.generateMCJSONObj(malletPath)
return render_template('multicloud.html', JSONObj = JSONObj, labels=labels, loading='loading')
@app.route("/viz", methods=["GET", "POST"]) # Tells Flask to load this function when someone is at '/viz'
def viz():
"""
Handles the functionality on the alternate bubbleViz page with performance improvements.
Note: Returns a response object (often a render_template call) to flask and eventually
to the browser.
"""
fileManager = session_functions.loadFileManager()
if request.method == "GET":
# "GET" request occurs when the page is first loaded.
labels = fileManager.getActiveLabels()
return render_template('viz.html', JSONObj="", labels=labels)
if request.method == "POST":
# "POST" request occur when html form is submitted (i.e. 'Get Dendrogram', 'Download...')
labels = fileManager.getActiveLabels()
JSONObj = fileManager.generateJSONForD3(mergedSet=True)
return render_template('viz.html', JSONObj=JSONObj, labels=labels, loading='loading')
@app.route("/extension", methods=["GET", "POST"]) # Tells Flask to load this function when someone is at '/extension'
def extension():
"""
Handles the functionality on the External Tools page -- a prototype for displaying
possible external analysis options.
Note: Returns a response object (often a render_template call) to flask and eventually
to the browser.
"""
return render_template('extension.html')
@app.route("/clustering", methods=["GET", "POST"]) # Tells Flask to load this function when someone is at '/clustering'
def clustering():
"""
Menu page for clustering. Let's you select either hierarchical or kmeans clustering page.
"""
fileManager = session_functions.loadFileManager()
labels = fileManager.getActiveLabels()
if request.method == 'GET':
# 'GET' request occurs when the page is first loaded
return render_template('clustering.html', labels=labels)
if request.method == "POST":
# 'POST' request occur when html form is submitted (i.e. 'Get Graphs', 'Download...')
return render_template('clustering.html', labels=labels)
@app.route("/kmeans", methods=["GET", "POST"]) # Tells Flask to load this function when someone is at '/kmeans'
def kmeans():
"""
Handles the functionality on the kmeans page. It analyzes the various texts and
displays the class label of the files.
Note: Returns a response object (often a render_template call) to flask and eventually
to the browser.
"""
fileManager = session_functions.loadFileManager()
labels = fileManager.getActiveLabels()
defaultK = int(len(labels)/2)
if request.method == 'GET':
# 'GET' request occurs when the page is first loaded
session['kmeansdatagenerated'] = False
return render_template('kmeans.html', labels=labels, silhouettescore='', kmeansIndex=[], fileNameStr='', fileNumber=len(labels), KValue=0, defaultK=defaultK)
if request.method == "POST":
# 'POST' request occur when html form is submitted (i.e. 'Get Graphs', 'Download...')
session['kmeansdatagenerated'] = True
kmeansIndex, silhouetteScore, fileNameStr, KValue = fileManager.generateKMeans()
session_functions.saveFileManager(fileManager)
return render_template('kmeans.html', labels=labels, silhouettescore=silhouetteScore, kmeansIndex=kmeansIndex, fileNameStr=fileNameStr, fileNumber=len(labels), KValue=KValue, defaultK=defaultK)
@app.route("/similarity", methods=["GET", "POST"]) # Tells Flask to load this function when someone is at '/similarity'
def similarity():
"""
Handles the similarity query page functionality. Returns ranked list of files and their cosine similarities to a comparison document.
"""
fileManager = session_functions.loadFileManager()
labels = fileManager.getActiveLabels()
if 'uploadname' not in session:
session['similarities'] = constants.DEFAULT_MC_OPTIONS
if request.method == 'GET':
# 'GET' request occurs when the page is first loaded
similaritiesgenerated = False
return render_template('similarity.html', labels=labels, docsListScore="", docsListName="", similaritiesgenerated=similaritiesgenerated)
if request.method == "POST":
# 'POST' request occur when html form is submitted (i.e. 'Get Graphs', 'Download...')
compFile = request.form['uploadname']
docsListScore, docsListName = fileManager.generateSimilarities(compFile)
similaritiesgenerated = True
return render_template('similarity.html', labels=labels, docsListScore=docsListScore, docsListName=docsListName, similaritiesgenerated=similaritiesgenerated)
@app.route("/topword", methods=["GET", "POST"]) # Tells Flask to load this function when someone is at '/topword'
def topword():
"""
Handles the topword page functionality. Returns ranked list of topwords
"""
fileManager = session_functions.loadFileManager()
labels = fileManager.getActiveLabels()
if 'uploadname' not in session:
session['topword'] = constants.DEFAULT_MC_OPTIONS
if request.method == 'GET':
# 'GET' request occurs when the page is first loaded
return render_template('topword.html', labels=labels, docsListScore="", docsListName="", topwordsgenerated=False)
if request.method == "POST":
# 'POST' request occur when html form is submitted (i.e. 'Get Graphs', 'Download...')
inputFiles = request.form['chunkgroups']
docsListScore, docsListName = fileManager.generateSimilarities(inputFiles)
return render_template('topword.html', labels=labels, docsListScore=docsListScore, docsListName=docsListName, topwordsgenerated=True)
# =================== Helpful functions ===================
def install_secret_key(fileName='secret_key'):
"""
Creates an encryption key for a secure session.
Args:
fileName: A string representing the secret key.
Returns:
None
"""
fileName = os.path.join(app.static_folder, fileName)
try:
app.config['SECRET_KEY'] = open(fileName, 'rb').read()
except IOError:
print 'Error: No secret key. Create it with:'
if not os.path.isdir(os.path.dirname(fileName)):
print 'mkdir -p', os.path.dirname(fileName)
print 'head -c 24 /dev/urandom >', fileName
sys.exit(1)
# ================ End of Helpful functions ===============
# =========== Temporary development functions =============
@app.route("/select2", methods=["GET", "POST"]) # Tells Flask to load this function when someone is at '/select'
def select2():
"""
Handles the functionality of the select page. Its primary role is to activate/deactivate
specific files depending on the user's input.
Note: Returns a response object (often a render_template call) to flask and eventually
to the browser.
"""
fileManager = session_functions.loadFileManager() # Usual loading of the FileManager
if request.method == "GET":
activePreviews = fileManager.getPreviewsOfActive()
inactivePreviews = fileManager.getPreviewsOfInactive()
return render_template('select2.html', activeFiles=activePreviews, inactiveFiles=inactivePreviews)
if 'toggleFile' in request.headers:
# Catch-all for any POST request.
# On the select page, POSTs come from JavaScript AJAX XHRequests.
fileID = int(request.data)
fileManager.toggleFile(fileID) # Toggle the file from active to inactive or vice versa
elif 'setLabel' in request.headers:
newLabel = (request.headers['setLabel']).decode('utf-8')
fileID = int(request.data)
fileManager.files[fileID].label = newLabel
elif 'disableAll' in request.headers:
fileManager.disableAll()
elif 'selectAll' in request.headers:
fileManager.enableAll()
elif 'applyClassLabel' in request.headers:
fileManager.classifyActiveFiles()
elif 'deleteActive' in request.headers:
fileManager.deleteActiveFiles()
session_functions.saveFileManager(fileManager)
return '' # Return an empty string because you have to return something
# ======== End of temporary development functions ==========
install_secret_key()
app.debug = True
app.jinja_env.filters['type'] = type
app.jinja_env.filters['str'] = str
app.jinja_env.filters['tuple'] = tuple
app.jinja_env.filters['len'] = len
app.jinja_env.filters['unicode'] = unicode
app.jinja_env.filters['time'] = time.time()
app.jinja_env.filters['natsort'] = general_functions.natsort
# app.config['PROFILE'] = True
# app.wsgi_app = ProfilerMiddleware(app.wsgi_app, restrictions = [30])
if __name__ == '__main__':
app.run()