Skip to content

Commit 4d42657

Browse files
derek73claude
andcommitted
fix: make the fold idempotent for letters that casefold across scripts
The review of d862daa found _fold not idempotent where a combining mark follows the micro sign U+00B5 or U+2183: the mark was read as spelling before casefolding turned the letter Greek or Latin, so a second fold dropped it. A wider sweep found a full stop or apostrophe before a mark doing the same, the mark landing on the letter once the stop was removed. _fold now casefolds and removes the stops and apostrophes before it reads the marks. The code-point test now also folds each code point after a hyphen and with a mark behind a Latin letter, a hyphen and a Devanagari letter (about 3.5 s serial); the old _fold and a fold dropping a hyphen after a mark each fail it. The 283,367-pair oracle is byte-identical, so no table or key moved. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 4922f13 commit 4d42657

3 files changed

Lines changed: 26 additions & 14 deletions

File tree

‎docs/usage.rst‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1397,7 +1397,8 @@ Accepting more
13971397
members, and *replaces* the default set rather than adding to it, so
13981398
extend the default with the ``accept`` of a default matcher. The keys
13991399
follow what is accepted and the ``other_readings`` switch, so one
1400-
matcher's ``keys()`` and ``check()`` always agree; never group with one matcher and check with another:
1400+
matcher's ``keys()`` and ``check()`` always agree; never group with one
1401+
matcher and check with another:
14011402

14021403
.. doctest::
14031404

‎nameparser/_match.py‎

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -598,18 +598,24 @@ def _cross_texts(name: ParsedName, role: Role) -> list[str]:
598598

599599
def _fold(text: str) -> str:
600600
"""The text with case, accents, full stops and apostrophes folded
601-
away: NFC then NFD, casefolded, `.` and the apostrophes `'`, U+2019
602-
and U+02BC removed, then NFC again, so the folded text, and every key
601+
away: NFC, casefolded, `.` and the apostrophes `'`, U+2019 and U+02BC
602+
removed, then NFD, then NFC again, so the folded text, and every key
603603
cut from it, is precomposed (a Hangul syllable, never a conjoining
604604
jamo). Hyphens and spaces are kept. A combining mark (Mn) is dropped
605605
only where the letter it attaches to is Latin, Greek or Cyrillic,
606606
where it is an accent; in another script (a Devanagari virama,
607-
anusvara or vowel sign) it is spelling and is kept."""
607+
anusvara or vowel sign) it is spelling and is kept. The letter's
608+
script is read after casefolding, so a letter that casefolds into
609+
one of those scripts (the micro sign U+00B5 into Greek mu, U+2183
610+
into Latin) loses its mark on the first fold, which keeps the fold
611+
idempotent, as does removing the full stops and apostrophes before
612+
the marks are read (a mark after one then attaches to the letter
613+
before it, and is read with it)."""
608614
if text.isascii():
609615
# no accent, no composition, and no apostrophe but the typed one
610616
return text.casefold().translate(_DROPPED)
611617
decomposed = unicodedata.normalize(
612-
"NFD", unicodedata.normalize("NFC", text))
618+
"NFD", unicodedata.normalize("NFC", text).casefold().translate(_DROPPED))
613619
kept = []
614620
accented = False
615621
for c in decomposed:
@@ -619,8 +625,7 @@ def _fold(text: str) -> str:
619625
else:
620626
accented = unicodedata.name(c, "").startswith(_ACCENTED_SCRIPTS)
621627
kept.append(c)
622-
return unicodedata.normalize(
623-
"NFC", "".join(kept).casefold().translate(_DROPPED))
628+
return unicodedata.normalize("NFC", "".join(kept))
624629

625630

626631
def _strength(result: MatchResult) -> tuple[object, ...]:
@@ -684,7 +689,8 @@ def _spelling_keys(word: str, group: str, lexicon: Lexicon) -> set[str]:
684689
"""The folded members of `word`'s spelling set in `group`, which
685690
`keys` cuts its keys from: every spelling set holds each member's
686691
folded form, so two sets that meet share a folded member. `_fold` is
687-
idempotent (test_match.py holds it over every code point), so the
692+
idempotent (test_match.py holds it over every assigned code point,
693+
alone and beside letters, hyphens and combining marks), so the
688694
folded members are the folds of `_spellings`' own."""
689695
return {_fold(h) for h in _spellings(word, group, lexicon)}
690696

@@ -785,8 +791,8 @@ def _family_words(base: str, lexicon: Lexicon) -> list[str]:
785791
hyphenated one as its halves less any listed particle, an empty half
786792
dropped. Read by `part-of-other` (`_base_words`) and by `keys`, so
787793
the two set aside the same halves. Raw and folded agree half for
788-
half: `_fold` never adds or removes a hyphen or a space (checked over
789-
every code point), so a word's halves fold to its folded halves,
794+
half: `_fold` never adds or removes a hyphen or a space (test_match.py
795+
holds it over every assigned code point in context), so a word's halves fold to its folded halves,
790796
save that a word or half of full stops folds to empty."""
791797
words: list[str] = []
792798
for word in base.split():

‎tests/v2/test_match.py‎

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -258,15 +258,20 @@ def test_fold_is_idempotent_and_keeps_hyphens_over_every_code_point() -> None:
258258
# The key guarantee rests on these two: keys fold a spelling set's
259259
# members again, and a family word's hyphen halves are cut from the
260260
# raw word by position (_spelling_keys, _family_words). Every
261-
# assigned code point is folded alone, after a letter, and beside a
262-
# hyphen (an unassigned one has no decomposition, case or mark to
263-
# fold). Measured 2026-11-08: about 1.2 s serial on py3.11.
261+
# assigned code point is folded alone, after a letter, beside a
262+
# hyphen on either side, and with a combining mark after it behind a
263+
# Latin letter, a hyphen and a Devanagari letter (an unassigned one
264+
# has no decomposition, case or mark to fold). The mark contexts
265+
# found the micro sign U+00B5 and U+2183, which casefold into Greek
266+
# and Latin, and a full stop or apostrophe before a mark (review of
267+
# #1221). Measured 2026-11-08: about 3.5 s serial on py3.11.
264268
bad = []
265269
for i in range(0x110000):
266270
c = chr(i)
267271
if unicodedata.category(c) in ("Cn", "Cs"):
268272
continue
269-
for text in (c, "a" + c, c + "-a"):
273+
for text in (c, "a" + c, c + "-a", "-" + c, "a" + c + "\u0301",
274+
"-" + c + "\u0301", "\u0915" + c + "\u0301"):
270275
folded = _fold(text)
271276
if _fold(folded) != folded or folded.count("-") != text.count("-"):
272277
bad.append((hex(i), text, folded))

0 commit comments

Comments
 (0)