File size: 4,758 Bytes
24aaa76 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 | """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}")
@functools.lru_cache(maxsize=None)
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
|