2020-11-04 18:02:15 +00:00
|
|
|
import argparse
|
|
|
|
import os
|
|
|
|
import shutil
|
|
|
|
import lxml.etree as lxml
|
|
|
|
|
|
|
|
from constants import *
|
|
|
|
|
|
|
|
arg_parser = argparse.ArgumentParser(description='Assign parsed Slovene strings to structures and generate lexicon.')
|
2020-12-15 15:59:41 +00:00
|
|
|
arg_parser.add_argument('-intei', type=str, required=True, help='Parsed and manually edited TEI file')
|
2021-01-13 20:27:42 +00:00
|
|
|
arg_parser.add_argument('-instructures', type=str, required=True, help='Input structure file')
|
2020-12-15 15:59:41 +00:00
|
|
|
arg_parser.add_argument('-outlexicon', type=str, required=True, help='Output lexicon file')
|
|
|
|
arg_parser.add_argument('-outstructures', type=str, required=True, help='Output structure file')
|
2020-11-04 18:02:15 +00:00
|
|
|
arguments = arg_parser.parse_args()
|
2020-12-15 15:59:41 +00:00
|
|
|
|
2020-11-04 18:02:15 +00:00
|
|
|
input_tei_file_name = arguments.intei
|
2021-01-13 20:27:42 +00:00
|
|
|
input_structure_file_name = arguments.instructures
|
2020-11-10 13:07:44 +00:00
|
|
|
output_lexicon_file_name = arguments.outlexicon
|
2020-12-15 15:59:41 +00:00
|
|
|
output_structure_file_name = arguments.outstructures
|
2020-11-04 18:02:15 +00:00
|
|
|
|
2021-01-13 20:27:42 +00:00
|
|
|
def run_pipeline(input_tei_file_name, input_structure_file_name, output_lexicon_file_name, output_structure_file_name):
|
2020-12-15 16:00:09 +00:00
|
|
|
|
|
|
|
# setup and split
|
2020-11-04 18:02:15 +00:00
|
|
|
shutil.rmtree(TMP_DIRECTORY, True)
|
|
|
|
os.makedirs(TMP_DIRECTORY, exist_ok=True)
|
|
|
|
shutil.copyfile(input_tei_file_name, TEI_INIT_FILE_NAME)
|
2021-01-13 20:27:42 +00:00
|
|
|
shutil.copyfile(input_structure_file_name, STRUCTURE_OLD_FILE_NAME)
|
2020-12-15 16:00:09 +00:00
|
|
|
split_tei_input(TEI_INIT_FILE_NAME, TEI_SINGLE_FILE_NAME, TEI_MULTIPLE_FILE_NAME)
|
|
|
|
|
|
|
|
# single-token units
|
2021-01-13 20:27:42 +00:00
|
|
|
run_structure_single_assignment(TEI_SINGLE_FILE_NAME, STRUCTURE_OLD_FILE_NAME, TEI_SINGLE_STRUCTURE_FILE_NAME)
|
2021-01-13 20:13:59 +00:00
|
|
|
run_dictionary_conversion(TEI_SINGLE_STRUCTURE_FILE_NAME, DICTIONARY_SINGLE_FILE_NAME)
|
2020-12-15 16:00:09 +00:00
|
|
|
|
|
|
|
# multiple-token units
|
2021-01-13 20:27:42 +00:00
|
|
|
run_mwe_extraction(STRUCTURE_OLD_FILE_NAME, TEI_MULTIPLE_FILE_NAME, MWE_CSV_1_FILE_NAME)
|
2021-01-13 20:13:59 +00:00
|
|
|
run_structure_multiple_assignment(TEI_MULTIPLE_FILE_NAME, MWE_CSV_1_FILE_NAME, TEI_MULTIPLE_STRUCTURE_1_FILE_NAME)
|
2021-01-13 20:27:42 +00:00
|
|
|
run_structure_creation(STRUCTURE_OLD_FILE_NAME, TEI_MULTIPLE_STRUCTURE_1_FILE_NAME, STRUCTURE_NEW_FILE_NAME)
|
2020-11-04 18:02:15 +00:00
|
|
|
validate_structures(STRUCTURE_NEW_FILE_NAME)
|
2020-12-15 16:00:09 +00:00
|
|
|
run_mwe_extraction(STRUCTURE_NEW_FILE_NAME, TEI_MULTIPLE_FILE_NAME, MWE_CSV_2_FILE_NAME)
|
2021-01-13 20:13:59 +00:00
|
|
|
run_structure_multiple_assignment(TEI_MULTIPLE_FILE_NAME, MWE_CSV_2_FILE_NAME, TEI_MULTIPLE_STRUCTURE_2_FILE_NAME)
|
|
|
|
run_dictionary_conversion(TEI_MULTIPLE_STRUCTURE_2_FILE_NAME, DICTIONARY_MULTIPLE_FILE_NAME)
|
2020-12-15 16:00:09 +00:00
|
|
|
|
|
|
|
# merge and finish
|
|
|
|
merge_dictionaries(DICTIONARY_SINGLE_FILE_NAME, DICTIONARY_MULTIPLE_FILE_NAME, DICTIONARY_FILE_NAME)
|
|
|
|
validate_dictionary(DICTIONARY_FILE_NAME)
|
|
|
|
shutil.copyfile(DICTIONARY_FILE_NAME, output_lexicon_file_name)
|
2020-11-04 18:02:15 +00:00
|
|
|
shutil.copyfile(STRUCTURE_NEW_FILE_NAME, output_structure_file_name)
|
|
|
|
|
2020-12-15 16:00:09 +00:00
|
|
|
def split_tei_input(input_file_name, single_file_name, multiple_file_name):
|
|
|
|
print('Splitting TEI input file ...')
|
2021-01-13 18:07:35 +00:00
|
|
|
split_command = ' '.join(['python', TEI_SPLIT_SCRIPT_NAME, '-infile', input_file_name, '-single', single_file_name, '-multiple', multiple_file_name])
|
2020-12-15 16:00:09 +00:00
|
|
|
os.system(split_command)
|
|
|
|
|
2020-11-04 18:02:15 +00:00
|
|
|
def run_mwe_extraction(structure_file_name, tei_file_name, mwe_csv_file_name):
|
|
|
|
print('Extracting MWEs from tei ...')
|
2021-01-13 18:08:17 +00:00
|
|
|
extraction_command = ' '.join(['python', MWE_EXTRACTION_SCRIPT_NAME, structure_file_name, tei_file_name, '--all', mwe_csv_file_name, '--skip-id-check', '--fixed-restriction-order', '--new-tei'])
|
2020-11-04 18:02:15 +00:00
|
|
|
print(extraction_command)
|
|
|
|
os.system(extraction_command)
|
|
|
|
|
|
|
|
def validate_structures(structure_file_name):
|
|
|
|
print('Validating updated structure specifications ...')
|
|
|
|
xml_schema = lxml.XMLSchema(lxml.parse(STRUCTURE_SCHEMA_FILE_NAME))
|
|
|
|
xml_tree = lxml.parse(structure_file_name)
|
|
|
|
xml_schema.assertValid(xml_tree)
|
|
|
|
|
2021-01-13 20:13:59 +00:00
|
|
|
def run_structure_single_assignment(input_file_name, structure_file_name, output_file_name):
|
|
|
|
print('Assigning structure ids ...')
|
|
|
|
assignment_command = ' '.join(['python', STRUCTURE_SINGLE_ASSIGNMENT_SCRIPT_NAME, '-infile', input_file_name, '-structures', structure_file_name, '-outfile', output_file_name])
|
|
|
|
os.system(assignment_command)
|
|
|
|
|
|
|
|
def run_structure_multiple_assignment(tei_file_name, mwe_csv_file_name, output_file_name):
|
2020-11-04 18:02:15 +00:00
|
|
|
print('Assigning structure ids ...')
|
2020-11-19 16:03:44 +00:00
|
|
|
assignment_command = ' '.join(['python', STRUCTURE_ASSIGNMENT_SCRIPT_NAME, tei_file_name, mwe_csv_file_name, output_file_name])
|
2020-11-04 18:02:15 +00:00
|
|
|
os.system(assignment_command)
|
|
|
|
|
|
|
|
def run_structure_creation(input_file_name, tei_file_name, output_file_name):
|
|
|
|
print('Creating missing structures ...')
|
|
|
|
creation_command = ' '.join(['python', STRUCTURE_CREATION_SCRIPT_NAME, '-infile', input_file_name, '-tei', tei_file_name, '-outfile', output_file_name])
|
|
|
|
os.system(creation_command)
|
|
|
|
|
|
|
|
def run_dictionary_conversion(tei_file_name, xml_file_name):
|
|
|
|
print('Converting to dictionary xml format ...')
|
2021-01-13 18:07:35 +00:00
|
|
|
convert_command = ' '.join(['python', TEI_DICTIONARY_SCRIPT_NAME, '-infile', tei_file_name, '-outfile', xml_file_name])
|
2020-11-04 18:02:15 +00:00
|
|
|
os.system(convert_command)
|
|
|
|
|
2021-01-13 18:07:35 +00:00
|
|
|
def merge_dictionaries(single_file_name, multiple_file_name, output_file_name):
|
2020-12-15 16:00:09 +00:00
|
|
|
print('Merging dictionary files ...')
|
2021-01-13 18:07:35 +00:00
|
|
|
merge_command = ' '.join(['python', DICTIONARY_MERGE_SCRIPT_NAME, '-single', single_file_name, '-multiple', multiple_file_name, '-outfile', output_file_name])
|
2020-12-15 16:00:09 +00:00
|
|
|
os.system(merge_command)
|
|
|
|
|
2020-11-04 18:02:15 +00:00
|
|
|
def validate_dictionary(dictionary_file_name):
|
|
|
|
print('Validating output dictionary file ...')
|
|
|
|
xml_schema = lxml.XMLSchema(lxml.parse(DICTIONARY_SCHEMA_FILE_NAME))
|
|
|
|
xml_tree = lxml.parse(dictionary_file_name)
|
|
|
|
xml_schema.assertValid(xml_tree)
|
|
|
|
|
2021-01-13 20:27:42 +00:00
|
|
|
run_pipeline(input_tei_file_name, input_structure_file_name, output_lexicon_file_name, output_structure_file_name)
|