From 02c1dcec8e722f6b3e4004c7099518cd55cddd09 Mon Sep 17 00:00:00 2001 From: Jim Barlow Date: Fri, 13 Feb 2015 13:41:14 -0800 Subject: [PATCH] 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. --- src/ocrpage.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/src/ocrpage.py b/src/ocrpage.py index b5b3bcfe..fc5d3041 100755 --- a/src/ocrpage.py +++ b/src/ocrpage.py @@ -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,