File size: 4,429 Bytes
1ef1de7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from __future__ import annotations

import json
from pathlib import Path

from ocrtech.manifest import ManifestEntry, sha256_file, sha256_text, write_manifest
from ocrtech.schemas import BBox, Document, Page, Table, TableCell
from ocrtech.table_analysis import analyze_table_cells


def test_analyze_table_cells_reads_benchmark_documents(tmp_path: Path) -> None:
    image = tmp_path / "sample.png"
    image.write_bytes(b"image")
    reference = tmp_path / "reference.json"
    reference.write_text(
        json.dumps({"text": "ignored", "tables": [[["नाम", "रकम"], ["राम", "१२"]]]}, ensure_ascii=False) + "\n",
        encoding="utf-8",
    )
    text_hash = sha256_text("ignored")
    manifest = tmp_path / "eval.jsonl"
    write_manifest(
        [
            ManifestEntry(
                sample_id="sample-1",
                dataset="fixture",
                split="eval",
                image_path=str(image),
                text="ignored",
                sha256=sha256_file(image),
                metadata={"reference_path": str(reference), "text_sha256": text_hash},
            )
        ],
        manifest,
    )
    benchmark_dir = tmp_path / "benchmark"
    rows = []
    for system, grid in {
        "candidate": [["नाम", "रकम"], ["राम", ""]],
        "surya": [["नाम", "रकम"], ["श्याम", "१२"]],
        "tesseract": [["नाम", "रकम"], ["राम", "१२"]],
    }.items():
        output_dir = benchmark_dir / system / image.stem
        output_dir.mkdir(parents=True)
        document = _document(image, grid, duplicate_table=system == "surya")
        (output_dir / "document.json").write_text(json.dumps(document.to_dict(), ensure_ascii=False, indent=2) + "\n", encoding="utf-8")
        rows.append(
            {
                "baseline": system,
                "input_path": str(image),
                "status": "ok",
                "latency_seconds": 0.1,
                "sample_id": "sample-1",
                "slices": ["table"],
                "metrics": {},
            }
        )
    report = benchmark_dir / "report.json"
    report.write_text(json.dumps(rows, ensure_ascii=False, indent=2) + "\n", encoding="utf-8")

    analysis = analyze_table_cells(report, manifest, tmp_path / "analysis", candidate="candidate", baselines=["surya", "tesseract"])

    assert analysis.systems["candidate"].reference_cells == 4
    assert analysis.systems["candidate"].reference_tables == 1
    assert analysis.systems["candidate"].predicted_tables == 1
    assert analysis.systems["candidate"].exact_cells == 3
    assert analysis.systems["candidate"].missing_cells == 1
    assert analysis.systems["surya"].predicted_tables == 2
    assert analysis.systems["surya"].duplicate_overlapping_tables == 1
    assert analysis.systems["tesseract"].exact_cells == 4
    assert analysis.paired["surya"].candidate_better == 1
    assert analysis.paired["surya"].baseline_better == 1
    assert analysis.paired["tesseract"].baseline_exact_candidate_not == 1
    candidate_sample = next(item for item in analysis.samples if item.system == "candidate")
    assert candidate_sample.reference_shapes == [[2, 2]]
    assert candidate_sample.predicted_shapes == [[2, 2]]
    assert candidate_sample.exact_cells == 3
    surya_sample = next(item for item in analysis.samples if item.system == "surya")
    assert surya_sample.predicted_tables == 2
    assert surya_sample.duplicate_overlapping_tables == 1
    assert (tmp_path / "analysis" / "table-cell-analysis.md").exists()


def _document(image: Path, grid: list[list[str]], *, duplicate_table: bool = False) -> Document:
    cells = [
        TableCell(row=row_index, col=col_index, text=value, bbox=BBox(col_index * 10, row_index * 10, 10, 10))
        for row_index, row in enumerate(grid)
        for col_index, value in enumerate(row)
    ]
    tables = [Table(table_id="p0-t0", page_index=0, bbox=BBox(0, 0, 20, 20), cells=cells)]
    if duplicate_table:
        tables.append(
            Table(
                table_id="p0-t1",
                page_index=0,
                bbox=BBox(0, 0, 20, 20),
                cells=[TableCell(row=0, col=0, text="".join(value for row in grid for value in row), bbox=BBox(0, 0, 20, 20))],
            )
        )
    return Document(
        source_path=str(image),
        pages=[Page(page_index=0)],
        tables=tables,
    )