Remove filenames from .hocr files

As documented, Tesseract does not escape the filename when inserting it
into .hocr, potentially creating an invalid XML file as a result. Since
there is no use for the title, regex it and nuke it.
This commit is contained in:
Jim Barlow
2015-02-13 13:41:14 -08:00
parent 52dc74d3ce
commit 02c1dcec8e
+13
View File
@@ -5,6 +5,8 @@ import argparse
import logging
import sys
import os.path
import fileinput
import re
from parse import parse
from subprocess import Popen, check_call, PIPE, CalledProcessError, \
@@ -522,6 +524,17 @@ def ocr_tesseract(
re_symlink(output_file + ".hocr", output_file,
logger, logger_mutex)
# The filename gets inserted to hocr
# but Tesseract does not verify that it is escaped XML
# it's not necessary so strip it out
regex_nested_single_quotes = re.compile(
r"""title='image "([^"]*)";""")
with fileinput.input(files=(output_file,), inplace=True) as f:
for line in f:
line = regex_nested_single_quotes.sub(
r"""title='image " ";""", line)
print(line, end='') # stdout is redirected here
@active_if(ocr_required)
@merge([unpack_with_ghostscript, convert_to_png,