ocr-tech-k2 / tests /test_normalization_metrics.py
prosperious's picture
Add 195 files (runs/hf-recognizer-mixed-v2-full/finalized/package/artifacts/model/model.safetensors ...)
1ef1de7 verified
Raw History Blame Contribute Delete
3.37 kB
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