|
|
|
#!/usr/bin/env python3
|
|
|
|
|
|
|
|
import os, sys
|
|
|
|
from sys import stdout
|
|
|
|
import flask
|
|
|
|
from flask import request
|
|
|
|
|
|
|
|
import tfidf
|
|
|
|
import readings
|
|
|
|
|
|
|
|
|
|
|
|
# Create the application.
|
|
|
|
APP = flask.Flask(__name__)
|
|
|
|
|
|
|
|
# Jinja filters
|
|
|
|
def prettyfilename(filename):
|
|
|
|
return filename.replace('_', ' ').replace('-', ' ').replace('.txt', '')
|
|
|
|
APP.jinja_env.filters['prettyfilename'] = prettyfilename
|
|
|
|
|
|
|
|
# Jinja globals
|
|
|
|
def get_random(x, y):
|
|
|
|
from random import randint
|
|
|
|
return randint(x, y)
|
|
|
|
APP.jinja_env.globals.update(get_random=get_random)
|
|
|
|
|
|
|
|
@APP.route('/', methods=['GET', 'POST'])
|
|
|
|
def index():
|
|
|
|
"""
|
|
|
|
Displays the index page accessible at '/'
|
|
|
|
Which is either the start page (using the index.html template)
|
|
|
|
or a results page (using the results.html template).
|
|
|
|
"""
|
|
|
|
query = None
|
|
|
|
results = None
|
|
|
|
|
|
|
|
query = request.args.get('q', '')
|
|
|
|
suggestions = open('words.txt', 'r').readlines()
|
|
|
|
|
|
|
|
if request.args.get('q', ''):
|
|
|
|
filenames, results, analytics = readings.request_results(query)
|
|
|
|
return flask.render_template('results.html', query=query, results=results, filenames=filenames, suggestions=suggestions, analytics=analytics)
|
|
|
|
|
|
|
|
else:
|
|
|
|
index = readings.load_index()
|
|
|
|
filenames = [document for document, _ in index.items()]
|
|
|
|
return flask.render_template('index.html', filenames=filenames, suggestions=suggestions)
|
|
|
|
|
|
|
|
@APP.route('/document/<name>', methods=['GET', 'POST'])
|
|
|
|
def open_document(name):
|
|
|
|
"""
|
|
|
|
Read document.
|
|
|
|
"""
|
|
|
|
index = readings.load_index()
|
|
|
|
suggestions = open('words.txt', 'r').readlines()
|
|
|
|
filenames = sorted([document for document, _ in index.items()])
|
|
|
|
txt = open('txt/{}.txt'.format(name), 'r').readlines()
|
|
|
|
return flask.render_template('document.html', filenames=filenames, txt=txt, suggestions=suggestions)
|
|
|
|
|
|
|
|
@APP.route('/list/<list_type>/<filename>', methods=['GET', 'POST'])
|
|
|
|
def render_list_for_document(list_type, filename):
|
|
|
|
"""
|
|
|
|
Show list of (TF / IDF / TF-IDF) values of one document.
|
|
|
|
"""
|
|
|
|
index = readings.load_index()
|
|
|
|
suggestions = open('words.txt', 'r').readlines()
|
|
|
|
filenames = sorted([document for document, _ in index.items()])
|
|
|
|
values_list = [(value, word) for word, value in index[filename][list_type].items()]
|
|
|
|
return flask.render_template('list.html', filenames=filenames, suggestions=suggestions, list=values_list, list_type=list_type, filename=filename)
|
|
|
|
|
|
|
|
@APP.route('/mappings/<mapping_type>', methods=['GET', 'POST'])
|
|
|
|
def get_contrast_mappings(mapping_type):
|
|
|
|
"""
|
|
|
|
Displays the page accessible at '/mappings'.
|
|
|
|
A listed overview of all values in the dataset is displayed.
|
|
|
|
"""
|
|
|
|
mappings, filenames = readings.request_mappings(mapping_type)
|
|
|
|
suggestions = open('words.txt', 'r').readlines()
|
|
|
|
return flask.render_template('mappings.html', filenames=filenames, suggestions=suggestions, mappings=mappings, mapping_type=mapping_type)
|
|
|
|
|
|
|
|
@APP.route('/mapping/<name>', methods=['GET', 'POST'])
|
|
|
|
def contrast_mappings_name(name):
|
|
|
|
"""
|
|
|
|
Displays the page accessible at '/mappings/<name>'.
|
|
|
|
A TF-IDF visualisation is displayed from the specific document, using the TF-IDF values as font-size.
|
|
|
|
"""
|
|
|
|
mappings, filenames = readings.request_mappings_for_document(name)
|
|
|
|
print(name)
|
|
|
|
suggestions = open('words.txt', 'r').readlines()
|
|
|
|
return flask.render_template('mappings.name.html', filenames=filenames, suggestions=suggestions, mappings=mappings[name], document=name)
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
if not 'index.json' in os.listdir('.'):
|
|
|
|
tfidf.create_index()
|
|
|
|
APP.debug=True
|
|
|
|
APP.run(port=5001)
|