From 30404f53f033e78111a255b888cef1028f01f264 Mon Sep 17 00:00:00 2001 From: "James R. Barlow" Date: Mon, 22 Jun 2020 16:18:38 -0700 Subject: [PATCH] Add test to sanity check our pdf renderers --- src/ocrmypdf/hocrtransform.py | 1 + tests/test_hocrtransform.py | 58 ++++++++++++++++++++++++++++++++++- 2 files changed, 58 insertions(+), 1 deletion(-) diff --git a/src/ocrmypdf/hocrtransform.py b/src/ocrmypdf/hocrtransform.py index 7275dd17..606a2850 100755 --- a/src/ocrmypdf/hocrtransform.py +++ b/src/ocrmypdf/hocrtransform.py @@ -219,6 +219,7 @@ class HocrTransform: chain( self.hocr.iterfind(self._child_xpath('span', 'ocr_header')), self.hocr.iterfind(self._child_xpath('span', 'ocr_line')), + self.hocr.iterfind(self._child_xpath('span', 'ocr_textfloat')), ), key=self.topdown_position, ): diff --git a/tests/test_hocrtransform.py b/tests/test_hocrtransform.py index e8ed04bb..cf128a52 100644 --- a/tests/test_hocrtransform.py +++ b/tests/test_hocrtransform.py @@ -15,20 +15,46 @@ # You should have received a copy of the GNU General Public License # along with OCRmyPDF. If not, see . +import re +from io import StringIO +from pathlib import Path + import pytest +from pdfminer.converter import TextConverter +from pdfminer.layout import LAParams +from pdfminer.pdfdocument import PDFDocument +from pdfminer.pdfinterp import PDFPageInterpreter, PDFResourceManager +from pdfminer.pdfpage import PDFPage +from pdfminer.pdfparser import PDFParser from PIL import Image from ocrmypdf import hocrtransform from ocrmypdf._exec.tesseract import HOCR_TEMPLATE from ocrmypdf.helpers import check_pdf + +def text_from_pdf(filename): + output_string = StringIO() + with open(filename, 'rb') as in_file: + parser = PDFParser(in_file) + doc = PDFDocument(parser) + rsrcmgr = PDFResourceManager() + device = TextConverter(rsrcmgr, output_string, laparams=LAParams()) + interpreter = PDFPageInterpreter(rsrcmgr, device) + for page in PDFPage.create_pages(doc): + interpreter.process_page(page) + return output_string.getvalue() + + # pylint: disable=redefined-outer-name +check_ocrmypdf = pytest.helpers.check_ocrmypdf # pylint: disable=no-member + @pytest.fixture def blank_hocr(tmp_path): filename = tmp_path / "blank.hocr" - filename.write_text(HOCR_TEMPLATE) # pylint: disable=E1101 + filename.write_text(HOCR_TEMPLATE) return filename @@ -42,3 +68,33 @@ def test_mono_image(blank_hocr, outdir): hocr.to_pdf(str(outdir / 'mono.pdf'), image_filename=str(outdir / 'mono.tif')) check_pdf(str(outdir / 'mono.pdf')) + + +def test_hocrtransform_matches_sandwich(resources, outdir): + check_ocrmypdf( + resources / 'ccitt.pdf', + outdir / 'hocr.pdf', + '--pdf-renderer=hocr', + # '--plugin', + # 'tests/plugins/tesseract_cache.py', + ) + check_ocrmypdf( + resources / 'ccitt.pdf', + outdir / 'tess.pdf', + '--pdf-renderer=sandwich', + # '--plugin', + # 'tests/plugins/tesseract_cache.py', + ) + + def clean(s): + s = re.sub(r'[ ]+', ' ', s) + s = re.sub(r'[ ]?[\n]+', r'\n', s) + return s + + hocr_txt = clean(text_from_pdf(outdir / 'hocr.pdf')) + tess_txt = clean(text_from_pdf(outdir / 'tess.pdf')) + + # Path('hocr.txt').write_text(hocr_txt) + # Path('tess.txt').write_text(tess_txt) + + assert hocr_txt == tess_txt