Added visualization of sentences.
This commit is contained in:
@@ -1,10 +1,13 @@
|
||||
import configparser
|
||||
import csv
|
||||
import json
|
||||
import os
|
||||
import random
|
||||
import re
|
||||
import shutil
|
||||
import string
|
||||
import time
|
||||
from pathlib import Path
|
||||
|
||||
import requests
|
||||
from flask import Flask, render_template, request, send_file, redirect, url_for
|
||||
@@ -14,7 +17,7 @@ from stark import run
|
||||
|
||||
UPLOAD_FOLDER = 'uploads'
|
||||
ALLOWED_EXTENSIONS = {'conllu'}
|
||||
DAYS_BEFORE_DELETION = 1
|
||||
DAYS_BEFORE_DELETION = 7
|
||||
DEFAULT_LANGUAGE = 'en'
|
||||
LANGUAGES = ['en', 'sl']
|
||||
|
||||
@@ -120,22 +123,39 @@ def create_app():
|
||||
def about():
|
||||
return render_template('about.html')
|
||||
|
||||
@app.route('/stark/visualization/<result_id>/<sentence_id>/<subtree_ids>', methods=['GET'])
|
||||
def visualization(result_id, sentence_id, subtree_ids):
|
||||
annodoc = ''
|
||||
subtree_ids = subtree_ids.split('+')
|
||||
for subtree_id in subtree_ids:
|
||||
annodoc += f'# visual-style {subtree_id} bgColor:lightgreen\n'
|
||||
with open(os.path.join('media', result_id, 'annodoc', sentence_id), 'r') as rf:
|
||||
# annodoc += '\n'.join(rf.readlines())
|
||||
annodoc += rf.read() + '\n\n'
|
||||
return {'annodoc': annodoc}
|
||||
# return '<div>' + annodoc + '</div>'
|
||||
|
||||
@app.route('/stark/result/<result_id>', methods=['GET', 'POST'])
|
||||
def result(result_id):
|
||||
|
||||
|
||||
if request.method == 'POST':
|
||||
for filename in os.listdir('media'):
|
||||
file_path = os.path.join('media', filename)
|
||||
path = os.path.join('media', filename)
|
||||
if os.path.isdir(path):
|
||||
file_path = os.path.join('media', filename, 'result.tsv')
|
||||
else:
|
||||
file_path = path
|
||||
f_t = os.path.getmtime(file_path)
|
||||
c_t = time.time()
|
||||
file_age_seconds = c_t - f_t
|
||||
if file_age_seconds > DAYS_BEFORE_DELETION * 86400:
|
||||
os.remove(file_path)
|
||||
return send_file(os.path.join('media', result_id), as_attachment=True, download_name='results.tsv')
|
||||
if os.path.isdir(path):
|
||||
shutil.rmtree(os.path.join('media', filename), ignore_errors=True)
|
||||
else:
|
||||
os.remove(path)
|
||||
return send_file(os.path.join('media', result_id, 'result.tsv'), as_attachment=True, download_name='results.tsv')
|
||||
|
||||
|
||||
with open(os.path.join('media', result_id), 'r') as rf:
|
||||
with open(os.path.join('media', result_id, 'result.tsv'), 'r') as rf:
|
||||
|
||||
content = list(csv.reader(rf, delimiter='\t'))
|
||||
head = content[0]
|
||||
@@ -191,6 +211,12 @@ def create_app():
|
||||
else:
|
||||
displayed_content_dict[f_h] = content_dict[displayed_table_columns2table_columns[h]]
|
||||
|
||||
# add visualization parts to dict
|
||||
annodoc_data = [json.loads(el) for el in content_dict['Annodoc']]
|
||||
displayed_content_dict['example_id'] = [el['id'] for el in annodoc_data]
|
||||
displayed_content_dict['example_positions'] = ['+'.join([str(p) for p in el['positions']]) for el in annodoc_data]
|
||||
|
||||
|
||||
return render_template('result.html', head=head, content=displayed_content_dict)
|
||||
|
||||
@app.route('/stark/', methods=['GET', 'POST'])
|
||||
@@ -330,6 +356,7 @@ def create_app():
|
||||
configs['frequency_threshold'] = int(form['frequency_threshold'])
|
||||
|
||||
configs['lines_threshold'] = None
|
||||
configs['node_info'] = None
|
||||
|
||||
configs['continuation_processing'] = False
|
||||
|
||||
@@ -342,9 +369,17 @@ def create_app():
|
||||
|
||||
configs['grew_match'] = True
|
||||
configs['depsearch'] = False
|
||||
configs['example'] = False
|
||||
configs['example'] = True
|
||||
name = ''.join(random.choices(string.ascii_uppercase + string.digits, k=60))
|
||||
configs['output'] = os.path.join('media', name)
|
||||
|
||||
|
||||
output_path = Path(os.path.join('media', name))
|
||||
if output_path.exists():
|
||||
shutil.rmtree(output_path, ignore_errors=True)
|
||||
output_path.mkdir()
|
||||
|
||||
configs['output'] = os.path.join(output_path, 'result.tsv')
|
||||
configs['annodoc_example_dir'] = os.path.join(output_path, 'annodoc')
|
||||
if len(validation) > 0:
|
||||
return render_template('index.html', validation=validation, translations=translations)
|
||||
try:
|
||||
@@ -354,7 +389,7 @@ def create_app():
|
||||
if len(validation) > 0:
|
||||
return render_template('index.html', validation=validation, translations=translations)
|
||||
# check if there are no results
|
||||
with open(os.path.join('media', name), 'r') as rf:
|
||||
with open(os.path.join('media', name, 'result.tsv'), 'r') as rf:
|
||||
content = list(csv.reader(rf, delimiter='\t'))
|
||||
if len(content) == 1:
|
||||
validation['results'] = False
|
||||
|
||||
Reference in New Issue
Block a user