From 695dd78379705c6d26a58b8cfb10f4673d8311fc Mon Sep 17 00:00:00 2001 From: Cyprian Laskowski Date: Thu, 25 Mar 2021 15:02:50 +0100 Subject: [PATCH] Redmine #1835: improved and expanded API calls --- package/structure_assignment/api.py | 102 ++++++++++++++++++++++------ 1 file changed, 80 insertions(+), 22 deletions(-) diff --git a/package/structure_assignment/api.py b/package/structure_assignment/api.py index 6cd045d..6409e46 100644 --- a/package/structure_assignment/api.py +++ b/package/structure_assignment/api.py @@ -1,8 +1,9 @@ import os import shutil import tempfile +import lxml.etree as lxml -from flask import Flask, Response +from flask import Flask, Response, request from flask_httpauth import HTTPBasicAuth from structure_assignment.pipeline import Runner @@ -14,48 +15,105 @@ resource_directory = os.environ['API_RESOURCE_DIR'] runner = Runner(resource_directory, True) -@app.route(api_prefix + '/string_to_parse/', methods=['GET']) -def string_to_parse(string): +@app.route(api_prefix + '/string_to_parse', methods=['GET', 'POST']) +def string_to_parse(): tmp_directory = tempfile.mkdtemp() - string_file_name = tmp_directory + '/tmp/string.txt' - parsed_file_name = tmp_directory + '/tmp/parsed.xml' + string_file_name = tmp_directory + '/input_string.txt' + parsed_file_name = tmp_directory + '/output_parsed.xml' - with open(string_file_name, 'w') as string_file: - string_file.write(string + '\n') + if (request.method == 'GET'): + string = request.args.get('string') + with open(string_file_name, 'w') as string_file: + string_file.write(string + '\n') + elif (request.method == 'POST'): + file_data = request.files['file'] + file_data.save(string_file_name) try: runner.strings_to_parse(string_file_name, parsed_file_name) - root = lxml.parse(parse_file_name).getroot() + root = lxml.parse(parsed_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('' + str(e) + '').decode() + message = '' + str(e) + '' return Response(message, mimetype='text/xml') -@app.route(api_prefix + '/string_to_dictionary/', methods=['GET']) -def string_to_dictionary(string): +@app.route(api_prefix + '/parse_to_dictionary', methods=['POST']) +def parse_to_dictionary(): 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') + parsed_file_name = tmp_directory + '/input_parsed.xml' + dictionary_file_name = tmp_directory + '/output_dictionary.xml' + structure_file_name = tmp_directory + '/output_structures.xml' try: - runner.strings_to_dictionary(string_file_name, dictionary_file_name, structure_file_name) + + file_data = request.files['file'] + file_data.save(parsed_file_name) + + runner.parse_to_dictionary(parsed_file_name, dictionary_file_name, structure_file_name) + root = lxml.Element('response') + 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) + structure_root = lxml.parse(structure_file_name).getroot() + new_structure_count = len(structure_root.xpath('.//syntactic_structure[@tempId]')) + root.set('new_structures', str(new_structure_count)) + structure_ids = set(dictionary_root.xpath('.//lexicalUnit/@structure_id')) + structures = structure_root.xpath('syntactic_structure') + structures_element = lxml.SubElement(root, 'syntactic_structures') + for structure in structures: + if (structure.get('id') in structure_ids or structure.get('tempId') in structure_ids): + structures_element.append(structure) message = lxml.tostring(root, encoding='UTF-8', pretty_print=True).decode() shutil.rmtree(tmp_directory) + except Exception as e: - message = lxml.tostring('' + str(e) + '').decode() + message = '' + str(e) + '' return Response(message, mimetype='text/xml') + + +@app.route(api_prefix + '/string_to_dictionary', methods=['GET', 'POST']) +def string_to_dictionary(): + + tmp_directory = tempfile.mkdtemp() + string_file_name = tmp_directory + '/input_string.txt' + dictionary_file_name = tmp_directory + '/output_dictionary.xml' + structure_file_name = tmp_directory + '/output_structures.xml' + + try: + + if (request.method == 'GET'): + string = request.args.get('string') + with open(string_file_name, 'w') as string_file: + string_file.write(string + '\n') + elif (request.method == 'POST'): + file_data = request.files['file'] + file_data.save(string_file_name) + + runner.strings_to_dictionary(string_file_name, dictionary_file_name, structure_file_name) + root = lxml.Element('response') + + dictionary_root = lxml.parse(dictionary_file_name).getroot() + root.append(dictionary_root) + structure_root = lxml.parse(structure_file_name).getroot() + new_structure_count = len(structure_root.xpath('.//syntactic_structure[@tempId]')) + root.set('new_structures', str(new_structure_count)) + structure_ids = set(dictionary_root.xpath('.//lexicalUnit/@structure_id')) + structures = structure_root.xpath('syntactic_structure') + structures_element = lxml.SubElement(root, 'syntactic_structures') + for structure in structures: + if (structure.get('id') in structure_ids or structure.get('tempId') in structure_ids): + structures_element.append(structure) + message = lxml.tostring(root, encoding='UTF-8', pretty_print=True).decode() + shutil.rmtree(tmp_directory) + + except Exception as e: + message = '' + str(e) + '' + + return Response(message, mimetype='text/xml') +