2020-11-04 18:02:15 +00:00
|
|
|
import argparse
|
|
|
|
import os
|
|
|
|
import shutil
|
|
|
|
import codecs
|
|
|
|
|
|
|
|
import classla
|
|
|
|
from classla import Document
|
|
|
|
from classla.models.common.conll import CoNLLFile
|
|
|
|
|
|
|
|
from constants import *
|
|
|
|
|
|
|
|
arg_parser = argparse.ArgumentParser(description='Parse Slovene strings and convert to TEI.')
|
|
|
|
arg_parser.add_argument('-inlist', type=str, help='Input list file')
|
|
|
|
arg_parser.add_argument('-outtei', type=str, help='Output TEI file')
|
|
|
|
arguments = arg_parser.parse_args()
|
2020-11-10 13:07:44 +00:00
|
|
|
input_file_name = arguments.inlist
|
|
|
|
output_file_name = arguments.outtei
|
2020-11-04 18:02:15 +00:00
|
|
|
|
|
|
|
NLP_CONFIG_MAP = {
|
|
|
|
'treebank': 'sl_ssj_jos',
|
|
|
|
'processors': 'tokenize,pos,lemma,depparse',
|
|
|
|
'tokenize_pretokenized': True,
|
|
|
|
'models_dir': CLASSLA_MODELS_DIRECTORY
|
|
|
|
}
|
|
|
|
|
|
|
|
XML_ID_PREFIX = 's'
|
|
|
|
|
|
|
|
def run_pipeline(input_file_name, output_file_name):
|
|
|
|
shutil.rmtree(TMP_DIRECTORY, True)
|
|
|
|
os.makedirs(TMP_DIRECTORY, exist_ok=True)
|
|
|
|
shutil.copyfile(input_file_name, STRING_LIST_FILE_NAME)
|
2021-01-12 09:00:40 +00:00
|
|
|
run_obeliks(STRING_LIST_FILE_NAME, OBELIKS_RAW_FILE_NAME)
|
2020-12-02 22:51:28 +00:00
|
|
|
tweak_conllu(OBELIKS_RAW_FILE_NAME, OBELIKS_TWEAKED_FILE_NAME)
|
2020-11-04 18:02:15 +00:00
|
|
|
run_classla(OBELIKS_TWEAKED_FILE_NAME, CLASSLA_FILE_NAME)
|
|
|
|
run_tei_conversion(CLASSLA_FILE_NAME, TEI_INIT_FILE_NAME)
|
|
|
|
shutil.copyfile(TEI_INIT_FILE_NAME, output_file_name)
|
|
|
|
|
2021-01-12 09:00:40 +00:00
|
|
|
def run_obeliks(list_file_name, conllu_file_name):
|
2020-11-04 18:02:15 +00:00
|
|
|
print('Running obeliks ...')
|
2021-01-12 09:00:40 +00:00
|
|
|
obeliks_command = ' '.join(['obeliks', '-c', '-if', list_file_name, '-o', conllu_file_name])
|
2020-11-04 18:02:15 +00:00
|
|
|
os.system(obeliks_command)
|
|
|
|
|
2020-12-02 22:51:28 +00:00
|
|
|
def tweak_conllu(input_file_name, output_file_name):
|
|
|
|
print('Tweaking conllu results ...')
|
|
|
|
tweak_command = ' '.join(['python', CONLLU_TWEAK_SCRIPT_NAME, '-infile', input_file_name, '-outfile', output_file_name])
|
|
|
|
os.system(tweak_command)
|
2020-11-04 18:02:15 +00:00
|
|
|
|
|
|
|
def run_classla(obeliks_file_name, classla_file_name):
|
|
|
|
print('Running classla ...')
|
|
|
|
doc = Document(text=None)
|
|
|
|
conll_file = CoNLLFile(filename=obeliks_file_name)
|
|
|
|
doc.conll_file = conll_file
|
|
|
|
nlp = classla.Pipeline('sl', **NLP_CONFIG_MAP)
|
|
|
|
result = nlp(doc)
|
|
|
|
result.conll_file.write_conll(classla_file_name)
|
|
|
|
|
|
|
|
def run_tei_conversion(classla_file_name, tei_file_name):
|
|
|
|
print('Converting to tei ...')
|
2021-01-08 08:58:33 +00:00
|
|
|
convert_command = ' '.join(['python', CONLLU_TEI_SCRIPT_NAME, '-o', tei_file_name, classla_file_name])
|
|
|
|
print(convert_command)
|
2020-11-04 18:02:15 +00:00
|
|
|
os.system(convert_command)
|
|
|
|
|
|
|
|
run_pipeline(input_file_name, output_file_name)
|