"""Language-Specific Grapheme Recall, and why it is scored as an F1. When a target language L has a better-resourced close neighbour C, multilingual systems transcribe L *as* C. The output is fluent and reads as a working transcript to anyone who does not speak L. WER punishes it, but WER punishes everything, so a high WER never says *why*; CER makes it look mild, because the phonemes are approximately right. G_L is the set of graphemes that occur in L's orthography and not in C's, which falls out of the two alphabets with no supervision or training data. Scored as precision/recall/F1 over grapheme *counts*, not as bare recall. Recall alone is unbounded above and rewards over-production: in our evaluation a Whisper turbo variant emitted 1.51x the reference's schwas and scored 151%, which reads as "better than perfect" and is nothing of the sort. Counting the overlap as min(hyp, ref) bounds the score at 1.0 and charges for inventing the marker as well as for omitting it. What the index does NOT measure, stated here because the evaluation makes it unmissable: it is a test of conformance to the target *orthography*, not of language understanding. Two MMS adapters that transcribe Azerbaijani correctly into Cyrillic and Arabic script both score 0.0, exactly as the systems that transcribe it as Turkish do. Those are different failures and the index cannot tell them apart. For a deployment that requires a specific script that distinction may not matter; for a claim about language support it matters a great deal, so report script detection alongside it or state the limit. """ from __future__ import annotations # Graphemes in Azerbaijani (Latin) that Turkish does not have. Schwa is the # whole story here -- it is frequent, it is unambiguous, and Turkish has no # counterpart -- but the set form is what makes the construction transferable. AZ_VS_TR = set("əƏ") # Reference sets for other pairs the construction applies to unchanged. Listed # to make the point that G_L is derived, not tuned. PAIRS = { ("az", "tr"): AZ_VS_TR, ("kk", "ru"): set("әғқңөұүһӘҒҚҢӨҰҮҺ"), ("ca", "es"): set("çÇ"), ("fa", "ar"): set("پچژگ"), ("ha", "en"): set("ɓɗƙƁƊƘ"), } def counts(text: str, graphemes: set[str]) -> int: return sum(1 for ch in (text or "") if ch in graphemes) def lsgr(ref: str, hyp: str, graphemes: set[str] = AZ_VS_TR) -> dict[str, float]: """Precision, recall and F1 over target-specific grapheme counts. A reference with no target-specific graphemes carries no signal, so the caller gets n_ref = 0 and should pool rather than average: per-utterance averaging would let short schwa-free lines dominate. """ r = counts(ref, graphemes) h = counts(hyp, graphemes) overlap = min(r, h) p = overlap / h if h else 0.0 rec = overlap / r if r else 0.0 f1 = (2 * p * rec / (p + rec)) if (p + rec) else 0.0 return {"n_ref": r, "n_hyp": h, "precision": p, "recall": rec, "f1": f1} def pooled(pairs, graphemes: set[str] = AZ_VS_TR) -> dict[str, float]: """LSGR over a corpus: sum the counts, then compute once. Pooling rather than averaging, for the same reason WER is pooled over words. """ R = H = O = 0 for ref, hyp in pairs: r, h = counts(ref, graphemes), counts(hyp, graphemes) R += r H += h O += min(r, h) p = O / H if H else 0.0 rec = O / R if R else 0.0 f1 = (2 * p * rec / (p + rec)) if (p + rec) else 0.0 return {"n_ref": R, "n_hyp": H, "precision": p, "recall": rec, "f1": f1} if __name__ == "__main__": CASES = [ ("perfect", "əə bir", "əə bir", 1.0), ("wrong orthography", "əə bir", "ee bir", 0.0), ("half omitted", "əəəə", "əə", 2 / 3), ("doubled", "əə", "əəəə", 2 / 3), ("empty hyp", "əə", "", 0.0), ("no signal in ref", "bir az", "bir az", 0.0), ] bad = 0 for name, ref, hyp, want in CASES: got = lsgr(ref, hyp)["f1"] ok = abs(got - want) < 1e-9 bad += not ok print(f"{'ok ' if ok else 'FAIL'} {name:<20} f1={got:.3f} want={want:.3f}") print(f"\n{len(CASES)-bad}/{len(CASES)} passed") raise SystemExit(1 if bad else 0)