Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions src/tetrak_ocr/accuracy.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,15 @@
# Armenian punctuation -> the ASCII character it is visually identical to.
_PUNCTUATION_HOMOGLYPHS = str.maketrans({"։": ":", "․": "."})

# The two-letter եւ and the ligature և are one thing for scoring (brief 014,
# decided 2026-10-08). Classical-orthography presses split 21 to 16 on which
# they set, reformed presses set the ligature, and transcribers follow the
# page or do not; ground truth stays as printed so the model learns to read
# both, and the metric refuses to rank an engine on which form it emitted.
# Applied after lowercasing, so Եւ and ԵՒ fold too. The trainer's copy of
# this module carries the same change.
_EW_LIGATURE = ("եւ", "և")


def _is_word(token: str) -> bool:
"""A token with at least one letter or digit; ``-`` or ``…`` alone is not."""
Expand All @@ -40,9 +49,11 @@ def normalise(text: str) -> str:
throughout; every register uses ``.`` for ``․``) that holding an engine
to either form would rank engines on which habit their output happens
to share with the transcript, not on what they read. Mapped towards
ASCII so that text with no Armenian in it is unaffected.
ASCII so that text with no Armenian in it is unaffected. The two-letter
``եւ`` is scored as the ligature ``և`` for the same reason (brief 014).
"""
return re.sub(r"\s+", " ", text.lower().translate(_PUNCTUATION_HOMOGLYPHS)).strip()
lowered = text.lower().translate(_PUNCTUATION_HOMOGLYPHS).replace(*_EW_LIGATURE)
return re.sub(r"\s+", " ", lowered).strip()


def character_similarity(actual: str, expected: str) -> float:
Expand Down
6 changes: 6 additions & 0 deletions tests/ocr/test_accuracy.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@ def test_armenian_full_stop_and_colon_are_equal(self) -> None:
"""Transcripts type ':' for '։'; neither engine nor transcript is penalised."""
assert normalise("բժշկի։") == normalise("բժշկի:")

def test_two_letter_ew_and_ligature_are_equal(self) -> None:
"""Brief 014: classical presses set եւ, reformed presses և; neither
form is a reading error. Capitals fold too, via lowercasing."""
assert word_recall("Երեւան եւ Եւրոպա", "Երևան և Եւրոպա") == 1.0
assert normalise("ԵՒ Եւ եւ") == "և և և"

def test_abbreviation_dot_and_full_stop_are_equal(self) -> None:
"""Transcripts type '.' for the Armenian abbreviation dot '․'."""
assert normalise("Ա․ Գրկ․") == normalise("Ա. Գրկ.")
Expand Down
Loading