ocr-projects-archive / ocr-tech /tests /test_apply_page_reading_order_strategy.py
prosperious's picture
Add 1000 files (OCR_INDRA/.gitignore ...)
f9bc0c1 verified
Raw History Blame Contribute Delete
4.47 kB
from __future__ import annotations
import importlib.util
import json
import sys
from argparse import Namespace
from pathlib import Path
def _load_tool():
path = Path(__file__).resolve().parents[1] / "tools" / "apply_page_reading_order_strategy.py"
spec = importlib.util.spec_from_file_location("apply_page_reading_order_strategy", 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_document(root: Path) -> None:
doc_dir = root / "sample-1"
doc_dir.mkdir(parents=True)
lines = [
("right-title", "right title", [2700, 794, 1100, 146]),
("left-title-a", "left a", [496, 783, 850, 268]),
("left-title-b", "left b", [1346, 774, 745, 300]),
("left-title-c", "left c", [2105, 802, 348, 244]),
("second-row", "second row", [2800, 941, 800, 160]),
]
lines.extend((f"body-{index}", f"body {index}", [500, 1200 + index * 60, 800, 50]) for index in range(8))
payload = {
"schema_version": "1.0",
"source_path": "page.png",
"tables": [],
"figures": [],
"metadata": {},
"pages": [
{
"page_index": 0,
"width": 4000,
"height": 6200,
"metadata": {},
"text_lines": [
{
"line_id": line_id,
"page_index": 0,
"text": text,
"bbox": bbox,
"confidence": 0.9,
"metadata": {},
}
for line_id, text, bbox in lines
],
"blocks": [
{
"block_id": f"block-{index}",
"block_type": "text",
"page_index": 0,
"bbox": bbox,
"order": index,
"text": text,
"confidence": 0.9,
"line_ids": [line_id],
"table_id": None,
"figure_id": None,
"metadata": {},
}
for index, (line_id, text, bbox) in enumerate(lines, start=1)
],
}
],
}
(doc_dir / "document.json").write_text(json.dumps(payload, ensure_ascii=False, indent=2) + "\n", encoding="utf-8")
def test_rows_ltr_top_orders_variable_height_headlines_by_top_row(tmp_path: Path) -> None:
tool = _load_tool()
source = tmp_path / "docs"
_write_document(source)
report = tool.apply_strategy(
Namespace(
source_documents_dir=source,
out=tmp_path / "out",
mode="Rows LTR Top",
force=False,
)
)
assert report["document_count"] == 1
document = json.loads((tmp_path / "out" / "sample-1" / "document.json").read_text(encoding="utf-8"))
page = document["pages"][0]
assert [line["line_id"] for line in page["text_lines"]] == [
"left-title-a",
"left-title-b",
"left-title-c",
"right-title",
"second-row",
"body-0",
"body-1",
"body-2",
"body-3",
"body-4",
"body-5",
"body-6",
"body-7",
]
assert [block["line_ids"][0] for block in page["blocks"]] == [
"left-title-a",
"left-title-b",
"left-title-c",
"right-title",
"second-row",
"body-0",
"body-1",
"body-2",
"body-3",
"body-4",
"body-5",
"body-6",
"body-7",
]
assert page["metadata"]["reading_order_strategy"]["resolved_mode"] == "Rows LTR Top"
def test_rows_ltr_center_keeps_existing_center_based_behavior(tmp_path: Path) -> None:
tool = _load_tool()
source = tmp_path / "docs"
_write_document(source)
tool.apply_strategy(
Namespace(
source_documents_dir=source,
out=tmp_path / "out",
mode="Rows LTR",
force=False,
)
)
document = json.loads((tmp_path / "out" / "sample-1" / "document.json").read_text(encoding="utf-8"))
page = document["pages"][0]
assert page["text_lines"][0]["line_id"] == "right-title"