2020-04-06 07:21:52 +00:00
|
|
|
#!/usr/bin/env python3.7
|
2018-10-09 12:43:23 +00:00
|
|
|
# coding=utf-8
|
|
|
|
|
2021-02-19 12:04:03 +00:00
|
|
|
""""Merges hOCR files into a TEI file."""
|
|
|
|
|
2018-10-09 12:43:23 +00:00
|
|
|
from xml.sax.saxutils import escape
|
2020-04-03 15:35:30 +00:00
|
|
|
from argparse import ArgumentParser
|
2021-03-15 11:45:05 +00:00
|
|
|
import re
|
2019-05-16 12:21:01 +00:00
|
|
|
import xml.etree.ElementTree as ET
|
2018-10-09 12:43:23 +00:00
|
|
|
|
2021-02-19 12:04:03 +00:00
|
|
|
parser = ArgumentParser(description='Merges hOCR files into a TEI file.')
|
2021-03-15 11:45:05 +00:00
|
|
|
parser.add_argument('i', metavar='hOCR-sourcefile')
|
|
|
|
parser.add_argument('o', metavar='TEI-destfile')
|
2019-05-16 12:21:01 +00:00
|
|
|
args = parser.parse_args()
|
|
|
|
|
2019-05-20 09:10:40 +00:00
|
|
|
output_file = open(args.o, 'w')
|
2019-05-13 13:03:43 +00:00
|
|
|
output_file.write(
|
|
|
|
'<?xml version="1.0" encoding="UTF-8"?>\n'
|
2021-03-15 11:45:05 +00:00
|
|
|
+ '<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-13 13:03:43 +00:00
|
|
|
)
|
2021-03-15 11:45:05 +00:00
|
|
|
tree = ET.parse(args.i)
|
|
|
|
for page in tree.findall('.//*[@class="ocr_page"]'):
|
|
|
|
page_properties = page.attrib.get('title')
|
|
|
|
facsimile = re.search(r'image \"(.*?)\"', page_properties).group(1)
|
|
|
|
page_number = re.search(r'ppageno (\d+)', page_properties).group(1)
|
|
|
|
output_file.write(' <pb facs="%s" n="%s"/>\n' % (facsimile, page_number)) # noqa
|
|
|
|
for para in page.findall('.//*[@class="ocr_par"]'):
|
2018-10-09 12:43:23 +00:00
|
|
|
output_file.write(' <p>\n')
|
2019-05-20 09:10:40 +00:00
|
|
|
for line in para.findall('.//*[@class="ocr_line"]'):
|
2021-03-15 11:45:05 +00:00
|
|
|
output_file.write(' <lb/>')
|
|
|
|
indent = ''
|
2019-05-20 09:10:40 +00:00
|
|
|
for word in line.findall('.//*[@class="ocrx_word"]'):
|
2018-10-09 12:43:23 +00:00
|
|
|
if word.text is not None:
|
2021-03-15 11:45:05 +00:00
|
|
|
output_file.write(indent + escape(word.text.strip()))
|
|
|
|
indent = ' '
|
|
|
|
output_file.write('\n')
|
2018-10-09 12:43:23 +00:00
|
|
|
output_file.write(' </p>\n')
|
2019-05-13 13:03:43 +00:00
|
|
|
output_file.write(
|
|
|
|
' </body>\n'
|
2021-03-15 11:45:05 +00:00
|
|
|
+ ' </text>\n'
|
|
|
|
+ '</TEI>'
|
|
|
|
)
|
2019-04-14 12:33:40 +00:00
|
|
|
output_file.close()
|