2018-10-29 09:38:50 +00:00
#!/usr/bin/env python3.5
2018-10-09 12:43:23 +00:00
# coding=utf-8
from xml.sax.saxutils import escape
2019-05-16 12:21:01 +00:00
import argparse
import xml.etree.ElementTree as ET
2018-10-09 12:43:23 +00:00
2019-05-16 12:59:22 +00:00
parser = argparse.ArgumentParser(description='hocrtotei merges several hOCR files in order of their occurrence on command line to one TEI result file.')
2019-05-16 12:21:01 +00:00
parser.add_argument(
'i',
2019-05-16 12:59:22 +00:00
metavar='hOCR-sourcefile',
nargs='+'
2019-05-16 12:21:01 +00:00
)
parser.add_argument(
'o',
2019-05-16 12:59:22 +00:00
metavar='TEI-destfile',
2019-05-13 13:03:43 +00:00
)
2019-05-16 12:21:01 +00:00
args = parser.parse_args()
output_file = open(args.o, "w")
2018-10-09 12:43:23 +00:00
2019-05-13 13:03:43 +00:00
output_file.write(
'<?xml version="1.0" encoding="UTF-8"?>\n'
+ '<TEI xmlns="http://www.tei-c.org/ns/1.0" xml:id="dtabf">\n'
+ ' <teiHeader>\n'
+ ' <fileDesc>\n'
+ ' <titleStmt/>\n'
+ ' <publicationStmt/>\n'
+ ' <sourceDesc/>\n'
+ ' </fileDesc>\n'
+ ' <encodingDesc/>\n'
+ ' <profileDesc/>\n'
+ ' </teiHeader>\n'
+ ' <text>\n'
+ ' <body>\n'
)
2019-05-16 12:21:01 +00:00
for index, input_file in enumerate(args.i):
tree = ET.parse(input_file)
output_file.write(' <pb n="%i"/>\n' % (index + 1))
2018-10-09 12:43:23 +00:00
for para in tree.findall(".//*[@class='ocr_par']"):
output_file.write(' <p>\n')
for line in para.findall(".//*[@class='ocr_line']"):
first_word_in_line = True
for word in line.findall(".//*[@class='ocrx_word']"):
if word.text is not None:
2019-05-16 12:21:01 +00:00
output_file.write((' ' if first_word_in_line else ' ') + escape(word.text.strip()))
2018-10-09 12:43:23 +00:00
first_word_in_line = False
if not first_word_in_line:
output_file.write('<lb/>\n')
output_file.write(' </p>\n')
2019-05-13 13:03:43 +00:00
output_file.write(
' </body>\n'
+ ' </text>\n'
+ '</TEI>')
2019-04-14 12:33:40 +00:00
output_file.close()