File size: 2,264 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
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" / "analyze_page_reading_order_geometry.py"
    spec = importlib.util.spec_from_file_location("analyze_page_reading_order_geometry", 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 test_analyze_page_reading_order_geometry_splits_reference_order_chunks(tmp_path: Path) -> None:
    tool = _load_tool()
    alignment = tmp_path / "line-alignment.csv"
    alignment.write_text(
        "\n".join(
            [
                "sample_id,kind,reference_line_id,predicted_line_id,reference_bbox,predicted_bbox,page_index,iou,reference_text,predicted_text,text_codepoint_edits,text_codepoint_ref_len,text_cer_codepoint,text_grapheme_edits,text_grapheme_ref_len,text_cer_grapheme",
                's,matched,span-0001,p1,"[100,100,50,10]","[100,100,50,10]",0,1,a,a,0,1,0,0,1,0',
                's,matched,span-0002,p2,"[100,120,50,10]","[100,120,50,10]",0,1,b,b,0,1,0,0,1,0',
                's,matched,span-0003,p3,"[600,80,50,10]","[600,80,50,10]",0,1,c,c,0,1,0,0,1,0',
            ]
        )
        + "\n",
        encoding="utf-8",
    )

    report = tool.analyze(
        Namespace(
            line_alignment=alignment,
            out=tmp_path / "out",
            min_dx=200.0,
            min_upward_dy=200.0,
            min_downward_dy=120.0,
        )
    )

    assert report["matched_line_count"] == 3
    assert report["chunk_count"] == 2
    assert report["jump_count"] == 1
    assert report["chunks"][0]["start_reference_line_id"] == "span-0001"
    assert report["chunks"][0]["end_reference_line_id"] == "span-0002"
    assert report["chunks"][1]["start_reference_line_id"] == "span-0003"
    assert (tmp_path / "out" / "reading-order-geometry-chunks.tsv").is_file()
    persisted = json.loads((tmp_path / "out" / "reading-order-geometry-analysis.json").read_text(encoding="utf-8"))
    assert persisted["claim_boundary"].startswith("diagnostic geometry analysis")