from __future__ import annotations import importlib.util import json import sys from pathlib import Path import pytest from ocrtech.schemas import BBox, Block, Document, Page, TextLine def _load_tool(): path = Path(__file__).resolve().parents[1] / "tools" / "assemble_page_ocr_lines.py" spec = importlib.util.spec_from_file_location("assemble_page_ocr_lines", path) assert spec is not None assert spec.loader is not None module = importlib.util.module_from_spec(spec) sys.modules[spec.name] = module spec.loader.exec_module(module) return module def _load_audit_tool(): path = Path(__file__).resolve().parents[1] / "tools" / "audit_page_ocr_line_assembly.py" spec = importlib.util.spec_from_file_location("audit_page_ocr_line_assembly", path) assert spec is not None assert spec.loader is not None module = importlib.util.module_from_spec(spec) sys.modules[spec.name] = module spec.loader.exec_module(module) return module def _write_manifest(path: Path, sample_id: str = "sample-1") -> None: path.write_text( json.dumps( { "sample_id": sample_id, "dataset": "unit", "image_path": "page.png", "reference_path": "reference.json", "language": "Limbu", "script": "Devanagari", "domain": "unit", "split": "eval", "claim_eligible": False, } ) + "\n", encoding="utf-8", ) def _block_for(line: TextLine, order: int) -> Block: return Block( block_id=f"block-{line.line_id}", block_type="text", page_index=line.page_index, bbox=line.bbox, order=order, text=line.text, confidence=line.confidence, line_ids=[str(line.line_id)], ) def _write_document(path: Path) -> None: lines = [ TextLine( text="पे़रे़म्साप्", bbox=BBox(100, 20, 120, 30), confidence=0.80, page_index=0, line_id="frag-a", metadata={"structural_role": "text"}, ), TextLine( text="कुरे़ङ्साप्", bbox=BBox(260, 22, 110, 30), confidence=0.70, page_index=0, line_id="frag-b", metadata={"structural_role": "text"}, ), TextLine( text="long line should not merge", bbox=BBox(100, 80, 520, 30), confidence=0.90, page_index=0, line_id="long", metadata={"structural_role": "text"}, ), TextLine( text="२", bbox=BBox(100, 130, 20, 20), confidence=0.95, page_index=0, line_id="marker", metadata={"structural_role": "page_marker"}, ), TextLine( text="body", bbox=BBox(150, 130, 50, 20), confidence=0.95, page_index=0, line_id="body", metadata={"structural_role": "text"}, ), ] doc = Document( source_path="page.png", pages=[ Page( page_index=0, width=1000, height=200, text_lines=lines, blocks=[_block_for(line, order) for order, line in enumerate(lines)], ) ], ) path.parent.mkdir(parents=True, exist_ok=True) path.write_text(json.dumps(doc.to_dict(), ensure_ascii=False, indent=2) + "\n", encoding="utf-8") def test_assemble_page_ocr_lines_merges_adjacent_same_row_fragments(tmp_path: Path) -> None: tool = _load_tool() manifest = tmp_path / "manifest.jsonl" documents = tmp_path / "docs" _write_manifest(manifest) _write_document(documents / "sample-1" / "document.json") result = tool.main( [ "--eval-manifest", str(manifest), "--documents-dir", str(documents), "--out", str(tmp_path / "assembled"), "--y-tolerance", "8", "--max-gap", "80", "--max-merged-width", "320", "--max-component-width", "180", ] ) assert result == 0 run = json.loads((tmp_path / "assembled" / "ocr-line-assembly-run.json").read_text(encoding="utf-8")) assert run["source_line_count"] == 5 assert run["assembled_line_count"] == 4 assert run["merged_group_count"] == 1 assert run["merged_source_line_count"] == 2 assembled = Document.from_dict( json.loads((tmp_path / "assembled" / "sample-1" / "document.json").read_text(encoding="utf-8")) ) page = assembled.pages[0] assert [line.text for line in page.text_lines] == [ "पे़रे़म्साप् कुरे़ङ्साप्", "long line should not merge", "२", "body", ] merged = page.text_lines[0] assert merged.line_id == "assembled-0001" assert merged.bbox.to_list() == [100.0, 20.0, 270.0, 32.0] assert merged.confidence == 0.70 assert merged.metadata["line_assembly"]["source_line_ids"] == ["frag-a", "frag-b"] assert page.blocks[0].text == "पे़रे़म्साप् कुरे़ङ्साप्" assert page.blocks[0].line_ids == ["assembled-0001"] assert assembled.metadata["ocr_line_assembly"]["max_merged_width"] == 320.0 reports = [ json.loads(line) for line in (tmp_path / "assembled" / "ocr-line-assembly-lines.jsonl").read_text(encoding="utf-8").splitlines() ] assert reports[0]["assembled"] is True assert reports[0]["source_line_ids"] == ["frag-a", "frag-b"] assert "component_width_above_max" in reports[1]["blocked_next_reasons"] assert "non_text_structural_role" in reports[2]["blocked_next_reasons"] def test_assemble_page_ocr_lines_allows_configured_small_horizontal_overlap(tmp_path: Path) -> None: tool = _load_tool() manifest = tmp_path / "manifest.jsonl" documents = tmp_path / "docs" _write_manifest(manifest) line_a = TextLine( text="left", bbox=BBox(100, 20, 120, 30), confidence=0.90, page_index=0, line_id="left", metadata={"structural_role": "text"}, ) line_b = TextLine( text="right", bbox=BBox(215, 21, 100, 30), confidence=0.85, page_index=0, line_id="right", metadata={"structural_role": "text"}, ) doc = Document( source_path="page.png", pages=[ Page( page_index=0, width=500, height=100, text_lines=[line_a, line_b], blocks=[_block_for(line_a, 0), _block_for(line_b, 1)], ) ], ) document_path = documents / "sample-1" / "document.json" document_path.parent.mkdir(parents=True) document_path.write_text(json.dumps(doc.to_dict(), ensure_ascii=False, indent=2) + "\n", encoding="utf-8") tool.main( [ "--eval-manifest", str(manifest), "--documents-dir", str(documents), "--out", str(tmp_path / "assembled"), "--y-tolerance", "8", "--max-gap", "80", "--max-horizontal-overlap", "8", ] ) assembled = Document.from_dict( json.loads((tmp_path / "assembled" / "sample-1" / "document.json").read_text(encoding="utf-8")) ) assert [line.text for line in assembled.pages[0].text_lines] == ["left right"] assert assembled.metadata["ocr_line_assembly"]["max_horizontal_overlap"] == 8.0 def test_assemble_page_ocr_lines_rejects_bad_thresholds(tmp_path: Path) -> None: tool = _load_tool() manifest = tmp_path / "manifest.jsonl" documents = tmp_path / "docs" _write_manifest(manifest) _write_document(documents / "sample-1" / "document.json") with pytest.raises(ValueError, match="--max-gap must be non-negative"): tool.assemble_documents( type( "Args", (), { "eval_manifest": manifest, "documents_dir": documents, "out": tmp_path / "assembled", "y_tolerance": 8, "max_gap": -1, "max_horizontal_overlap": 0, "max_merged_width": None, "max_merged_width_ratio": None, "max_component_width": None, "max_component_width_ratio": None, "min_vertical_overlap_ratio": 0.5, "joiner": " ", "require_text_structural_role": True, }, )() ) def test_assemble_page_ocr_lines_preserves_original_block_order_for_export(tmp_path: Path) -> None: tool = _load_tool() marker = TextLine( text="२", bbox=BBox(10, 100, 20, 20), confidence=0.95, page_index=0, line_id="marker", metadata={"structural_role": "page_marker"}, ) body = TextLine( text="आङ्‌गाॽ आसङ्जुमिबा", bbox=BBox(80, 100, 300, 20), confidence=0.75, page_index=0, line_id="body", metadata={"structural_role": "text"}, ) doc = Document( source_path="page.png", pages=[ Page( page_index=0, width=500, height=150, text_lines=[marker, body], blocks=[ Block( block_id="body-block", block_type="text", page_index=0, bbox=body.bbox, order=0, text=body.text, confidence=body.confidence, line_ids=["body"], ), Block( block_id="marker-block", block_type="text", page_index=0, bbox=marker.bbox, order=1, text=marker.text, confidence=marker.confidence, line_ids=["marker"], ), ], ) ], ) manifest = tmp_path / "manifest.jsonl" documents = tmp_path / "docs" _write_manifest(manifest) document_path = documents / "sample-1" / "document.json" document_path.parent.mkdir(parents=True) document_path.write_text(json.dumps(doc.to_dict(), ensure_ascii=False, indent=2) + "\n", encoding="utf-8") tool.assemble_documents( type( "Args", (), { "eval_manifest": manifest, "documents_dir": documents, "out": tmp_path / "assembled", "y_tolerance": 8, "max_gap": 80, "max_horizontal_overlap": 0, "max_merged_width": 320, "max_merged_width_ratio": None, "max_component_width": 180, "max_component_width_ratio": None, "min_vertical_overlap_ratio": 0.5, "joiner": " ", "require_text_structural_role": True, }, )() ) assembled = Document.from_dict( json.loads((tmp_path / "assembled" / "sample-1" / "document.json").read_text(encoding="utf-8")) ) assert [line.line_id for line in assembled.pages[0].text_lines] == ["marker", "body"] assert [block.line_ids for block in assembled.pages[0].blocks] == [["body"], ["marker"]] assert assembled.text == "आङ्‌गाॽ आसङ्जुमिबा\n२" def test_assemble_page_ocr_lines_source_breaks_preserve_document_text(tmp_path: Path) -> None: tool = _load_tool() manifest = tmp_path / "manifest.jsonl" documents = tmp_path / "docs" _write_manifest(manifest) _write_document(documents / "sample-1" / "document.json") original = Document.from_dict(json.loads((documents / "sample-1" / "document.json").read_text(encoding="utf-8"))) tool.assemble_documents( type( "Args", (), { "eval_manifest": manifest, "documents_dir": documents, "out": tmp_path / "assembled", "y_tolerance": 8, "max_gap": 80, "max_horizontal_overlap": 0, "max_merged_width": 320, "max_merged_width_ratio": None, "max_component_width": 180, "max_component_width_ratio": None, "min_vertical_overlap_ratio": 0.5, "joiner": "\n", "require_text_structural_role": True, }, )() ) assembled = Document.from_dict( json.loads((tmp_path / "assembled" / "sample-1" / "document.json").read_text(encoding="utf-8")) ) assert len(assembled.pages[0].text_lines) == len(original.pages[0].text_lines) - 1 assert assembled.pages[0].text_lines[0].text == "पे़रे़म्साप्\nकुरे़ङ्साप्" assert assembled.text == original.text def test_audit_page_ocr_line_assembly_replays_assembled_documents(tmp_path: Path) -> None: tool = _load_tool() audit_tool = _load_audit_tool() manifest = tmp_path / "manifest.jsonl" documents = tmp_path / "docs" _write_manifest(manifest) _write_document(documents / "sample-1" / "document.json") out = tmp_path / "assembled" tool.assemble_documents( type( "Args", (), { "eval_manifest": manifest, "documents_dir": documents, "out": out, "y_tolerance": 8, "max_gap": 80, "max_horizontal_overlap": 0, "max_merged_width": 320, "max_merged_width_ratio": None, "max_component_width": 180, "max_component_width_ratio": None, "min_vertical_overlap_ratio": 0.5, "joiner": " ", "require_text_structural_role": True, }, )() ) report = audit_tool.audit_assembly_output(out) assert report["passed"] is True assert report["source_line_count"] == 5 assert report["assembled_line_count"] == 4 assert report["merged_group_count"] == 1 assert (out / "ocr-line-assembly-audit.json").exists() def test_audit_page_ocr_line_assembly_rejects_tampered_document(tmp_path: Path) -> None: tool = _load_tool() audit_tool = _load_audit_tool() manifest = tmp_path / "manifest.jsonl" documents = tmp_path / "docs" _write_manifest(manifest) _write_document(documents / "sample-1" / "document.json") out = tmp_path / "assembled" tool.assemble_documents( type( "Args", (), { "eval_manifest": manifest, "documents_dir": documents, "out": out, "y_tolerance": 8, "max_gap": 80, "max_horizontal_overlap": 0, "max_merged_width": 320, "max_merged_width_ratio": None, "max_component_width": 180, "max_component_width_ratio": None, "min_vertical_overlap_ratio": 0.5, "joiner": " ", "require_text_structural_role": True, }, )() ) document_path = out / "sample-1" / "document.json" payload = json.loads(document_path.read_text(encoding="utf-8")) payload["pages"][0]["text_lines"][0]["text"] = "tampered" document_path.write_text(json.dumps(payload, ensure_ascii=False, indent=2) + "\n", encoding="utf-8") report = audit_tool.audit_assembly_output(out, tmp_path / "audit") assert report["passed"] is False assert any("document.json does not match replayed OCR-line assembly output" in issue for issue in report["issues"]) assert any("document_sha256 mismatch" in issue for issue in report["issues"]) assert (tmp_path / "audit" / "ocr-line-assembly-audit.md").exists()