Skip to content

Simplify _classify.py with no reading moved - #1528

Merged
derek73 merged 5 commits into
masterfrom
refactor/simplify-classify
Nov 22, 2026
Merged

derek73 merged 5 commits into
masterfrom
refactor/simplify-classify

Conversation

@derek73

@derek73 derek73 commented Nov 22, 2026 •

Copy link
Copy Markdown
Owner

Step 0 of the 3.2 release checklist: a /simplify pass over nameparser/_pipeline/_classify.py, limited to the code the cycle changed (git diff v3.1.0 origin/master -- nameparser/_pipeline/_classify.py, 202 lines), plus its citation comments. It is quality only: no reading and no tag moves, and no other library file changes. It touches no test expectation; it adds classify tests and two scaling rows.

Changes

  • Two quadratics in rules/P12's pair walks, found by the efficiency review. Each ran only on a rare shape, so no frame guard saw it; the new _SHAPES rows now hold them.
  • One rebuild for both P12 walks. Each pair's marks go into one dict, and the tokens are rebuilt once in a loop, where each walk had rebuilt them with a generator (a frame per token).
    • It is equivalent because the walk from a second word reads a word's tags only to ask whether it has none, or none but TITLE_AMBIGUOUS_TAG. Every head the first walk marks is a bound given name, so it already carries vocab:bound-given, and adding the phrase tag cannot change either answer.
    • rules/C10's rebuild (a delimiter glued to a word) drops its generator the same way.
  • The comma test in the walk from a second word is skipped where the name has no comma. Before, it built a generator for every candidate pair.
  • Smaller tidies.
    • classify binds lex and sets once, where it had three spellings of the lexicon's sets.
    • not tags - X becomes tags <= X.
    • _name_words drops a stem and that its letter test already implies.
    • H10's second disjointness test is labelled as re-asking after the stops come off.
  • Docstring and comments.
    • Reads: now names bound_seconds_listed, the one set tools/stage_reads.py --stage classify lists that the line did not. The trace was checked against every field the line names and every field it leaves out: given_names is not read, and surnames and honorific_tails are read only through helpers.
    • Citation comments now carry the three parts: the quote, the clause, and decisions/X (#N). Pointers were added for S2, S3, S4, H10, H5 (×3), H8, S11, P7, N4, T4, H2, P10 (×2), H9 and S12. Each pointer was checked to name an entry holding that issue.
    • H5's joined-title comment loses its corpus measurement (д-р ending a name), which decisions/H5's Should Hans Meier Priv.-Doz. read the listed title at the end, as Hans Meier Prof. does? #1116 entry holds.
    • T4's comment loses its issue history, which decisions/T4 holds.
    • _name_words' docstring said a numeral "VI to X"; the code excludes any I to X not in Title case, a lone capital being Title case. It now says so.
    • _tags_for's one_case paragraph told the history of a Should John Smith XYZ read XYZ as a credential when no wordlist has those letters? #516 review finding through an example that no longer shows it. JOHN SMITH NEE's NEE is a listed marker, so the caps test declines it on its wordlist test whichever fact it is handed.
      • The history moved to a dated decisions/S9 bullet (2026-11-22), with a witness that still shows the case: JOHN SMITH NEE JONES XYZ under CapsSuffixes.EVERYWHERE. Handed the own-words fact, it reads suffix 'XYZ', maiden 'JONES', and reports both words.
      • test_the_caps_half_asks_the_own_words_case_of_a_clauses_word holds it. Its control was run: it fails with one_case_own passed in, and its JOHN SMITH nee Jones XYZ row fails with the whole name's case passed in.
  • Tests the review found missing: test_the_p12_walks_mark_the_first_word_of_each_pair (a pair across a comma, a bound given name counted after the pair, a word both walks mark) and test_every_word_a_delimiter_is_glued_to_loses_its_joining_tags. Each kills the mutant it was written for: the comma gate inverted, <= made <, the C10 loop over the first word only, setdefault for the second walk's marks.

Proof that nothing moved

Guard Result
classify's own output, compared across trees (control: dropping BOUND_SECOND_TAG moves 176 rows) byte-identical: 7,612 distinct names from every tools/differential/corpus*.jsonl × 8 parsers (default, both family-first orders, CapsSuffixes.EVERYWHERE with dotted off, two extra delimiters, locales.ES, locales.AR, locales.MS) = 60,896 parses. Each row records every token's text, role and sorted tags straight out of classify, its pending ambiguities and one_case, and then the final repr, ambiguities and each token's tags and reading. sha256 406b1d7b… in both trees, base asserted by nameparser.__file__
tools/differential/snapshot.jsonl byte-identical, not re-recorded; test_snapshot.py passes
full suite 57,932 passed, 47 skipped, 3 xfailed (py3.11, parallel)
serial benchmarks (-n0 -m serial) 41 passed, the two new rows and the ±2% frame band included, at load average ~200
measure.py --base (master af58ca0) 30 steps, no moves in any: the committed snapshot, CLDR and GND agreement, the local snapshot of all 18 fetched corpora, the agreement of the 6 fielded ones and the cross-spelling check of 4
mypy, ruff check clean

Frames per parse

sys.setprofile "call" events, mean of 20 after a warm-up, py3.11. Master is af58ca0 in a worktree of its own, path asserted. Each cell shows the default order / FAMILY_FIRST.

name master branch
Dr. Juan0001 de la Vega III (reference) 196 / 197 196 / 197
Smith, John 98 / 97 98 / 97
John Smith 90 / 91 90 / 91
Dr. John Quincy Xavier Adams de la Vega y Lopez III, PhD, MBA (long) 519 / 511 519 / 511
Tariq Abdul Aziz Hassan 154 / 155 149 / 150
Ali Abdel Mahdi Hassan Omar 184 / 185 178 / 179
سامر عز الدين حداد 206 / 207 200 / 201
Tariq Abdul Aziz, Abu Bakr Mohammed Ali Hassan Omar Khalid, John (Jack) Smith née Jones, Jane Doe Ph. D., Associate Dean John Smith, JOHN E SMITH, Smith; Jr, John unchanged unchanged

No row rises. The names that reach a P12 rebuild drop 5 or 6 frames. The reference row and the comma name are unchanged, since no code they run changed, so _CALL_BASELINE's band is untouched.

Behavior-changing candidates (not applied)

  1. Move long_names_only's count out of classify (altitude review). _name_words is a 50-line hand copy of readings later stages make: H2, H9 and H10's leading title run, the trailing run's suffix and numeral take, and Should Ram Bahadur B.K. keep B.K. as its family name? #864's paired initials. nameparser/AGENTS.md says such a guard "belongs where the parse has read them (post_rules here)", and Should جميل عز الدين read family عز الدين, the الدين joining the word before it? #1072 already moved its own two-word guard that way (_post_rules._split_a_lone_backward_pair).
    • The form: tag every completes_any pair in classify, and let post_rules split a long_names_only pair where the name words it actually read number fewer than four. That would delete _name_words, _LEADING_TITLE_TAGS and _BOUND_TAGS.
    • What it moves: the guess is already off both ways. Under Policy(two_family_names=True), Tariq Abdul Aziz Pope and Tariq Abdul Aziz Bishop leave the pair unjoined (family 'Pope'), though the parse ends with four name words. Tariq Abdul Aziz Jr Haddad joins, counting Jr as no word, and the parse then reads Jr as a family word (family 'Jr Haddad').
    • It needs _post_rules.py. decisions/P12 leaves this to "a change that measures it".
  2. One pair walk for both P12 joins (altitude review). The walks share their guards, but only the walk from a second word refuses a pair written across a comma. A shared walk would have to decide whether the forward walk refuses it too. Tariq Abdel, Aziz already reads family 'Tariq Abdel', given 'Aziz' through another stage, but a shared comma guard would need a tag comparison before it could be called quality-only. Not tried.
  3. _name_words' numeral test asking _vocab.reads_as_generation (reuse review; mechanisms.md#ONE-PREDICATE-PER-QUESTION). The two answer differently:
    • a capital one-letter V, I or X after the pair is a name word to the count and the generation to the predicate;
    • a numeral behind an initial (… J. VI) is excluded by the count and not read as the generation by the predicate.
    • Swapping would move abdul names of exactly four words, and adds a frame per word after the pair.

Considered and left alone

  • The alias-marker comma-strip skip (altitude review) would be one exclusion if _vocab.tag_marker_runs mapped alias markers as it maps maiden markers. That needs _vocab.py, outside this pass, and could move tags, since a maiden marker's tag runs across the words it takes.
  • caps_shape_candidate(..., mixed_case=False) (altitude review): one boolean switching the predicate between two shapes, only _comma.decide passing True. A predicate of its own for the comma slot needs _vocab.py and _comma.py.
  • The comma test via bisect (efficiency and reuse reviews): frameless with commas present, but a hand copy of _state.comma_bucket. Calling comma_bucket instead costs two frames. The gate on an empty comma_offsets takes the frame off every comma-less name; a name with commas still pays it on this rare path.
  • _HAS_LETTER built from _vocab._LETTER (reuse review): the same answer in a search. It is left as is; its comment says why it is a search.
  • lex._sets. → sets. inside _tags_for: a binding there moves the recorded count in test_layering.py::test_the_check_refuses_every_read_4i_a_moved (214 → 215) and buys nothing, so _tags_for keeps its spelling.

Reviews

The simplify pass ran four reviewers (reuse, simplification, efficiency, altitude). The findings applied are listed under Changes; the ones refused as behavior changes are listed above.

PR review toolkit, on 684c1b0:

Second round on 4e125bb and dd40883: no defects. One test row's comment claimed more than the row pins; reworded in af66925. The second round also confirmed that segment's recorded one_case and classify's fallback are the same own-words fact.

🤖 Generated with Claude Code

derek73 and others added 2 commits November 22, 2026 00:32
The two rules/P12 walks now collect each pair's marks in one dict and
rebuild the tokens once, in a loop rather than a generator; the walk
from a second word kept its pairs in a list, tested by `in` once per
token, quadratic on a long run of pairs. `_name_words` returns at four,
all `long_names_only` asks, so it no longer walks the whole name once
per candidate pair. The C10 rebuild loses its generator too, the comma
test is skipped where the name has no comma, and the lexicon's sets
are bound once in classify. No tag or reading moves.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Each citation now carries its quote, the clause it implements and a
decisions pointer; the corpus measurement and issue history that had
grown in the H5 and T4 comments are left to the entries that hold them.
The `one_case` paragraph's example had stopped reaching its case, `NEE`
being a listed marker the caps test declines whichever fact it reads,
so its history moves to a dated decisions/S9 bullet with a witness that
still shows it, held by a new test. The Reads: line names
`bound_seconds_listed`, which the stage reads. Two _SHAPES rows hold
the P12 pair walks the previous commit made linear.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@derek73 derek73 added this to the 3.2 milestone Nov 22, 2026
@derek73 derek73 added the internals Refactors and parse cost with no behavior change label Nov 22, 2026
@derek73 derek73 self-assigned this Nov 22, 2026
derek73 and others added 3 commits November 22, 2026 00:51
The caps half reads the own words' one-case fact for every token, a
clause's word included, not the whole name's case: the decisions/S9
bullet, the `_tags_for` docstring and the new test said otherwise, and
the test gains the row that tells the two apart (`JOHN SMITH nee Jones
XYZ`). The S9 bullet is dated by the decision (#516) and points at the
wordlist test's live roster; three citation pointers name the entry
that holds their issue (S8 for #1394, S9 for #516, S10 for #721).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
The test review found three refactored sites no test pinned at the tag
level: a pair read across a comma, a bound given name counted after the
pair by `long_names_only`, and a second delimiter-glued word keeping its
joining tags. A word heading both P12 pairs takes the second walk's
marks by assignment: they hold the phrase mark, so the union was a
no-op.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@derek73
derek73 merged commit 9d04b68 into master Nov 22, 2026
14 checks passed
@derek73
derek73 deleted the refactor/simplify-classify branch November 22, 2026 10:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

internals Refactors and parse cost with no behavior change

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant