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