File size: 4,465 Bytes
f9bc0c1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
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"