"""ArPDF failure-mode classifier. Author: Syamjith NK Shared by pdf_bench.py (when generating results) and reclassify.py (when re-deriving verdicts from the stored `extracted` text). Kept in one place so the two can never disagree. Verdicts, checked in this order: clean the source string is present in logical order bidi_controls present in logical order once invisible bidi format characters (U+202A..U+202E, U+2066..U+2069, ...) are removed ligature logical order, but an adjacent pair containing lam is swapped: الإمارات -> اإلمارات, المهيري -> املهيري reversed visual order: Arabic runs reversed, digit and Latin runs kept left-to-right, the way a visual-order PDF stores them reversed_ligature visual order AND a lam pair swapped reordered every character present, but whole segments out of order mangled characters missing Corrected 2026-10-27. The earlier verdict called anything that was neither clean nor an exact character-by-character reversal "ligature". That swept in reversals of strings containing digits or Latin (which can never be an exact character reversal), reversals that also had a swapped lam pair, and text that was correct apart from embedded bidi controls. "spacing" could never fire, because the comparison removes whitespace before matching. """ import unicodedata LAM = "ل" def normalise(s: str) -> str: """Strip diacritics and whitespace and fold presentation forms to base letters.""" s = unicodedata.normalize("NFKC", s) return "".join(c for c in s if not unicodedata.combining(c) and not c.isspace()) def strip_controls(s: str) -> str: """Remove Unicode format characters (category Cf), which include bidi controls.""" return "".join(c for c in s if unicodedata.category(c) != "Cf") def _rtl(c: str) -> bool: """Arabic letters run right-to-left; Arabic-Indic digits, like Western ones, do not.""" cp = ord(c) if 0x0660 <= cp <= 0x0669 or 0x06F0 <= cp <= 0x06F9: return False return 0x0600 <= cp <= 0x06FF or 0x0750 <= cp <= 0x077F def visual_order(s: str) -> str: """`s` as a visual-order PDF stores it: RTL runs reversed, LTR runs left intact.""" runs: list[list] = [] for c in s: r = _rtl(c) if runs and runs[-1][0] == r: runs[-1][1].append(c) else: runs.append([r, [c]]) return "".join("".join(reversed(cs)) if r else "".join(cs) for r, cs in reversed(runs)) def lam_swapped_in(target: str, got: str) -> bool: """True if `target` occurs in `got` with at least one adjacent lam pair swapped.""" n = len(target) for start in range(len(got) - n + 1): w = got[start:start + n] i = swaps = 0 while i < n: if target[i] == w[i]: i += 1 elif (i + 1 < n and target[i] == w[i + 1] and target[i + 1] == w[i] and LAM in (target[i], target[i + 1])): swaps += 1 i += 2 else: break else: if swaps: return True return False def classify(text: str, got_raw: str) -> tuple[bool, bool, str]: """Return (order_ok, chars_ok, verdict) for one extraction.""" want = normalise(text) got = normalise(got_raw) order_ok = want in got chars_ok = sorted(want) == sorted("".join(c for c in got if c in set(want))) bare = strip_controls(got) rev = (visual_order(want), want[::-1]) if order_ok: verdict = "clean" elif want in bare: verdict = "bidi_controls" elif lam_swapped_in(want, bare): verdict = "ligature" elif any(r in bare for r in rev): verdict = "reversed" elif any(lam_swapped_in(r, bare) for r in rev): verdict = "reversed_ligature" elif chars_ok: verdict = "reordered" else: verdict = "mangled" return order_ok, chars_ok, verdict VERDICTS = ("clean", "bidi_controls", "ligature", "reversed", "reversed_ligature", "reordered", "mangled")