Redmine #1835: updated api to use refactored pipeline

This commit is contained in:
Cyprian Laskowski 2021-03-18 21:59:28 +01:00
parent 753cfad794
commit b931955dcb

View File

@ -1,44 +1,61 @@
import os
import lxml.etree as lxml
import shutil
import tempfile
from flask import Flask, Response
from flask_httpauth import HTTPBasicAuth
from structure_assignment.pipeline import Pipeline, create_nlp
from structure_assignment.pipeline import Runner
app = Flask(__name__)
api_prefix = os.environ['API_PREFIX']
resource_directory = os.environ['API_RESOURCE_DIR']
nlp = create_nlp(resource_directory)
runner = Runner(resource_directory, True)
@app.route(api_prefix + '/test/<string:string>', methods=['GET'])
def test(string):
string_file_name = '/tmp/string.txt'
parse_file_name = '/tmp/parse.xml'
@app.route(api_prefix + '/string_to_parse/<string:string>', methods=['GET'])
def string_to_parse(string):
tmp_directory = tempfile.mkdtemp()
string_file_name = tmp_directory + '/tmp/string.txt'
parsed_file_name = tmp_directory + '/tmp/parsed.xml'
with open(string_file_name, 'w') as string_file:
string_file.write(string + '\n')
try:
# pipeline = Pipeline(nlp)
# pipeline.import_file(string_file_name, 'strings-list')
# pipeline.do_tokenise()
# pipeline.do_tweak_conllu()
# pipeline.do_parse()
# pipeline.do_translate_jos()
# pipeline.do_conllu_to_tei()
# pipeline.export_file(parse_file_name, 'tei-initial')
# pipeline.cleanup()
import sys
sys.path.insert(0, resource_directory)
print(sys.path)
import wani
tei = lxml.parse(parse_file_name).getroot()
message = lxml.tostring(tei, encoding='UTF-8', pretty_print=True).decode()
runner.strings_to_parse(string_file_name, parsed_file_name)
root = lxml.parse(parse_file_name).getroot()
message = lxml.tostring(root, encoding='UTF-8', pretty_print=True).decode()
shutil.rmtree(tmp_directory)
except Exception as e:
message = lxml.tostring('<error>' + str(e) + '</error>').decode()
return Response(message, mimetype='text/xml')
@app.route(api_prefix + '/string_to_dictionary/<string:string>', methods=['GET'])
def string_to_dictionary(string):
tmp_directory = tempfile.mkdtemp()
string_file_name = tmp_directory + '/tmp/string.txt'
dictionary_file_name = tmp_directory + '/tmp/dict.xml'
structure_file_name = tmp_directory + '/tmp/structures.xml'
with open(string_file_name, 'w') as string_file:
string_file.write(string + '\n')
try:
runner.strings_to_dictionary(string_file_name, dictionary_file_name, structure_file_name)
dictionary_root = lxml.parse(dictionary_file_name).getroot()
structure_root = lxml.parse(structure_file_name).getroot()
root = lxml.Element('root')
root.append(structure_root)
root.append(dictionary_root)
message = lxml.tostring(root, encoding='UTF-8', pretty_print=True).decode()
shutil.rmtree(tmp_directory)
except Exception as e:
message = lxml.tostring('<error>' + str(e) + '</error>').decode()
return Response(message, mimetype='text/xml')