nlp/vrt-creator
Patrick Jentsch 0ba0c14b72 First attempt
2021-08-10 14:43:55 +02:00

37 lines
1.2 KiB
Python
Executable File

#!/usr/bin/env python3.7
# coding=utf-8
from argparse import ArgumentParser
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:
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)
if text_md5.hexdigest() != stand_off_data.meta['file']['md5']:
raise Exception('md5 not equal')
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()