turkic-ocr-code / src /r5eval /lookalike.py
treoa's picture
Release 1
24aaa76
Raw History Blame Contribute Delete
3.13 kB
"""The look-alike table (prerequisite P-A of eval_spec.json, rule 0.6(c)) and the reference repair that uses it.
The table lists Latin and Cyrillic letters that look the same in ordinary fonts. It is a PROJECT ASSUMPTION, written by hand by
stage EVALCODE on 2026-11-21: visual identity in common fonts plus the two pairs the KOHTD labels actually contain (Latin i and
the schwa). It is not read from Unicode UTS #39 confusables.txt (not consulted in this round); extending it changes its sha256
and therefore every score file (the sha256 is part of the incremental key).
Frozen file: data/lookalike_table_v1.tsv, sha256 and UTC time in data/lookalike_table_v1.sha256.
Use 1 (rule 0.6(c)): repair_label() maps look-alike letters inside mixed-script tokens of a REFERENCE to the script of the row
(Cyrl rows: Latin look-alike -> Cyrillic; Latn rows: Cyrillic look-alike -> Latin). Only tokens where the row's script
has at least as many letters as the other script are touched. Outputs are never repaired.
Use 2 (M12b): homoglyph substitution rate = aligned substitutions (reference letter, output letter) that are look-alike
partners in either direction.
"""
import os
import re
from .textnorm import latin_cyrillic_counts
# (latin, cyrillic) in code points
_PAIRS = [
(0x0041, 0x0410), (0x0042, 0x0412), (0x0043, 0x0421), (0x0045, 0x0415), (0x0048, 0x041D), (0x0049, 0x0406),
(0x004A, 0x0408), (0x004B, 0x041A), (0x004D, 0x041C), (0x004F, 0x041E), (0x0050, 0x0420), (0x0053, 0x0405),
(0x0054, 0x0422), (0x0058, 0x0425), (0x0059, 0x0423), (0x018F, 0x04D8),
(0x0061, 0x0430), (0x0063, 0x0441), (0x0065, 0x0435), (0x0068, 0x04BB), (0x0069, 0x0456), (0x006A, 0x0458),
(0x006F, 0x043E), (0x0070, 0x0440), (0x0073, 0x0455), (0x0078, 0x0445), (0x0079, 0x0443), (0x0259, 0x04D9),
]
LATIN_TO_CYR = {chr(a): chr(b) for a, b in _PAIRS}
CYR_TO_LATIN = {chr(b): chr(a) for a, b in _PAIRS}
PARTNER = {}
PARTNER.update(LATIN_TO_CYR)
PARTNER.update(CYR_TO_LATIN)
_WS_SPLIT = re.compile(r"(\s+)")
def table_tsv():
"""Canonical text of the frozen table file."""
lines = ["latin\tcyrillic\tlatin_cp\tcyrillic_cp"]
for a, b in sorted(_PAIRS):
lines.append("%s\t%s\tU+%04X\tU+%04X" % (chr(a), chr(b), a, b))
return "\n".join(lines) + "\n"
def table_path():
return os.path.join(os.path.dirname(os.path.abspath(__file__)), "data", "lookalike_table_v1.tsv")
def repair_token(tok, script):
nl, nc = latin_cyrillic_counts(tok)
if nl < 1 or nc < 1:
return tok
if script == "Cyrl" and nc >= nl:
return "".join(LATIN_TO_CYR.get(c, c) for c in tok)
if script == "Latn" and nl >= nc:
return "".join(CYR_TO_LATIN.get(c, c) for c in tok)
return tok
def repair_label(text, script):
"""Returns (repaired text, list of (old token, new token)). Whitespace is kept as it is."""
parts = _WS_SPLIT.split(text)
changes = []
for i, p in enumerate(parts):
if i % 2 == 0 and p:
q = repair_token(p, script)
if q != p:
changes.append((p, q))
parts[i] = q
return "".join(parts), changes