Download src/r5eval/textnorm.py from treoa/turkic-ocr-code: direct link, hf CLI and curl.
- Browser
- Download file 4.76 kB
-
https://huggingface.co/treoa/turkic-ocr-code/resolve/main/src/r5eval/textnorm.py
- Command line
-
hf download hf://treoa/turkic-ocr-code/src/r5eval/textnorm.py
-
curl -L -o textnorm.py https://huggingface.co/treoa/turkic-ocr-code/resolve/main/src/r5eval/textnorm.py
4.76 kB
| """Text normalisation profiles of eval_spec.json (N0-N3), the P2 key (N1), apostrophe folding (N2), and script tests. | |
| N0 standard scoring text: NFC, whitespace runs to one space, strip; case, punctuation, look-alikes kept (= J2 norm()) | |
| N1 P2 key: NFC, lower case (Turkic fold for tr and az), keep letters (category L*) and decimal digits (Nd) (clarification G3) | |
| N2 apostrophe folding of version 3 section 1: U+0027 U+2018 U+2019 U+02BC U+02BB -> U+02BB (= VLMA fold()) | |
| N3 case fold that respects Turkish and Azerbaijani I / dotless i (clarification G7) | |
| Fixes against J2 (RESEVAL findings, eval_spec.json differences_from_J2_scorelib): | |
| other_script(): decided with Unicode Script_Extensions (module regex), so Uzbek Latin o + U+02BB is not a third script. | |
| case fold: not str.casefold() for tr and az. | |
| """ | |
| import functools | |
| import unicodedata | |
| import regex | |
| from .frozen import j2, vlma_pp | |
| norm = j2.norm # N0 | |
| strip_wrappers_j2 = j2.strip_wrappers | |
| strip_wrappers_pp = vlma_pp.strip_wrappers | |
| has_loop = j2.has_loop | |
| fold_apostrophes = vlma_pp.fold # N2 | |
| TURKIC_FOLD_LANGS = ("tr", "az") | |
| APOSTROPHE_LIKE = "'‘’ʼ" # U+0027 U+2018 U+2019 U+02BC (U+02BB is a letter, reported with the letters) | |
| def plain_output(raw): | |
| """VLMA PLAIN figure: N0 only.""" | |
| return vlma_pp.plain_output(raw) | |
| def pp_output(raw): | |
| """VLMA POST-PROCESSED figure: N2(N0(strip_wrappers(raw))).""" | |
| return vlma_pp.pp_output(raw) | |
| def pp_ref(ref): | |
| return vlma_pp.pp_ref(ref) | |
| def j2strip_output(raw): | |
| """J2 screen figure (rule 0.3 literal reading): N0(J2 strip_wrappers(raw)); no apostrophe folding.""" | |
| return j2.prep_output(raw) | |
| def fold_lower(text, lang=None): | |
| """N3 lower-casing. For tr and az: I -> dotless i and dotted capital I -> i before lower(); else str.lower().""" | |
| if lang in TURKIC_FOLD_LANGS: | |
| text = text.replace("İ", "i").replace("I", "ı") | |
| return text.lower() | |
| def n1_key(text, lang=None): | |
| """P2 key (clarification G3): NFC, N3 lower case, keep letters (L*) and decimal digits (Nd) only.""" | |
| t = fold_lower(unicodedata.normalize("NFC", text), lang) | |
| return "".join(c for c in t if unicodedata.category(c).startswith("L") or unicodedata.category(c) == "Nd") | |
| # ---- scripts (Unicode Script_Extensions) ------------------------------------------------------------------------------- | |
| _CANDIDATES = ["Latin", "Cyrillic", "Greek", "Arabic", "Han", "Hiragana", "Katakana", "Hangul", "Hebrew", "Armenian", | |
| "Georgian", "Thai", "Devanagari"] | |
| _RX = {s: regex.compile(r"\p{Script_Extensions=%s}" % s) for s in _CANDIDATES} | |
| _RX_COMMON = regex.compile(r"\p{Script=Common}|\p{Script=Inherited}") | |
| def char_script_set(ch): | |
| """Resolved script set of one character, None = Common or Inherited (belongs to every script, UTS #39 section 5).""" | |
| found = frozenset(s for s, rx in _RX.items() if rx.match(ch)) | |
| if found: | |
| return found | |
| if _RX_COMMON.match(ch): | |
| return None | |
| return frozenset({"Other"}) | |
| def resolved_scripts(token): | |
| """Intersection of the script sets of the characters of a token (Common/Inherited ignored). None: no constraint. | |
| An empty frozenset means the token mixes scripts (for example Latin a inside a Cyrillic word).""" | |
| res = None | |
| for ch in token: | |
| s = char_script_set(ch) | |
| if s is None: | |
| continue | |
| res = s if res is None else (res & s) | |
| if not res: | |
| return frozenset() | |
| return res | |
| def is_mixed_script(token): | |
| r = resolved_scripts(token) | |
| return r is not None and len(r) == 0 | |
| def other_script(text): | |
| """True if some letter belongs to neither Latin nor Cyrillic (and is not Common/Inherited). Replaces J2 other_script().""" | |
| for ch in text: | |
| if unicodedata.category(ch).startswith("L"): | |
| s = char_script_set(ch) | |
| if s is not None and not (s & {"Latin", "Cyrillic"}): | |
| return True | |
| return False | |
| def latin_cyrillic_counts(token): | |
| """(number of letters that are Latin only, number that are Cyrillic only) in a token.""" | |
| nl = nc = 0 | |
| for ch in token: | |
| if unicodedata.category(ch).startswith("L"): | |
| s = char_script_set(ch) | |
| if s is None: | |
| continue | |
| if "Latin" in s and "Cyrillic" not in s: | |
| nl += 1 | |
| elif "Cyrillic" in s and "Latin" not in s: | |
| nc += 1 | |
| return nl, nc | |
| def base_letter(ch): | |
| """Letter without combining marks after NFD (C-cedilla -> C). ı and the Kazakh letters have no decomposition.""" | |
| d = unicodedata.normalize("NFD", ch) | |
| return "".join(c for c in d if not unicodedata.category(c).startswith("M")) or ch | |