Update file handling. Now md5 is correct

This commit is contained in:
Patrick Jentsch
2021-05-18 10:26:03 +02:00
parent bd5d8ddedb
commit fd39246e4b
4 changed files with 20 additions and 15 deletions

View File

@ -16,29 +16,32 @@ spacy_models = {spacy.info(pipeline)['lang']: pipeline
# Parse the given arguments
parser = ArgumentParser(description='Create annotations for a given txt file')
parser.add_argument('input', metavar='Path to txt input file')
parser.add_argument('output', metavar='Path to JSON output file')
parser.add_argument('input', help='Path to txt input file')
parser.add_argument('output', help='Path to JSON output file')
parser.add_argument('-l', '--language',
choices=spacy_models.keys(),
help='Language of the input (2-character ISO 639-1 language codes)', # noqa
required=True)
parser.add_argument('-c', '--check-encoding', action='store_true')
parser.add_argument('-c', '--check-encoding',
action='store_true',
help='Check encoding of the input file, UTF-8 is used instead') # noqa
args = parser.parse_args()
if args.check_encoding:
with open(args.input, "rb") as text_file:
if args.check_encoding:
encoding = chardet.detect(text_file.read())['encoding']
else:
encoding = 'utf-8'
# If requested: Check the encoding of the text contents from the input file
# Else: Use utf-8
with open(args.input, "rb") as text_file:
if args.check_encoding:
encoding = chardet.detect(text_file.read())['encoding']
else:
encoding = 'utf-8'
text_md5 = hashlib.md5()
for chunk in iter(lambda: text_file.read(128 * text_md5.block_size), b''):
text_md5.update(chunk)
# Load the text contents from the input file
with open(args.input, encoding=encoding) as text_file:
# spaCy NLP is limited to strings with maximum 1 million characters at
# spaCy NLP is limited to strings with a maximum of 1 million characters at
# once. So we split it into suitable chunks.
text_chunks = textwrap.wrap(
text_file.read(),