|
9 | 9 | from __future__ import annotations |
10 | 10 |
|
11 | 11 | import re |
| 12 | +import unicodedata |
12 | 13 |
|
13 | 14 | from nameparser._lexicon import Lexicon, _normalize |
14 | 15 | from nameparser._policy import Script |
|
42 | 43 | # 𠮷, U+20BB7), so leaving them out silently mis-orders those names; |
43 | 44 | # unassigned gaps inside the span are harmless, since no real name |
44 | 45 | # contains an unassigned codepoint. HANGUL: precomposed syllables |
45 | | -# only -- modern Korean text never writes names as bare jamo. Kana is |
46 | | -# DELIBERATELY absent: a kana token identifies Japanese, whose |
47 | | -# conventions are #272's segmenter, not this table's. The ranges |
48 | | -# below must stay mutually disjoint: single_script returns the FIRST |
49 | | -# covering entry (dict iteration order), so an overlapping future |
50 | | -# script (e.g. a ja entry that also covers Han) would make the result |
51 | | -# order-dependent instead of well-defined. |
| 46 | +# only -- modern Korean text never writes names as bare jamo. |
| 47 | +# HIRAGANA/KATAKANA (#272): the two kana blocks, each in full. There |
| 48 | +# IS a supplementary-plane kana repertoire (Kana Supplement, Kana |
| 49 | +# Extended-A/B, Small Kana Extension, U+1AFF0-U+1B16F, 311 assigned |
| 50 | +# codepoints) but none of it is WORTH chasing the way Han's astral |
| 51 | +# block is: those codepoints are hentaigana and other archaic/ |
| 52 | +# phonetic-extension forms no modern Japanese name uses, unlike |
| 53 | +# supplementary Han, which real surnames genuinely need. Halfwidth |
| 54 | +# kana (U+FF65-U+FF9D) is likewise deliberately excluded -- legacy |
| 55 | +# bank/CSV data uses it, but it is a separate normalization problem; |
| 56 | +# Task 2b's separator handling only touches the halfwidth DOT |
| 57 | +# (U+FF65), not the rest of that block. This table classifies by |
| 58 | +# Unicode BLOCK, not the UAX #24 Script property: U+30A0, U+30FB |
| 59 | +# (the middle dot), and U+30FC (the prolonged sound mark) all carry |
| 60 | +# Script=Common under UAX #24, and the combining kana voicing marks |
| 61 | +# U+3099-U+309C are Common/Inherited -- yet every one of them is |
| 62 | +# needed here, and block membership, not the Script property, is |
| 63 | +# what puts them in range. The katakana block's upper end (U+30FF) |
| 64 | +# including the middle dot U+30FB is load-bearing for |
| 65 | +# effective_script's kana license below (see its docstring) -- a |
| 66 | +# later task turns U+30FB into a tokenize-level separator, but until |
| 67 | +# then it classifies as ordinary katakana. The ranges below must stay |
| 68 | +# mutually disjoint: single_script returns the FIRST covering entry |
| 69 | +# (dict iteration order), so an overlapping future script would make |
| 70 | +# the result order-dependent instead of well-defined. |
52 | 71 | _SCRIPT_RANGES: dict[Script, tuple[tuple[int, int], ...]] = { |
53 | 72 | Script.HAN: ((0x3400, 0x4DBF), (0x4E00, 0x9FFF), (0xF900, 0xFAFF), |
54 | 73 | (0x20000, 0x323AF)), |
55 | 74 | Script.HANGUL: ((0xAC00, 0xD7A3),), |
| 75 | + Script.HIRAGANA: ((0x3040, 0x309F),), |
| 76 | + Script.KATAKANA: ((0x30A0, 0x30FF),), |
56 | 77 | } |
57 | 78 |
|
58 | 79 | # Derived, never hand-written: one character class per script, in the |
|
65 | 86 | for script, ranges in _SCRIPT_RANGES.items() |
66 | 87 | } |
67 | 88 |
|
| 89 | +#: The Japanese repertoire: the union effective_script's kana license |
| 90 | +#: quantifies over. A tuple in the table's own order (HAN, then |
| 91 | +#: HIRAGANA, then KATAKANA -- HANGUL simply omitted, the relative |
| 92 | +#: order of the rest is unchanged), not a frozenset: nothing consults |
| 93 | +#: membership today, only iterates to build the pattern below, so |
| 94 | +#: there is nothing set-ness would buy; a later task that needs |
| 95 | +#: membership can convert it then. |
| 96 | +_JA_SCRIPTS = (Script.HAN, Script.HIRAGANA, Script.KATAKANA) |
| 97 | +_JA_PATTERN = re.compile( |
| 98 | + "[" |
| 99 | + + "".join(f"\\U{lo:08x}-\\U{hi:08x}" |
| 100 | + for s in _JA_SCRIPTS |
| 101 | + for lo, hi in _SCRIPT_RANGES[s]) |
| 102 | + + "]+") |
| 103 | + |
68 | 104 |
|
69 | 105 | def is_initial(text: str) -> bool: |
70 | 106 | """'A.' / 'j.' / bare capital -- v1's is_an_initial.""" |
@@ -162,18 +198,72 @@ def period_joined_vocab(text: str, lexicon: Lexicon) -> str | None: |
162 | 198 | return None |
163 | 199 |
|
164 | 200 |
|
| 201 | +def _normalized_for_script(text: str) -> str | None: |
| 202 | + """The guard AND the NFC normalization single_script and |
| 203 | + effective_script's license path both need, single-sourced so they |
| 204 | + cannot drift: None for the two shapes neither ever classifies |
| 205 | + (empty, and the common all-ASCII Latin token -- skipped before |
| 206 | + normalizing, since ASCII is already NFC and every _SCRIPT_RANGES |
| 207 | + entry is non-ASCII regardless), else an NFC-normalized copy. |
| 208 | +
|
| 209 | + NFC, not raw: NFD input decomposes precomposed katakana onto a |
| 210 | + base character plus a COMBINING mark (U+3099/U+309A, which sit in |
| 211 | + the HIRAGANA block, not katakana's), so classifying raw NFD text |
| 212 | + can hand a pure-katakana token the kana license by accident; NFD |
| 213 | + also decomposes Hangul syllables onto bare jamo (U+1100-U+11FF), |
| 214 | + entirely outside the HANGUL range, so raw NFD Korean input misses |
| 215 | + the shipped family-first order rule rather than merely misfiring. |
| 216 | + Normalizing first fixes both. This is classification-only and |
| 217 | + read-only: the returned copy is never what gets tokenized, so |
| 218 | + token text and spans stay exactly what the caller wrote. |
| 219 | +
|
| 220 | + MATCHING (is_initial, suffix lookups, etc.) deliberately stays on |
| 221 | + raw text elsewhere in this module -- unlike script classification, |
| 222 | + NFD only ever costs a match there (a suffix word written NFD fails |
| 223 | + to match its NFC vocabulary entry), never wrong-matches, so the |
| 224 | + asymmetry is safe: one direction needs a fix, the other doesn't. |
| 225 | + """ |
| 226 | + if not text or text.isascii(): |
| 227 | + return None |
| 228 | + return unicodedata.normalize("NFC", text) |
| 229 | + |
| 230 | + |
165 | 231 | def single_script(text: str) -> Script | None: |
166 | 232 | """The one Script whose ranges cover EVERY char of `text`, else |
167 | 233 | None (mixed-script text has no well-defined convention to apply; |
168 | | - the caller falls back to the positional default).""" |
169 | | - if not text: |
170 | | - return None # the + below needs one char; "" belongs to no script |
171 | | - if text.isascii(): |
172 | | - # every _SCRIPT_RANGES entry is non-ASCII (lowest today is |
173 | | - # U+3400): skip the patterns for the overwhelmingly common |
174 | | - # Latin token (the _tokenize._ignorable ASCII-floor precedent) |
| 234 | + the caller falls back to the positional default). Classifies an |
| 235 | + NFC-normalized copy of `text` -- see _normalized_for_script. |
| 236 | + Callers wanting the kana-mixed license (a kanji+kana composite |
| 237 | + resolving to HIRAGANA) want effective_script, not this function.""" |
| 238 | + normalized = _normalized_for_script(text) |
| 239 | + if normalized is None: |
175 | 240 | return None |
176 | 241 | for script, pattern in _SCRIPT_PATTERNS.items(): |
177 | | - if pattern.fullmatch(text): |
| 242 | + if pattern.fullmatch(normalized): |
178 | 243 | return script |
179 | 244 | return None |
| 245 | + |
| 246 | + |
| 247 | +def effective_script(text: str) -> Script | None: |
| 248 | + """single_script, extended by the kana license (#272 amendment): |
| 249 | + a MIXED token wholly within Han∪hiragana∪katakana is Japanese -- |
| 250 | + it necessarily contains kana (pure Han is not mixed), cannot be |
| 251 | + Chinese, and is not a foreign transcription (those are |
| 252 | + katakana-only: マイケル has no kanji, but さくらエミ -- hiragana |
| 253 | + plus katakana -- is kana-only AND licensed) -- and resolves to the |
| 254 | + HIRAGANA carrier entry. Pure-katakana stays KATAKANA |
| 255 | + (single_script's answer): a lone katakana token is predominantly a |
| 256 | + transcribed foreign name, so nothing defaults on it.""" |
| 257 | + script = single_script(text) |
| 258 | + if script is not None: |
| 259 | + return script |
| 260 | + # normalized is None for both shapes _JA_PATTERN could never match |
| 261 | + # anyway (empty text, or the all-ASCII text single_script's fast |
| 262 | + # path already ruled out) -- real work, not a leftover "if text" |
| 263 | + # guard: unlike the pre-NFC version, None here also covers the |
| 264 | + # ASCII case, which single_script's own empty check alone would |
| 265 | + # not. |
| 266 | + normalized = _normalized_for_script(text) |
| 267 | + if normalized is not None and _JA_PATTERN.fullmatch(normalized): |
| 268 | + return Script.HIRAGANA |
| 269 | + return None |
0 commit comments