File size: 3,374 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 | from __future__ import annotations
from ocrtech.metrics import cer, edit_distance, exact_line_accuracy, reading_order_pair_accuracy, table_cell_score, table_structure_similarity, table_teds_structure, table_teds_text, wer
from ocrtech.normalization import normalize_ocr_text, script_counts, unsupported_characters
from ocrtech.schemas import TableCell
def test_normalization_preserves_nepali_graphemes() -> None:
text = "काठमाण्डौँ\u200d \u0000नेपाल"
normalized = normalize_ocr_text(text)
assert "काठमाण्डौँ" in normalized
assert "\u200d" in normalized
assert "\u0000" not in normalized
def test_script_counts_and_supported_dictionary() -> None:
counts = script_counts("नेपाल English 123")
assert counts["devanagari"] > 0
assert counts["latin"] > 0
assert counts["digit"] == 3
assert unsupported_characters("नेपाल English 123") == []
def test_ocr_metrics_are_deterministic() -> None:
assert edit_distance("kitten", "sitting") == 3
assert cer("नेपाल", "नेपाल") == 0
assert wer("hello brave world", "hello world") == 0.5
assert exact_line_accuracy(["a", "b"], ["a", "c"]) == 0.5
assert reading_order_pair_accuracy(["a", "b", "c"], ["a", "b", "c"]) == 1.0
assert reading_order_pair_accuracy(["b", "a", "c"], ["a", "b", "c"]) < 1.0
def test_table_cell_score() -> None:
score = table_cell_score([["A", "B"], ["1", "2"]], [["A", "B"], ["1", "3"]])
assert score.precision == 0.75
assert score.recall == 0.75
assert score.f1 == 0.75
assert score.exact_match == 0.0
assert score.structure_similarity == 1.0
def test_table_structure_similarity_scores_shape_not_text() -> None:
assert table_structure_similarity([["wrong", "text"], ["1", "2"]], [["A", "B"], ["1", "3"]]) == 1.0
assert table_structure_similarity([["A", "B"]], [["A", "B"], ["1", "3"]]) < 1.0
assert table_structure_similarity([["A", "B", "C"], ["1", "2", "3"]], [["A", "B"], ["1", "3"]]) < 1.0
def test_table_teds_scores_tree_structure_and_text_separately() -> None:
reference = [
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"),
]
same_structure_wrong_text = [
TableCell(row=0, col=0, text="Wrong", 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"),
]
flattened = [
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"),
]
assert table_teds_structure(reference, reference) == 1.0
assert table_teds_text(reference, reference) == 1.0
assert table_teds_structure(same_structure_wrong_text, reference) == 1.0
assert table_teds_text(same_structure_wrong_text, reference) < 1.0
assert table_teds_structure(flattened, reference) < 1.0
|