diff --git a/docs/customize.rst b/docs/customize.rst index 555a803e0..6207976e2 100644 --- a/docs/customize.rst +++ b/docs/customize.rst @@ -88,6 +88,9 @@ list is one reading per word: >>> Parser(lexicon=lex).parse("John Smith LEED AP").suffix 'LEED AP' +To find the credentials your own data holds that the defaults do not +list, see :ref:`finding-missing-credentials`. + A surname particle is added the same way. The defaults leave out the Dutch ``in`` of ``in 't Veld``, which as a default would take a middle name spelled like it into the surname of names in every language; add diff --git a/docs/policy.rst b/docs/policy.rst index 0fbe83c27..0ce0bb333 100644 --- a/docs/policy.rst +++ b/docs/policy.rst @@ -1326,7 +1326,8 @@ an unlisted word as a credential from how it is written: in periods (``X.Y.Z.``), or in capitals (``XYZ``). The writing cannot settle it alone, because a surname can be written either way, so both fields also decide by position: the word has to stand behind a name that can -spare it. +spare it. For the ones written otherwise, see +:ref:`finding-missing-credentials`. Both fields share one exception. Two letters alone right after a comma are how a person's initials are written, and the words before the comma @@ -1532,6 +1533,75 @@ two-word surname, which the default reads as a credential: >>> caps_off.parse("García Márquez, GABRIEL").given 'GABRIEL' +.. _finding-missing-credentials: + +Finding the credentials your data uses +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +A credential written in mixed case that the vocabulary does not list +(``PhDc``, ``Pharm-D``) is read by neither shape above. After a comma +behind a full name it becomes the given name, and the words before the +comma become the family name, the way ``Ruiz Paz, Ana`` is read. To +find the ones your data holds: + +1. Parse every name, and keep those whose text holds a comma and whose + family name came out two or more words long. +2. Count the given names of those kept. +3. Read the count from the top. The credentials stand near it, among + real listings of a two-word surname (``Ana`` below), which only a + reader can tell apart; in data rich in two-word surnames, such as + Spanish or Portuguese names, the given names can outnumber them. +4. Add each credential to a :class:`~nameparser.Lexicon` (see + :ref:`adding-a-word`) and build a :class:`~nameparser.Parser` on it. + +The count finds the comma shape, where an unlisted credential stands +out as a given name. An added +word is read as a credential after a comma and where it ends a name +with no comma, where unlisted it reads as the family name; before the +end of the name it stays a name word. + +.. doctest:: + + >>> from collections import Counter + >>> from nameparser import Lexicon, Parser, parse + >>> records = ["Ana Ruiz Paz, PhDc", "Lena Moreau Diaz, Pharm-D", + ... "Tomas Vidal Ortega, PhDc", "Ruiz Paz, Ana"] + >>> tally = Counter() + >>> for cred_text in records: + ... cred_parsed = parse(cred_text) + ... if "," in cred_text and len(cred_parsed.family.split()) >= 2: + ... tally[cred_parsed.given] += 1 + >>> tally.most_common() + [('PhDc', 2), ('Pharm-D', 1), ('Ana', 1)] + >>> with_creds = Parser(lexicon=Lexicon.default().add( + ... "phdc", "pharm-d", kind="credential")) + >>> fixed = with_creds.parse("Ana Ruiz Paz, PhDc") + >>> fixed.given, fixed.family, fixed.suffix + ('Ana', 'Paz', 'PhDc') + >>> with_creds.parse("Ruiz Paz, Ana").given + 'Ana' + >>> parse("Ana Ruiz Paz PhDc").family + 'PhDc' + >>> with_creds.parse("Ana Ruiz Paz PhDc").suffix + 'PhDc' + +A non-empty ``ambiguities`` holding ``suffix-or-name`` marks a name +where the parser weighed a credential against a name, as it does for +the capitals shape, and for an unlisted word behind a listed +credential, which is read as a credential and reported +(``Ana Ruiz Paz, PhD PhDc``). A mixed-case word opening the part +after the comma is never weighed, so it carries no report, and the count +above is the way to find it: + +.. doctest:: + + >>> [a.kind.value for a in parse("Ana Ruiz Paz, XYZ").ambiguities] + ['suffix-or-name'] + >>> parse("Ana Ruiz Paz, PhDc").ambiguities + () + >>> [a.kind.value for a in parse("Ana Ruiz Paz, PhD PhDc").ambiguities] + ['suffix-or-name'] + .. _strip-flags: Keeping emoji and control characters