nlp/vrt-creator

37 lines
1.2 KiB
Plaintext
Raw Normal View History

#!/usr/bin/env python3.7
# coding=utf-8
from argparse import ArgumentParser
2021-08-06 14:50:22 +00:00
from stand_off_data import StandOffData
import hashlib
import json
def main():
# Parse the given arguments
parser = ArgumentParser(description='Create a vrt from JSON and txt')
parser.add_argument('text', help='Path to txt file')
parser.add_argument('stand_off_data', help='Path to JSON file')
parser.add_argument('output', help='Path to vrt output file')
args = parser.parse_args()
with open(args.stand_off_data) as stand_of_data_file:
2021-08-06 14:50:22 +00:00
stand_off_data = StandOffData(json.load(stand_of_data_file))
with open(args.text, "rb") as text_file:
text_md5 = hashlib.md5()
for chunk in iter(lambda: text_file.read(128 * text_md5.block_size), b''): # noqa
text_md5.update(chunk)
2021-08-06 14:50:22 +00:00
if text_md5.hexdigest() != stand_off_data.meta['file']['md5']:
raise Exception('md5 not equal')
2021-08-10 12:43:55 +00:00
with open(args.text, encoding=stand_off_data.meta['file']['encoding']) as text_file:
text = text_file.read()
with open(args.output, 'w') as vrt_file:
vrt_file.write(stand_off_data.to_vrt(text))
if __name__ == '__main__':
main()