diff --git a/tests/v2/test_local_agreement.py b/tests/v2/test_local_agreement.py index e62397cce..19e652036 100644 --- a/tests/v2/test_local_agreement.py +++ b/tests/v2/test_local_agreement.py @@ -547,10 +547,14 @@ def test_a_peeled_suffix_is_compared_inside_the_family_box_of_a_joined_source() "B.H. Nuwan", # given B., middle H. Nuwan "V.G.Ravi", # given V., middle G.Ravi "A.B.C", # given A., middle B.C + "Md.Karim", # given Md., middle Karim (#1477) + "Moh.Rafi Nuwan", # given Moh., middle Rafi Nuwan + "Jo.Ann", # given Jo., middle Ann, no vocabulary involved ]) -def test_joined_initials_the_parse_spaces_at_their_full_stops_agree(given: str) -> None: - # #1470: the parse holds the box's letters in the box's field, a - # space after a full stop being all that differs. +def test_a_box_the_parse_cuts_after_its_full_stops_agrees(given: str) -> None: + # #1470, #1477: the parse holds the box's letters in the box's + # field, a space after a full stop the filer typed being all that + # differs. record = _held(given, "Kumarasiri", "LK") assert agreement.read(record, _ORCID_FIELDS, Parser(), _LISTED).got == {} # The control: compared folded alone, as before #1470, it is a miss. @@ -559,67 +563,103 @@ def test_joined_initials_the_parse_spaces_at_their_full_stops_agree(given: str) @pytest.mark.parametrize(("given", "family", "got"), [ - # A word before the full stop, not initials: Md is Muhammad written - # short, and Jo.Ann the same shape with no vocabulary involved. - ("Md.Karim", "Hossain", {"given+middle": "Md. Karim"}), - ("Jo.Ann", "Lee", {"given+middle": "Jo. Ann"}), # The box's letters read as a title: no field holds them. ("AB", "Lee", {"given+middle": ""}), # A word of the family box read into the given box's field. ("K.S", "Perera Silva", {"given+middle": "K. S Perera", "family": "Silva"}), # A title glued before the box's initials, peeled to a field ORCID has no box for. ("Dr.A.B.", "Perera", {"given+middle": "A. B."}), + # A word peeled out of the field at the full stop it is glued by. + ("Dr.Ahmed", "Hassan", {"given+middle": "Ahmed"}), ]) -def test_a_misreading_beside_joined_initials_stays_a_miss( +def test_a_misreading_beside_a_cut_at_a_full_stop_stays_a_miss( given: str, family: str, got: dict[str, str]) -> None: assert agreement.read(_held(given, family), _ORCID_FIELDS, Parser(), _LISTED).got == got +def test_a_cut_across_two_boxes_stays_a_miss() -> None: + # psc-individuals compares each box alone: `Md.Karim` typed in the + # forename box and read given `Md.`, middle `Karim` puts a word in + # the middle-name box the filer left empty (#1477). + assert _read(_filed("", "Md.Karim", surname="Hossain"))[0] == { + "given": "Md.", "middle": "Karim"} + + @pytest.mark.parametrize(("box", "got", "same"), [ ("a.b.", "a. b.", True), # box joined, reading spaced ("a. b.", "a.b.", True), # box spaced, reading joined ("A.B. Lee", "a. b. lee", True), ("A.B.", "A. B. Lee", False), # a word left over ("A.B. Lee", "A. B. Ann Lee", False), # a word left over in the middle - ("A.Bc.D.", "A. Bc. D.", False), # a word inside the run - ("1.2.", "1. 2.", False), # digits are not initials + ("A.Bc.D.", "A. Bc. D.", True), # a word inside the run (#1477) + ("Md.Karim", "Md. Karim", True), # a word before the full stop + ("1.2.", "1. 2.", True), # whatever stands before it ("A.B.", "A. C.", False), # other letters ("A.B.", "A.", False), # letters missing ("AB", "A B", False), # no full stops: a word, not initials ("A B", "AB", False), ("MD", "M D", False), ("Al", "A l", False), - ("Jo.Ann", "Jo. Ann", False), # a word before the full stop + ("Jo.Ann", "Jo. Ann", True), ("K.S", "K S", False), # the reading's full stop gone + ("Md.Karim", "Md Karim", False), + ("Md.Karim", "Md. Karin", False), # other letters ("क.ख.", "क. ख.", True), # letters of any script - ("कि.ख.", "कि. ख.", True), # a letter with its vowel sign, a combining mark + ("कि.ख.", "कि. ख.", True), # a letter with its vowel sign (a regression row) + ("Md.Karim", "Md. Karim Lee", False), # a word left over after a cut word ]) -def test_agreement_across_a_joint_after_initials_only(box: str, got: str, same: bool) -> None: +def test_agreement_across_a_cut_after_a_full_stop_only(box: str, got: str, same: bool) -> None: assert agreement.agrees(box, got) is same assert agreement.agrees(got, box) is same -#: The joined-initials grid: a run of initials, with a name word glued -#: to it, one or two spaced from it, or none, before a one- and a -#: two-word family. -_INITIAL_RUNS = ("A.", "A.B.", "A.B", "A.B.C.", "A.B.C", "Т.В.", "क.ख.") +#: The grid: a run of initials or a word ending in a full stop, with a +#: name word glued to it, one or two spaced from it, or none, before a +#: one- and a two-word family. +_RUNS = ("A.", "A.B.", "A.B", "A.B.C.", "A.B.C", "Т.В.", "क.ख.", "Md.", "Jo.", "Moh.R.") #: How many of the grid's records the comparison before #1470 scored a #: miss, though the reading held the box's letters in the box's field: the #: guard's negative control, re-taken whenever the grid or the parse moves. -_UNGUARDED_EFFECT = 22 +_UNGUARDED_EFFECT = 25 #: The grid's records whose reading holds the box's letters in the box's #: field, and the rest, read with a word of the family box in the given #: box's field (`A.B. Kumarasiri Silva`), which stay misses. -_HELD, _MISREAD = 28, 28 - - -def test_a_reading_cut_only_after_initials_is_never_scored_a_miss( +_HELD, _MISREAD = 40, 40 +#: How many of the grid's held records #1470's rule, a cut forgiven only +#: after initials, still scored a miss: the rows #1477 added +#: (`Md.Nuwan`, `Jo.Nuwan`, `Moh.R.Nuwan`). +_INITIALS_ONLY_EFFECT = 3 + + +def _joined_after_initials(joined: list[str], spaced: list[str]) -> bool: + """#1470's _joined, kept here as the guard's second control: a cut + forgiven only after a piece that is one or more initials.""" + def initials(word: str) -> bool: + return word.endswith(".") and all( + chunk[:1].isalpha() and all(unicodedata.category(c).startswith("M") for c in chunk[1:]) + for chunk in word[:-1].split(".")) + i = 0 + for word in joined: + if i < len(spaced) and agreement.fold(word) == agreement.fold(spaced[i]): + i += 1 + continue + built, j = spaced[i] if i < len(spaced) else "", i + 1 + while j < len(spaced) and len(built) < len(word) and initials(spaced[j - 1]): + built, j = built + spaced[j], j + 1 + if j - i < 2 or built.casefold() != word.casefold(): + return False + i = j + return i == len(spaced) + + +def test_a_reading_cut_only_after_a_full_stop_is_never_scored_a_miss( monkeypatch: pytest.MonkeyPatch) -> None: # #1470: 3.1's #1296 began reading `B.A.S.D.` as given `B.`, middle # `A.S.D.`, and the space the scorer's join puts between the two, # which the fold kept, scored a miss where the box was read right. # Wherever the box's field holds the box's letters, the cut a parse - # draws after initials must not decide the score. + # draws after a full stop the filer typed must not decide the score, + # whatever stands before it (#1477). # "The box's letters" is the fold with its spaces removed, which is # looser than the rule (`AB` against `A B` would pass it): every run # in the grid holds a full stop, so the filter never meets that case. @@ -627,7 +667,7 @@ def test_a_reading_cut_only_after_initials_is_never_scored_a_miss( rows: list[dict[str, str]] = [] misread: list[dict[str, str]] = [] for run, rest, family in itertools.product( - _INITIAL_RUNS, ("", " Nuwan", "Nuwan", " Nuwan Ravi"), ("Perera", "Kumarasiri Silva")): + _RUNS, ("", " Nuwan", "Nuwan", " Nuwan Ravi"), ("Perera", "Kumarasiri Silva")): record = _held(run + rest, family, "LK") name = parser.parse(record["name"]) held = " ".join(filter(None, (name.given, name.middle))) @@ -640,10 +680,14 @@ def misses(records: list[dict[str, str]]) -> int: for r in records) assert misses(rows) == 0 - # The other half: a word of the family box read beside the initials - # stays a miss. + # The other half: a word of the family box read into the given box's + # field stays a miss. assert misses(misread) == _MISREAD - # The negative control: compared folded alone, as before #1470. + # The negative controls: a cut forgiven only after initials, as + # #1470 had it, and compared folded alone, as before #1470. + with monkeypatch.context() as patch: + patch.setattr(agreement, "_joined", _joined_after_initials) + assert misses(rows) == _INITIALS_ONLY_EFFECT monkeypatch.setattr(agreement, "agrees", lambda box, got: agreement.fold(box) == agreement.fold(got)) assert misses(rows) == _UNGUARDED_EFFECT diff --git a/tools/differential/README.md b/tools/differential/README.md index 39eacd589..de5fb3290 100644 --- a/tools/differential/README.md +++ b/tools/differential/README.md @@ -361,7 +361,7 @@ Its score reads the title before the name against `TITULPRED`, the given and mid `my-candidates` downloads the five pinned files and refuses one whose sha256 is not the pinned one (retried first, as a body cut short), then, in this order, one of another header than the file's pinned one, one whose body does not end with a line break or whose last row is short (as `opensanctions-parliaments` does), one holding another number of rows than pinned, and past their shares rows of another width (`MY_MAX_BAD`) and rows with an empty name in a column read (`MY_MAX_NAMELESS`), a row counted once however many of its columns are empty. Its data date is the pinned commit's, and a corpus read at another `MY_COMMIT` or by another `MY_READ_VERSION` is refused on load. It holds no fields, so it is a `snapshot.py --local` population, not a `local_agreement.py` one. The fetch took under a second on 2026-11-19. -`local_agreement.py` does the SCORE job for a local corpus. It parses each filed display name and compares the parser's roles with the boxes the source states (title, given, middle and family for `psc-individuals`; given and middle joined against the given-names box, then family, for `orcid-researchers` and `patentsview-inventors`; title, given and middle joined, family and suffix for `cz-candidates`; family, middle and given, the parts derived, for `uit-vinames`; and title, family, middle and given for `vn-assembly`), folding case, periods, commas and spacing. Where the source has no box after the name, a suffix the parser peels is compared inside the surname box: the display name joins the surname last, so a trailing suffix was typed into it whenever it is filled, with or without a comma (`Smith, Jr.`); the fold read the comma as a miss until 2026-10-28, which moved 83 `psc-individuals` records and 561 `orcid-researchers` records to agreeing and none away. Joining two fields to compare them with one box puts a space between them, which the fold cannot tell from one the filer typed, and a reading that cuts one written word across the two makes it false: 3.1's run of initials (#1296, rules/T7) reads the box `B.A.S.D.` as given `B.`, middle `A.S.D.`, and 1,140 `orcid-researchers` records (403 of them Sri Lanka's) and 35 `patentsview-inventors` records scored a miss with the box's letters in the box's field (measured 2026-11-19), until #1470 had a word agree with the reading that cuts it after one or more of its initials (`agrees`, guarded by `test_a_reading_cut_only_after_initials_is_never_scored_a_miss` with a recorded negative control); a cut after anything but initials still scores a miss (`Md.Karim` read `Md.`, `Karim`: 168 `orcid-researchers` records and 1 `patentsview-inventors` record on 2026-11-19). A parser change that cuts one written word across two fields, or joins two written words, reads `agrees` before trusting the score it moves. Where a source has a title box, and save `cz-candidates` (above), a title the parser reads otherwise, holding a word the vocabulary lists as neither a title nor a connective (a job typed as a title), is set aside as filer error rather than scored, and the report lists the commonest such titles as leads for the vocabulary. The report gives agreement overall and by the group the source states (nationality, country), misses per field, and the commonest disagreement shapes, as counts, word counts and the commonest set-aside titles; `--list N` prints moved records, names and all, for reading locally. Its record, `agreement.jsonl` beside the corpus, holds every record that did not wholly agree, so a re-run reports what moved. As with the local snapshot, check it from the branch against a base you name (`--base `, whose record is kept under `agreements/` beside the corpus, keyed by the fetch, the base's `nameparser/` files, the interpreter's minor version and the scorer, the ten most recent kept; the scorer's fingerprint reads only its own source's `FIELDED` entry, so adding a source leaves the others' records standing, which the whole table, hashed until `patentsview-inventors` was added on 2026-10-28, did not), or, alone, record the shared one from the base checkout (`--record --tree `) and run it from the branch; it refuses a record from another fetch, or one written by another version of the scorer, whose own change would otherwise read as the parser's moves. `--locale cs` scores the parser of a locale pack, `parser_for(locales.CS)`, against a record of its own (`agreement.cs.jsonl`, and kept records keyed by the locale too), so a pack's score and the default's never stand for each other; `measure.py` runs the default's alone. No CI run sees it. +`local_agreement.py` does the SCORE job for a local corpus. It parses each filed display name and compares the parser's roles with the boxes the source states (title, given, middle and family for `psc-individuals`; given and middle joined against the given-names box, then family, for `orcid-researchers` and `patentsview-inventors`; title, given and middle joined, family and suffix for `cz-candidates`; family, middle and given, the parts derived, for `uit-vinames`; and title, family, middle and given for `vn-assembly`), folding case, periods, commas and spacing. Where the source has no box after the name, a suffix the parser peels is compared inside the surname box: the display name joins the surname last, so a trailing suffix was typed into it whenever it is filled, with or without a comma (`Smith, Jr.`); the fold read the comma as a miss until 2026-10-28, which moved 83 `psc-individuals` records and 561 `orcid-researchers` records to agreeing and none away. Joining two fields to compare them with one box puts a space between them, which the fold cannot tell from one the filer typed, and a reading that cuts one written word across the two makes it false: 3.1's run of initials (#1296, rules/T7) reads the box `B.A.S.D.` as given `B.`, middle `A.S.D.`, and 1,140 `orcid-researchers` records (403 of them Sri Lanka's) and 35 `patentsview-inventors` records scored a miss with the box's letters in the box's field (measured 2026-11-19), until #1470 had a word agree with the reading that cuts it after one or more of its initials, and #1477 after any full stop the filer typed, whatever stands before it (`Md.Karim` read `Md.`, `Karim`: 168 `orcid-researchers` records and 1 `patentsview-inventors` record moved on 2026-11-19, 146 of them Bangladesh's). `agrees` holds the rule, guarded by `test_a_reading_cut_only_after_a_full_stop_is_never_scored_a_miss` with a recorded negative control; a run with no full stop, a full stop the reading lost, a word left over or peeled to another field (`Dr.Ahmed` read title `Dr.`), other letters, and a cut across two boxes a source states apart (`psc-individuals`' forename and middle name) still score a miss. A parser change that cuts one written word across two fields, or joins two written words, reads `agrees` before trusting the score it moves. Where a source has a title box, and save `cz-candidates` (above), a title the parser reads otherwise, holding a word the vocabulary lists as neither a title nor a connective (a job typed as a title), is set aside as filer error rather than scored, and the report lists the commonest such titles as leads for the vocabulary. The report gives agreement overall and by the group the source states (nationality, country), misses per field, and the commonest disagreement shapes, as counts, word counts and the commonest set-aside titles; `--list N` prints moved records, names and all, for reading locally. Its record, `agreement.jsonl` beside the corpus, holds every record that did not wholly agree, so a re-run reports what moved. As with the local snapshot, check it from the branch against a base you name (`--base `, whose record is kept under `agreements/` beside the corpus, keyed by the fetch, the base's `nameparser/` files, the interpreter's minor version and the scorer, the ten most recent kept; the scorer's fingerprint reads only its own source's `FIELDED` entry, so adding a source leaves the others' records standing, which the whole table, hashed until `patentsview-inventors` was added on 2026-10-28, did not), or, alone, record the shared one from the base checkout (`--record --tree `) and run it from the branch; it refuses a record from another fetch, or one written by another version of the scorer, whose own change would otherwise read as the parser's moves. `--locale cs` scores the parser of a locale pack, `parser_for(locales.CS)`, against a record of its own (`agreement.cs.jsonl`, and kept records keyed by the locale too), so a pack's score and the default's never stand for each other; `measure.py` runs the default's alone. No CI run sees it. `pairs` reads a paired source (`tse-donors`, whose records carry the registry's name beside the filed one) as two spellings of one person, and sorts each pair by how the filed name departs from the reference: the first of its shapes that fits, from case, accents (case unchanged) and case and accents together, through words left out, shortened or misspelled, to few words in common. A word left out or added counts once per pair, and is listed only when ten pairs share it. `--agreement` also parses both spellings of every pair with a reference name that differs, and counts the pairs whose two readings end the family name in the same word (two readings with no family name share none). It lists the accents-only pairs read with two surnames by the two spellings' last words, the same ten-pair floor applied. diff --git a/tools/differential/local_agreement.py b/tools/differential/local_agreement.py index 4eebc41ae..bb569d90f 100644 --- a/tools/differential/local_agreement.py +++ b/tools/differential/local_agreement.py @@ -54,11 +54,13 @@ boxes joined, so the parser sees the filer's own spelling, and the fold only keeps a box's case, periods, commas or spacing from reading as a miss: a suffix typed into the surname box after a comma - (`Smith, Jr.`) is the parser's family and suffix rejoined. Initials - written joined agree with a reading that cuts them after one or more - of them (agrees): since 3.1 (#1296, rules/T7) a box holding `B.A.S.D.` is - read given `B.`, middle `A.S.D.`, and the scorer's join of the two - puts a space where the filer typed none (#1470). + (`Smith, Jr.`) is the parser's family and suffix rejoined. A word + the filer wrote joined agrees with a reading that cuts it after one or + more of its full stops (agrees): a box holding `B.A.S.D.` is read + given `B.`, middle `A.S.D.` since 3.1 (#1296, rules/T7; #1470), and + `Md.Karim` given `Md.` (Muhammad written short), middle `Karim` + (#1477), and the scorer's join of the two fields puts a space where + the filer typed none. - Where a source has no suffix box, a suffix the parser peels (`Jr`, `OBE`) stood last in the display name, so it was typed into the last filled box, normally the surname: the parser's family is compared @@ -87,7 +89,6 @@ import subprocess import sys import types -import unicodedata from collections import Counter from collections.abc import Callable, Iterator from dataclasses import dataclass, field @@ -186,10 +187,10 @@ def fold(text: str) -> str: def agrees(box: str, got: str) -> bool: """Whether a box and the parser's value for it agree: the same once folded (fold), or the same words save that one side writes as one - word what the other cuts after one or more of its initials - (_joined): the parser reads the box `B.A.S.D.` as given - `B.`, middle `A.S.D.`, and `V.G.Ravi` as `V.`, `G.Ravi`, the box's - letters in the box's field (#1470).""" + word what the other cuts after one or more of its full stops + (_joined): the parser reads the box `B.A.S.D.` as given `B.`, middle + `A.S.D.` (#1470), and `Md.Karim` as `Md.`, `Karim` (#1477), the + box's letters in the box's field.""" if fold(box) == fold(got): return True box_words, got_words = _words(box), _words(got) @@ -201,28 +202,20 @@ def _words(text: str) -> list[str]: return [w for w in text.split() if fold(w)] -def _initials(word: str) -> bool: - """Whether `word` is one or more initials, each a letter (with any - combining marks) and a full stop: `B.`, `A.S.D.`, never `Md.`.""" - return word.endswith(".") and all( - chunk[:1].isalpha() and all(unicodedata.category(c).startswith("M") for c in chunk[1:]) - for chunk in word[:-1].split(".")) - - def _joined(joined: list[str], spaced: list[str]) -> bool: - """Whether `spaced` is `joined` with some words cut after initials: - each word of `joined` is the next of `spaced` as fold reads them, or - is spelled, case aside, by the next two or more of `spaced`, each - but the last initials (`B.A.S.D.` by `B.` `A.S.D.`, `K.S` by `K.` - `S`, never `AB` by `A` `B` or `Md.Karim` by `Md.` `Karim`), and no - word of `spaced` is left over.""" + """Whether `spaced` is `joined` with some words cut after a full + stop: each word of `joined` is the next of `spaced` as fold reads + them, or is spelled, case aside, by the next two or more of + `spaced`, each but the last ending in a full stop (`B.A.S.D.` by + `B.` `A.S.D.`, `Md.Karim` by `Md.` `Karim`, never `AB` by `A` `B` + or `K.S` by `K` `S`), and no word of `spaced` is left over.""" i = 0 for word in joined: if i < len(spaced) and fold(word) == fold(spaced[i]): i += 1 continue built, j = spaced[i] if i < len(spaced) else "", i + 1 - while j < len(spaced) and len(built) < len(word) and _initials(spaced[j - 1]): + while j < len(spaced) and len(built) < len(word) and spaced[j - 1].endswith("."): built, j = built + spaced[j], j + 1 if j - i < 2 or built.casefold() != word.casefold(): return False @@ -467,7 +460,7 @@ def read_record(path: Path, local: local_corpora.Local, locale: str = "" #: What decides a record's reading, besides the parser: scorer_fingerprint #: hashes them, as local_corpora.fold_fingerprint hashes the word fold. -_SCORER_CODE = ("fold", "agrees", "_words", "_initials", "_joined", "_title_words", "unlisted", "read", "_box", "Reading", +_SCORER_CODE = ("fold", "agrees", "_words", "_joined", "_title_words", "unlisted", "read", "_box", "Reading", "key", "score", "_read_chunk", "parser_of")