Codestyle

This commit is contained in:
Patrick Jentsch
2019-09-11 16:15:41 +02:00
parent 43717de313
commit af293d6141
2 changed files with 53 additions and 64 deletions

View File

@ -7,29 +7,25 @@ import spacy
import textwrap
parser = argparse.ArgumentParser(
description='Tag a text file with spaCy and save it as a verticalized text file.'
)
parser.add_argument(
'i',
metavar='txt-sourcefile',
)
parser.add_argument(
'-l',
choices=['de', 'el', 'en', 'es', 'fr', 'it', 'nl', 'pt'],
dest='lang',
required=True
)
parser.add_argument(
'o',
metavar='vrt-destfile',
description=('Tag a text file with spaCy and save it as a verticalized '
'text file.')
)
parser.add_argument('i', metavar='txt-sourcefile')
parser.add_argument('-l',
choices=['de', 'el', 'en', 'es', 'fr', 'it', 'nl', 'pt'],
dest='lang',
required=True)
parser.add_argument('o', metavar='vrt-destfile')
args = parser.parse_args()
SPACY_MODELS = {
'de': 'de_core_news_sm', 'el': 'el_core_news_sm', 'en': 'en_core_web_sm',
'es': 'es_core_news_sm', 'fr': 'fr_core_news_sm', 'it': 'it_core_news_sm',
'nl': 'nl_core_news_sm', 'pt': 'pt_core_news_sm'
}
SPACY_MODELS = {'de': 'de_core_news_sm',
'el': 'el_core_news_sm',
'en': 'en_core_web_sm',
'es': 'es_core_news_sm',
'fr': 'fr_core_news_sm',
'it': 'it_core_news_sm',
'nl': 'nl_core_news_sm',
'pt': 'pt_core_news_sm'}
# Set the language model for spacy
nlp = spacy.load(SPACY_MODELS[args.lang])
@ -45,9 +41,9 @@ with open(args.i) as input_file:
output_file = open(args.o, 'w+')
output_file.write(
'<?xml version="1.0" encoding="UTF-8"?>\n<corpus>\n<text id="%s">\n' % (
os.path.basename(args.i).rsplit(".", 1)[0]
)
'<?xml version="1.0" encoding="UTF-8"?>\n'
'<corpus>\n'
'<text id="{}">\n'.format(os.path.basename(args.i).rsplit(".", 1)[0])
)
for text in texts:
# Run spacy nlp over the text (partial string if above 1 million chars)
@ -61,11 +57,18 @@ for text in texts:
# Write all information in .vrt style to the output file
# text, lemma, simple_pos, pos, ner
output_file.write(
token.text + '\t' + token.lemma_ + '\t'
+ token.pos_ + '\t' + token.tag_ + '\t'
+ (token.ent_type_ if token.ent_type_ != '' else 'NULL') + '\n'
'{}\t{}\t{}\t{}\t{}\n'.format(
token.text,
token.lemma_,
token.pos_,
token.tag_,
token.ent_type_ if token.ent_type_ != '' else 'NULL'
)
)
output_file.write('</s>\n')
output_file.write('</text>\n</corpus>')
output_file.write(
'</text>\n'
'</corpus>'
)
output_file.close()