Skip to content

Commit 427c01e

Browse files
authored
Merge pull request #310 from derek73/v2.1/cjk-honorifics
Recognize spaced CJK postnominal honorifics as suffixes
2 parents 75b3c9c + 4ebb4c4 commit 427c01e

7 files changed

Lines changed: 197 additions & 0 deletions

File tree

docs/release_log.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ Release Log
2525
- Fix names containing 〆 (U+3006, the shime mark that opens Japanese surnames like 〆木 and 〆谷) parsing given-first: the script classifier now counts 〆 as Han, extending the 々 entry the table already carries, and going a step further than it — 々 is Script=Han and merely outside the ideograph blocks, while 〆 is Script=Common — so these names take the East Asian family-first reading like any other wholly-Han name. ``Policy(script_orders={})`` restores the positional reading for these names exactly as for other wholly-Han names (#303)
2626
- Fix the katakana middle dot ``・`` (U+30FB, and its halfwidth twin U+FF65) being read as part of a name rather than as the divider it is: it now separates tokens exactly as a space does. A transcribed foreign name therefore divides into its parts and, being wholly katakana, keeps its source order — ``"マイケル・ジャクソン"`` gives given ``マイケル``, family ``ジャクソン``, where 1.x left the whole string in ``first`` — while a kanji pair written the same way takes the family-first rule (``"高橋・一郎"`` → family ``高橋``). Native Japanese names never contain this character and no other script's names use it, so the separation is unconditional, which also means it is **not** covered by the two policy opt-outs. Rendering follows, the way the Korean split's does: the dot comes back as a space, so ``str(HumanName("マイケル・ジャクソン"))`` is now ``"マイケル ジャクソン"``, and that reaches delimited content too — the nickname in ``"山田 太郎 (マイケル・ジャクソン)"`` renders ``"マイケル ジャクソン"``. The Chinese interpunct ``·`` is not an unconditional separator like these two: U+00B7 is also the Catalan punt volat and appears inside legitimate names (``Gal·la``), so it divides only between classified-script characters — the transcription treatment it marks is #298's (#272)
2727
- Fix 间隔号-divided transcriptions parsing as one unsplit token: U+00B7 — the interpunct Chinese text divides a transcribed foreign name with, ``威廉·莎士比亚`` for William Shakespeare — is now a token separator between characters of a classified script, and a name it divides keeps its source order and is never segmented: the dot is the transcription marker, playing the role pure katakana plays in the kana license. It divides ONLY between classified-script characters, so the Catalan punt volat interior to names like ``Gal·la`` is untouched. The Japanese nakaguro is deliberately not a transcription marker — ``高橋・一郎`` is roster formatting, 姓・名, and keeps its family-first reading — so a transcription typed with the wrong dot (``威廉・莎士比亚``) reads by the convention of the codepoint it was typed with; like the spaced form, only the Chinese dot rescues it. Rendering stays space-joined (``str(HumanName("威廉·莎士比亚"))`` is ``"威廉 莎士比亚"``); a custom ``string_format`` such as ``"{first}·{last}"`` reinstates the dot (#298)
28+
- Fix spaced CJK postnominal honorifics parsing as name parts: 씨, 박사, 선생님, 교수님, 군, 양 (Korean — standardly written as their own token), 先生, 女士, 小姐, 博士, 教授 (Chinese, with 先生/博士/教授 shared with Japanese), and 様, 氏 (Japanese) now route to ``suffix``, so ``王小明 先生`` reads family ``王小明`` where the family-first default had confidently made 先生 the given name. Whole-token matching — which also reaches a glued surname+honorific token, since segmentation splits off the surname first (``김씨`` reads family 김, suffix 씨; ``王先生`` the same under ``locales.ZH``); a glued honorific after a given name (``김민준씨``) and glued kana (``山田太郎様``) stay out of reach, tracked as #308 (closes #307)
2829
- Fix NFD-decomposed input missing the East Asian defaults entirely: script classification now normalizes to NFC before deciding, so a Korean or Japanese name typed on macOS — where decomposed text is routine — gets the same order rule as its composed twin, which it silently did not before. Segmentation MATCHING deliberately stays raw, so an unspaced NFD hangul name is ordered correctly but not split, rather than being split in the wrong place. One gotcha worth stating plainly: parse output preserves the encoding it was given, so for NFD input ``name.family == "김"`` is ``False`` even though it is the same name — compare NFC-normalized text when comparing across encodings (#272)
2930
- Fix the Ukrainian conjunction ``й`` not joining the pieces around it: it is the euphonic alternate of ``і``, the two chosen by the surrounding vowel and consonant rather than by meaning (``"Олесь і Олена"`` but ``"Марія й Петро"``), so real Ukrainian data carries both spellings and shipping only ``і`` recognized just one of them. ``"Олесь й Олена Коваленки"`` now gives given ``"Олесь й Олена"`` where the ``й`` previously landed in ``middle``. Same treatment as the ``и``/``і`` entries added in 2.0.0, single-letter carve-out included: the conjunction joins only once the name has enough pieces, and a punctuated initial still wins, so ``"Й. Сліпий"`` is unaffected. Raised in a comment on #267
3031

docs/usage.rst

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,16 @@ Chinese dot rescues the source order. And a comma disables the script
295295
behaviors entirely, on the reasoning ``name_order`` already follows: whoever
296296
wrote the comma has already said where the family name ends.
297297

298+
Honorifics and degrees follow a CJK name, and the spaced forms are
299+
recognized as suffixes — ``王小明 先生`` reads family 王小明 with 先生
300+
in ``suffix``, and Korean's standardly-spaced 씨 routes the same way.
301+
A glued honorific is reached in exactly one shape — surname +
302+
honorific, where segmentation splits off the surname and the
303+
honorific is what remains: 김씨 reads family 김 with suffix 씨, and
304+
王先生 the same way under the zh pack. A glued honorific after a
305+
given name (김민준씨, 王小明先生) and glued kana (山田太郎様, 田中さん)
306+
stay part of the name.
307+
298308
A division the parser had to choose is reported rather than hidden.
299309
When an unspaced name has more than one vocabulary-supported split —
300310
``남궁민수`` is 남궁 + 민수 by the two-syllable surname but 남 + 궁민수

nameparser/config/suffixes.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,45 @@
3636
'ז״ל', # same, U+05F4 gershayim
3737
'שליט"א', # honorific for a living rabbi, ASCII quote
3838
'שליט״א', # same, U+05F4 gershayim
39+
40+
# #307: CJK postnominal honorifics and degrees, whole-token
41+
# matched. That reaches the spaced forms directly, and glued
42+
# forms in exactly ONE shape: surname+honorific (김씨; 王先生
43+
# under the zh pack), where segmentation splits off the surname
44+
# before suffix classification and the honorific is what remains.
45+
# A glued honorific after a GIVEN name (김민준씨, 王小明先生) and
46+
# glued kana (山田太郎様; 田中さん additionally ships no さん
47+
# entry at all) stay out of reach, tracked as #308.
48+
# Self-selecting like the Korean surnames: a Han or hangul entry
49+
# can only ever match CJK text. Vetting per the мл/ст standard
50+
# above -- three entries worth naming:
51+
# - 氏: a rare Japanese surname reading exists, but a bare
52+
# trailing 氏 after a name is the news-style honorific, and a
53+
# 氏-surnamed person writes it FIRST.
54+
# - 양/군: 양 is also a top-tier surname (Yang) -- but a surname
55+
# LEADS, so the trailing-only suffix gate never sees it there
56+
# (양 미선 keeps family 양); as single-syllable GIVEN names in
57+
# final position both are vanishingly rare in practice.
58+
# - 博士: an attested Japanese given name (ひろし, Hiroshi) --
59+
# the doctorate reading vastly dominates the spaced trailing
60+
# position this set matches, and a glued 田中博士 is untouched.
61+
# Bare hangul 선생/교수 are deliberately absent (only the -님
62+
# honorific forms ship): the bare forms read as common nouns as
63+
# readily as address terms. Further candidates (여사, 太太, 殿)
64+
# wait on the same case-by-case argument.
65+
'씨', # ko Mr./Ms. -- standardly spaced in Korean orthography
66+
'박사', # ko doctorate holder ("Dr.")
67+
'선생님', # ko teacher/respected elder
68+
'교수님', # ko professor (honorific form)
69+
'군', # ko young man ("Master")
70+
'양', # ko young woman ("Miss")
71+
'先生', # zh Mr. / ja teacher-master -- honorific in both
72+
'女士', # zh Ms./Madam
73+
'小姐', # zh Miss
74+
'博士', # zh+ja doctorate holder, shared Han
75+
'教授', # zh+ja professor, shared Han
76+
'様', # ja formal Mr./Ms. (the mail-addressing honorific)
77+
'氏', # ja news-style Mr. (田中氏)
3978
}
4079
"""
4180

tests/v2/cases.py

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -707,4 +707,80 @@ def __post_init__(self) -> None:
707707
notes="one classified neighbor is not enough: the guard "
708708
"requires both, so the undivided dot remains part of "
709709
"the word -- declining, not deciding"),
710+
Case("zh_honorific_suffix_spaced", "王小明 先生",
711+
{"family": "王小明", "suffix": "先生"},
712+
classification="fix(#307)",
713+
notes="CJK honorifics FOLLOW the name; a spaced 先生 (Mr.) is "
714+
"a suffix, and recognizing it must come before the "
715+
"family-first order hands it a role -- unrecognized it "
716+
"read as the GIVEN name under the 2.1 defaults"),
717+
Case("ko_honorific_ssi", "김민준 씨",
718+
{"family": "김", "given": "민준", "suffix": "씨"},
719+
classification="fix(#307)",
720+
notes="Korean orthography standardly SPACES 씨, so the "
721+
"whole-token suffix machinery reaches it; the name "
722+
"still segments (suffix classification runs after the "
723+
"script_segment stage, which only ever saw 김민준)"),
724+
Case("ko_degree_baksa", "김민준 박사",
725+
{"family": "김", "given": "민준", "suffix": "박사"},
726+
classification="fix(#307)",
727+
notes="박사 (doctorate) is the ko analogue of a trailing "
728+
"PhD: fix(suffix-routing)'s two-token shape, one "
729+
"script over"),
730+
Case("ja_sama_spaced", "田中 太郎 様",
731+
{"family": "田中", "given": "太郎", "suffix": "様"},
732+
classification="fix(#307)",
733+
notes="the spaced 様 of forms and databases; the glued "
734+
"mail-addressing form 山田太郎様 is #308's mechanism, "
735+
"out of reach of whole-token matching"),
736+
Case("ko_honorific_glued_via_segmentation", "김씨",
737+
{"family": "김", "suffix": "씨"},
738+
classification="fix(#307)",
739+
notes="glued hangul is reached ONLY in the surname+honorific "
740+
"shape: default segmentation splits off the surname, "
741+
"and the honorific is what remains -- a partial "
742+
"delivery of #308 that falls out of stage order. A "
743+
"glued honorific after a GIVEN name (김민준씨, the row "
744+
"below) stays out of reach"),
745+
Case("ko_honorific_after_comma", "김민준, 씨",
746+
{"family": "김민준", "suffix": "씨"},
747+
classification="fix(#307)",
748+
notes="the post-comma lenient gate admits the honorific too; "
749+
"the comma disables segmentation per the comma "
750+
"doctrine, so 김민준 stays whole"), Case("ko_honorific_glued_given_stays", "김민준씨",
751+
{"family": "김", "given": "민준씨"},
752+
notes="the boundary of the glued reach: segmentation splits "
753+
"off 김 and the REMAINDER is 민준씨, not a listed "
754+
"honorific token -- the common full-name glued shape "
755+
"is #308's mechanism, pinned here so the docs' scoped "
756+
"claim stays true"),
757+
Case("zh_honorific_glued_surname", "王先生",
758+
{"family": "王", "suffix": "先生"},
759+
locale="zh",
760+
classification="fix(#307)",
761+
notes="the Han twin of 김씨: the zh pack's segmentation "
762+
"splits off the surname and the remaining 先生 is the "
763+
"honorific token"),
764+
Case("zh_honorific_glued_given_stays", "王小明先生",
765+
{"family": "王", "given": "小明先生"},
766+
locale="zh",
767+
notes="the Han boundary twin: 小明先生 is the remainder, not "
768+
"an honorific token -- glued full names stay #308"),
769+
Case("ko_suffix_matching_is_whole_token", "김지양",
770+
{"family": "김", "given": "지양"},
771+
notes="지양 ENDS with the honorific 양 but is a given name: "
772+
"suffix matching is whole-token, never endswith -- the "
773+
"pin the differential rule's anchor mirrors at the "
774+
"name-string level"),
775+
Case("ko_surname_yang_leads", "양 미선",
776+
{"family": "양", "given": "미선"},
777+
notes="양 is both a top-tier surname (Yang) and a shipped "
778+
"honorific: position decides, and a surname LEADS -- "
779+
"the trailing-only suffix gate never sees it here"),
780+
Case("ko_honorific_stack", "김민준 박사 씨",
781+
{"family": "김", "given": "민준", "suffix": "박사, 씨"},
782+
classification="fix(#307)",
783+
notes="a trailing RUN of honorifics peels whole, like "
784+
"'Smith PhD MD' -- the multi-suffix loop the peel "
785+
"shares with Latin suffixes"),
710786
)

tests/v2/test_regex_sync.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -334,3 +334,33 @@ def test_cjk_corpus_matches_the_case_table() -> None:
334334
assert checked_in == module.selected_names(), (
335335
"corpus_cjk.jsonl is stale: regenerate with "
336336
"`uv run python tools/differential/build_cjk_corpus.py`")
337+
338+
339+
def test_differential_honorific_rule_matches_the_suffix_vocabulary() -> None:
340+
"""The fix(cjk-honorific-suffix) rule's alternation is a hand copy
341+
of SUFFIX_NOT_ACRONYMS' CJK entries (#307) -- the toml cannot
342+
import them. The expected set is DERIVED from the config by script
343+
membership (a classified codepoint anywhere in the entry), so
344+
adding a CJK honorific without widening the rule -- or widening
345+
the rule with something the vocabulary does not ship -- fails
346+
here. The span-bearing pins above skip this rule on purpose: its
347+
trigger is the alternation, not a character class.
348+
"""
349+
from nameparser.config.suffixes import SUFFIX_NOT_ACRONYMS
350+
351+
toml_path = (Path(__file__).parents[2] / "tools" / "differential"
352+
/ "expected_changes.toml")
353+
rules = tomllib.loads(toml_path.read_text())["change"]
354+
matched = [r for r in rules if "cjk-honorific-suffix" in r["issue"]]
355+
assert len(matched) == 1
356+
regex = matched[0]["name_regex"]
357+
prefix = "(?:^| )(?:"
358+
assert regex.startswith(prefix) and regex.endswith(")$"), (
359+
"the honorific rule's shape changed; update this parser")
360+
declared = set(regex[len(prefix):-2].split("|"))
361+
has_classified = _policy._script_matcher(*_policy._SCRIPT_RANGES)
362+
expected = {entry for entry in SUFFIX_NOT_ACRONYMS
363+
if has_classified(entry)}
364+
assert declared == expected, (
365+
f"rule declares {sorted(declared)}; the config's CJK suffix "
366+
f"entries are {sorted(expected)}")

tools/differential/corpus_cjk.jsonl

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@
2222
"毛 김"
2323
"毛泽东"
2424
"王·Smith"
25+
"王先生"
26+
"王小明 先生"
27+
"王小明先生"
28+
"田中 太郎 様"
2529
"田中『ハナ』花子"
2630
"諸葛亮"
2731
"阿明"
@@ -32,7 +36,15 @@
3236
"高橋一郎"
3337
"김 민준"
3438
"김민준"
39+
"김민준 박사"
40+
"김민준 박사 씨"
41+
"김민준 씨"
42+
"김민준, 씨"
43+
"김민준씨"
44+
"김씨"
45+
"김지양"
3546
"남궁"
3647
"남궁민수"
3748
"남궁민수, 지훈"
3849
"마이클·잭슨"
50+
"양 미선"

tools/differential/expected_changes.toml

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,35 @@ issue = "fix(cjk-comma-compound) comma routing compounds with the CJK order flip
177177
name_regex = "(?s)(?=.*,)(?=.*[\\u3005-\\u3006\\u3040-\\u309F\\u30A0-\\u30FF\\u3400-\\u4DBF\\u4E00-\\u9FFF\\uF900-\\uFAFF\\uAC00-\\uD7A3\\uFF65-\\uFF65])"
178178
fields = ["first", "middle", "last", "title", "suffix"]
179179

180+
[[change]]
181+
issue = "fix(cjk-honorific-suffix) postnominal honorifics recognized, compounding with the CJK order flip"
182+
# '王小明 先生', '김민준 씨', '田中 太郎 様': #307 ships the spaced CJK
183+
# honorifics as suffix vocabulary, so a trailing 先生/씨/様 moves to
184+
# `suffix` where 1.4 read it as a name part -- and the remaining
185+
# wholly-CJK name simultaneously takes the family-first flip, so a
186+
# three-token name moves `middle` too, past the fields-only
187+
# suffix-routing rule. Keyed on the honorific alternation itself
188+
# (the maiden-marker rule's pattern): the trigger tokens are wholly
189+
# CJK characters, so the rule matches only a name whose TRAILING
190+
# token is a listed honorific -- a mostly-Latin name with one
191+
# ('Wang Xiaoming 先生') is inside its shadow, accepted because the
192+
# recognized honorific is the diff's cause there too, and the
193+
# alternation is a hand copy of SUFFIX_NOT_ACRONYMS' CJK entries --
194+
# pinned by tests/v2/test_regex_sync.py, which derives the expected
195+
# set from the config by script membership. Anchored to a WHOLE
196+
# trailing token ((?:^| )...$), judged on the NAME STRING: without
197+
# it, any name string merely ENDING in 양/군 -- the glued given name
198+
# 김지양, the spaced 김 지양 -- would match and could have a real
199+
# regression absorbed as intentional (the case table pins the
200+
# parser-side twin: suffix matching is whole-token, never endswith).
201+
# Glued surname+honorific diffs (김씨 -- reached via segmentation)
202+
# fall to the fields-only suffix-routing rule by their
203+
# {first, last, suffix} shape (v1 held the whole token in first, so
204+
# last moves too), which is field-honest even though that rule's
205+
# prose describes the two-token Latin case.
206+
name_regex = "(?:^| )(?:씨|박사|선생님|교수님|군|양|先生|女士|小姐|博士|教授|様|氏)$"
207+
fields = ["first", "middle", "last", "suffix"]
208+
180209
[[change]]
181210
issue = "feat(#269) non-Latin titles/conjunctions recognized"
182211
# 'г-н Иван Петров' (Cyrillic title), 'Хосе И Мария Сантос' (Cyrillic

0 commit comments

Comments
 (0)