File size: 7,895 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 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 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 | from __future__ import annotations
from pathlib import Path
from ocrtech.metrics import table_structure_similarity
from ocrtech.schemas import BBox, Table, TableCell
from ocrtech.tables import cells_to_html, infer_repeated_header_colspans, normalize_table_grid_structure, table_to_grid, write_table_files
def test_infer_repeated_header_colspans_collapses_flat_merged_header() -> None:
table = Table(
table_id="p0-t0",
page_index=0,
bbox=BBox(0, 0, 300, 100),
cells=[
TableCell(row=0, col=0, text="Ledger Summary"),
TableCell(row=0, col=1, text="Ledger Summary"),
TableCell(row=0, col=2, text="Status"),
TableCell(row=1, col=0, text="नाम"),
TableCell(row=1, col=1, text="रकम"),
TableCell(row=1, col=2, text="Remarks"),
],
)
reference_cells = [
TableCell(row=0, col=0, text="Ledger Summary", colspan=2),
TableCell(row=0, col=2, text="Status"),
TableCell(row=1, col=0, text="नाम"),
TableCell(row=1, col=1, text="रकम"),
TableCell(row=1, col=2, text="Remarks"),
]
inferred, count = infer_repeated_header_colspans(table)
assert count == 1
assert inferred.cells[0].colspan == 2
assert [cell.to_dict() for cell in inferred.cells] == [cell.to_dict() for cell in reference_cells]
assert table_structure_similarity(inferred.cells, reference_cells) == 1.0
assert 'colspan="2"' in inferred.html
def test_infer_repeated_header_colspans_does_not_collapse_body_repeats() -> None:
table = Table(
table_id="p0-t0",
page_index=0,
bbox=BBox(0, 0, 300, 100),
cells=[
TableCell(row=0, col=0, text="नाम"),
TableCell(row=0, col=1, text="रकम"),
TableCell(row=1, col=0, text="Paid"),
TableCell(row=1, col=1, text="Paid"),
],
)
inferred, count = infer_repeated_header_colspans(table)
assert count == 0
assert inferred.cells == table.cells
def test_cells_to_html_and_export_preserve_spans(tmp_path: Path) -> None:
table = Table(
table_id="p0-t0",
page_index=0,
bbox=BBox(0, 0, 300, 100),
cells=[
TableCell(row=0, col=0, text="Ledger Summary", colspan=2),
TableCell(row=0, col=2, text="Status"),
TableCell(row=1, col=0, text="नाम"),
TableCell(row=1, col=1, text="रकम"),
TableCell(row=1, col=2, text="Remarks"),
],
)
html = cells_to_html(table.cells)
written = write_table_files(table, tmp_path)
assert 'colspan="2"' in html
assert 'colspan="2"' in Path(written.html_path or "").read_text(encoding="utf-8")
def test_normalize_table_grid_structure_drops_separator_and_duplicate_columns() -> None:
table = Table(
table_id="p0-t0",
page_index=0,
bbox=BBox(0, 0, 100, 100),
cells=[
TableCell(row=0, col=0, text="—"),
TableCell(row=0, col=1, text="Skull cap"),
TableCell(row=0, col=2, text="1D"),
TableCell(row=0, col=3, text="1D"),
TableCell(row=1, col=0, text="—"),
TableCell(row=1, col=1, text="Helmet"),
TableCell(row=1, col=2, text="2D"),
TableCell(row=1, col=3, text="2D"),
],
)
normalized, dropped = normalize_table_grid_structure(table)
assert dropped == 2
assert table_to_grid(normalized) == [["Skull cap", "1D"], ["Helmet", "2D"]]
assert normalized.metadata["normalized_table_grid_dropped_columns"] == 2
def test_normalize_table_grid_structure_keeps_non_duplicate_content_columns() -> None:
table = Table(
table_id="p0-t0",
page_index=0,
bbox=BBox(0, 0, 100, 100),
cells=[
TableCell(row=0, col=0, text="Name"),
TableCell(row=0, col=1, text="Value"),
TableCell(row=1, col=0, text="A"),
TableCell(row=1, col=1, text="1"),
],
)
normalized, dropped = normalize_table_grid_structure(table)
assert dropped == 0
assert normalized is table
def test_normalize_table_grid_structure_merges_sparse_bullet_continuation_rows() -> None:
table = Table(
table_id="p0-t0",
page_index=0,
bbox=BBox(0, 0, 100, 100),
cells=[
TableCell(row=0, col=0, text="Type of Anomaly"),
TableCell(row=0, col=1, text="Variant"),
TableCell(row=0, col=2, text="Type"),
TableCell(row=0, col=3, text="Treatment"),
TableCell(row=0, col=4, text="Case Example"),
TableCell(row=1, col=0, text=""),
TableCell(row=1, col=1, text="Coronaries originating"),
TableCell(row=1, col=2, text="- left main"),
TableCell(row=1, col=3, text="Surgery"),
TableCell(row=1, col=4, text="None"),
TableCell(row=2, col=0, text="- right coronary"),
TableCell(row=2, col=1, text=""),
TableCell(row=2, col=2, text=""),
TableCell(row=2, col=3, text=""),
TableCell(row=2, col=4, text=""),
TableCell(row=3, col=0, text="- circumflex"),
TableCell(row=3, col=1, text=""),
TableCell(row=3, col=2, text=""),
TableCell(row=3, col=3, text=""),
TableCell(row=3, col=4, text=""),
TableCell(row=4, col=0, text="Anomalies of Origin"),
TableCell(row=4, col=1, text="Anomalous aortic"),
TableCell(row=4, col=2, text="- left main from right sinus"),
TableCell(row=4, col=3, text="Revascularization"),
TableCell(row=4, col=4, text="1,3,5"),
TableCell(row=5, col=0, text="- right coronary from left sinus"),
TableCell(row=5, col=1, text=""),
TableCell(row=5, col=2, text=""),
TableCell(row=5, col=3, text=""),
TableCell(row=5, col=4, text=""),
],
)
normalized, changed = normalize_table_grid_structure(table)
assert changed == 3
assert table_to_grid(normalized) == [
["Type of Anomaly", "Variant", "Type", "Treatment", "Case Example"],
["", "Coronaries originating", "- left main\n- right coronary\n- circumflex", "Surgery", "None"],
["Anomalies of Origin", "Anomalous aortic", "- left main from right sinus\n- right coronary from left sinus", "Revascularization", "1,3,5"],
]
assert normalized.metadata["normalized_table_grid_merged_rows"] == 3
def test_normalize_table_grid_structure_does_not_merge_non_bullet_single_cell_rows() -> None:
table = Table(
table_id="p0-t0",
page_index=0,
bbox=BBox(0, 0, 100, 100),
cells=[
TableCell(row=0, col=0, text="Name"),
TableCell(row=0, col=1, text="Notes"),
TableCell(row=1, col=0, text="A"),
TableCell(row=1, col=1, text="- first"),
TableCell(row=2, col=0, text="continued prose"),
TableCell(row=2, col=1, text=""),
],
)
normalized, changed = normalize_table_grid_structure(table)
assert changed == 0
assert normalized is table
def test_normalize_table_grid_structure_skips_wide_sparse_tables() -> None:
cells = [TableCell(row=0, col=col, text=f"H{col}") for col in range(14)]
cells.extend(
[
TableCell(row=1, col=0, text="- legitimate sparse row"),
TableCell(row=1, col=1, text=""),
TableCell(row=2, col=0, text="- another sparse row"),
TableCell(row=2, col=1, text=""),
]
)
table = Table(
table_id="p0-t0",
page_index=0,
bbox=BBox(0, 0, 100, 100),
cells=cells,
)
normalized, changed = normalize_table_grid_structure(table)
assert changed == 0
assert normalized is table
|