mirror of
https://gitlab.ub.uni-bielefeld.de/sfb1288inf/ocr.git
synced 2024-12-27 05:34:18 +00:00
Use argparse in hocrtotei
This commit is contained in:
parent
c39edec1ab
commit
b81ad4cc67
37
hocrtotei
37
hocrtotei
@ -1,21 +1,23 @@
|
|||||||
#!/usr/bin/env python3.5
|
#!/usr/bin/env python3.5
|
||||||
# coding=utf-8
|
# coding=utf-8
|
||||||
|
|
||||||
import xml.etree.ElementTree as ET
|
|
||||||
from xml.sax.saxutils import escape
|
from xml.sax.saxutils import escape
|
||||||
import os
|
import argparse
|
||||||
import re
|
import xml.etree.ElementTree as ET
|
||||||
import sys
|
|
||||||
|
|
||||||
input_files = sorted(
|
parser = argparse.ArgumentParser()
|
||||||
filter(
|
parser.add_argument(
|
||||||
lambda x: x.endswith(".hocr"),
|
'i',
|
||||||
os.listdir(sys.argv[1])
|
help='The input files.',
|
||||||
),
|
nargs='*',
|
||||||
key=lambda x: int(re.search(r'\d+', x).group(0))
|
|
||||||
)
|
)
|
||||||
# "page-1.hocr" -> "1"
|
parser.add_argument(
|
||||||
output_file = open(sys.argv[2], "w")
|
'o',
|
||||||
|
help='The output file.',
|
||||||
|
)
|
||||||
|
args = parser.parse_args()
|
||||||
|
|
||||||
|
output_file = open(args.o, "w")
|
||||||
|
|
||||||
output_file.write(
|
output_file.write(
|
||||||
'<?xml version="1.0" encoding="UTF-8"?>\n'
|
'<?xml version="1.0" encoding="UTF-8"?>\n'
|
||||||
@ -32,23 +34,20 @@ output_file.write(
|
|||||||
+ ' <text>\n'
|
+ ' <text>\n'
|
||||||
+ ' <body>\n'
|
+ ' <body>\n'
|
||||||
)
|
)
|
||||||
|
for index, input_file in enumerate(args.i):
|
||||||
for input_file in input_files:
|
tree = ET.parse(input_file)
|
||||||
tree = ET.parse(os.path.join(sys.argv[1], input_file))
|
output_file.write(' <pb n="%i"/>\n' % (index + 1))
|
||||||
page_number = int(re.search(r'\d+', input_file.split(".")[0]).group(0))
|
|
||||||
output_file.write(' <pb n="%i"/>\n' % (page_number))
|
|
||||||
for para in tree.findall(".//*[@class='ocr_par']"):
|
for para in tree.findall(".//*[@class='ocr_par']"):
|
||||||
output_file.write(' <p>\n')
|
output_file.write(' <p>\n')
|
||||||
for line in para.findall(".//*[@class='ocr_line']"):
|
for line in para.findall(".//*[@class='ocr_line']"):
|
||||||
first_word_in_line = True
|
first_word_in_line = True
|
||||||
for word in line.findall(".//*[@class='ocrx_word']"):
|
for word in line.findall(".//*[@class='ocrx_word']"):
|
||||||
if word.text is not None:
|
if word.text is not None:
|
||||||
output_file.write((" " if first_word_in_line else " ") + escape(word.text.strip()))
|
output_file.write((' ' if first_word_in_line else ' ') + escape(word.text.strip()))
|
||||||
first_word_in_line = False
|
first_word_in_line = False
|
||||||
if not first_word_in_line:
|
if not first_word_in_line:
|
||||||
output_file.write('<lb/>\n')
|
output_file.write('<lb/>\n')
|
||||||
output_file.write(' </p>\n')
|
output_file.write(' </p>\n')
|
||||||
|
|
||||||
output_file.write(
|
output_file.write(
|
||||||
' </body>\n'
|
' </body>\n'
|
||||||
+ ' </text>\n'
|
+ ' </text>\n'
|
||||||
|
11
ocr
11
ocr
@ -307,8 +307,15 @@ class OCRWorkflow(WorkflowRunner):
|
|||||||
'''
|
'''
|
||||||
hocr_to_tei_jobs = []
|
hocr_to_tei_jobs = []
|
||||||
for index, job in enumerate(self.jobs):
|
for index, job in enumerate(self.jobs):
|
||||||
cmd = 'hocrtotei "%s" "%s"' % (
|
files = os.listdir(os.path.join(job['output_dir'], 'tmp'))
|
||||||
os.path.join(job['output_dir'], 'tmp'),
|
files = filter(lambda x: x.endswith('.hocr'), files)
|
||||||
|
files.sort(key=lambda x: int(re.search(r'\d+', x).group(0)))
|
||||||
|
files = map(
|
||||||
|
lambda x: '"' + os.path.join(job['output_dir'], 'tmp', x) + '"',
|
||||||
|
files
|
||||||
|
)
|
||||||
|
cmd = 'hocrtotei %s "%s"' % (
|
||||||
|
' '.join(files),
|
||||||
os.path.join(
|
os.path.join(
|
||||||
job['output_dir'],
|
job['output_dir'],
|
||||||
os.path.join(job['output_dir'], job['name'] + '.xml')
|
os.path.join(job['output_dir'], job['name'] + '.xml')
|
||||||
|
Loading…
Reference in New Issue
Block a user