From b74141257443f217393bce2abf82ccd1d425b10d Mon Sep 17 00:00:00 2001 From: Derek Gulbranson Date: Fri, 31 Aug 2018 15:59:44 -0700 Subject: [PATCH 01/99] documentation fixes --- .gitignore | 1 - CONTRIBUTING.md | 2 +- docs/customize.rst | 16 ++++++++-------- nameparser/config/prefixes.py | 16 ++++++++-------- nameparser/parser.py | 2 +- 5 files changed, 18 insertions(+), 19 deletions(-) diff --git a/.gitignore b/.gitignore index c586728..c3fe42b 100644 --- a/.gitignore +++ b/.gitignore @@ -5,7 +5,6 @@ __pycache__/ .python2/ MANIFEST nameparser.egg-info/ -dummycert.pem build *.egg .coverage diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 329716d..bb9fc4a 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -4,7 +4,7 @@ Contributing Development Environment Setup -------------------------------- -There are some exernal dependencies required in order to run the +There are some external dependencies required in order to run the tests, located in the dev-requirements.txt file. pip install -r dev-requirements.txt diff --git a/docs/customize.rst b/docs/customize.rst index 46a60c9..6097050 100644 --- a/docs/customize.rst +++ b/docs/customize.rst @@ -39,14 +39,14 @@ instantiate the :py:class:`~nameparser.parser.HumanName` class (see below). Editable attributes of nameparser.config.CONSTANTS ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -* :py:obj:`~nameparser.config.CONSTANTS.titles` - Pieces that come before the name. Includes all `first_name_titles`. Cannot include things that may be first names. -* :py:obj:`~nameparser.config.CONSTANTS.first_name_titles` - Titles that, when followed by a single name, that name is a first name, e.g. "King David". -* :py:obj:`~nameparser.config.CONSTANTS.suffix_acronyms` - Pieces that come at the end of the name that may or may not have periods separating the letters, e.g. "m.d.". -* :py:obj:`~nameparser.config.CONSTANTS.suffix_not_acronyms` - Pieces that come at the end of the name that never have periods separating the letters, e.g. "Jr.". -* :py:obj:`~nameparser.config.CONSTANTS.conjunctions` - Connectors like "and" that join the preceding piece to the following piece. -* :py:obj:`~nameparser.config.CONSTANTS.prefixes` - Connectors like "del" and "bin" that join to the following piece but not the preceding, similar to titles but can appear anywhere in the name. -* :py:obj:`~nameparser.config.CONSTANTS.capitalization_exceptions` - Dictionary of pieces that do not capitalize the first letter, e.g. "Ph.D". -* :py:obj:`~nameparser.config.CONSTANTS.regexes` - Regular expressions used to find words, initials, nicknames, etc. +* :py:data:`~nameparser.config.titles.TITLES` - Pieces that come before the name. Includes all `first_name_titles`. Cannot include things that may be first names. +* :py:data:`~nameparser.config.FIRST_NAME_TITLES` - Titles that, when followed by a single name, that name is a first name, e.g. "King David". +* :py:data:`~nameparser.config.SUFFIX_ACRONYMS` - Pieces that come at the end of the name that may or may not have periods separating the letters, e.g. "m.d.". +* :py:data:`~nameparser.config.SUFFIX_NOT_ACRONYMS` - Pieces that come at the end of the name that never have periods separating the letters, e.g. "Jr.". +* :py:data:`~nameparser.config.conjunctions.CONJUNCTIONS` - Connectors like "and" that join the preceding piece to the following piece. +* :py:data:`~nameparser.config.prefixes.PREFIXES` - Connectors like "del" and "bin" that join to the following piece but not the preceding, similar to titles but can appear anywhere in the name. +* :py:data:`~nameparser.config.CAPITALIZATION_EXCEPTIONS` - Dictionary of pieces that do not capitalize the first letter, e.g. "Ph.D". +* :py:data:`~nameparser.config.regexes.REGEXES` - Regular expressions used to find words, initials, nicknames, etc. Each set of constants comes with :py:func:`~nameparser.config.SetManager.add` and :py:func:`~nameparser.config.SetManager.remove` methods for tuning the constants for your project. These methods automatically lower case and diff --git a/nameparser/config/prefixes.py b/nameparser/config/prefixes.py index fbcc3f2..542ea03 100644 --- a/nameparser/config/prefixes.py +++ b/nameparser/config/prefixes.py @@ -2,14 +2,14 @@ from __future__ import unicode_literals #: Name pieces that appear before a last name. Prefixes join to the piece -# that follows them to make one new piece. They can be chained together, e.g -# "von der" and "de la". Because they only appear in middle or last names, -# they also signifiy that all following name pieces should be in the same name -# part, for example, "von" will be joined to all following pieces that are not -# prefixes or suffixes, allowing recognition of double last names when they -# appear after a prefixes. So in "pennie von bergen wessels MD", "von" will -# join with all following name pieces until the suffix "MD", resulting in the -# correct parsing of the last name "von bergen wessels". +#: that follows them to make one new piece. They can be chained together, e.g +#: "von der" and "de la". Because they only appear in middle or last names, +#: they also signifiy that all following name pieces should be in the same name +#: part, for example, "von" will be joined to all following pieces that are not +#: prefixes or suffixes, allowing recognition of double last names when they +#: appear after a prefixes. So in "pennie von bergen wessels MD", "von" will +#: join with all following name pieces until the suffix "MD", resulting in the +#: correct parsing of the last name "von bergen wessels". PREFIXES = set([ 'abu', 'bin', diff --git a/nameparser/parser.py b/nameparser/parser.py index 1b20018..d8e5498 100644 --- a/nameparser/parser.py +++ b/nameparser/parser.py @@ -296,7 +296,7 @@ def is_conjunction(self, piece): def is_prefix(self, piece): """ Lowercase and no periods version of piece is in the - `~nameparser.config.titles.PREFIXES` set. + :py:data:`~nameparser.config.prefixes.PREFIXES` set. """ return lc(piece) in self.C.prefixes From 8c55eb9f0a58c5bfa371bdd565251acd44206e11 Mon Sep 17 00:00:00 2001 From: Derek Gulbranson Date: Fri, 31 Aug 2018 16:51:41 -0700 Subject: [PATCH 02/99] Fix overzealous regex for "Ph. D." (#43) --- docs/release_log.rst | 2 ++ nameparser/config/regexes.py | 2 +- nameparser/parser.py | 2 +- tests.py | 6 ++++++ 4 files changed, 10 insertions(+), 2 deletions(-) diff --git a/docs/release_log.rst b/docs/release_log.rst index 2878ccf..95867e3 100644 --- a/docs/release_log.rst +++ b/docs/release_log.rst @@ -1,5 +1,7 @@ Release Log =========== +* 1.0.1 - August 30, 2018 + - Fix overzealous regex for "Ph. D." (#43) * 1.0.0 - August 30, 2018 - Fix support for nicknames in single quotes (#74) - Change prefix handling to support prefixes on first names (#60) diff --git a/nameparser/config/regexes.py b/nameparser/config/regexes.py index b5c49c3..beac95f 100644 --- a/nameparser/config/regexes.py +++ b/nameparser/config/regexes.py @@ -30,7 +30,7 @@ ("no_vowels",re.compile(r'^[^aeyiuo]+$', re.I | re.U)), ("period_not_at_end",re.compile(r'.*\..+$', re.I | re.U)), ("emoji",re_emoji), - ("phd", re.compile(r'ph\.?\s+d\.?', re.I | re.U)), + ("phd", re.compile(r'\s(ph\.?\s+d\.?)', re.I | re.U)), ]) """ All regular expressions used by the parser are precompiled and stored in the config. diff --git a/nameparser/parser.py b/nameparser/parser.py index d8e5498..34282de 100644 --- a/nameparser/parser.py +++ b/nameparser/parser.py @@ -390,7 +390,7 @@ def fix_phd(self): _re = self.C.regexes.phd match = _re.search(self._full_name) if match: - self.suffix_list.append(match.group(0)) + self.suffix_list.append(match.group(1)) self._full_name = _re.sub('', self._full_name) def parse_nicknames(self): diff --git a/tests.py b/tests.py index fb15674..87dcb94 100644 --- a/tests.py +++ b/tests.py @@ -1614,6 +1614,12 @@ def test_phd_with_erroneous_space(self): self.m(hn.last, "Smith", hn) self.m(hn.suffix, "Ph. D.", hn) + def test_phd_conflict(self): + hn = HumanName("Adolph D") + self.m(hn.first, "Adolph", hn) + self.m(hn.last, "D", hn) + + # http://en.wikipedia.org/wiki/Ma_(surname) def test_potential_suffix_that_is_also_last_name(self): hn = HumanName("Jack Ma") From 9a3d187c7ef5cc9c590729ebcd6b3bac3f1da298 Mon Sep 17 00:00:00 2001 From: Derek Gulbranson Date: Fri, 31 Aug 2018 17:04:03 -0700 Subject: [PATCH 03/99] Add `surnames` attribute as aggregate of middle and last names --- docs/release_log.rst | 1 + nameparser/parser.py | 16 +++++++++++++++- tests.py | 9 ++++++++- 3 files changed, 24 insertions(+), 2 deletions(-) diff --git a/docs/release_log.rst b/docs/release_log.rst index 95867e3..70e2529 100644 --- a/docs/release_log.rst +++ b/docs/release_log.rst @@ -2,6 +2,7 @@ Release Log =========== * 1.0.1 - August 30, 2018 - Fix overzealous regex for "Ph. D." (#43) + - Add `surnames` attribute as aggregate of middle and last names * 1.0.0 - August 30, 2018 - Fix support for nicknames in single quotes (#74) - Change prefix handling to support prefixes on first names (#60) diff --git a/nameparser/parser.py b/nameparser/parser.py index 34282de..e13bdfb 100644 --- a/nameparser/parser.py +++ b/nameparser/parser.py @@ -243,7 +243,21 @@ def nickname(self): parenthesis (``()``) """ return " ".join(self.nickname_list) or self.C.empty_attribute_default - + + @property + def surnames_list(self): + """ + List of middle names followed by last name. + """ + return self.middle_list + self.last_list + + @property + def surnames(self): + """ + A string of all middle names followed by the last name. + """ + return " ".join(self.surnames_list) or self.C.empty_attribute_default + ### setter methods def _set_list(self, attr, value): diff --git a/tests.py b/tests.py index 87dcb94..11bb3e8 100644 --- a/tests.py +++ b/tests.py @@ -188,6 +188,14 @@ def test_blank_name(self): self.m(hn.first, "", hn) self.m(hn.last, "", hn) + def test_surnames_list_attribute(self): + hn = HumanName("John Edgar Casey Williams III") + self.m(hn.surnames_list, ["Edgar", "Casey", "Williams"], hn) + + def test_surnames_attribute(self): + hn = HumanName("John Edgar Casey Williams III") + self.m(hn.surnames, "Edgar Casey Williams", hn) + class FirstNameHandlingTests(HumanNameTestBase): def test_first_name(self): @@ -1438,7 +1446,6 @@ def test_parenthesis_and_quotes_together(self): self.m(hn.nickname, "Jen Duff", hn) - class PrefixesTestCase(HumanNameTestBase): def test_prefix(self): From 438bc7d5b74de82388745a99ead56264b30c120c Mon Sep 17 00:00:00 2001 From: Derek Gulbranson Date: Fri, 31 Aug 2018 17:12:47 -0700 Subject: [PATCH 04/99] v1.0.1, document surnames attribute --- docs/usage.rst | 2 ++ nameparser/__init__.py | 2 +- tests.py | 6 ++++++ 3 files changed, 9 insertions(+), 1 deletion(-) diff --git a/docs/usage.rst b/docs/usage.rst index dd313d2..45f67a4 100644 --- a/docs/usage.rst +++ b/docs/usage.rst @@ -23,6 +23,8 @@ The examples use Python 3, but Python 2.6+ is supported. 'de la Vega' >>> name.suffix 'III' + >>> name.surnames + 'Q. Xavier de la Vega' >>> name.full_name = "Juan Q. Xavier Velasquez y Garcia, Jr." >>> name 0) hn.C.titles.add('te') + self.assertEqual(start_len + 1, len(hn.C.titles)) hn.parse_full_name() self.m(hn.title, "Te", hn) self.m(hn.first, "Awanui-a-Rangi", hn) @@ -1268,7 +1271,10 @@ def test_add_title(self): def test_remove_title(self): hn = HumanName("Hon Solo", constants=None) + start_len = len(hn.C.titles) + self.assert_(start_len > 0) hn.C.titles.remove('hon') + self.assertEqual(start_len - 1, len(hn.C.titles)) hn.parse_full_name() self.m(hn.first, "Hon", hn) self.m(hn.last, "Solo", hn) From efe255834e2bff86c160b7c1d063ab24fa78a62e Mon Sep 17 00:00:00 2001 From: Derek Gulbranson Date: Fri, 31 Aug 2018 17:18:15 -0700 Subject: [PATCH 05/99] update surname docs --- nameparser/parser.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/nameparser/parser.py b/nameparser/parser.py index e13bdfb..09726d4 100644 --- a/nameparser/parser.py +++ b/nameparser/parser.py @@ -43,7 +43,8 @@ class HumanName(object): * :py:attr:`last` * :py:attr:`suffix` * :py:attr:`nickname` - + * :py:attr:`surnames` + :param str full_name: The name string to be parsed. :param constants constants: a :py:class:`~nameparser.config.Constants` instance. Pass ``None`` for From 9e3e06d229347b03e79c3d26e848f03bcc15dc8d Mon Sep 17 00:00:00 2001 From: Derek Gulbranson Date: Fri, 14 Sep 2018 12:37:45 -0700 Subject: [PATCH 06/99] comments cleanup --- nameparser/parser.py | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/nameparser/parser.py b/nameparser/parser.py index 09726d4..e390438 100644 --- a/nameparser/parser.py +++ b/nameparser/parser.py @@ -470,7 +470,6 @@ def parse_full_name(self): self.last_list = [] self.suffix_list = [] self.nickname_list = [] - self.prefix_joins = [] self.unparsable = True @@ -765,7 +764,7 @@ def join_on_conjunctions(self, pieces, additional_parts_count=0): conj_index[j] = val - rm_count - # join prefixes to following lastnames: ['de la Vega'], ['van Buren III'] + # join prefixes to following lastnames: ['de la Vega'], ['van Buren'] prefixes = list(filter(self.is_prefix, pieces)) if prefixes: for prefix in prefixes: @@ -781,11 +780,7 @@ def join_on_conjunctions(self, pieces, additional_parts_count=0): new_piece = '' - # join everything after the prefix until the next non prefix - # store joined pieces in prefix_joins. When a prefix occurs in a last name, - # I think it means the rest of the name is part of the last name, so prefix_joins - # lets us do that in the parser flow. - # for prefix in prefixes: + # join everything after the prefix until the next prefix or suffix try: next_prefix = next(iter(filter(self.is_prefix, pieces[i + 1:]))) From 878c3f914251ca3d7a5685498befb5f8b441ad42 Mon Sep 17 00:00:00 2001 From: Derek Gulbranson Date: Sat, 22 Sep 2018 13:37:34 -0700 Subject: [PATCH 07/99] fix RST error, update badges --- README.rst | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/README.rst b/README.rst index c5419fe..b347593 100644 --- a/README.rst +++ b/README.rst @@ -1,10 +1,7 @@ Name Parser =========== -.. image:: https://travis-ci.org/derek73/python-nameparser.svg?branch=master - :target: https://travis-ci.org/derek73/python-nameparser -.. image:: https://badge.fury.io/py/nameparser.svg - :target: http://badge.fury.io/py/nameparser +|Build Status| |PyPI| |PyPI version| |Documentation| A simple Python (3.2+ & 2.6+) module for parsing human names into their individual components. @@ -15,6 +12,7 @@ individual components. * hn.last * hn.suffix * hn.nickname +* hn.surnames *(middle + last)* Supported Name Structures ~~~~~~~~~~~~~~~~~~~~~~~~~ @@ -54,7 +52,7 @@ install with pip using the command below. If you need to handle lists of names, check out `namesparser `_, a - compliment to this module that handles multiple names in a string. +compliment to this module that handles multiple names in a string. Quick Start Example @@ -135,4 +133,14 @@ https://github.com/derek73/python-nameparser .. _CONTRIBUTING.md: https://github.com/derek73/python-nameparser/tree/master/CONTRIBUTING.md .. _Start a New Issue: https://github.com/derek73/python-nameparser/issues -.. _click here to propose changes to the titles: https://github.com/derek73/python-nameparser/edit/master/nameparser/config/titles.py \ No newline at end of file +.. _click here to propose changes to the titles: https://github.com/derek73/python-nameparser/edit/master/nameparser/config/titles.py + + +.. |Build Status| image:: https://travis-ci.org/derek73/python-nameparser.svg?branch=master + :target: https://travis-ci.org/derek73/python-nameparser +.. |PyPI| image:: https://img.shields.io/pypi/v/nameparser.svg + :target: https://pypi.org/project/nameparser/ +.. |Documentation| image:: https://readthedocs.org/projects/nameparser/badge/?version=latest + :target: http://nameparser.readthedocs.io/en/latest/?badge=latest +.. |PyPI version| image:: https://img.shields.io/pypi/pyversions/nameparser.svg + :target: https://pypi.org/project/nameparser/ From 0b1001852fcc176774d2d68a2ade062b05d8dfc3 Mon Sep 17 00:00:00 2001 From: Derek Gulbranson Date: Sat, 22 Sep 2018 13:39:09 -0700 Subject: [PATCH 08/99] remove minor python versions since we always support all of them --- setup.py | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/setup.py b/setup.py index 4b71aca..ba0cc5a 100755 --- a/setup.py +++ b/setup.py @@ -26,14 +26,8 @@ def read(fname): 'Operating System :: OS Independent', "License :: OSI Approved :: GNU Library or Lesser General Public License (LGPL)", 'Programming Language :: Python', - 'Programming Language :: Python :: 2.6', - 'Programming Language :: Python :: 2.7', + 'Programming Language :: Python :: 2', 'Programming Language :: Python :: 3', - 'Programming Language :: Python :: 3.2', - 'Programming Language :: Python :: 3.3', - 'Programming Language :: Python :: 3.4', - 'Programming Language :: Python :: 3.5', - 'Programming Language :: Python :: 3.6', 'Development Status :: 5 - Production/Stable', 'Natural Language :: English', "Topic :: Software Development :: Libraries :: Python Modules", From e2c90432f1affc203b08013a3786f3e6285db3a3 Mon Sep 17 00:00:00 2001 From: Derek Gulbranson Date: Fri, 26 Oct 2018 14:43:44 -0700 Subject: [PATCH 09/99] v1.0.2, narrow fix to handle only nickname and last name (#78) --- docs/release_log.rst | 2 ++ nameparser/__init__.py | 2 +- nameparser/parser.py | 3 ++ tests.py | 66 +++++++++++++++++++++++++++++++++++++----- 4 files changed, 65 insertions(+), 8 deletions(-) diff --git a/docs/release_log.rst b/docs/release_log.rst index 70e2529..7364ad0 100644 --- a/docs/release_log.rst +++ b/docs/release_log.rst @@ -1,5 +1,7 @@ Release Log =========== +* 1.0.2 - Oct 26, 2018 + - Fix handling of only nickname and last name (#78) * 1.0.1 - August 30, 2018 - Fix overzealous regex for "Ph. D." (#43) - Add `surnames` attribute as aggregate of middle and last names diff --git a/nameparser/__init__.py b/nameparser/__init__.py index febcb0b..0e8663a 100644 --- a/nameparser/__init__.py +++ b/nameparser/__init__.py @@ -1,4 +1,4 @@ -VERSION = (1, 0, 1) +VERSION = (1, 0, 2) __version__ = '.'.join(map(str, VERSION)) __author__ = "Derek Gulbranson" __author_email__ = 'derek73@gmail.com' diff --git a/nameparser/parser.py b/nameparser/parser.py index e390438..bf438f7 100644 --- a/nameparser/parser.py +++ b/nameparser/parser.py @@ -503,6 +503,9 @@ def parse_full_name(self): self.title_list.append(piece) continue if not self.first: + if p_len == 1 and self.nickname: + self.last_list.append(piece) + continue self.first_list.append(piece) continue if self.are_suffixes(pieces[i+1:]) or \ diff --git a/tests.py b/tests.py index 290e6af..0ee7d75 100644 --- a/tests.py +++ b/tests.py @@ -46,7 +46,7 @@ def m(self, actual, expected, hn): self.assertEqual(actual, expected, "'%s' != '%s' for '%s'\n%r" % ( actual, expected, - hn.full_name, + hn.original, hn )) except UnicodeDecodeError: @@ -1358,7 +1358,7 @@ def test_add_constant_with_explicit_encoding(self): self.assertIn('béck', c.titles) -class HumanNameNicknameTestCase(HumanNameTestBase): +class NicknameTestCase(HumanNameTestBase): # https://code.google.com/p/python-nameparser/issues/detail?id=33 def test_nickname_in_parenthesis(self): hn = HumanName("Benjamin (Ben) Franklin") @@ -1445,12 +1445,64 @@ def test_duplicate_parenthesis_are_removed_from_name(self): self.m(hn.last, "Jones", hn) self.m(hn.suffix, "Jr.", hn) - def test_parenthesis_and_quotes_together(self): - hn = HumanName("Jennifer 'Jen' Jones (Duff)") - self.m(hn.first, "Jennifer", hn) - self.m(hn.last, "Jones", hn) - self.m(hn.nickname, "Jen Duff", hn) + def test_nickname_and_last_name(self): + hn = HumanName('"Rick" Edmonds') + self.m(hn.first, "", hn) + self.m(hn.last, "Edmonds", hn) + self.m(hn.nickname, "Rick", hn) + @unittest.expectedFailure + def test_nickname_and_last_name_with_title(self): + hn = HumanName('Senator "Rick" Edmonds') + self.m(hn.title, "Senator", hn) + self.m(hn.first, "", hn) + self.m(hn.last, "Edmonds", hn) + self.m(hn.nickname, "Rick", hn) + + + +# class MaidenNameTestCase(HumanNameTestBase): +# +# def test_parenthesis_and_quotes_together(self): +# hn = HumanName("Jennifer 'Jen' Jones (Duff)") +# self.m(hn.first, "Jennifer", hn) +# self.m(hn.last, "Jones", hn) +# self.m(hn.nickname, "Jen", hn) +# self.m(hn.maiden, "Duff", hn) +# +# def test_maiden_name_with_nee(self): +# # https://en.wiktionary.org/wiki/née +# hn = HumanName("Mary Toogood nee Johnson") +# self.m(hn.first, "Mary", hn) +# self.m(hn.last, "Toogood", hn) +# self.m(hn.maiden, "Johnson", hn) +# +# def test_maiden_name_with_accented_nee(self): +# # https://en.wiktionary.org/wiki/née +# hn = HumanName("Mary Toogood née Johnson") +# self.m(hn.first, "Mary", hn) +# self.m(hn.last, "Toogood", hn) +# self.m(hn.maiden, "Johnson", hn) +# +# def test_maiden_name_with_nee_and_comma(self): +# # https://en.wiktionary.org/wiki/née +# hn = HumanName("Mary Toogood, née Johnson") +# self.m(hn.first, "Mary", hn) +# self.m(hn.last, "Toogood", hn) +# self.m(hn.maiden, "Johnson", hn) +# +# def test_maiden_name_with_nee_with_parenthesis(self): +# hn = HumanName("Mary Toogood (nee Johnson)") +# self.m(hn.first, "Mary", hn) +# self.m(hn.last, "Toogood", hn) +# self.m(hn.maiden, "Johnson", hn) +# +# def test_maiden_name_with_parenthesis(self): +# hn = HumanName("Mary Toogood (Johnson)") +# self.m(hn.first, "Mary", hn) +# self.m(hn.last, "Toogood", hn) +# self.m(hn.maiden, "Johnson", hn) +# class PrefixesTestCase(HumanNameTestBase): From 285057e6900782aca26ae8a9e4895d5437a7e322 Mon Sep 17 00:00:00 2001 From: Derek Gulbranson Date: Fri, 26 Oct 2018 14:48:32 -0700 Subject: [PATCH 10/99] add python 3.7 to travis --- .travis.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.travis.yml b/.travis.yml index dc37c42..4da19f2 100644 --- a/.travis.yml +++ b/.travis.yml @@ -7,6 +7,7 @@ python: - "3.4" - "3.5" - "3.6" + - "3.7" # command to install dependencies install: - if [[ $TRAVIS_PYTHON_VERSION == '2.6' ]]; then pip install unittest2; fi From b9b29cbbbfc138d4958b63e00a047610f30ff6de Mon Sep 17 00:00:00 2001 From: Derek Gulbranson Date: Fri, 26 Oct 2018 14:50:59 -0700 Subject: [PATCH 11/99] 3.7 still not available on ubuntu --- .travis.yml | 1 - 1 file changed, 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 4da19f2..dc37c42 100644 --- a/.travis.yml +++ b/.travis.yml @@ -7,7 +7,6 @@ python: - "3.4" - "3.5" - "3.6" - - "3.7" # command to install dependencies install: - if [[ $TRAVIS_PYTHON_VERSION == '2.6' ]]; then pip install unittest2; fi From b066aa7e9f1b79426b3e75467a4cfdb9ae7f3bd5 Mon Sep 17 00:00:00 2001 From: TyVik Date: Thu, 18 Apr 2019 21:07:41 +0300 Subject: [PATCH 12/99] Implemented support for escaping log entry arguments. --- nameparser/parser.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/nameparser/parser.py b/nameparser/parser.py index bf438f7..8c10c8f 100644 --- a/nameparser/parser.py +++ b/nameparser/parser.py @@ -480,8 +480,8 @@ def parse_full_name(self): # break up full_name by commas parts = [x.strip() for x in self._full_name.split(",")] - log.debug("full_name: {0}".format(self._full_name)) - log.debug("parts: {0}".format(parts)) + log.debug("full_name: %s", self._full_name) + log.debug("parts: %s", parts) if len(parts) == 1: @@ -538,7 +538,7 @@ def parse_full_name(self): self.suffix_list += parts[1:] pieces = self.parse_pieces(parts[0].split(' ')) - log.debug("pieces: {0}".format(u(pieces))) + log.debug("pieces: %s", u(pieces)) for i, piece in enumerate(pieces): try: nxt = pieces[i + 1] @@ -568,7 +568,7 @@ def parse_full_name(self): # parts[0], parts[1], parts[2:...] pieces = self.parse_pieces(parts[1].split(' '), 1) - log.debug("pieces: {0}".format(u(pieces))) + log.debug("pieces: %s", u(pieces)) # lastname part may have suffixes in it lastname_pieces = self.parse_pieces(parts[0].split(' '), 1) @@ -605,7 +605,7 @@ def parse_full_name(self): pass if len(self) < 0: - log.info("Unparsable: \"{}\" ".format(self.original)) + log.info("Unparsable: \"%s\" ", self.original) else: self.unparsable = False self.post_process() @@ -806,7 +806,7 @@ def join_on_conjunctions(self, pieces, additional_parts_count=0): new_piece = ' '.join(pieces[i:]) pieces = pieces[:i] + [new_piece] - log.debug("pieces: {0}".format(pieces)) + log.debug("pieces: %s", pieces) return pieces From b948e2c647ff114c29058732948f745b9beed8a4 Mon Sep 17 00:00:00 2001 From: Derek Gulbranson Date: Thu, 18 Apr 2019 21:54:05 -0700 Subject: [PATCH 13/99] fix #82, test for sys.stdin --- nameparser/config/__init__.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/nameparser/config/__init__.py b/nameparser/config/__init__.py index 3b11e88..b9d6e1e 100644 --- a/nameparser/config/__init__.py +++ b/nameparser/config/__init__.py @@ -92,7 +92,10 @@ def add_with_encoding(self, s, encoding=None): explicit `encoding` parameter to specify the encoding of binary strings that are not DEFAULT_ENCODING (UTF-8). """ - encoding = encoding or sys.stdin.encoding or DEFAULT_ENCODING + stdin_encoding = None + if sys.stdin: + stdin_encoding = sys.stdin.encoding + encoding = encoding or stdin_encoding or DEFAULT_ENCODING if type(s) == binary_type: s = s.decode(encoding) self.elements.add(lc(s)) From 9b8c30b4c95e0830ff78bcba1d1a171aa417ccb1 Mon Sep 17 00:00:00 2001 From: Derek Gulbranson Date: Thu, 18 Apr 2019 21:56:39 -0700 Subject: [PATCH 14/99] v1.0.3 --- docs/release_log.rst | 3 +++ nameparser/__init__.py | 2 +- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/docs/release_log.rst b/docs/release_log.rst index 7364ad0..59e7cbe 100644 --- a/docs/release_log.rst +++ b/docs/release_log.rst @@ -1,5 +1,8 @@ Release Log =========== +* 1.0.3 - April 18, 2018 + - fix sys.stdin usage when stdin doesn't exist (#82) + - support for escaping log entry arguments (#84) * 1.0.2 - Oct 26, 2018 - Fix handling of only nickname and last name (#78) * 1.0.1 - August 30, 2018 diff --git a/nameparser/__init__.py b/nameparser/__init__.py index 0e8663a..bdcad4c 100644 --- a/nameparser/__init__.py +++ b/nameparser/__init__.py @@ -1,4 +1,4 @@ -VERSION = (1, 0, 2) +VERSION = (1, 0, 3) __version__ = '.'.join(map(str, VERSION)) __author__ = "Derek Gulbranson" __author_email__ = 'derek73@gmail.com' From 8d6ef523d98b736b1229dda91bcd95e6c499ca7b Mon Sep 17 00:00:00 2001 From: Matt VanEseltine Date: Thu, 20 Jun 2019 16:26:16 -0400 Subject: [PATCH 15/99] Prevent false nicknames due to multiple quotes Certain Anglicized names such as those from some Hawaiian, Samoan, and Kenyan traditions, include multiple single quotation marks. This adjusts the quoted_word regex to only capture single quote marks that are not inside words. Without this fix, false nicknames are extracted from inside names like Ng'ang'a and Kawai'ae'a. Tests are included to cover; existing Benjamin 'Ben' Franklin test assures that the typical nickname case is unchanged. --- nameparser/config/regexes.py | 2 +- nameparser/parser.py | 6 +++--- tests.py | 28 ++++++++++++++++++++++++++++ 3 files changed, 32 insertions(+), 4 deletions(-) diff --git a/nameparser/config/regexes.py b/nameparser/config/regexes.py index beac95f..bd4b320 100644 --- a/nameparser/config/regexes.py +++ b/nameparser/config/regexes.py @@ -23,7 +23,7 @@ ("word", re.compile(r"(\w|\.)+", re.U)), ("mac", re.compile(r'^(ma?c)(\w{2,})', re.I | re.U)), ("initial", re.compile(r'^(\w\.|[A-Z])?$', re.U)), - ("quoted_word", re.compile(r'\'([^\s]*?)\'', re.U)), + ("quoted_word", re.compile(r'(? Date: Wed, 26 Jun 2019 18:24:04 -0700 Subject: [PATCH 16/99] get full_name should return current string output, not original name --- nameparser/parser.py | 4 ++-- tests.py | 6 ++++++ 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/nameparser/parser.py b/nameparser/parser.py index 5aa754d..a2dfa8a 100644 --- a/nameparser/parser.py +++ b/nameparser/parser.py @@ -363,8 +363,8 @@ def is_an_initial(self, value): @property def full_name(self): - """The name string to be parsed.""" - return self._full_name + """The string output of the HumanName instance.""" + return self.__str__() @full_name.setter def full_name(self, value): diff --git a/tests.py b/tests.py index b764955..5e2ddab 100644 --- a/tests.py +++ b/tests.py @@ -111,6 +111,12 @@ def test_assignment_to_full_name(self): self.m(hn.last, "Velasquez y Garcia", hn) self.m(hn.suffix, "III", hn) + def test_get_full_name_attribute_references_internal_lists(self): + hn = HumanName("John Williams") + hn.first_list = ["Larry"] + self.m(hn.full_name, "Larry Williams", hn) + + def test_assignment_to_attribute(self): hn = HumanName("John A. Kenneth Doe, Jr.") hn.last = "de la Vega" From ce92f379e96a324178e259a378aab628cbc12a6e Mon Sep 17 00:00:00 2001 From: Derek Gulbranson Date: Wed, 26 Jun 2019 18:24:25 -0700 Subject: [PATCH 17/99] v1.0.4 --- docs/release_log.rst | 5 ++++- nameparser/__init__.py | 2 +- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/docs/release_log.rst b/docs/release_log.rst index 59e7cbe..0700af2 100644 --- a/docs/release_log.rst +++ b/docs/release_log.rst @@ -1,6 +1,9 @@ Release Log =========== -* 1.0.3 - April 18, 2018 +* 1.0.4 - June 26, 2019 + - Better nickname handling of multiple single quotes (#86) + - full_name attribute now returns formatted string output instead of original string (#87) +* 1.0.3 - April 18, 2019 - fix sys.stdin usage when stdin doesn't exist (#82) - support for escaping log entry arguments (#84) * 1.0.2 - Oct 26, 2018 diff --git a/nameparser/__init__.py b/nameparser/__init__.py index bdcad4c..650cf32 100644 --- a/nameparser/__init__.py +++ b/nameparser/__init__.py @@ -1,4 +1,4 @@ -VERSION = (1, 0, 3) +VERSION = (1, 0, 4) __version__ = '.'.join(map(str, VERSION)) __author__ = "Derek Gulbranson" __author_email__ = 'derek73@gmail.com' From afcf74f4f0a1e2692e47ec2a4288cda0879a07eb Mon Sep 17 00:00:00 2001 From: "James C. Palmer" Date: Sat, 20 Jul 2019 10:20:53 -0400 Subject: [PATCH 18/99] Remove deprecated test case aliases and add editorconfig This also fixes a few minor spelling and spacing errors and ignores Pipenv files --- .editorconfig | 20 ++++++++++++++++++++ .gitignore | 2 ++ nameparser/config/capitalization.py | 10 +++++----- nameparser/config/prefixes.py | 2 +- tests.py | 10 ++++------ 5 files changed, 32 insertions(+), 12 deletions(-) create mode 100644 .editorconfig diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..55170d0 --- /dev/null +++ b/.editorconfig @@ -0,0 +1,20 @@ +# http://editorconfig.org + +root = true + +[*] +charset = utf-8 +end_of_line = lf +insert_final_newline = true +trim_trailing_whitespace = true + +[*.{py,rst,ini}] +indent_style = space +indent_size = 4 + +[*.{html,json,yml}] +indent_style = space +indent_size = 2 + +[*.md] +trim_trailing_whitespace = false diff --git a/.gitignore b/.gitignore index c3fe42b..3874fab 100644 --- a/.gitignore +++ b/.gitignore @@ -10,6 +10,8 @@ build .coverage dist .idea +Pipfile +Pipfile.lock # docs docs/_* diff --git a/nameparser/config/capitalization.py b/nameparser/config/capitalization.py index 4aa3214..84dfbef 100644 --- a/nameparser/config/capitalization.py +++ b/nameparser/config/capitalization.py @@ -2,11 +2,11 @@ from __future__ import unicode_literals CAPITALIZATION_EXCEPTIONS = ( - ('ii' ,'II'), - ('iii','III'), - ('iv' ,'IV'), - ('md' ,'M.D.'), - ('phd','Ph.D.'), + ('ii', 'II'), + ('iii', 'III'), + ('iv', 'IV'), + ('md', 'M.D.'), + ('phd', 'Ph.D.'), ) """ Any pieces that are not capitalized by capitalizing the first letter. diff --git a/nameparser/config/prefixes.py b/nameparser/config/prefixes.py index 542ea03..2f5eb31 100644 --- a/nameparser/config/prefixes.py +++ b/nameparser/config/prefixes.py @@ -4,7 +4,7 @@ #: Name pieces that appear before a last name. Prefixes join to the piece #: that follows them to make one new piece. They can be chained together, e.g #: "von der" and "de la". Because they only appear in middle or last names, -#: they also signifiy that all following name pieces should be in the same name +#: they also signify that all following name pieces should be in the same name #: part, for example, "von" will be joined to all following pieces that are not #: prefixes or suffixes, allowing recognition of double last names when they #: appear after a prefixes. So in "pennie von bergen wessels MD", "von" will diff --git a/tests.py b/tests.py index 5e2ddab..c87ad03 100644 --- a/tests.py +++ b/tests.py @@ -40,7 +40,7 @@ class HumanNameTestBase(unittest.TestCase): def m(self, actual, expected, hn): - """assertEquals with a better message and awareness of hn.C.empty_attribute_default""" + """assertEqual with a better message and awareness of hn.C.empty_attribute_default""" expected = expected or hn.C.empty_attribute_default try: self.assertEqual(actual, expected, "'%s' != '%s' for '%s'\n%r" % ( @@ -50,7 +50,7 @@ def m(self, actual, expected, hn): hn )) except UnicodeDecodeError: - self.assertEquals(actual, expected) + self.assertEqual(actual, expected) class HumanNamePythonTests(HumanNameTestBase): @@ -62,8 +62,6 @@ def test_utf8(self): def test_string_output(self): hn = HumanName("de la Véña, Jüan") - print(hn) - print(repr(hn)) def test_escaped_utf8_bytes(self): hn = HumanName(b'B\xc3\xb6ck, Gerald') @@ -1267,7 +1265,7 @@ class ConstantsCustomization(HumanNameTestBase): def test_add_title(self): hn = HumanName("Te Awanui-a-Rangi Black", constants=None) start_len = len(hn.C.titles) - self.assert_(start_len > 0) + self.assertTrue(start_len > 0) hn.C.titles.add('te') self.assertEqual(start_len + 1, len(hn.C.titles)) hn.parse_full_name() @@ -1278,7 +1276,7 @@ def test_add_title(self): def test_remove_title(self): hn = HumanName("Hon Solo", constants=None) start_len = len(hn.C.titles) - self.assert_(start_len > 0) + self.assertTrue(start_len > 0) hn.C.titles.remove('hon') self.assertEqual(start_len - 1, len(hn.C.titles)) hn.parse_full_name() From f4bbf42bd7b43444e15063f0866a4c4ff30a1869 Mon Sep 17 00:00:00 2001 From: "James C. Palmer" Date: Sat, 20 Jul 2019 14:17:27 -0400 Subject: [PATCH 19/99] Added capitalization rules to Constants class These changes let users apply capitalization rules to all `HumanName` instances. --- docs/customize.rst | 2 ++ docs/usage.rst | 30 +++++++++++++++++++++++++++--- nameparser/config/__init__.py | 33 +++++++++++++++++++++++++++++++-- nameparser/parser.py | 27 +++++++++++++++++++++------ tests.py | 22 ++++++++++++++++++++++ 5 files changed, 103 insertions(+), 11 deletions(-) diff --git a/docs/customize.rst b/docs/customize.rst index 6097050..1e4f38d 100644 --- a/docs/customize.rst +++ b/docs/customize.rst @@ -57,6 +57,8 @@ Other editable attributes * :py:obj:`~nameparser.config.Constants.string_format` - controls output from `str()` * :py:obj:`~nameparser.config.Constants.empty_attribute_default` - value returned by empty attributes, defaults to empty string +* :py:obj:`~nameparser.config.Constants.capitalize_name` - If set, applies :py:meth:`~nameparser.parser.HumanName.capitalize` to :py:class:`~nameparser.parser.HumanName` instance. +* :py:obj:`~nameparser.config.Constants.force_mixed_case_capitalization` - If set, forces the capitalization of mixed case strings when :py:meth:`~nameparser.parser.HumanName.capitalize` is called. diff --git a/docs/usage.rst b/docs/usage.rst index 45f67a4..6a65c4e 100644 --- a/docs/usage.rst +++ b/docs/usage.rst @@ -72,9 +72,8 @@ Capitalization Support The HumanName class can try to guess the correct capitalization of name entered in all upper or lower case. By default, it will not adjust -the case of names entered in mixed case. To run capitalization on all names -pass the parameter `force=True`. - +the case of names entered in mixed case. To run capitalization on a +`HumanName` instance, pass the parameter `force=True`. Capitalize the name. @@ -94,6 +93,31 @@ pass the parameter `force=True`. >>> str(name) 'Shirley MacLaine' +To apply capitalization to all `HumanName` instances, set +:py:attr:`~nameparser.config.Constants.capitalize_name` to `True`. + +.. doctest:: capitalize_name + :options: +NORMALIZE_WHITESPACE + + >>> from nameparser.config import CONSTANTS + >>> CONSTANTS.capitalize_name = True + >>> name = HumanName("bob v. de la macdole-eisenhower phd") + >>> str(name) + 'Bob V. de la MacDole-Eisenhower Ph.D.' + +To force the capitalization of mixed case strings on all `HumanName` instances, +set :py:attr:`~nameparser.config.Constants.force_mixed_case_capitalization` to `True`. + +.. doctest:: force_mixed_case_capitalization + :options: +NORMALIZE_WHITESPACE + + >>> from nameparser.config import CONSTANTS + >>> CONSTANTS.force_mixed_case_capitalization = True + >>> name = HumanName('Shirley Maclaine') + >>> name.capitalize() + >>> str(name) + 'Shirley MacLaine' + Nickname Handling ------------------ diff --git a/nameparser/config/__init__.py b/nameparser/config/__init__.py index b9d6e1e..602afe8 100644 --- a/nameparser/config/__init__.py +++ b/nameparser/config/__init__.py @@ -179,8 +179,37 @@ class Constants(object): 'John' """ - - + capitalize_name = False + """ + If set, applies :py:meth:`~nameparser.parser.HumanName.capitalize` to + :py:class:`~nameparser.parser.HumanName` instance. + + .. doctest:: + + >>> from nameparser.config import CONSTANTS + >>> CONSTANTS.capitalize_name = True + >>> name = HumanName("bob v. de la macdole-eisenhower phd") + >>> str(name) + 'Bob V. de la MacDole-Eisenhower Ph.D.' + + """ + force_mixed_case_capitalization = False + """ + If set, forces the capitalization of mixed case strings when + :py:meth:`~nameparser.parser.HumanName.capitalize` is called. + + .. doctest:: + + >>> from nameparser.config import CONSTANTS + >>> CONSTANTS.force_mixed_case_capitalization = True + >>> name = HumanName('Shirley Maclaine') + >>> name.capitalize() + >>> str(name) + 'Shirley MacLaine' + + """ + + def __init__(self, prefixes=PREFIXES, suffix_acronyms=SUFFIX_ACRONYMS, diff --git a/nameparser/parser.py b/nameparser/parser.py index a2dfa8a..e90c99b 100644 --- a/nameparser/parser.py +++ b/nameparser/parser.py @@ -387,7 +387,7 @@ def pre_process(self): This method happens at the beginning of the :py:func:`parse_full_name` before any other processing of the string aside from unicode normalization, so it's a good place to do any custom handling in a - subclass. Runs :py:func:`parse_nicknames` and py:func:`squash_emoji`. + subclass. Runs :py:func:`parse_nicknames` and :py:func:`squash_emoji`. """ self.fix_phd() @@ -397,9 +397,11 @@ def pre_process(self): def post_process(self): """ This happens at the end of the :py:func:`parse_full_name` after - all other processing has taken place. Runs :py:func:`handle_firstnames`. + all other processing has taken place. Runs :py:func:`handle_firstnames` + and :py:func:`handle_capitalization`. """ self.handle_firstnames() + self.handle_capitalization() def fix_phd(self): _re = self.C.regexes.phd @@ -675,9 +677,9 @@ def join_on_conjunctions(self, pieces, additional_parts_count=0): :param list pieces: name pieces strings after split on spaces :param int additional_parts_count: :return: new list with piece next to conjunctions merged into one piece - with spaces in it. + with spaces in it. :rtype: list - + """ length = len(pieces) + additional_parts_count # don't join on conjunctions if there's only 2 parts @@ -833,14 +835,16 @@ def cap_piece(self, piece, attribute): replacement = lambda m: self.cap_word(m.group(0), attribute) return self.C.regexes.word.sub(replacement, piece) - def capitalize(self, force=False): + def capitalize(self, force=None): """ The HumanName class can try to guess the correct capitalization of name entered in all upper or lower case. By default, it will not adjust the case of names entered in mixed case. To run capitalization on all names pass the parameter `force=True`. - :param bool force: force capitalization of strings that include mixed case + :param bool force: Forces capitalization of mixed case strings. This + parameter overrides rules set within + :py:class:`~nameparser.config.CONSTANTS`. **Usage** @@ -861,6 +865,9 @@ def capitalize(self, force=False): """ name = u(self) + force = self.C.force_mixed_case_capitalization \ + if force is None else force + if not force and not (name == name.upper() or name == name.lower()): return self.title_list = self.cap_piece(self.title , 'title').split(' ') @@ -868,3 +875,11 @@ def capitalize(self, force=False): self.middle_list = self.cap_piece(self.middle, 'middle').split(' ') self.last_list = self.cap_piece(self.last , 'last').split(' ') self.suffix_list = self.cap_piece(self.suffix, 'suffix').split(', ') + + def handle_capitalization(self): + """ + Handles capitalization configurations set within + :py:class:`~nameparser.config.CONSTANTS`. + """ + if self.C.capitalize_name: + self.capitalize() diff --git a/tests.py b/tests.py index c87ad03..0e948c7 100644 --- a/tests.py +++ b/tests.py @@ -2088,6 +2088,28 @@ def test_formatting_constants_attribute(self): self.assertEqual(u(hn), "TEST2") CONSTANTS.string_format = _orig + def test_capitalize_name_constants_attribute(self): + from nameparser.config import CONSTANTS + CONSTANTS.capitalize_name = True + hn = HumanName("bob v. de la macdole-eisenhower phd") + self.assertEqual(str(hn), "Bob V. de la MacDole-Eisenhower Ph.D.") + CONSTANTS.capitalize_name = False + + def test_force_mixed_case_capitalization_constants_attribute(self): + from nameparser.config import CONSTANTS + CONSTANTS.force_mixed_case_capitalization = True + hn = HumanName('Shirley Maclaine') + hn.capitalize() + self.assertEqual(str(hn), "Shirley MacLaine") + CONSTANTS.force_mixed_case_capitalization = False + + def test_capitalize_name_and_force_mixed_case_capitalization_constants_attributes(self): + from nameparser.config import CONSTANTS + CONSTANTS.capitalize_name = True + CONSTANTS.force_mixed_case_capitalization = True + hn = HumanName('Shirley Maclaine') + self.assertEqual(str(hn), "Shirley MacLaine") + def test_quote_nickname_formating(self): hn = HumanName("Rev John A. Kenneth Doe III (Kenny)") hn.string_format = "{title} {first} {middle} {last} {suffix} '{nickname}'" From ca408b8e1899f1cbe3ad8c5bc30fb037cc199be1 Mon Sep 17 00:00:00 2001 From: Chris Erickson Date: Fri, 2 Aug 2019 14:08:33 -0500 Subject: [PATCH 20/99] Dep warning with importing from collections --- nameparser/config/__init__.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/nameparser/config/__init__.py b/nameparser/config/__init__.py index b9d6e1e..a6fb152 100644 --- a/nameparser/config/__init__.py +++ b/nameparser/config/__init__.py @@ -29,7 +29,7 @@ unexpected results. See `Customizing the Parser `_. """ from __future__ import unicode_literals -import collections +from collections.abc import Set import sys from nameparser.util import binary_type @@ -45,7 +45,7 @@ DEFAULT_ENCODING = 'UTF-8' -class SetManager(collections.Set): +class SetManager(Set): ''' Easily add and remove config variables per module or instance. Subclass of ``collections.Set``. From c60ba11d2526fa39ba2cd43ef1b221c7d4f8e2e0 Mon Sep 17 00:00:00 2001 From: Amrish Parmar Date: Wed, 18 Sep 2019 11:01:58 +0100 Subject: [PATCH 21/99] Fix deprecated use of `collections` module In Python 3.8 imports of abstract base classes directly from the `collections` module will stop working entirely. This change maintains existing behaviour whilst providing backwards compatibility. --- nameparser/config/__init__.py | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/nameparser/config/__init__.py b/nameparser/config/__init__.py index b9d6e1e..0b31c4e 100644 --- a/nameparser/config/__init__.py +++ b/nameparser/config/__init__.py @@ -29,8 +29,13 @@ unexpected results. See `Customizing the Parser `_. """ from __future__ import unicode_literals -import collections + import sys +try: + # Python 3.3+ + from collections.abc import Set +except ImportError: + from collections import Set from nameparser.util import binary_type from nameparser.util import lc @@ -45,10 +50,10 @@ DEFAULT_ENCODING = 'UTF-8' -class SetManager(collections.Set): +class SetManager(Set): ''' Easily add and remove config variables per module or instance. Subclass of - ``collections.Set``. + ``collections.abc.Set``. Only special functionality beyond that provided by set() is to normalize constants for comparison (lower case, no periods) From e83ee3183296ff5671ad2034a8dbeed6a489a3be Mon Sep 17 00:00:00 2001 From: andrew Date: Mon, 11 Nov 2019 19:08:06 -0800 Subject: [PATCH 22/99] run the parsing of pieces for post-comma parts prior to checking if all post-comma elements are suffixes. this ensures that suffixes are properly captured in the constants prior to the all-suffix check --- nameparser/parser.py | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/nameparser/parser.py b/nameparser/parser.py index a2dfa8a..5a64af3 100644 --- a/nameparser/parser.py +++ b/nameparser/parser.py @@ -528,6 +528,9 @@ def parse_full_name(self): # in the first part. (Suffixes will never appear after last names # only, and allows potential first names to be in suffixes, e.g. # "Johnson, Bart" + + post_comma_pieces = self.parse_pieces(parts[1].split(' '), 1) + if self.are_suffixes(parts[1].split(' ')) \ and len(parts[0].split(' ')) > 1: @@ -566,9 +569,8 @@ def parse_full_name(self): # lastname comma: # last [suffix], title first middles[,] suffix [,suffix] # parts[0], parts[1], parts[2:...] - pieces = self.parse_pieces(parts[1].split(' '), 1) - log.debug("pieces: %s", u(pieces)) + log.debug("post-comma pieces: %s", u(post_comma_pieces)) # lastname part may have suffixes in it lastname_pieces = self.parse_pieces(parts[0].split(' '), 1) @@ -580,14 +582,14 @@ def parse_full_name(self): else: self.last_list.append(piece) - for i, piece in enumerate(pieces): + for i, piece in enumerate(post_comma_pieces): try: - nxt = pieces[i + 1] + nxt = post_comma_pieces[i + 1] except IndexError: nxt = None if self.is_title(piece) \ - and (nxt or len(pieces) == 1) \ + and (nxt or len(post_comma_pieces) == 1) \ and not self.first: self.title_list.append(piece) continue From 2fb91dcb4adca54cd5dfd333889e15d13d13969c Mon Sep 17 00:00:00 2001 From: Derek Gulbranson Date: Wed, 11 Dec 2019 19:03:32 -0800 Subject: [PATCH 23/99] remove deprecated python versions no longer supported by travis --- .travis.yml | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/.travis.yml b/.travis.yml index dc37c42..62e7e8b 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,14 +1,12 @@ language: python python: - - "2.6" - "2.7" - - "3.2" - - "3.3" - "3.4" - "3.5" - "3.6" + - "3.7" # command to install dependencies -install: +install: - if [[ $TRAVIS_PYTHON_VERSION == '2.6' ]]; then pip install unittest2; fi - "pip install dill" - "python setup.py install" From be1e85185f2083b708360b1286f508b1afb7b7f4 Mon Sep 17 00:00:00 2001 From: Derek Gulbranson Date: Wed, 11 Dec 2019 19:05:32 -0800 Subject: [PATCH 24/99] add python 3.8 to travis --- .travis.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.travis.yml b/.travis.yml index 62e7e8b..45394bd 100644 --- a/.travis.yml +++ b/.travis.yml @@ -5,6 +5,7 @@ python: - "3.5" - "3.6" - "3.7" + - "3.8" # command to install dependencies install: - if [[ $TRAVIS_PYTHON_VERSION == '2.6' ]]; then pip install unittest2; fi From 72d2a13d4cecc83af344109ad562b260de3903ee Mon Sep 17 00:00:00 2001 From: Derek Gulbranson Date: Wed, 11 Dec 2019 19:33:25 -0800 Subject: [PATCH 25/99] fix duplicate import from merge --- nameparser/config/__init__.py | 1 - 1 file changed, 1 deletion(-) diff --git a/nameparser/config/__init__.py b/nameparser/config/__init__.py index c1f7bbc..4f1e4f2 100644 --- a/nameparser/config/__init__.py +++ b/nameparser/config/__init__.py @@ -29,7 +29,6 @@ unexpected results. See `Customizing the Parser `_. """ from __future__ import unicode_literals -from collections.abc import Set import sys try: # Python 3.3+ From c0d14c5300136bffdbcf993c3a05bb8c813b348b Mon Sep 17 00:00:00 2001 From: Derek Gulbranson Date: Wed, 11 Dec 2019 20:37:28 -0800 Subject: [PATCH 26/99] remove Elder from constants because it is sometimes a first name, fix #96 --- nameparser/config/titles.py | 1 - 1 file changed, 1 deletion(-) diff --git a/nameparser/config/titles.py b/nameparser/config/titles.py index dfcbd07..3d5892f 100644 --- a/nameparser/config/titles.py +++ b/nameparser/config/titles.py @@ -259,7 +259,6 @@ 'educator', 'effendi', 'ekegbian', - 'elder', 'elerunwon', 'eminence', 'emperor', From af5bdabc160fc15054b59e078c658ac80a3cb1ff Mon Sep 17 00:00:00 2001 From: Derek Gulbranson Date: Wed, 11 Dec 2019 21:01:27 -0800 Subject: [PATCH 27/99] add post-nominal list from wikipedia. fix #93 --- nameparser/config/suffixes.py | 533 +++++++++++++++++++++++++++++++++- 1 file changed, 531 insertions(+), 2 deletions(-) diff --git a/nameparser/config/suffixes.py b/nameparser/config/suffixes.py index 7f01581..9765b92 100644 --- a/nameparser/config/suffixes.py +++ b/nameparser/config/suffixes.py @@ -24,104 +24,633 @@ """ SUFFIX_ACRONYMS = set([ + '(ret)', + '(vet)', + '8-vsb', + 'aas', + 'aba', + 'abc', + 'abd', + 'abpp', + 'abr', + 'aca', + 'acas', + 'ace', + 'acha', + 'acp', 'ae', + 'ae', + 'aem', + 'afasma', + 'afc', 'afc', 'afm', + 'afm', + 'agsf', + 'aia', + 'aicp', + 'ala', + 'alc', + 'alp', + 'am', + 'amd', + 'ame', + 'amieee', + 'ams', + 'aphr', + 'apn aprn', + 'apr', + 'apss', + 'aqp', + 'arm', 'arrc', - 'bart', + 'asa', + 'asc', + 'asid', + 'asla', + 'asp', + 'atc', + 'awb', + 'bca', + 'bcl', + 'bcss', + 'bds', + 'bem', 'bem', + 'bls-i', + 'bpe', + 'bpi', + 'bpt', 'bt', + 'btcs', + 'bts', + 'cacts', + 'cae', + 'caha', + 'caia', + 'cams', + 'cap', + 'capa', + 'capm', + 'capp', + 'caps', + 'caro', + 'cas', + 'casp', 'cb', 'cbe', + 'cbm', + 'cbne', + 'cbnt', + 'cbp', + 'cbrte', + 'cbs', + 'cbsp', + 'cbt', + 'cbte', + 'cbv', + 'cca', + 'ccc', + 'ccca', + 'cccm', + 'cce', + 'cchp', + 'ccie', + 'ccim', + 'cciso', + 'ccm', + 'ccmt', + 'ccna', + 'ccnp', + 'ccp', + 'ccp-c', + 'ccpr', + 'ccs', + 'ccufc', + 'cd', + 'cdal', + 'cdfm', + 'cdmp', + 'cds', + 'cdt', + 'cea', + 'ceas', + 'cebs', + 'ceds', + 'ceh', + 'cela', + 'cem', + 'cep', + 'cera', + 'cet', + 'cfa', + 'cfc', + 'cfcc', + 'cfce', + 'cfcm', + 'cfe', + 'cfeds', + 'cfi', + 'cfm', 'cfp', + 'cfps', + 'cfr', + 'cfre', + 'cga', + 'cgap', + 'cgb', 'cgc', + 'cgfm', + 'cgfo', 'cgm', + 'cgm', + 'cgma', + 'cgp', + 'cgr', + 'cgsp', + 'ch', 'ch', + 'cha', + 'chba', + 'chdm', + 'che', + 'ches', + 'chfc', 'chfc', + 'chi', + 'chmc', + 'chmm', + 'chp', + 'chpa', + 'chpe', + 'chpln', + 'chpse', + 'chrm', + 'chsc', + 'chse', + 'chse-a', + 'chsos', + 'chss', + 'cht', + 'cia', + 'cic', 'cie', + 'cig', + 'cip', + 'cipm', + 'cips', + 'ciro', + 'cisa', + 'cism', + 'cissp', + 'cla', + 'clsd', + 'cltd', 'clu', + 'cm', + 'cma', + 'cmas', + 'cmc', + 'cmfo', 'cmg', + 'cmp', + 'cms', + 'cmsp', + 'cmt', + 'cna', + 'cnm', + 'cnp', + 'cp', + 'cp-c', 'cpa', + 'cpacc', + 'cpbe', + 'cpcm', + 'cpcu', + 'cpe', + 'cpfa', + 'cpfo', + 'cpg', + 'cph', + 'cpht', + 'cpim', + 'cpl', + 'cplp', 'cpm', + 'cpo', + 'cpp', + 'cprc', + 'cpre', + 'cprp', + 'cpsc', + 'cpsi', + 'cpss', + 'cpt', + 'cpwa', + 'crde', + 'crisc', + 'crma', + 'crme', + 'crna', + 'cro', + 'crp', + 'crt', + 'crtt', + 'csa', + 'csbe', + 'csc', + 'cscp', + 'cscu', + 'csep', 'csi', 'csm', + 'csp', + 'cspo', + 'csre', + 'csrte', + 'csslp', + 'cssm', + 'cst', + 'cste', + 'ctbs', + 'ctfa', + 'cto', + 'ctp', + 'cts', + 'cua', + 'cusp', + 'cva', + 'cva[22]', 'cvo', + 'cvp', + 'cvrs', + 'cwap', + 'cwb', + 'cwdp', + 'cwep', + 'cwna', + 'cwne', + 'cwp', + 'cwsp', + 'cxa', + 'cyds', + 'cysa', + 'dabfm', + 'dabvlm', + 'dacvim', 'dbe', + 'dc', 'dcb', 'dcm', 'dcmg', 'dcvo', + 'dd', 'dds', + 'ded', + 'dep', 'dfc', 'dfm', + 'diplac', + 'diplom', + 'djur', + 'dma', 'dmd', + 'dmin', + 'dnp', 'do', 'dpm', + 'dpt', + 'drb', + 'drmp', + 'drph', 'dsc', 'dsm', 'dso', + 'dss', + 'dtr', + 'dvep', 'dvm', + 'ea', 'ed', + 'edd', + 'ei', + 'eit', + 'els', + 'emd', + 'emt-b', + 'emt-i/85', + 'emt-i/99', + 'emt-p', + 'enp', 'erd', + 'esq', + 'evp', + 'faafp', + 'faan', + 'faap', + 'fac-c', + 'facc', + 'facd', + 'facem', + 'facep', + 'facha', + 'facofp', + 'facog', + 'facp', + 'facph', + 'facs', + 'faia', + 'faicp', + 'fala', + 'fashp', + 'fasid', + 'fasla', + 'fasma', + 'faspen', + 'fca', + 'fcas', + 'fcela', + 'fd', + 'fec', + 'fhames', + 'fic', + 'ficf', + 'fieee', + 'fmp', + 'fmva', + 'fnss', + 'fp&a', + 'fp-c', + 'fpc', + 'frm', + 'fsa', + 'fsdp', + 'fws', + 'gaee[14]', + 'gba', 'gbe', 'gc', 'gcb', + 'gcb', + 'gchs', 'gcie', 'gcmg', + 'gcmg', 'gcsi', 'gcvo', + 'gcvo', + 'gisp', + 'git', 'gm', + 'gmb', + 'gmr', + 'gphr', + 'gri', + 'grp', + 'gsmieee', + 'hccp', + 'hrs', + 'iaccp', + 'iaee', + 'iccm-d', + 'iccm-f', 'idsm', + 'ifgict', 'iom', + 'ipep', + 'ipm', 'iso', + 'issp-csp', + 'issp-sa', + 'itil', 'jd', + 'jp', 'kbe', 'kcb', + 'kchs/dchs', + 'kcie', 'kcie', 'kcmg', 'kcsi', + 'kcsi', 'kcvo', 'kg', + 'khs/dhs', 'kp', 'kt', + 'lac', + 'lcmt', + 'lcpc', + 'lcsw', + 'leed ap', 'lg', + 'litk', + 'litl', + 'litp', + 'llm', + 'lm', + 'lmsw', + 'lmt', + 'lp', + 'lpa', + 'lpc', + 'lpn', + 'lpss', + 'lsi', + 'lsit', 'lt', + 'lvn', 'lvo', + 'lvt', 'ma', + 'maaa', + 'mai', 'mba', 'mbe', + 'mbs', 'mc', + 'mcct', + 'mcdba', + 'mches', + 'mcm', + 'mcp', + 'mcpd', + 'mcsa', + 'mcsd', + 'mcse', + 'mct', 'md', + 'mdiv', + 'mem', + 'mfa', + 'micp', + 'mieee', + 'mirm', + 'mle', + 'mls', + 'mlse', + 'mlt', 'mm', + 'mmad', + 'mmas', + 'mnaa', + 'mnae', 'mp', + 'mpa', + 'mph', + 'mpse', + 'mra', + 'ms', + 'msa', 'msc' + 'mscmsm', 'msm', + 'mt', + 'mts', 'mvo', + 'nbc-his', + 'nbcch', + 'nbcch-ps', + 'nbcdch', + 'nbcdch-ps', + 'nbcfch', + 'nbcfch-ps', + 'nbct', + 'ncarb', + 'nccp', + 'ncidq', + 'ncps', + 'ncso', + 'ncto', + 'nd', + 'ndtr', + 'nicet i', + 'nicet ii', + 'nicet iii', + 'nicet iv', + 'nmd', + 'np', + 'np[18]', + 'nraemt', + 'nremr', + 'nremt', + 'nrp', 'obe', 'obi', + 'oca', + 'ocm', + 'ocp', + 'od', 'om', + 'oscp', + 'ot', + 'pa-c', + 'pcc', + 'pci', + 'pe', + 'pfmp', + 'pg', + 'pgmp', + 'ph', + 'pharmd', + 'phc', 'phd', 'phr', + 'phrca', + 'pla', + 'pls', + 'pmc', + 'pmi-acp', 'pmp', + 'pp', + 'pps', + 'prm', + 'psm i', + 'psm ii', + 'psm', + 'psp', + 'psyd', + 'pt', + 'pta', 'qam', 'qc', + 'qcsw', 'qfsm', 'qgm', 'qpm', + 'qsd', + 'qsp', + 'ra', + 'rai', + 'rba', + 'rci', + 'rcp', 'rd', + 'rdcs', + 'rdh', + 'rdms', + 'rdn', + 'res', + 'rfp', + 'rhca', + 'rid', + 'rls', + 'rmsks', + 'rn', + 'rp', + 'rpa', + 'rph', + 'rpl', 'rrc', + 'rrt', + 'rrt-accs', + 'rrt-nps', + 'rrt-sds', + 'rtrp', 'rvm', + 'rvt', + 'sa', + 'same', + 'sasm', + 'sccp', + 'scmp', + 'se', + 'secb', + 'sfp', 'sgm', + 'shrm-cp', + 'shrm-scp', + 'si', + 'siie', + 'smieee', + 'sphr', + 'sra', + 'sscp', + 'stmieee', + 'tbr-ct', 'td', + 'thd', + 'thm', 'ud', + 'usa', + 'usaf', + 'usar', + 'uscg', + 'usmc', + 'usn', + 'usnr', + 'uxc', + 'uxmc', + 'vc', 'vc', + 'vcp', 'vd', 'vrd', ]) """ Post-nominal acronyms. Titles, degrees and other things people stick after their name -that may or may not have periods between the letters. The parser removes periods +that may or may not have periods between the letters. The parser removes periods when matching against these pieces. """ From ba4d69a66d23f0bbe3b9e6894c9b76258b213221 Mon Sep 17 00:00:00 2001 From: Derek Gulbranson Date: Wed, 11 Dec 2019 21:03:22 -0800 Subject: [PATCH 28/99] v1.0.5, update release log --- docs/release_log.rst | 6 ++++++ nameparser/__init__.py | 2 +- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/docs/release_log.rst b/docs/release_log.rst index 0700af2..f53cbcc 100644 --- a/docs/release_log.rst +++ b/docs/release_log.rst @@ -1,5 +1,11 @@ Release Log =========== +* 1.0.5 - Dec 12, 2019 + - Fix suffix parsing bug in comma parts (#98) + - Fix deprecation warning on Python 3.7 (#94) + - Improved capitalization support of mixed case names (#90) + - Remove "elder" from titles (#96) + - Add post-nominal list from Wikipedia to suffixes (#93) * 1.0.4 - June 26, 2019 - Better nickname handling of multiple single quotes (#86) - full_name attribute now returns formatted string output instead of original string (#87) diff --git a/nameparser/__init__.py b/nameparser/__init__.py index 650cf32..b2644e7 100644 --- a/nameparser/__init__.py +++ b/nameparser/__init__.py @@ -1,4 +1,4 @@ -VERSION = (1, 0, 4) +VERSION = (1, 0, 5) __version__ = '.'.join(map(str, VERSION)) __author__ = "Derek Gulbranson" __author_email__ = 'derek73@gmail.com' From 6da7fb3d53bb0d25858a5df0f7e0a37d2aab8f2a Mon Sep 17 00:00:00 2001 From: Derek Gulbranson Date: Wed, 11 Dec 2019 21:09:04 -0800 Subject: [PATCH 29/99] remove tests for Bart as suffix, bart no longer in suffixes --- tests.py | 14 +------------- 1 file changed, 1 insertion(+), 13 deletions(-) diff --git a/tests.py b/tests.py index 0e948c7..5f976b8 100644 --- a/tests.py +++ b/tests.py @@ -1727,19 +1727,7 @@ def test_potential_suffix_that_is_also_last_name_comma(self): hn = HumanName("Ma, Jack") self.m(hn.first, "Jack", hn) self.m(hn.last, "Ma", hn) - - def test_potential_suffix_that_is_also_first_name_comma(self): - hn = HumanName("Johnson, Bart") - self.m(hn.first, "Bart", hn) - self.m(hn.last, "Johnson", hn) - - # TODO: handle conjunctions in last names followed by first names clashing with suffixes - @unittest.expectedFailure - def test_potential_suffix_that_is_also_first_name_comma_with_conjunction(self): - hn = HumanName("De la Vina, Bart") - self.m(hn.first, "Bart", hn) - self.m(hn.last, "De la Vina", hn) - + def test_potential_suffix_that_is_also_last_name_with_suffix(self): hn = HumanName("Jack Ma Jr") self.m(hn.first, "Jack", hn) From 859eb38e74e25ceb9ba55b70ec60aecb0462ea1e Mon Sep 17 00:00:00 2001 From: Derek Gulbranson Date: Wed, 11 Dec 2019 22:07:27 -0800 Subject: [PATCH 30/99] Add publish python package workflow --- .github/workflows/pythonpublish.yml | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 .github/workflows/pythonpublish.yml diff --git a/.github/workflows/pythonpublish.yml b/.github/workflows/pythonpublish.yml new file mode 100644 index 0000000..21f2f01 --- /dev/null +++ b/.github/workflows/pythonpublish.yml @@ -0,0 +1,26 @@ +name: Upload Python Package + +on: + release: + types: [created] + +jobs: + deploy: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v1 + - name: Set up Python + uses: actions/setup-python@v1 + with: + python-version: '3.x' + - name: Install dependencies + run: | + python -m pip install --upgrade pip + pip install setuptools wheel twine + - name: Build and publish + env: + TWINE_USERNAME: ${{ secrets.PYPI_USERNAME }} + TWINE_PASSWORD: ${{ secrets.PYPI_PASSWORD }} + run: | + python setup.py sdist bdist_wheel + twine upload dist/* From ee8b8e047febd7b64fd97cbc7c717df02eab2950 Mon Sep 17 00:00:00 2001 From: Derek Gulbranson Date: Tue, 4 Feb 2020 19:09:51 -0800 Subject: [PATCH 31/99] remove publish workflow --- .github/workflows/pythonpublish.yml | 26 -------------------------- 1 file changed, 26 deletions(-) delete mode 100644 .github/workflows/pythonpublish.yml diff --git a/.github/workflows/pythonpublish.yml b/.github/workflows/pythonpublish.yml deleted file mode 100644 index 21f2f01..0000000 --- a/.github/workflows/pythonpublish.yml +++ /dev/null @@ -1,26 +0,0 @@ -name: Upload Python Package - -on: - release: - types: [created] - -jobs: - deploy: - runs-on: ubuntu-latest - steps: - - uses: actions/checkout@v1 - - name: Set up Python - uses: actions/setup-python@v1 - with: - python-version: '3.x' - - name: Install dependencies - run: | - python -m pip install --upgrade pip - pip install setuptools wheel twine - - name: Build and publish - env: - TWINE_USERNAME: ${{ secrets.PYPI_USERNAME }} - TWINE_PASSWORD: ${{ secrets.PYPI_PASSWORD }} - run: | - python setup.py sdist bdist_wheel - twine upload dist/* From 845d61a5978957cdc6276991730140ffb76ef3cd Mon Sep 17 00:00:00 2001 From: Karthikeyan Singaravelan Date: Sat, 8 Feb 2020 10:16:33 +0530 Subject: [PATCH 32/99] Fix syntax warnings due to comparison of literals using is. --- nameparser/parser.py | 264 +++++++++++++++++++++---------------------- 1 file changed, 132 insertions(+), 132 deletions(-) diff --git a/nameparser/parser.py b/nameparser/parser.py index caf4ded..bd79057 100644 --- a/nameparser/parser.py +++ b/nameparser/parser.py @@ -17,7 +17,7 @@ def group_contiguous_integers(data): """ - return list of tuples containing first and last index + return list of tuples containing first and last index position of contiguous numbers in a series """ ranges = [] @@ -30,13 +30,13 @@ def group_contiguous_integers(data): class HumanName(object): """ Parse a person's name into individual components. - + Instantiation assigns to ``full_name``, and assignment to :py:attr:`full_name` triggers :py:func:`parse_full_name`. After parsing the name, these instance attributes are available. - + **HumanName Instance Attributes** - + * :py:attr:`title` * :py:attr:`first` * :py:attr:`middle` @@ -46,61 +46,61 @@ class HumanName(object): * :py:attr:`surnames` :param str full_name: The name string to be parsed. - :param constants constants: - a :py:class:`~nameparser.config.Constants` instance. Pass ``None`` for - `per-instance config `_. + :param constants constants: + a :py:class:`~nameparser.config.Constants` instance. Pass ``None`` for + `per-instance config `_. :param str encoding: string representing the encoding of your input - :param str string_format: python string formatting + :param str string_format: python string formatting """ - + C = CONSTANTS """ A reference to the configuration for this instance, which may or may not be - a reference to the shared, module-wide instance at - :py:mod:`~nameparser.config.CONSTANTS`. See `Customizing the Parser + a reference to the shared, module-wide instance at + :py:mod:`~nameparser.config.CONSTANTS`. See `Customizing the Parser `_. """ - + original = '' """ The original string, untouched by the parser. """ - + _count = 0 _members = ['title','first','middle','last','suffix','nickname'] unparsable = True _full_name = '' - + def __init__(self, full_name="", constants=CONSTANTS, encoding=DEFAULT_ENCODING, string_format=None): self.C = constants if type(self.C) is not type(CONSTANTS): self.C = Constants() - + self.encoding = encoding self.string_format = string_format or self.C.string_format # full_name setter triggers the parse self.full_name = full_name - + def __iter__(self): return self - + def __len__(self): l = 0 for x in self: l += 1 return l - + def __eq__(self, other): """ - HumanName instances are equal to other objects whose + HumanName instances are equal to other objects whose lower case unicode representation is the same. """ return (u(self)).lower() == (u(other)).lower() - + def __ne__(self, other): return not (u(self)).lower() == (u(other)).lower() - + def __getitem__(self, key): if isinstance(key, slice): return [getattr(self, x) for x in self._members[key]] @@ -133,12 +133,12 @@ def __unicode__(self): _s = _s.replace(str(self.C.empty_attribute_default),'').replace(" ()","").replace(" ''","").replace(' ""',"") return self.collapse_whitespace(_s).strip(', ') return " ".join(self) - + def __str__(self): if sys.version_info[0] >= 3: return self.__unicode__() return self.__unicode__().encode(self.encoding) - + def __repr__(self): if self.unparsable: _string = "<%(class)s : [ Unparsable ] >" % {'class': self.__class__.__name__,} @@ -155,22 +155,22 @@ def __repr__(self): if sys.version_info[0] >= 3: return _string return _string.encode(self.encoding) - + def as_dict(self, include_empty=True): """ Return the parsed name as a dictionary of its attributes. - + :param bool include_empty: Include keys in the dictionary for empty name attributes. :rtype: dict - + .. doctest:: - + >>> name = HumanName("Bob Dole") >>> name.as_dict() {'last': 'Dole', 'suffix': '', 'title': '', 'middle': '', 'nickname': '', 'first': 'Bob'} >>> name.as_dict(False) {'last': 'Dole', 'first': 'Bob'} - + """ d = {} for m in self._members: @@ -181,66 +181,66 @@ def as_dict(self, include_empty=True): if val: d[m] = val return d - + @property def has_own_config(self): """ - True if this instance is not using the shared module-level + True if this instance is not using the shared module-level configuration. """ return self.C is not CONSTANTS - + ### attributes - + @property def title(self): """ - The person's titles. Any string of consecutive pieces in - :py:mod:`~nameparser.config.titles` or + The person's titles. Any string of consecutive pieces in + :py:mod:`~nameparser.config.titles` or :py:mod:`~nameparser.config.conjunctions` at the beginning of :py:attr:`full_name`. """ return " ".join(self.title_list) or self.C.empty_attribute_default - + @property def first(self): """ - The person's first name. The first name piece after any known + The person's first name. The first name piece after any known :py:attr:`title` pieces parsed from :py:attr:`full_name`. """ return " ".join(self.first_list) or self.C.empty_attribute_default - + @property def middle(self): """ - The person's middle names. All name pieces after the first name and + The person's middle names. All name pieces after the first name and before the last name parsed from :py:attr:`full_name`. """ return " ".join(self.middle_list) or self.C.empty_attribute_default - + @property def last(self): """ - The person's last name. The last name piece parsed from + The person's last name. The last name piece parsed from :py:attr:`full_name`. """ return " ".join(self.last_list) or self.C.empty_attribute_default - + @property def suffix(self): """ The persons's suffixes. Pieces at the end of the name that are found in :py:mod:`~nameparser.config.suffixes`, or pieces that are at the end - of comma separated formats, e.g. - "Lastname, Title Firstname Middle[,] Suffix [, Suffix]" parsed + of comma separated formats, e.g. + "Lastname, Title Firstname Middle[,] Suffix [, Suffix]" parsed from :py:attr:`full_name`. """ return ", ".join(self.suffix_list) or self.C.empty_attribute_default - + @property def nickname(self): """ - The person's nicknames. Any text found inside of quotes (``""``) or + The person's nicknames. Any text found inside of quotes (``""``) or parenthesis (``()``) """ return " ".join(self.nickname_list) or self.C.empty_attribute_default @@ -260,7 +260,7 @@ def surnames(self): return " ".join(self.surnames_list) or self.C.empty_attribute_default ### setter methods - + def _set_list(self, attr, value): if isinstance(value, list): val = value @@ -273,60 +273,60 @@ def _set_list(self, attr, value): "Can only assign strings, lists or None to name attributes." " Got {0}".format(type(value))) setattr(self, attr+"_list", self.parse_pieces(val)) - + @title.setter def title(self, value): self._set_list('title', value) - + @first.setter def first(self, value): self._set_list('first', value) - + @middle.setter def middle(self, value): self._set_list('middle', value) - + @last.setter def last(self, value): self._set_list('last', value) - + @suffix.setter def suffix(self, value): self._set_list('suffix', value) - + @nickname.setter def nickname(self, value): self._set_list('nickname', value) - + ### Parse helpers - + def is_title(self, value): """Is in the :py:data:`~nameparser.config.titles.TITLES` set.""" return lc(value) in self.C.titles - + def is_conjunction(self, piece): """Is in the conjuctions set and not :py:func:`is_an_initial()`.""" return piece.lower() in self.C.conjunctions and not self.is_an_initial(piece) - + def is_prefix(self, piece): """ - Lowercase and no periods version of piece is in the + Lowercase and no periods version of piece is in the :py:data:`~nameparser.config.prefixes.PREFIXES` set. """ return lc(piece) in self.C.prefixes def is_roman_numeral(self, value): """ - Matches the ``roman_numeral`` regular expression in + Matches the ``roman_numeral`` regular expression in :py:data:`~nameparser.config.regexes.REGEXES`. """ return bool(self.C.regexes.roman_numeral.match(value)) - + def is_suffix(self, piece): """ - Is in the suffixes set and not :py:func:`is_an_initial()`. - - Some suffixes may be acronyms (M.B.A) while some are not (Jr.), + Is in the suffixes set and not :py:func:`is_an_initial()`. + + Some suffixes may be acronyms (M.B.A) while some are not (Jr.), so we remove the periods from `piece` when testing against `C.suffix_acronyms`. """ @@ -341,31 +341,31 @@ def are_suffixes(self, pieces): if not self.is_suffix(piece): return False return True - + def is_rootname(self, piece): """ Is not a known title, suffix or prefix. Just first, middle, last names. """ return lc(piece) not in self.C.suffixes_prefixes_titles \ - and not self.is_an_initial(piece) - + and not self.is_an_initial(piece) + def is_an_initial(self, value): """ Words with a single period at the end, or a single uppercase letter. - - Matches the ``initial`` regular expression in + + Matches the ``initial`` regular expression in :py:data:`~nameparser.config.regexes.REGEXES`. """ return bool(self.C.regexes.initial.match(value)) - + ### full_name parser - + @property def full_name(self): """The string output of the HumanName instance.""" return self.__str__() - + @full_name.setter def full_name(self, value): self.original = value @@ -373,7 +373,7 @@ def full_name(self, value): if isinstance(value, binary_type): self._full_name = value.decode(self.encoding) self.parse_full_name() - + def collapse_whitespace(self, string): # collapse multiple spaces into single space string = self.C.regexes.spaces.sub(" ", string.strip()) @@ -383,12 +383,12 @@ def collapse_whitespace(self, string): def pre_process(self): """ - + This method happens at the beginning of the :py:func:`parse_full_name` before any other processing of the string aside from unicode normalization, so it's a good place to do any custom handling in a subclass. Runs :py:func:`parse_nicknames` and :py:func:`squash_emoji`. - + """ self.fix_phd() self.parse_nicknames() @@ -412,17 +412,17 @@ def fix_phd(self): def parse_nicknames(self): """ - The content of parenthesis or quotes in the name will be added to the + The content of parenthesis or quotes in the name will be added to the nicknames list. This happens before any other processing of the name. - + Single quotes cannot span white space characters and must border white space to allow for quotes in names like O'Connor and Kawai'ae'a. Double quotes and parenthesis can span white space. - - Loops through 3 :py:data:`~nameparser.config.regexes.REGEXES`; + + Loops through 3 :py:data:`~nameparser.config.regexes.REGEXES`; `quoted_word`, `double_quotes` and `parenthesis`. """ - + re_quoted_word = self.C.regexes.quoted_word re_double_quotes = self.C.regexes.double_quotes re_parenthesis = self.C.regexes.parenthesis @@ -445,7 +445,7 @@ def handle_firstnames(self): If there are only two parts and one is a title, assume it's a last name instead of a first name. e.g. Mr. Johnson. Unless it's a special title like "Sir", then when it's followed by a single name that name is always - a first name. + a first name. """ if self.title \ and len(self) == 2 \ @@ -454,18 +454,18 @@ def handle_firstnames(self): def parse_full_name(self): """ - + The main parse method for the parser. This method is run upon assignment to the :py:attr:`full_name` attribute or instantiation. Basic flow is to hand off to :py:func:`pre_process` to handle nicknames. It then splits on commas and chooses a code path depending on the number of commas. - + :py:func:`parse_pieces` then splits those parts on spaces and - :py:func:`join_on_conjunctions` joins any pieces next to conjunctions. + :py:func:`join_on_conjunctions` joins any pieces next to conjunctions. """ - + self.title_list = [] self.first_list = [] self.middle_list = [] @@ -473,23 +473,23 @@ def parse_full_name(self): self.suffix_list = [] self.nickname_list = [] self.unparsable = True - - + + self.pre_process() - + self._full_name = self.collapse_whitespace(self._full_name) - + # break up full_name by commas parts = [x.strip() for x in self._full_name.split(",")] - + log.debug("full_name: %s", self._full_name) log.debug("parts: %s", parts) - + if len(parts) == 1: - + # no commas, title first middle middle middle last suffix # part[0] - + pieces = self.parse_pieces(parts) p_len = len(pieces) for i, piece in enumerate(pieces): @@ -497,7 +497,7 @@ def parse_full_name(self): nxt = pieces[i + 1] except IndexError: nxt = None - + # title must have a next piece, unless it's just a title if self.is_title(piece) \ and (nxt or p_len == 1) \ @@ -511,10 +511,10 @@ def parse_full_name(self): self.first_list.append(piece) continue if self.are_suffixes(pieces[i+1:]) or \ - ( + ( # if the next piece is the last piece and a roman # numeral but this piece is not an initial - self.is_roman_numeral(nxt) and i == p_len - 2 + self.is_roman_numeral(nxt) and i == p_len - 2 and not self.is_an_initial(piece) ): self.last_list.append(piece) @@ -523,7 +523,7 @@ def parse_full_name(self): if not nxt: self.last_list.append(piece) continue - + self.middle_list.append(piece) else: # if all the end parts are suffixes and there is more than one piece @@ -535,12 +535,12 @@ def parse_full_name(self): if self.are_suffixes(parts[1].split(' ')) \ and len(parts[0].split(' ')) > 1: - - # suffix comma: + + # suffix comma: # title first middle last [suffix], suffix [suffix] [, suffix] # parts[0], parts[1:...] - - + + self.suffix_list += parts[1:] pieces = self.parse_pieces(parts[0].split(' ')) log.debug("pieces: %s", u(pieces)) @@ -567,13 +567,13 @@ def parse_full_name(self): continue self.middle_list.append(piece) else: - - # lastname comma: + + # lastname comma: # last [suffix], title first middles[,] suffix [,suffix] # parts[0], parts[1], parts[2:...] - + log.debug("post-comma pieces: %s", u(post_comma_pieces)) - + # lastname part may have suffixes in it lastname_pieces = self.parse_pieces(parts[0].split(' '), 1) for piece in lastname_pieces: @@ -583,13 +583,13 @@ def parse_full_name(self): self.suffix_list.append(piece) else: self.last_list.append(piece) - + for i, piece in enumerate(post_comma_pieces): try: nxt = post_comma_pieces[i + 1] except IndexError: nxt = None - + if self.is_title(piece) \ and (nxt or len(post_comma_pieces) == 1) \ and not self.first: @@ -607,7 +607,7 @@ def parse_full_name(self): self.suffix_list += parts[2:] except IndexError: pass - + if len(self) < 0: log.info("Unparsable: \"%s\" ", self.original) else: @@ -621,24 +621,24 @@ def parse_pieces(self, parts, additional_parts_count=0): lastname prefixes. If parts have periods in the middle, try splitting on periods and check if the parts are titles or suffixes. If they are add to the constant so they will be found. - + :param list parts: name part strings from the comma split - :param int additional_parts_count: - - if the comma format contains other parts, we need to know - how many there are to decide if things should be considered a + :param int additional_parts_count: + + if the comma format contains other parts, we need to know + how many there are to decide if things should be considered a conjunction. :return: pieces split on spaces and joined on conjunctions :rtype: list """ - + output = [] for part in parts: if not isinstance(part, text_types): raise TypeError("Name parts must be strings. " "Got {0}".format(type(part))) output += [x.strip(' ,') for x in part.split(' ')] - + # If part contains periods, check if it's multiple titles or suffixes # together without spaces if so, add the new part with periods to the # constants so they get parsed correctly later @@ -650,7 +650,7 @@ def parse_pieces(self, parts, additional_parts_count=0): period_chunks = part.split(".") titles = list(filter(self.is_title, period_chunks)) suffixes = list(filter(self.is_suffix, period_chunks)) - + # add the part to the constant so it will be found if len(list(titles)): self.C.titles.add(part) @@ -658,27 +658,27 @@ def parse_pieces(self, parts, additional_parts_count=0): if len(list(suffixes)): self.C.suffix_not_acronyms.add(part) continue - + return self.join_on_conjunctions(output, additional_parts_count) - + def join_on_conjunctions(self, pieces, additional_parts_count=0): """ Join conjunctions to surrounding pieces. Title- and prefix-aware. e.g.: - + ['Mr.', 'and'. 'Mrs.', 'John', 'Doe'] ==> ['Mr. and Mrs.', 'John', 'Doe'] - + ['The', 'Secretary', 'of', 'State', 'Hillary', 'Clinton'] ==> ['The Secretary of State', 'Hillary', 'Clinton'] - + When joining titles, saves newly formed piece to the instance's titles constant so they will be parsed correctly later. E.g. after parsing the example names above, 'The Secretary of State' and 'Mr. and Mrs.' would be present in the titles constant set. - + :param list pieces: name pieces strings after split on spaces - :param int additional_parts_count: - :return: new list with piece next to conjunctions merged into one piece + :param int additional_parts_count: + :return: new list with piece next to conjunctions merged into one piece with spaces in it. :rtype: list @@ -739,7 +739,7 @@ def join_on_conjunctions(self, pieces, additional_parts_count=0): # http://code.google.com/p/python-nameparser/issues/detail?id=11 continue - if i is 0: + if i == 0: new_piece = " ".join(pieces[i:i+2]) if self.is_title(pieces[i+1]): # when joining to a title, make new_piece a title too @@ -812,10 +812,10 @@ def join_on_conjunctions(self, pieces, additional_parts_count=0): log.debug("pieces: %s", pieces) return pieces - - + + ### Capitalization Support - + def cap_word(self, word, attribute): if (self.is_prefix(word) and attribute in ('last','middle')) \ or self.is_conjunction(word): @@ -843,15 +843,15 @@ def capitalize(self, force=None): entered in all upper or lower case. By default, it will not adjust the case of names entered in mixed case. To run capitalization on all names pass the parameter `force=True`. - + :param bool force: Forces capitalization of mixed case strings. This parameter overrides rules set within :py:class:`~nameparser.config.CONSTANTS`. **Usage** - + .. doctest:: capitalize - + >>> name = HumanName('bob v. de la macdole-eisenhower phd') >>> name.capitalize() >>> str(name) @@ -859,12 +859,12 @@ def capitalize(self, force=None): >>> # Don't touch good names >>> name = HumanName('Shirley Maclaine') >>> name.capitalize() - >>> str(name) + >>> str(name) 'Shirley Maclaine' >>> name.capitalize(force=True) - >>> str(name) + >>> str(name) 'Shirley MacLaine' - + """ name = u(self) force = self.C.force_mixed_case_capitalization \ From 1d0b8f85bdb64e6e0826760409bd1f759295a8a5 Mon Sep 17 00:00:00 2001 From: Derek Gulbranson Date: Sat, 8 Feb 2020 13:35:56 -0800 Subject: [PATCH 33/99] v1.0.6 --- docs/release_log.rst | 12 +++++++----- nameparser/__init__.py | 2 +- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/docs/release_log.rst b/docs/release_log.rst index f53cbcc..38e76e4 100644 --- a/docs/release_log.rst +++ b/docs/release_log.rst @@ -1,5 +1,7 @@ Release Log =========== +* 1.0.6 - February 8, 2020 + - Fix Python 3.8 syntax error (#104) * 1.0.5 - Dec 12, 2019 - Fix suffix parsing bug in comma parts (#98) - Fix deprecation warning on Python 3.7 (#94) @@ -110,7 +112,7 @@ Release Log - Generate documentation using sphinx and host on readthedocs. * 0.2.10 - May 6, 2014 - If name is only a title and one part, assume it's a last name instead of a first name, with exceptions for some titles like 'Sir'. (`#7 `_). - - Add some judicial and other common titles. (#9) + - Add some judicial and other common titles. (#9) * 0.2.9 - Apr 1, 2014 - Add a new nickname attribute containing anything in parenthesis or double quotes (`Issue 33 `_). * 0.2.8 - Oct 25, 2013 @@ -123,7 +125,7 @@ Release Log * 0.2.5 - Feb 11, 2013 - Set logging handler to NullHandler - Remove 'ben' from PREFIXES because it's more common as a name than a prefix. - - Deprecate BlankHumanNameError. Do not raise exceptions if full_name is empty string. + - Deprecate BlankHumanNameError. Do not raise exceptions if full_name is empty string. * 0.2.4 - Feb 10, 2013 - Adjust logging, don't set basicConfig. Fix `Issue 10 `_ and `Issue 26 `_. - Fix handling of single lower case initials that are also conjunctions, e.g. "john e smith". Re `Issue 11 `_. @@ -134,12 +136,12 @@ Release Log - tests/test.py can now take an optional name argument that will return repr() for that name. * 0.2.3 - Fix overzealous "Mac" regex * 0.2.2 - Fix parsing error -* 0.2.0 +* 0.2.0 - Significant refactor of parsing logic. Handle conjunctions and prefixes before parsing into attribute buckets. - Support attribute overriding by assignment. - - Support multiple titles. - - Lowercase titles constants to fix bug with comparison. + - Support multiple titles. + - Lowercase titles constants to fix bug with comparison. - Move documentation to README.rst, add release log. * 0.1.4 - Use set() in constants for improved speed. setuptools compatibility - sketerpot * 0.1.3 - Add capitalization feature - twotwo diff --git a/nameparser/__init__.py b/nameparser/__init__.py index b2644e7..6c898ba 100644 --- a/nameparser/__init__.py +++ b/nameparser/__init__.py @@ -1,4 +1,4 @@ -VERSION = (1, 0, 5) +VERSION = (1, 0, 6) __version__ = '.'.join(map(str, VERSION)) __author__ = "Derek Gulbranson" __author_email__ = 'derek73@gmail.com' From 6e43c995e0f7c0f4e40270e36fdba1d2fbacab54 Mon Sep 17 00:00:00 2001 From: Tim Gates Date: Thu, 27 Feb 2020 20:08:15 +1100 Subject: [PATCH 34/99] Fix simple typo: conjuctions -> conjunctions Closes #106 --- nameparser/parser.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/nameparser/parser.py b/nameparser/parser.py index bd79057..7ee06c6 100644 --- a/nameparser/parser.py +++ b/nameparser/parser.py @@ -305,7 +305,7 @@ def is_title(self, value): return lc(value) in self.C.titles def is_conjunction(self, piece): - """Is in the conjuctions set and not :py:func:`is_an_initial()`.""" + """Is in the conjunctions set and not :py:func:`is_an_initial()`.""" return piece.lower() in self.C.conjunctions and not self.is_an_initial(piece) def is_prefix(self, piece): From c87d42e3879af9041becd2c9c5ad093499f54d0e Mon Sep 17 00:00:00 2001 From: Derek Gulbranson Date: Sun, 9 Aug 2020 14:00:09 -0700 Subject: [PATCH 35/99] add baby names reference to resources --- docs/resources.rst | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/docs/resources.rst b/docs/resources.rst index 0c70695..6cc28e8 100644 --- a/docs/resources.rst +++ b/docs/resources.rst @@ -2,12 +2,14 @@ Naming Practices and Resources ============================== * US_Census_Surname_Data_2000_ + * US_Social_Security_Administration_Baby_Names_Index_ * Naming_practice_guide_UK_2006_ * Wikipedia_Anthroponymy_ * Wikipedia_Naming_conventions_ * Wikipedia_List_Of_Titles_ -.. _US_Census_Surname_Data_2000: http://www.census.gov/genealogy/www/data/2000surnames/index.html +.. _US_Census_Surname_Data_2000: https://www.census.gov/data/developers/data-sets/surnames/2000.html +.. _US_Social_Security_Administration_Baby_Names_Index: https://www.ssa.gov/oact/babynames/limits.html .. _Naming_practice_guide_UK_2006: https://www.fbiic.gov/public/2008/nov/Naming_practice_guide_UK_2006.pdf .. _Wikipedia_Anthroponymy: https://en.wikipedia.org/wiki/Anthroponymy .. _Wikipedia_Naming_conventions: http://en.wikipedia.org/wiki/Wikipedia:Naming_conventions_(people) From d498968e850577ffc4dfa01c27610500a9ef3a80 Mon Sep 17 00:00:00 2001 From: Derek Gulbranson Date: Sun, 9 Aug 2020 14:14:33 -0700 Subject: [PATCH 36/99] dill library no longer supports python 3.4 --- .travis.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 45394bd..42fadab 100644 --- a/.travis.yml +++ b/.travis.yml @@ -9,7 +9,7 @@ python: # command to install dependencies install: - if [[ $TRAVIS_PYTHON_VERSION == '2.6' ]]; then pip install unittest2; fi - - "pip install dill" + - if [[ $TRAVIS_PYTHON_VERSION -ne '3.4' ]]; then pip install dill; fi - "python setup.py install" # command to run tests script: python tests.py From e53f878bd49ef5705d4615ecba30f4bfb5c03895 Mon Sep 17 00:00:00 2001 From: zahna Date: Thu, 18 Feb 2021 16:29:49 -0500 Subject: [PATCH 37/99] Added a few additional prefixes for last names. Added a few additional prefixes for last names. --- nameparser/config/prefixes.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/nameparser/config/prefixes.py b/nameparser/config/prefixes.py index 2f5eb31..cce7805 100644 --- a/nameparser/config/prefixes.py +++ b/nameparser/config/prefixes.py @@ -34,11 +34,15 @@ 'ibn', 'la', 'le', + 'mac', + 'mc', 'san', 'santa', 'st', 'ste', 'van', + 'vander', + 'van der', 'vel', 'von', ]) From e553657ba1cdcb0b69c78a65dd7ee6eab24154cd Mon Sep 17 00:00:00 2001 From: zahna Date: Thu, 18 Feb 2021 16:47:22 -0500 Subject: [PATCH 38/99] Adding additional religious titles. Adding additional religious titles. --- nameparser/config/titles.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/nameparser/config/titles.py b/nameparser/config/titles.py index 3d5892f..91a9ac3 100644 --- a/nameparser/config/titles.py +++ b/nameparser/config/titles.py @@ -136,6 +136,7 @@ 'bodhisattva', 'bookseller', 'botanist', + 'bp', 'brigadier', 'briggen', 'british', @@ -223,6 +224,7 @@ 'cwo5', 'cyclist', 'dancer', + 'dcn', 'deacon', 'delegate', 'deputy', @@ -278,6 +280,7 @@ 'expert', 'fadm', 'family', + 'father', 'federal', 'field', 'film', @@ -288,6 +291,7 @@ 'foreign', 'forester', 'founder', + 'fr', 'friar', 'gaf', 'gen', @@ -314,6 +318,8 @@ 'high', 'highness', 'his', + 'his eminence', + 'his eminence metropolitan', 'historian', 'historicus', 'historien', @@ -395,6 +401,7 @@ 'member', 'memoirist', 'merchant', + 'met', 'metropolitan', 'mg', 'mgr', @@ -568,6 +575,7 @@ 'srta', 'ssg', 'ssgt', + 'st', 'staff', 'state', 'states', From 77cd216ddb4e5ec2153ebbe69f45906daa7a3d5f Mon Sep 17 00:00:00 2001 From: geritwagner Date: Tue, 22 Jun 2021 16:15:32 +0200 Subject: [PATCH 39/99] Update prefixes.py --- nameparser/config/prefixes.py | 1 + 1 file changed, 1 insertion(+) diff --git a/nameparser/config/prefixes.py b/nameparser/config/prefixes.py index 2f5eb31..48f5ac2 100644 --- a/nameparser/config/prefixes.py +++ b/nameparser/config/prefixes.py @@ -41,4 +41,5 @@ 'van', 'vel', 'von', + 'vom', ]) From 32c7613e17f60384a6e833c46e5a604e3ef2f760 Mon Sep 17 00:00:00 2001 From: Rink Stiekema Date: Wed, 20 Oct 2021 14:48:19 +0200 Subject: [PATCH 40/99] Parse initials from first and middle names --- nameparser/parser.py | 106 ++++++++++++++++++++++++++----------------- tests.py | 86 +++++++++++++++++------------------ 2 files changed, 108 insertions(+), 84 deletions(-) diff --git a/nameparser/parser.py b/nameparser/parser.py index bd79057..e58a428 100644 --- a/nameparser/parser.py +++ b/nameparser/parser.py @@ -15,6 +15,7 @@ ENCODING = 'utf-8' + def group_contiguous_integers(data): """ return list of tuples containing first and last index @@ -27,6 +28,7 @@ def group_contiguous_integers(data): ranges.append((group[0], group[-1])) return ranges + class HumanName(object): """ Parse a person's name into individual components. @@ -67,12 +69,12 @@ class HumanName(object): """ _count = 0 - _members = ['title','first','middle','last','suffix','nickname'] + _members = ['title', 'initials', 'first', 'middle', 'last', 'suffix', 'nickname'] unparsable = True _full_name = '' def __init__(self, full_name="", constants=CONSTANTS, encoding=DEFAULT_ENCODING, - string_format=None): + string_format=None): self.C = constants if type(self.C) is not type(CONSTANTS): self.C = Constants() @@ -130,7 +132,7 @@ def __unicode__(self): # string_format = "{title} {first} {middle} {last} {suffix} ({nickname})" _s = self.string_format.format(**self.as_dict()) # remove trailing punctuation from missing nicknames - _s = _s.replace(str(self.C.empty_attribute_default),'').replace(" ()","").replace(" ''","").replace(' ""',"") + _s = _s.replace(str(self.C.empty_attribute_default), '').replace(" ()", "").replace(" ''", "").replace(' ""', "") return self.collapse_whitespace(_s).strip(', ') return " ".join(self) @@ -141,9 +143,9 @@ def __str__(self): def __repr__(self): if self.unparsable: - _string = "<%(class)s : [ Unparsable ] >" % {'class': self.__class__.__name__,} + _string = "<%(class)s : [ Unparsable ] >" % {'class': self.__class__.__name__, } else: - _string = "<%(class)s : [\n\ttitle: '%(title)s' \n\tfirst: '%(first)s' \n\tmiddle: '%(middle)s' \n\tlast: '%(last)s' \n\tsuffix: '%(suffix)s'\n\tnickname: '%(nickname)s'\n]>" % { + _string = "<%(class)s : [\n\ttitle: '%(title)s' \n\tinitials: '%(initials)s' \n\tfirst: '%(first)s' \n\tmiddle: '%(middle)s' \n\tlast: '%(last)s' \n\tsuffix: '%(suffix)s'\n\tnickname: '%(nickname)s'\n]>" % { 'class': self.__class__.__name__, 'title': self.title or '', 'first': self.first or '', @@ -151,6 +153,7 @@ def __repr__(self): 'last': self.last or '', 'suffix': self.suffix or '', 'nickname': self.nickname or '', + 'initials': self.initials or '', } if sys.version_info[0] >= 3: return _string @@ -190,7 +193,7 @@ def has_own_config(self): """ return self.C is not CONSTANTS - ### attributes + # attributes @property def title(self): @@ -259,7 +262,14 @@ def surnames(self): """ return " ".join(self.surnames_list) or self.C.empty_attribute_default - ### setter methods + @property + def initials(self): + """" + A string of all initials + """ + return " ".join([initial + "." for initial in self.initials_list]) + + # setter methods def _set_list(self, attr, value): if isinstance(value, list): @@ -270,8 +280,8 @@ def _set_list(self, attr, value): val = [] else: raise TypeError( - "Can only assign strings, lists or None to name attributes." - " Got {0}".format(type(value))) + "Can only assign strings, lists or None to name attributes." + " Got {0}".format(type(value))) setattr(self, attr+"_list", self.parse_pieces(val)) @title.setter @@ -298,7 +308,11 @@ def suffix(self, value): def nickname(self, value): self._set_list('nickname', value) - ### Parse helpers + @initials.setter + def initials(self, value): + self._set_list('initials', value) + + # Parse helpers def is_title(self, value): """Is in the :py:data:`~nameparser.config.titles.TITLES` set.""" @@ -331,8 +345,8 @@ def is_suffix(self, piece): `C.suffix_acronyms`. """ # suffixes may have periods inside them like "M.D." - return ((lc(piece).replace('.','') in self.C.suffix_acronyms) \ - or (lc(piece) in self.C.suffix_not_acronyms)) \ + return ((lc(piece).replace('.', '') in self.C.suffix_acronyms) + or (lc(piece) in self.C.suffix_not_acronyms)) \ and not self.is_an_initial(piece) def are_suffixes(self, pieces): @@ -358,8 +372,7 @@ def is_an_initial(self, value): """ return bool(self.C.regexes.initial.match(value)) - - ### full_name parser + # full_name parser @property def full_name(self): @@ -376,7 +389,7 @@ def full_name(self, value): def collapse_whitespace(self, string): # collapse multiple spaces into single space - string = self.C.regexes.spaces.sub(" ", string.strip()) + string = self.C.regexes.spaces.sub(" ", string.strip()) if string.endswith(","): string = string[:-1] return string @@ -404,7 +417,7 @@ def post_process(self): self.handle_capitalization() def fix_phd(self): - _re = self.C.regexes.phd + _re = self.C.regexes.phd match = _re.search(self._full_name) if match: self.suffix_list.append(match.group(1)) @@ -442,15 +455,16 @@ def squash_emoji(self): def handle_firstnames(self): """ - If there are only two parts and one is a title, assume it's a last name + If there are only three parts and one is a title, assume it's a last name instead of a first name. e.g. Mr. Johnson. Unless it's a special title like "Sir", then when it's followed by a single name that name is always a first name. """ if self.title \ - and len(self) == 2 \ + and len(self) == 3 \ and not lc(self.title) in self.C.first_name_titles: self.last, self.first = self.first, self.last + self.initials_list = [] def parse_full_name(self): """ @@ -472,9 +486,9 @@ def parse_full_name(self): self.last_list = [] self.suffix_list = [] self.nickname_list = [] + self.initials_list = [] self.unparsable = True - self.pre_process() self._full_name = self.collapse_whitespace(self._full_name) @@ -486,7 +500,6 @@ def parse_full_name(self): log.debug("parts: %s", parts) if len(parts) == 1: - # no commas, title first middle middle middle last suffix # part[0] @@ -509,6 +522,8 @@ def parse_full_name(self): self.last_list.append(piece) continue self.first_list.append(piece) + if len(piece) > 0: + self.initials_list.append(piece[0]) continue if self.are_suffixes(pieces[i+1:]) or \ ( @@ -516,7 +531,7 @@ def parse_full_name(self): # numeral but this piece is not an initial self.is_roman_numeral(nxt) and i == p_len - 2 and not self.is_an_initial(piece) - ): + ): self.last_list.append(piece) self.suffix_list += pieces[i+1:] break @@ -525,6 +540,8 @@ def parse_full_name(self): continue self.middle_list.append(piece) + if len(piece) > 0: + self.initials_list.append(piece[0]) else: # if all the end parts are suffixes and there is more than one piece # in the first part. (Suffixes will never appear after last names @@ -540,7 +557,6 @@ def parse_full_name(self): # title first middle last [suffix], suffix [suffix] [, suffix] # parts[0], parts[1:...] - self.suffix_list += parts[1:] pieces = self.parse_pieces(parts[0].split(' ')) log.debug("pieces: %s", u(pieces)) @@ -557,6 +573,8 @@ def parse_full_name(self): continue if not self.first: self.first_list.append(piece) + if len(piece) > 0: + self.initials_list.append(piece[0]) continue if self.are_suffixes(pieces[i+1:]): self.last_list.append(piece) @@ -566,6 +584,8 @@ def parse_full_name(self): self.last_list.append(piece) continue self.middle_list.append(piece) + if len(piece) > 0: + self.initials_list.append(piece[0]) else: # lastname comma: @@ -597,11 +617,15 @@ def parse_full_name(self): continue if not self.first: self.first_list.append(piece) + if len(piece) > 0: + self.initials_list.append(piece[0]) continue if self.is_suffix(piece): self.suffix_list.append(piece) continue self.middle_list.append(piece) + if len(piece) > 0: + self.initials_list.append(piece[0]) try: if parts[2]: self.suffix_list += parts[2:] @@ -614,7 +638,6 @@ def parse_full_name(self): self.unparsable = False self.post_process() - def parse_pieces(self, parts, additional_parts_count=0): """ Split parts on spaces and remove commas, join on conjunctions and @@ -648,7 +671,7 @@ def parse_pieces(self, parts, additional_parts_count=0): # split on periods, any of the split pieces titles or suffixes? # ("Lt.Gov.") period_chunks = part.split(".") - titles = list(filter(self.is_title, period_chunks)) + titles = list(filter(self.is_title, period_chunks)) suffixes = list(filter(self.is_suffix, period_chunks)) # add the part to the constant so it will be found @@ -695,7 +718,7 @@ def join_on_conjunctions(self, pieces, additional_parts_count=0): # other, then join those newly joined conjunctions and any single # conjunctions to the piece before and after it conj_index = [i for i, piece in enumerate(pieces) - if self.is_conjunction(piece)] + if self.is_conjunction(piece)] contiguous_conj_i = [] for i, val in enumerate(conj_index): @@ -710,14 +733,14 @@ def join_on_conjunctions(self, pieces, additional_parts_count=0): delete_i = [] for i in contiguous_conj_i: if type(i) == tuple: - new_piece = " ".join(pieces[ i[0] : i[1]+1] ) - delete_i += list(range( i[0]+1, i[1]+1 )) + new_piece = " ".join(pieces[i[0]: i[1]+1]) + delete_i += list(range(i[0]+1, i[1]+1)) pieces[i[0]] = new_piece else: - new_piece = " ".join(pieces[ i : i+2 ]) + new_piece = " ".join(pieces[i: i+2]) delete_i += [i+1] pieces[i] = new_piece - #add newly joined conjunctions to constants to be found later + # add newly joined conjunctions to constants to be found later self.C.conjunctions.add(new_piece) for i in reversed(delete_i): @@ -747,9 +770,9 @@ def join_on_conjunctions(self, pieces, additional_parts_count=0): pieces[i] = new_piece pieces.pop(i+1) # subtract 1 from the index of all the remaining conjunctions - for j,val in enumerate(conj_index): + for j, val in enumerate(conj_index): if val > i: - conj_index[j]=val-1 + conj_index[j] = val-1 else: new_piece = " ".join(pieces[i-1:i+2]) @@ -766,11 +789,10 @@ def join_on_conjunctions(self, pieces, additional_parts_count=0): # subtract the number of removed pieces from the index # of all the remaining conjunctions - for j,val in enumerate(conj_index): + for j, val in enumerate(conj_index): if val > i: conj_index[j] = val - rm_count - # join prefixes to following lastnames: ['de la Vega'], ['van Buren'] prefixes = list(filter(self.is_prefix, pieces)) if prefixes: @@ -813,12 +835,11 @@ def join_on_conjunctions(self, pieces, additional_parts_count=0): log.debug("pieces: %s", pieces) return pieces - - ### Capitalization Support + # Capitalization Support def cap_word(self, word, attribute): - if (self.is_prefix(word) and attribute in ('last','middle')) \ - or self.is_conjunction(word): + if (self.is_prefix(word) and attribute in ('last', 'middle')) \ + or self.is_conjunction(word): return word.lower() exceptions = self.C.capitalization_exceptions if lc(word) in exceptions: @@ -834,7 +855,8 @@ def cap_after_mac(m): def cap_piece(self, piece, attribute): if not piece: return "" - replacement = lambda m: self.cap_word(m.group(0), attribute) + + def replacement(m): return self.cap_word(m.group(0), attribute) return self.C.regexes.word.sub(replacement, piece) def capitalize(self, force=None): @@ -872,11 +894,13 @@ def capitalize(self, force=None): if not force and not (name == name.upper() or name == name.lower()): return - self.title_list = self.cap_piece(self.title , 'title').split(' ') - self.first_list = self.cap_piece(self.first , 'first').split(' ') + + self.title_list = self.cap_piece(self.title, 'title').split(' ') + self.first_list = self.cap_piece(self.first, 'first').split(' ') self.middle_list = self.cap_piece(self.middle, 'middle').split(' ') - self.last_list = self.cap_piece(self.last , 'last').split(' ') + self.last_list = self.cap_piece(self.last, 'last').split(' ') self.suffix_list = self.cap_piece(self.suffix, 'suffix').split(', ') + self.initials_list = self.cap_piece(self.initials, 'initials').replace('.', '').split(' ') def handle_capitalization(self): """ diff --git a/tests.py b/tests.py index 5f976b8..e176634 100644 --- a/tests.py +++ b/tests.py @@ -1,5 +1,6 @@ # -*- coding: utf-8 -*- from __future__ import unicode_literals +import unittest """ Run this file to run the tests. @@ -30,7 +31,6 @@ log = logging.getLogger('HumanName') -import unittest try: unittest.expectedFailure except AttributeError: @@ -70,9 +70,9 @@ def test_escaped_utf8_bytes(self): def test_len(self): hn = HumanName("Doe-Ray, Dr. John P., CLU, CFP, LUTC") - self.m(len(hn), 5, hn) + self.m(len(hn), 6, hn) hn = HumanName("John Doe") - self.m(len(hn), 2, hn) + self.m(len(hn), 3, hn) @unittest.skipUnless(dill, "requires python-dill module to test pickling") def test_config_pickle(self): @@ -104,17 +104,18 @@ def test_assignment_to_full_name(self): self.m(hn.last, "Doe", hn) self.m(hn.middle, "A. Kenneth", hn) self.m(hn.suffix, "Jr.", hn) + self.m(hn.initials, "J. A. K.", hn) hn.full_name = "Juan Velasquez y Garcia III" self.m(hn.first, "Juan", hn) self.m(hn.last, "Velasquez y Garcia", hn) self.m(hn.suffix, "III", hn) + self.m(hn.initials, "J.", hn) def test_get_full_name_attribute_references_internal_lists(self): hn = HumanName("John Williams") hn.first_list = ["Larry"] self.m(hn.full_name, "Larry Williams", hn) - def test_assignment_to_attribute(self): hn = HumanName("John A. Kenneth Doe, Jr.") hn.last = "de la Vega" @@ -154,9 +155,9 @@ def test_comparison_case_insensitive(self): def test_slice(self): hn = HumanName("Doe-Ray, Dr. John P., CLU, CFP, LUTC") - self.m(list(hn), ['Dr.', 'John', 'P.', 'Doe-Ray', 'CLU, CFP, LUTC'], hn) - self.m(hn[1:], ['John', 'P.', 'Doe-Ray', 'CLU, CFP, LUTC', hn.C.empty_attribute_default], hn) - self.m(hn[1:-2], ['John', 'P.', 'Doe-Ray'], hn) + self.m(list(hn), ['Dr.', 'J. P.', 'John', 'P.', 'Doe-Ray', 'CLU, CFP, LUTC'], hn) + self.m(hn[1:], ['J. P.', 'John', 'P.', 'Doe-Ray', 'CLU, CFP, LUTC', hn.C.empty_attribute_default], hn) + self.m(hn[1:-2], ['J. P.', 'John', 'P.', 'Doe-Ray'], hn) def test_getitem(self): hn = HumanName("Dr. John A. Kenneth Doe, Jr.") @@ -210,16 +211,16 @@ def test_assume_title_and_one_other_name_is_last_name(self): hn = HumanName("Rev Andrews") self.m(hn.title, "Rev", hn) self.m(hn.last, "Andrews", hn) - + # TODO: Seems "Andrews, M.D.", Andrews should be treated as a last name - # but other suffixes like "George Jr." should be first names. Might be + # but other suffixes like "George Jr." should be first names. Might be # related to https://github.com/derek73/python-nameparser/issues/2 @unittest.expectedFailure def test_assume_suffix_title_and_one_other_name_is_last_name(self): hn = HumanName("Andrews, M.D.") self.m(hn.suffix, "M.D.", hn) self.m(hn.last, "Andrews", hn) - + def test_suffix_in_lastname_part_of_lastname_comma_format(self): hn = HumanName("Smith Jr., John") self.m(hn.last, "Smith", hn) @@ -230,22 +231,22 @@ def test_sir_exception_to_first_name_rule(self): hn = HumanName("Sir Gerald") self.m(hn.title, "Sir", hn) self.m(hn.first, "Gerald", hn) - + def test_king_exception_to_first_name_rule(self): hn = HumanName("King Henry") self.m(hn.title, "King", hn) self.m(hn.first, "Henry", hn) - + def test_queen_exception_to_first_name_rule(self): hn = HumanName("Queen Elizabeth") self.m(hn.title, "Queen", hn) self.m(hn.first, "Elizabeth", hn) - + def test_dame_exception_to_first_name_rule(self): hn = HumanName("Dame Mary") self.m(hn.title, "Dame", hn) self.m(hn.first, "Mary", hn) - + def test_first_name_is_not_prefix_if_only_two_parts(self): """When there are only two parts, don't join prefixes or conjunctions""" hn = HumanName("Van Nguyen") @@ -263,7 +264,7 @@ def test_first_name_is_prefix_if_three_parts(self): hn = HumanName("Mr. Van Nguyen") self.m(hn.first, "Van", hn) self.m(hn.last, "Nguyen", hn) - + class HumanNameBruteForceTests(HumanNameTestBase): @@ -1084,7 +1085,7 @@ def test_multiple_conjunctions(self): def test_multiple_conjunctions2(self): hn = HumanName("part1 of and The part2 of the part3 And part4") self.m(hn.first, "part1 of and The part2 of the part3 And part4", hn) - + def test_ends_with_conjunction(self): hn = HumanName("Jon Dough and") self.m(hn.first, "Jon", hn) @@ -1242,12 +1243,12 @@ def test_le_as_last_name_with_middle_initial(self): self.m(hn.first, "Yin", hn) self.m(hn.middle, "a", hn) self.m(hn.last, "Le", hn) - + def test_conjunction_in_an_address_with_a_title(self): hn = HumanName("His Excellency Lord Duncan") self.m(hn.title, "His Excellency Lord", hn) self.m(hn.last, "Duncan", hn) - + @unittest.expectedFailure def test_conjunction_in_an_address_with_a_first_name_title(self): hn = HumanName("Her Majesty Queen Elizabeth") @@ -1272,7 +1273,7 @@ def test_add_title(self): self.m(hn.title, "Te", hn) self.m(hn.first, "Awanui-a-Rangi", hn) self.m(hn.last, "Black", hn) - + def test_remove_title(self): hn = HumanName("Hon Solo", constants=None) start_len = len(hn.C.titles) @@ -1282,7 +1283,7 @@ def test_remove_title(self): hn.parse_full_name() self.m(hn.first, "Hon", hn) self.m(hn.last, "Solo", hn) - + def test_add_multiple_arguments(self): hn = HumanName("Assoc Dean of Chemistry Robert Johns", constants=None) hn.C.titles.add('dean', 'Chemistry') @@ -1310,7 +1311,7 @@ def test_can_change_global_constants(self): self.assertEqual(hn2.has_own_config, False) # clean up so we don't mess up other tests hn.C.titles.add('hon') - + def test_remove_multiple_arguments(self): hn = HumanName("Ms Hon Solo", constants=None) hn.C.titles.remove('hon', 'ms') @@ -1370,7 +1371,7 @@ def test_nickname_in_parenthesis(self): self.m(hn.middle, "", hn) self.m(hn.last, "Franklin", hn) self.m(hn.nickname, "Ben", hn) - + def test_two_word_nickname_in_parenthesis(self): hn = HumanName("Benjamin (Big Ben) Franklin") self.m(hn.first, "Benjamin", hn) @@ -1391,7 +1392,7 @@ def test_nickname_in_parenthesis_with_comma(self): self.m(hn.middle, "", hn) self.m(hn.last, "Franklin", hn) self.m(hn.nickname, "Ben", hn) - + def test_nickname_in_parenthesis_with_comma_and_suffix(self): hn = HumanName("Franklin, Benjamin (Ben), Jr.") self.m(hn.first, "Benjamin", hn) @@ -1399,7 +1400,7 @@ def test_nickname_in_parenthesis_with_comma_and_suffix(self): self.m(hn.last, "Franklin", hn) self.m(hn.suffix, "Jr.", hn) self.m(hn.nickname, "Ben", hn) - + def test_nickname_in_single_quotes(self): hn = HumanName("Benjamin 'Ben' Franklin") self.m(hn.first, "Benjamin", hn) @@ -1413,28 +1414,28 @@ def test_nickname_in_double_quotes(self): self.m(hn.middle, "", hn) self.m(hn.last, "Franklin", hn) self.m(hn.nickname, "Ben", hn) - + def test_single_quotes_on_first_name_not_treated_as_nickname(self): hn = HumanName("Brian Andrew O'connor") self.m(hn.first, "Brian", hn) self.m(hn.middle, "Andrew", hn) self.m(hn.last, "O'connor", hn) self.m(hn.nickname, "", hn) - + def test_single_quotes_on_both_name_not_treated_as_nickname(self): hn = HumanName("La'tanya O'connor") self.m(hn.first, "La'tanya", hn) self.m(hn.middle, "", hn) self.m(hn.last, "O'connor", hn) self.m(hn.nickname, "", hn) - + def test_single_quotes_on_end_of_last_name_not_treated_as_nickname(self): hn = HumanName("Mari' Aube'") self.m(hn.first, "Mari'", hn) self.m(hn.middle, "", hn) self.m(hn.last, "Aube'", hn) self.m(hn.nickname, "", hn) - + def test_okina_inside_name_not_treated_as_nickname(self): hn = HumanName("Harrieta Keōpūolani Nāhiʻenaʻena") self.m(hn.first, "Harrieta", hn) @@ -1492,7 +1493,6 @@ def test_nickname_and_last_name_with_title(self): self.m(hn.nickname, "Rick", hn) - # class MaidenNameTestCase(HumanNameTestBase): # # def test_parenthesis_and_quotes_together(self): @@ -1542,12 +1542,12 @@ def test_prefix(self): hn = HumanName("Juan del Sur") self.m(hn.first, "Juan", hn) self.m(hn.last, "del Sur", hn) - + def test_prefix_with_period(self): hn = HumanName("Jill St. John") self.m(hn.first, "Jill", hn) self.m(hn.last, "St. John", hn) - + def test_prefix_before_two_part_last_name(self): hn = HumanName("pennie von bergen wessels") self.m(hn.first, "pennie", hn) @@ -1641,7 +1641,7 @@ def test_comma_three_conjunctions(self): class SuffixesTestCase(HumanNameTestBase): - + def test_suffix(self): hn = HumanName("Joe Franklin Jr") self.m(hn.first, "Joe", hn) @@ -1716,13 +1716,13 @@ def test_phd_conflict(self): self.m(hn.first, "Adolph", hn) self.m(hn.last, "D", hn) - # http://en.wikipedia.org/wiki/Ma_(surname) + def test_potential_suffix_that_is_also_last_name(self): hn = HumanName("Jack Ma") self.m(hn.first, "Jack", hn) self.m(hn.last, "Ma", hn) - + def test_potential_suffix_that_is_also_last_name_comma(self): hn = HumanName("Ma, Jack") self.m(hn.first, "Jack", hn) @@ -1820,27 +1820,27 @@ def test_chained_title_first_name_title_is_initials(self): self.m(hn.first, "Marc", hn) self.m(hn.middle, "Thomas", hn) self.m(hn.last, "Treadwell", hn) - + def test_conflict_with_chained_title_first_name_initial(self): hn = HumanName("U. S. Grant") self.m(hn.first, "U.", hn) self.m(hn.middle, "S.", hn) self.m(hn.last, "Grant", hn) - + def test_chained_title_first_name_initial_with_no_period(self): hn = HumanName("US Magistrate Judge T Michael Putnam") self.m(hn.title, "US Magistrate Judge", hn) self.m(hn.first, "T", hn) self.m(hn.middle, "Michael", hn) self.m(hn.last, "Putnam", hn) - + def test_chained_hyphenated_title(self): hn = HumanName("US Magistrate-Judge Elizabeth E Campbell") self.m(hn.title, "US Magistrate-Judge", hn) self.m(hn.first, "Elizabeth", hn) self.m(hn.middle, "E", hn) self.m(hn.last, "Campbell", hn) - + def test_chained_hyphenated_title_with_comma_suffix(self): hn = HumanName("Mag-Judge Harwell G Davis, III") self.m(hn.title, "Mag-Judge", hn) @@ -1883,7 +1883,7 @@ def test_title_with_last_initial_is_suffix(self): self.m(hn.title, "King", hn) self.m(hn.first, "John", hn) self.m(hn.last, "V.", hn) - + def test_initials_also_suffix(self): hn = HumanName("Smith, J.R.") self.m(hn.first, "J.R.", hn) @@ -2062,10 +2062,10 @@ def test_capitalize_prefix_clash_on_first_name(self): class HumanNameOutputFormatTests(HumanNameTestBase): - + def test_formatting_init_argument(self): hn = HumanName("Rev John A. Kenneth Doe III (Kenny)", - string_format="TEST1") + string_format="TEST1") self.assertEqual(u(hn), "TEST1") def test_formatting_constants_attribute(self): @@ -2160,7 +2160,7 @@ def test_formating_of_nicknames_in_middle(self): self.assertEqual(u(hn), "Rev John (Kenny) A. Kenneth Doe III") hn.nickname = '' self.assertEqual(u(hn), "Rev John A. Kenneth Doe III") - + def test_remove_emojis(self): hn = HumanName("Sam Smith 😊") self.m(hn.first, "Sam", hn) @@ -2359,7 +2359,7 @@ def test_keep_emojis(self): "U.S. District Judge Marc Thomas Treadwell", "Dra. Andréia da Silva", "Srta. Andréia da Silva", - + ) From 155d608a38d0b6f768a9be7e38d83632f81f0769 Mon Sep 17 00:00:00 2001 From: Rink Stiekema Date: Wed, 20 Oct 2021 15:54:00 +0200 Subject: [PATCH 41/99] Remove initials from members and process initials upon post processing and setting first/middle names --- nameparser/parser.py | 37 ++++++++++------- tests.py | 99 +++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 115 insertions(+), 21 deletions(-) diff --git a/nameparser/parser.py b/nameparser/parser.py index e58a428..38b420d 100644 --- a/nameparser/parser.py +++ b/nameparser/parser.py @@ -69,7 +69,7 @@ class HumanName(object): """ _count = 0 - _members = ['title', 'initials', 'first', 'middle', 'last', 'suffix', 'nickname'] + _members = ['title', 'first', 'middle', 'last', 'suffix', 'nickname'] unparsable = True _full_name = '' @@ -267,7 +267,7 @@ def initials(self): """" A string of all initials """ - return " ".join([initial + "." for initial in self.initials_list]) + return " ".join([initial + "." for initial in self.initials_list]) or self.C.empty_attribute_default # setter methods @@ -291,10 +291,12 @@ def title(self, value): @first.setter def first(self, value): self._set_list('first', value) + self.handle_initials() @middle.setter def middle(self, value): self._set_list('middle', value) + self.handle_initials() @last.setter def last(self, value): @@ -414,6 +416,7 @@ def post_process(self): and :py:func:`handle_capitalization`. """ self.handle_firstnames() + self.handle_initials() self.handle_capitalization() def fix_phd(self): @@ -461,11 +464,26 @@ def handle_firstnames(self): a first name. """ if self.title \ - and len(self) == 3 \ + and len(self) == 2 \ and not lc(self.title) in self.C.first_name_titles: self.last, self.first = self.first, self.last self.initials_list = [] + def handle_initials(self): + """ + Initials are the concatination of the first letter of the first name and the first character of each middle name + """ + initials_list = [] + if self.first and len(self.first): + initials_list += [self.first[0]] + + if self.middle_list and len(self.middle_list): + for middle in self.middle_list: + if len(middle): + initials_list += [middle[0]] + + self.initials_list = initials_list + def parse_full_name(self): """ @@ -486,7 +504,6 @@ def parse_full_name(self): self.last_list = [] self.suffix_list = [] self.nickname_list = [] - self.initials_list = [] self.unparsable = True self.pre_process() @@ -522,8 +539,6 @@ def parse_full_name(self): self.last_list.append(piece) continue self.first_list.append(piece) - if len(piece) > 0: - self.initials_list.append(piece[0]) continue if self.are_suffixes(pieces[i+1:]) or \ ( @@ -540,8 +555,6 @@ def parse_full_name(self): continue self.middle_list.append(piece) - if len(piece) > 0: - self.initials_list.append(piece[0]) else: # if all the end parts are suffixes and there is more than one piece # in the first part. (Suffixes will never appear after last names @@ -573,8 +586,6 @@ def parse_full_name(self): continue if not self.first: self.first_list.append(piece) - if len(piece) > 0: - self.initials_list.append(piece[0]) continue if self.are_suffixes(pieces[i+1:]): self.last_list.append(piece) @@ -584,8 +595,6 @@ def parse_full_name(self): self.last_list.append(piece) continue self.middle_list.append(piece) - if len(piece) > 0: - self.initials_list.append(piece[0]) else: # lastname comma: @@ -617,15 +626,11 @@ def parse_full_name(self): continue if not self.first: self.first_list.append(piece) - if len(piece) > 0: - self.initials_list.append(piece[0]) continue if self.is_suffix(piece): self.suffix_list.append(piece) continue self.middle_list.append(piece) - if len(piece) > 0: - self.initials_list.append(piece[0]) try: if parts[2]: self.suffix_list += parts[2:] diff --git a/tests.py b/tests.py index e176634..89b302d 100644 --- a/tests.py +++ b/tests.py @@ -59,6 +59,7 @@ def test_utf8(self): hn = HumanName("de la Véña, Jüan") self.m(hn.first, "Jüan", hn) self.m(hn.last, "de la Véña", hn) + self.m(hn.initials, "J.", hn) def test_string_output(self): hn = HumanName("de la Véña, Jüan") @@ -67,12 +68,13 @@ def test_escaped_utf8_bytes(self): hn = HumanName(b'B\xc3\xb6ck, Gerald') self.m(hn.first, "Gerald", hn) self.m(hn.last, "Böck", hn) + self.m(hn.initials, "G.", hn) def test_len(self): hn = HumanName("Doe-Ray, Dr. John P., CLU, CFP, LUTC") - self.m(len(hn), 6, hn) + self.m(len(hn), 5, hn) hn = HumanName("John Doe") - self.m(len(hn), 3, hn) + self.m(len(hn), 2, hn) @unittest.skipUnless(dill, "requires python-dill module to test pickling") def test_config_pickle(self): @@ -120,12 +122,15 @@ def test_assignment_to_attribute(self): hn = HumanName("John A. Kenneth Doe, Jr.") hn.last = "de la Vega" self.m(hn.last, "de la Vega", hn) + self.m(hn.initials, "J. A. K.", hn) hn.title = "test" self.m(hn.title, "test", hn) hn.first = "test" self.m(hn.first, "test", hn) + self.m(hn.initials, "t. A. K.", hn) hn.middle = "test" self.m(hn.middle, "test", hn) + self.m(hn.initials, "t. t.", hn) hn.suffix = "test" self.m(hn.suffix, "test", hn) with self.assertRaises(TypeError): @@ -155,9 +160,9 @@ def test_comparison_case_insensitive(self): def test_slice(self): hn = HumanName("Doe-Ray, Dr. John P., CLU, CFP, LUTC") - self.m(list(hn), ['Dr.', 'J. P.', 'John', 'P.', 'Doe-Ray', 'CLU, CFP, LUTC'], hn) - self.m(hn[1:], ['J. P.', 'John', 'P.', 'Doe-Ray', 'CLU, CFP, LUTC', hn.C.empty_attribute_default], hn) - self.m(hn[1:-2], ['J. P.', 'John', 'P.', 'Doe-Ray'], hn) + self.m(list(hn), ['Dr.', 'John', 'P.', 'Doe-Ray', 'CLU, CFP, LUTC'], hn) + self.m(hn[1:], ['John', 'P.', 'Doe-Ray', 'CLU, CFP, LUTC', hn.C.empty_attribute_default], hn) + self.m(hn[1:-2], ['John', 'P.', 'Doe-Ray'], hn) def test_getitem(self): hn = HumanName("Dr. John A. Kenneth Doe, Jr.") @@ -166,6 +171,7 @@ def test_getitem(self): self.m(hn['last'], "Doe", hn) self.m(hn['middle'], "A. Kenneth", hn) self.m(hn['suffix'], "Jr.", hn) + self.m(hn.initials, "J. A. K.", hn) def test_setitem(self): hn = HumanName("Dr. John A. Kenneth Doe, Jr.") @@ -182,16 +188,19 @@ def test_conjunction_names(self): hn = HumanName("johnny y") self.m(hn.first, "johnny", hn) self.m(hn.last, "y", hn) + self.m(hn.initials, "j.", hn) def test_prefix_names(self): hn = HumanName("vai la") self.m(hn.first, "vai", hn) self.m(hn.last, "la", hn) + self.m(hn.initials, "v.", hn) def test_blank_name(self): hn = HumanName() self.m(hn.first, "", hn) self.m(hn.last, "", hn) + self.m(hn.initials, "", hn) def test_surnames_list_attribute(self): hn = HumanName("John Edgar Casey Williams III") @@ -201,6 +210,10 @@ def test_surnames_attribute(self): hn = HumanName("John Edgar Casey Williams III") self.m(hn.surnames, "Edgar Casey Williams", hn) + def test_initials_list_attribute(self): + hn = HumanName("John Edgar Casey Williams III") + self.m(hn.initials_list, ["J", "E", "C"], hn) + class FirstNameHandlingTests(HumanNameTestBase): def test_first_name(self): @@ -2184,6 +2197,82 @@ def test_keep_emojis(self): # test cleanup +class InitialsTestCase(HumanNameTestBase): + def test_initials(self): + hn = HumanName("Andrew Boris Petersen") + self.m(hn.initials, "A. B.", hn) + self.m(hn.initials_list, ["A", "B"], hn) + + def test_title_and_last_name(self): + hn = HumanName("Dr. Andrews") + self.m(hn.initials, "", hn) + self.m(hn.initials, [], hn) + + def test_reassignment_first_name(self): + hn = HumanName("Andrew Boris Petersen") + hn.first = "John" + self.m(hn.initials, "J. B.", hn) + self.m(hn.initials_list, ["J", "B"], hn) + + def test_reassignment_middle_names(self): + hn = HumanName("Andrew Boris Petersen") + hn.middle = "John" + self.m(hn.initials, "A. J.", hn) + self.m(hn.initials_list, ["A", "J"], hn) + + def test_reassignment_middle_names_list(self): + hn = HumanName("Andrew Boris Petersen") + hn.middle = ["John", "Peter"] + self.m(hn.initials, "A. J. P.", hn) + self.m(hn.initials_list, ["A", "J", "P"], hn) + + def test_capitalization(self): + hn = HumanName("andrew boris Petersen") + self.m(hn.initials, "a. b.", hn) + self.m(hn.initials_list, ["a", "b"], hn) + hn.capitalize(force=True) + self.m(hn.initials, "A. B.", hn) + self.m(hn.initials_list, ["A", "B"], hn) + + def test_parse_initial(self): + hn = HumanName("A. Petersen") + self.m(hn.initials, "A.", hn) + self.m(hn.initials_list, ["A"], hn) + + def test_parse_multiple_initials(self): + hn = HumanName("A. B. Petersen") + self.m(hn.initials, "A. B.", hn) + self.m(hn.initials_list, ["A", "B"], hn) + + def test_parse_mixed_initials(self): + hn1 = HumanName("Andrew B. Petersen") + self.m(hn1.initials, "A. B.", hn1) + self.m(hn1.initials_list, ["A", "B"], hn1) + + hn2 = HumanName("A. Boris Petersen") + self.m(hn2.initials, "A. B.", hn2) + self.m(hn2.initials_list, ["A", "B"], hn2) + + def test_parse_commas(self): + hn = HumanName("Petersen, Andrew Boris") + self.m(hn.initials, "A. B.", hn) + self.m(hn.initials_list, ["A", "B"], hn) + + def test_parse_commas_initials(self): + hn = HumanName("Petersen, A. B.") + self.m(hn.initials, "A. B.", hn) + self.m(hn.initials_list, ["A", "B"], hn) + + def test_parse_commas_mixed_initials(self): + hn1 = HumanName("Petersen, Andrew B.") + self.m(hn1.initials, "A. B.", hn1) + self.m(hn1.initials_list, ["A", "B"], hn1) + + hn2 = HumanName("Petersen, A. Boris") + self.m(hn2.initials, "A. B.", hn2) + self.m(hn2.initials_list, ["A", "B"], hn2) + + TEST_NAMES = ( "John Doe", "John Doe, Jr.", From 9893a538810984980403f69931294d32e313dfd4 Mon Sep 17 00:00:00 2001 From: Rink Stiekema Date: Wed, 20 Oct 2021 16:11:16 +0200 Subject: [PATCH 42/99] Added initials to members but remove from string --- README.rst | 4 +++- nameparser/parser.py | 12 +++++++----- tests.py | 10 +++++----- 3 files changed, 15 insertions(+), 11 deletions(-) diff --git a/README.rst b/README.rst index b347593..e47af60 100644 --- a/README.rst +++ b/README.rst @@ -13,6 +13,7 @@ individual components. * hn.suffix * hn.nickname * hn.surnames *(middle + last)* +* hn.initials Supported Name Structures ~~~~~~~~~~~~~~~~~~~~~~~~~ @@ -65,6 +66,7 @@ Quick Start Example >>> name >> name.last 'de la Vega' >>> name.as_dict() - {'last': 'de la Vega', 'suffix': 'III', 'title': 'Dr.', 'middle': 'Q. Xavier', 'nickname': 'Doc Vega', 'first': 'Juan'} + {'title': 'Dr.', 'first': 'Juan', 'middle': 'Q. Xavier', 'last': 'de la Vega', 'suffix': 'III', 'nickname': 'Doc Vega', 'initials': 'J. Q. X.'} >>> str(name) 'Dr. Juan Q. Xavier de la Vega III (Doc Vega)' >>> name.string_format = "{first} {last}" diff --git a/nameparser/parser.py b/nameparser/parser.py index 38b420d..a0a7fe3 100644 --- a/nameparser/parser.py +++ b/nameparser/parser.py @@ -69,7 +69,7 @@ class HumanName(object): """ _count = 0 - _members = ['title', 'first', 'middle', 'last', 'suffix', 'nickname'] + _members = ['title', 'first', 'middle', 'last', 'suffix', 'nickname', 'initials'] unparsable = True _full_name = '' @@ -130,7 +130,9 @@ def __next__(self): def __unicode__(self): if self.string_format: # string_format = "{title} {first} {middle} {last} {suffix} ({nickname})" - _s = self.string_format.format(**self.as_dict()) + dict_representation = self.as_dict() + dict_representation.pop('initials', None) + _s = self.string_format.format(**dict_representation) # remove trailing punctuation from missing nicknames _s = _s.replace(str(self.C.empty_attribute_default), '').replace(" ()", "").replace(" ''", "").replace(' ""', "") return self.collapse_whitespace(_s).strip(', ') @@ -170,10 +172,9 @@ def as_dict(self, include_empty=True): >>> name = HumanName("Bob Dole") >>> name.as_dict() - {'last': 'Dole', 'suffix': '', 'title': '', 'middle': '', 'nickname': '', 'first': 'Bob'} + {'last': 'Dole', 'suffix': '', 'title': '', 'middle': '', 'nickname': '', 'first': 'Bob', 'initials': 'B.'} >>> name.as_dict(False) - {'last': 'Dole', 'first': 'Bob'} - + {'last': 'Dole', 'first': 'Bob', 'initials': 'B.'} """ d = {} for m in self._members: @@ -504,6 +505,7 @@ def parse_full_name(self): self.last_list = [] self.suffix_list = [] self.nickname_list = [] + self.initials_list = [] self.unparsable = True self.pre_process() diff --git a/tests.py b/tests.py index 89b302d..4efefbf 100644 --- a/tests.py +++ b/tests.py @@ -72,9 +72,9 @@ def test_escaped_utf8_bytes(self): def test_len(self): hn = HumanName("Doe-Ray, Dr. John P., CLU, CFP, LUTC") - self.m(len(hn), 5, hn) + self.m(len(hn), 6, hn) hn = HumanName("John Doe") - self.m(len(hn), 2, hn) + self.m(len(hn), 3, hn) @unittest.skipUnless(dill, "requires python-dill module to test pickling") def test_config_pickle(self): @@ -160,9 +160,9 @@ def test_comparison_case_insensitive(self): def test_slice(self): hn = HumanName("Doe-Ray, Dr. John P., CLU, CFP, LUTC") - self.m(list(hn), ['Dr.', 'John', 'P.', 'Doe-Ray', 'CLU, CFP, LUTC'], hn) - self.m(hn[1:], ['John', 'P.', 'Doe-Ray', 'CLU, CFP, LUTC', hn.C.empty_attribute_default], hn) - self.m(hn[1:-2], ['John', 'P.', 'Doe-Ray'], hn) + self.m(list(hn), ['Dr.', 'John', 'P.', 'Doe-Ray', 'CLU, CFP, LUTC', 'J. P.'], hn) + self.m(hn[1:], ['John', 'P.', 'Doe-Ray', 'CLU, CFP, LUTC', hn.C.empty_attribute_default, 'J. P.'], hn) + self.m(hn[1:-3], ['John', 'P.', 'Doe-Ray'], hn) def test_getitem(self): hn = HumanName("Dr. John A. Kenneth Doe, Jr.") From 29b3e4fbab0eca67c450a09135e100231e6d7ae5 Mon Sep 17 00:00:00 2001 From: Rink Stiekema Date: Wed, 20 Oct 2021 16:31:51 +0200 Subject: [PATCH 43/99] Fix docstring of handle_firstnames() --- nameparser/parser.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/nameparser/parser.py b/nameparser/parser.py index a0a7fe3..b1275f3 100644 --- a/nameparser/parser.py +++ b/nameparser/parser.py @@ -459,7 +459,7 @@ def squash_emoji(self): def handle_firstnames(self): """ - If there are only three parts and one is a title, assume it's a last name + If there are only two parts and one is a title, assume it's a last name instead of a first name. e.g. Mr. Johnson. Unless it's a special title like "Sir", then when it's followed by a single name that name is always a first name. From 26829053830178ce00758ad54a5db67ebab3d7c3 Mon Sep 17 00:00:00 2001 From: Rink Stiekema Date: Wed, 20 Oct 2021 16:53:45 +0200 Subject: [PATCH 44/99] Change length function to exclude derived property initials from count --- nameparser/parser.py | 5 ++++- tests.py | 4 ++-- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/nameparser/parser.py b/nameparser/parser.py index b1275f3..64e5a25 100644 --- a/nameparser/parser.py +++ b/nameparser/parser.py @@ -88,7 +88,10 @@ def __iter__(self): return self def __len__(self): - l = 0 + """ + Initials are a derived value, so should not be considered for the length + """ + l = 0 if not self.initials else -1 for x in self: l += 1 return l diff --git a/tests.py b/tests.py index 4efefbf..168654e 100644 --- a/tests.py +++ b/tests.py @@ -72,9 +72,9 @@ def test_escaped_utf8_bytes(self): def test_len(self): hn = HumanName("Doe-Ray, Dr. John P., CLU, CFP, LUTC") - self.m(len(hn), 6, hn) + self.m(len(hn), 5, hn) hn = HumanName("John Doe") - self.m(len(hn), 3, hn) + self.m(len(hn), 2, hn) @unittest.skipUnless(dill, "requires python-dill module to test pickling") def test_config_pickle(self): From 7d7ac9ce92c2bd9908769d04517aa0433bf7ff8c Mon Sep 17 00:00:00 2001 From: Rink Stiekema Date: Thu, 21 Oct 2021 12:07:43 +0200 Subject: [PATCH 45/99] Added functions and which return the initials of the name --- docs/usage.rst | 26 ++++++ nameparser/config/__init__.py | 144 +++++++++++++++++++++++-------- nameparser/parser.py | 119 +++++++++++++++++-------- tests.py | 158 ++++++++++++++++++++++++++-------- 4 files changed, 337 insertions(+), 110 deletions(-) diff --git a/docs/usage.rst b/docs/usage.rst index 6a65c4e..01beb48 100644 --- a/docs/usage.rst +++ b/docs/usage.rst @@ -176,3 +176,29 @@ Don't want to include nicknames in your output? No problem. Just omit that keywo 'Dr. Juan de la Vega' +Initials Support +---------------- + +The HumanName class can try to get the correct representation of initials. +Initials can be tricky as different format usages exist. +If you want to exclude on of the name parts from the initials, you can use one of the following boolean parameters: +`exclude_last_name`, `exclude_middle_name` or `exclude_first_name` + +You can also force the behavior using the CONSTANTS: +:py:attr:`~nameparser.config.Constants.force_exclude_last_name` +:py:attr:`~nameparser.config.Constants.force_exclude_middle_name` +:py:attr:`~nameparser.config.Constants.force_exclude_first_name` + +Furthermore, the delimiter for the string output can be set through: +:py:attr:`~nameparser.config.Constants.initials_delimiter` + +.. doctest:: initials + + >>> name = HumanName("Doe, John A. Kenneth, Jr.") + >>> name.initials() + 'J. A. K. D.' + >>> name.initials(exclude_last_name) + 'J. A. K.' + >>> name.initials_list(exclude_middle_name): + ['J', 'D'] + diff --git a/nameparser/config/__init__.py b/nameparser/config/__init__.py index 4f1e4f2..488c899 100644 --- a/nameparser/config/__init__.py +++ b/nameparser/config/__init__.py @@ -49,35 +49,37 @@ DEFAULT_ENCODING = 'UTF-8' + class SetManager(Set): ''' Easily add and remove config variables per module or instance. Subclass of ``collections.abc.Set``. - + Only special functionality beyond that provided by set() is to normalize constants for comparison (lower case, no periods) when they are add()ed and remove()d and allow passing multiple string arguments to the :py:func:`add()` and :py:func:`remove()` methods. - + ''' + def __init__(self, elements): self.elements = set(elements) - + def __call__(self): return self.elements - + def __repr__(self): - return "SetManager({})".format(self.elements) # used for docs - + return "SetManager({})".format(self.elements) # used for docs + def __iter__(self): return iter(self.elements) - + def __contains__(self, value): return value in self.elements - + def __len__(self): return len(self.elements) - + def next(self): return self.__next__() @@ -89,7 +91,7 @@ def __next__(self): c = self.count self.count = c + 1 return getattr(self, self.elements[c]) or next(self) - + def add_with_encoding(self, s, encoding=None): """ Add the lower case and no-period version of the string to the set. Pass an @@ -111,7 +113,7 @@ def add(self, *strings): """ [self.add_with_encoding(s) for s in strings] return self - + def remove(self, *strings): """ Remove the lower case and no-period version of the string arguments from the set. @@ -126,10 +128,11 @@ class TupleManager(dict): A dictionary with dot.notation access. Subclass of ``dict``. Makes the tuple constants more friendly. ''' + def __getattr__(self, attr): return self.get(attr) - __setattr__= dict.__setitem__ - __delattr__= dict.__delitem__ + __setattr__ = dict.__setitem__ + __delattr__ = dict.__delitem__ def __getstate__(self): return dict(self) @@ -140,6 +143,7 @@ def __setstate__(self, state): def __reduce__(self): return (TupleManager, (), self.__getstate__()) + class Constants(object): """ An instance of this class hold all of the configuration constants for the parser. @@ -163,7 +167,7 @@ class Constants(object): :param regexes: :py:attr:`regexes` wrapped with :py:class:`TupleManager`. """ - + string_format = "{title} {first} {middle} {last} {suffix} ({nickname})" """ The default string format use for all new `HumanName` instances. @@ -183,6 +187,7 @@ class Constants(object): 'John' """ + capitalize_name = False """ If set, applies :py:meth:`~nameparser.parser.HumanName.capitalize` to @@ -197,6 +202,24 @@ class Constants(object): 'Bob V. de la MacDole-Eisenhower Ph.D.' """ + + initials_delimiter = '.' + """" + Determines how the initials from :py:meth:`~nameparser.parser.HumanName.initials` are seperated. + + .. doctest:: + + >>> from nameparser.config import CONSTANTS + >>> HumanName('Shirley Maclaine').initials() + 'S. M.' + >>> CONSTANTS.initials_delimiter = '' + >>> HumanName('Shirley Maclaine').initials() + 'S M' + >>> CONSTANTS.initials_delimiter = '-' + >>> HumanName('Shirley Maclaine').initials() + 'S- M-' + """ + force_mixed_case_capitalization = False """ If set, forces the capitalization of mixed case strings when @@ -213,27 +236,77 @@ class Constants(object): """ + force_exclude_last_name_initial = False + """ + If True, forces the last name to be excluded in the initials when + :py:meth:`~nameparser.parser.HumanName.initials` or + :py:meth:`~nameparser.parser.HumanName.initials_list` is called. + + .. doctest:: + + >>> from nameparser.config import CONSTANTS + >>> CONSTANTS.force_exclude_last_name_initial = True + >>> name = HumanName('Shirley Ashley Maclaine') + >>> name.initials() + 'S. A.' + >>> name.initials_list() + ['S', 'A'] + """ + + force_exclude_middle_name_initial = False + """ + If True, forces the middle name to be included in the initials when + :py:meth:`~nameparser.parser.HumanName.initials` or + :py:meth:`~nameparser.parser.HumanName.initials_list` is called. + + .. doctest:: + + >>> from nameparser.config import CONSTANTS + >>> CONSTANTS.force_exclude_middle_name_initial = True + >>> name = HumanName('Shirley Ashley Maclaine') + >>> name.initials() + 'S. M.' + >>> name.initials_list() + ['S', 'M'] + """ + + force_exclude_first_name_initial = False + """ + If True, forces the first name to be included in the initials when + :py:meth:`~nameparser.parser.HumanName.initials` or + :py:meth:`~nameparser.parser.HumanName.initials_list` is called. + + .. doctest:: + + >>> from nameparser.config import CONSTANTS + >>> CONSTANTS.force_exclude_first_name_initial = True + >>> name = HumanName('Shirley Ashley Maclaine') + >>> name.initials() + 'A. M.' + >>> name.initials_list() + ['A', 'M'] + """ - def __init__(self, - prefixes=PREFIXES, - suffix_acronyms=SUFFIX_ACRONYMS, - suffix_not_acronyms=SUFFIX_NOT_ACRONYMS, - titles=TITLES, - first_name_titles=FIRST_NAME_TITLES, - conjunctions=CONJUNCTIONS, - capitalization_exceptions=CAPITALIZATION_EXCEPTIONS, - regexes=REGEXES - ): - self.prefixes = SetManager(prefixes) - self.suffix_acronyms = SetManager(suffix_acronyms) + def __init__(self, + prefixes=PREFIXES, + suffix_acronyms=SUFFIX_ACRONYMS, + suffix_not_acronyms=SUFFIX_NOT_ACRONYMS, + titles=TITLES, + first_name_titles=FIRST_NAME_TITLES, + conjunctions=CONJUNCTIONS, + capitalization_exceptions=CAPITALIZATION_EXCEPTIONS, + regexes=REGEXES + ): + self.prefixes = SetManager(prefixes) + self.suffix_acronyms = SetManager(suffix_acronyms) self.suffix_not_acronyms = SetManager(suffix_not_acronyms) - self.titles = SetManager(titles) - self.first_name_titles = SetManager(first_name_titles) - self.conjunctions = SetManager(conjunctions) + self.titles = SetManager(titles) + self.first_name_titles = SetManager(first_name_titles) + self.conjunctions = SetManager(conjunctions) self.capitalization_exceptions = TupleManager(capitalization_exceptions) - self.regexes = TupleManager(regexes) + self.regexes = TupleManager(regexes) self._pst = None - + @property def suffixes_prefixes_titles(self): if not self._pst: @@ -242,15 +315,16 @@ def suffixes_prefixes_titles(self): def __repr__(self): return "" - + def __setstate__(self, state): self.__init__(state) - + def __getstate__(self): attrs = [x for x in dir(self) if not x.startswith('_')] - return dict([(a,getattr(self, a)) for a in attrs]) + return dict([(a, getattr(self, a)) for a in attrs]) + -#: A module-level instance of the :py:class:`Constants()` class. +#: A module-level instance of the :py:class:`Constants()` class. #: Provides a common instance for the module to share #: to easily adjust configuration for the entire module. #: See `Customizing the Parser with Your Own Configuration `_. diff --git a/nameparser/parser.py b/nameparser/parser.py index bd79057..6455629 100644 --- a/nameparser/parser.py +++ b/nameparser/parser.py @@ -15,6 +15,7 @@ ENCODING = 'utf-8' + def group_contiguous_integers(data): """ return list of tuples containing first and last index @@ -27,6 +28,7 @@ def group_contiguous_integers(data): ranges.append((group[0], group[-1])) return ranges + class HumanName(object): """ Parse a person's name into individual components. @@ -67,12 +69,12 @@ class HumanName(object): """ _count = 0 - _members = ['title','first','middle','last','suffix','nickname'] + _members = ['title', 'first', 'middle', 'last', 'suffix', 'nickname'] unparsable = True _full_name = '' def __init__(self, full_name="", constants=CONSTANTS, encoding=DEFAULT_ENCODING, - string_format=None): + string_format=None): self.C = constants if type(self.C) is not type(CONSTANTS): self.C = Constants() @@ -130,7 +132,7 @@ def __unicode__(self): # string_format = "{title} {first} {middle} {last} {suffix} ({nickname})" _s = self.string_format.format(**self.as_dict()) # remove trailing punctuation from missing nicknames - _s = _s.replace(str(self.C.empty_attribute_default),'').replace(" ()","").replace(" ''","").replace(' ""',"") + _s = _s.replace(str(self.C.empty_attribute_default), '').replace(" ()", "").replace(" ''", "").replace(' ""', "") return self.collapse_whitespace(_s).strip(', ') return " ".join(self) @@ -141,7 +143,7 @@ def __str__(self): def __repr__(self): if self.unparsable: - _string = "<%(class)s : [ Unparsable ] >" % {'class': self.__class__.__name__,} + _string = "<%(class)s : [ Unparsable ] >" % {'class': self.__class__.__name__, } else: _string = "<%(class)s : [\n\ttitle: '%(title)s' \n\tfirst: '%(first)s' \n\tmiddle: '%(middle)s' \n\tlast: '%(last)s' \n\tsuffix: '%(suffix)s'\n\tnickname: '%(nickname)s'\n]>" % { 'class': self.__class__.__name__, @@ -182,6 +184,54 @@ def as_dict(self, include_empty=True): d[m] = val return d + def initials_list(self, exclude_last_name=False, exclude_middle_name=False, exclude_first_name=False): + """ + Return period-delimited initials of the first, middle and optionally last name. + + :param bool exclude_last_name: Exclude the last name as part of the initials + :param bool exclude_middle_name: Exclude the middle name as part of the initials + :param bool exclude_first_name: Exclude the first name as part of the initials + :rtype: str + + .. doctest:: + + >>> name = HumanName("Sir Bob Andrew Dole") + >>> name.initials() + ["B", "A", "D"] + >>> name.initials(False) + ["B", "A"] + """ + initials_list = [] + if not self.C.force_exclude_first_name_initial and not exclude_first_name: + initials_list = [name[0] for name in self.first_list if len(name)] + + if not self.C.force_exclude_middle_name_initial and not exclude_middle_name: + initials_list += [name[0] for name in self.middle_list if len(name)] + + if not self.C.force_exclude_last_name_initial and not exclude_last_name: + initials_list += [name[0] for name in self.last_list if len(name)] + + return initials_list + + def initials(self, exclude_last_name=False, exclude_middle_name=False, exclude_first_name=False, ): + """ + Return period-delimited initials of the first, middle and optionally last name. + + :param bool include_last_name: Include the last name as part of the initials + :rtype: str + + .. doctest:: + + >>> name = HumanName("Sir Bob Andrew Dole") + >>> name.initials() + "B. A. D." + >>> name.initials(False) + "B. A." + """ + initials_list = self.initials_list(exclude_last_name, exclude_middle_name, exclude_first_name) + + return " ".join([initial + self.C.initials_delimiter for initial in initials_list]) or self.C.empty_attribute_default + @property def has_own_config(self): """ @@ -190,7 +240,7 @@ def has_own_config(self): """ return self.C is not CONSTANTS - ### attributes + # attributes @property def title(self): @@ -259,7 +309,7 @@ def surnames(self): """ return " ".join(self.surnames_list) or self.C.empty_attribute_default - ### setter methods + # setter methods def _set_list(self, attr, value): if isinstance(value, list): @@ -270,8 +320,8 @@ def _set_list(self, attr, value): val = [] else: raise TypeError( - "Can only assign strings, lists or None to name attributes." - " Got {0}".format(type(value))) + "Can only assign strings, lists or None to name attributes." + " Got {0}".format(type(value))) setattr(self, attr+"_list", self.parse_pieces(val)) @title.setter @@ -298,7 +348,7 @@ def suffix(self, value): def nickname(self, value): self._set_list('nickname', value) - ### Parse helpers + # Parse helpers def is_title(self, value): """Is in the :py:data:`~nameparser.config.titles.TITLES` set.""" @@ -331,8 +381,8 @@ def is_suffix(self, piece): `C.suffix_acronyms`. """ # suffixes may have periods inside them like "M.D." - return ((lc(piece).replace('.','') in self.C.suffix_acronyms) \ - or (lc(piece) in self.C.suffix_not_acronyms)) \ + return ((lc(piece).replace('.', '') in self.C.suffix_acronyms) + or (lc(piece) in self.C.suffix_not_acronyms)) \ and not self.is_an_initial(piece) def are_suffixes(self, pieces): @@ -358,8 +408,7 @@ def is_an_initial(self, value): """ return bool(self.C.regexes.initial.match(value)) - - ### full_name parser + # full_name parser @property def full_name(self): @@ -376,7 +425,7 @@ def full_name(self, value): def collapse_whitespace(self, string): # collapse multiple spaces into single space - string = self.C.regexes.spaces.sub(" ", string.strip()) + string = self.C.regexes.spaces.sub(" ", string.strip()) if string.endswith(","): string = string[:-1] return string @@ -404,7 +453,7 @@ def post_process(self): self.handle_capitalization() def fix_phd(self): - _re = self.C.regexes.phd + _re = self.C.regexes.phd match = _re.search(self._full_name) if match: self.suffix_list.append(match.group(1)) @@ -474,7 +523,6 @@ def parse_full_name(self): self.nickname_list = [] self.unparsable = True - self.pre_process() self._full_name = self.collapse_whitespace(self._full_name) @@ -516,7 +564,7 @@ def parse_full_name(self): # numeral but this piece is not an initial self.is_roman_numeral(nxt) and i == p_len - 2 and not self.is_an_initial(piece) - ): + ): self.last_list.append(piece) self.suffix_list += pieces[i+1:] break @@ -540,7 +588,6 @@ def parse_full_name(self): # title first middle last [suffix], suffix [suffix] [, suffix] # parts[0], parts[1:...] - self.suffix_list += parts[1:] pieces = self.parse_pieces(parts[0].split(' ')) log.debug("pieces: %s", u(pieces)) @@ -614,7 +661,6 @@ def parse_full_name(self): self.unparsable = False self.post_process() - def parse_pieces(self, parts, additional_parts_count=0): """ Split parts on spaces and remove commas, join on conjunctions and @@ -648,7 +694,7 @@ def parse_pieces(self, parts, additional_parts_count=0): # split on periods, any of the split pieces titles or suffixes? # ("Lt.Gov.") period_chunks = part.split(".") - titles = list(filter(self.is_title, period_chunks)) + titles = list(filter(self.is_title, period_chunks)) suffixes = list(filter(self.is_suffix, period_chunks)) # add the part to the constant so it will be found @@ -695,7 +741,7 @@ def join_on_conjunctions(self, pieces, additional_parts_count=0): # other, then join those newly joined conjunctions and any single # conjunctions to the piece before and after it conj_index = [i for i, piece in enumerate(pieces) - if self.is_conjunction(piece)] + if self.is_conjunction(piece)] contiguous_conj_i = [] for i, val in enumerate(conj_index): @@ -710,14 +756,14 @@ def join_on_conjunctions(self, pieces, additional_parts_count=0): delete_i = [] for i in contiguous_conj_i: if type(i) == tuple: - new_piece = " ".join(pieces[ i[0] : i[1]+1] ) - delete_i += list(range( i[0]+1, i[1]+1 )) + new_piece = " ".join(pieces[i[0]: i[1]+1]) + delete_i += list(range(i[0]+1, i[1]+1)) pieces[i[0]] = new_piece else: - new_piece = " ".join(pieces[ i : i+2 ]) + new_piece = " ".join(pieces[i: i+2]) delete_i += [i+1] pieces[i] = new_piece - #add newly joined conjunctions to constants to be found later + # add newly joined conjunctions to constants to be found later self.C.conjunctions.add(new_piece) for i in reversed(delete_i): @@ -747,9 +793,9 @@ def join_on_conjunctions(self, pieces, additional_parts_count=0): pieces[i] = new_piece pieces.pop(i+1) # subtract 1 from the index of all the remaining conjunctions - for j,val in enumerate(conj_index): + for j, val in enumerate(conj_index): if val > i: - conj_index[j]=val-1 + conj_index[j] = val-1 else: new_piece = " ".join(pieces[i-1:i+2]) @@ -766,11 +812,10 @@ def join_on_conjunctions(self, pieces, additional_parts_count=0): # subtract the number of removed pieces from the index # of all the remaining conjunctions - for j,val in enumerate(conj_index): + for j, val in enumerate(conj_index): if val > i: conj_index[j] = val - rm_count - # join prefixes to following lastnames: ['de la Vega'], ['van Buren'] prefixes = list(filter(self.is_prefix, pieces)) if prefixes: @@ -813,12 +858,11 @@ def join_on_conjunctions(self, pieces, additional_parts_count=0): log.debug("pieces: %s", pieces) return pieces - - ### Capitalization Support + # Capitalization Support def cap_word(self, word, attribute): - if (self.is_prefix(word) and attribute in ('last','middle')) \ - or self.is_conjunction(word): + if (self.is_prefix(word) and attribute in ('last', 'middle')) \ + or self.is_conjunction(word): return word.lower() exceptions = self.C.capitalization_exceptions if lc(word) in exceptions: @@ -834,7 +878,8 @@ def cap_after_mac(m): def cap_piece(self, piece, attribute): if not piece: return "" - replacement = lambda m: self.cap_word(m.group(0), attribute) + + def replacement(m): return self.cap_word(m.group(0), attribute) return self.C.regexes.word.sub(replacement, piece) def capitalize(self, force=None): @@ -872,10 +917,10 @@ def capitalize(self, force=None): if not force and not (name == name.upper() or name == name.lower()): return - self.title_list = self.cap_piece(self.title , 'title').split(' ') - self.first_list = self.cap_piece(self.first , 'first').split(' ') + self.title_list = self.cap_piece(self.title, 'title').split(' ') + self.first_list = self.cap_piece(self.first, 'first').split(' ') self.middle_list = self.cap_piece(self.middle, 'middle').split(' ') - self.last_list = self.cap_piece(self.last , 'last').split(' ') + self.last_list = self.cap_piece(self.last, 'last').split(' ') self.suffix_list = self.cap_piece(self.suffix, 'suffix').split(', ') def handle_capitalization(self): diff --git a/tests.py b/tests.py index 5f976b8..6bcb99d 100644 --- a/tests.py +++ b/tests.py @@ -1,5 +1,6 @@ # -*- coding: utf-8 -*- from __future__ import unicode_literals +import unittest """ Run this file to run the tests. @@ -30,7 +31,6 @@ log = logging.getLogger('HumanName') -import unittest try: unittest.expectedFailure except AttributeError: @@ -114,7 +114,6 @@ def test_get_full_name_attribute_references_internal_lists(self): hn.first_list = ["Larry"] self.m(hn.full_name, "Larry Williams", hn) - def test_assignment_to_attribute(self): hn = HumanName("John A. Kenneth Doe, Jr.") hn.last = "de la Vega" @@ -210,16 +209,16 @@ def test_assume_title_and_one_other_name_is_last_name(self): hn = HumanName("Rev Andrews") self.m(hn.title, "Rev", hn) self.m(hn.last, "Andrews", hn) - + # TODO: Seems "Andrews, M.D.", Andrews should be treated as a last name - # but other suffixes like "George Jr." should be first names. Might be + # but other suffixes like "George Jr." should be first names. Might be # related to https://github.com/derek73/python-nameparser/issues/2 @unittest.expectedFailure def test_assume_suffix_title_and_one_other_name_is_last_name(self): hn = HumanName("Andrews, M.D.") self.m(hn.suffix, "M.D.", hn) self.m(hn.last, "Andrews", hn) - + def test_suffix_in_lastname_part_of_lastname_comma_format(self): hn = HumanName("Smith Jr., John") self.m(hn.last, "Smith", hn) @@ -230,22 +229,22 @@ def test_sir_exception_to_first_name_rule(self): hn = HumanName("Sir Gerald") self.m(hn.title, "Sir", hn) self.m(hn.first, "Gerald", hn) - + def test_king_exception_to_first_name_rule(self): hn = HumanName("King Henry") self.m(hn.title, "King", hn) self.m(hn.first, "Henry", hn) - + def test_queen_exception_to_first_name_rule(self): hn = HumanName("Queen Elizabeth") self.m(hn.title, "Queen", hn) self.m(hn.first, "Elizabeth", hn) - + def test_dame_exception_to_first_name_rule(self): hn = HumanName("Dame Mary") self.m(hn.title, "Dame", hn) self.m(hn.first, "Mary", hn) - + def test_first_name_is_not_prefix_if_only_two_parts(self): """When there are only two parts, don't join prefixes or conjunctions""" hn = HumanName("Van Nguyen") @@ -263,7 +262,7 @@ def test_first_name_is_prefix_if_three_parts(self): hn = HumanName("Mr. Van Nguyen") self.m(hn.first, "Van", hn) self.m(hn.last, "Nguyen", hn) - + class HumanNameBruteForceTests(HumanNameTestBase): @@ -1084,7 +1083,7 @@ def test_multiple_conjunctions(self): def test_multiple_conjunctions2(self): hn = HumanName("part1 of and The part2 of the part3 And part4") self.m(hn.first, "part1 of and The part2 of the part3 And part4", hn) - + def test_ends_with_conjunction(self): hn = HumanName("Jon Dough and") self.m(hn.first, "Jon", hn) @@ -1242,12 +1241,12 @@ def test_le_as_last_name_with_middle_initial(self): self.m(hn.first, "Yin", hn) self.m(hn.middle, "a", hn) self.m(hn.last, "Le", hn) - + def test_conjunction_in_an_address_with_a_title(self): hn = HumanName("His Excellency Lord Duncan") self.m(hn.title, "His Excellency Lord", hn) self.m(hn.last, "Duncan", hn) - + @unittest.expectedFailure def test_conjunction_in_an_address_with_a_first_name_title(self): hn = HumanName("Her Majesty Queen Elizabeth") @@ -1272,7 +1271,7 @@ def test_add_title(self): self.m(hn.title, "Te", hn) self.m(hn.first, "Awanui-a-Rangi", hn) self.m(hn.last, "Black", hn) - + def test_remove_title(self): hn = HumanName("Hon Solo", constants=None) start_len = len(hn.C.titles) @@ -1282,7 +1281,7 @@ def test_remove_title(self): hn.parse_full_name() self.m(hn.first, "Hon", hn) self.m(hn.last, "Solo", hn) - + def test_add_multiple_arguments(self): hn = HumanName("Assoc Dean of Chemistry Robert Johns", constants=None) hn.C.titles.add('dean', 'Chemistry') @@ -1310,7 +1309,7 @@ def test_can_change_global_constants(self): self.assertEqual(hn2.has_own_config, False) # clean up so we don't mess up other tests hn.C.titles.add('hon') - + def test_remove_multiple_arguments(self): hn = HumanName("Ms Hon Solo", constants=None) hn.C.titles.remove('hon', 'ms') @@ -1370,7 +1369,7 @@ def test_nickname_in_parenthesis(self): self.m(hn.middle, "", hn) self.m(hn.last, "Franklin", hn) self.m(hn.nickname, "Ben", hn) - + def test_two_word_nickname_in_parenthesis(self): hn = HumanName("Benjamin (Big Ben) Franklin") self.m(hn.first, "Benjamin", hn) @@ -1391,7 +1390,7 @@ def test_nickname_in_parenthesis_with_comma(self): self.m(hn.middle, "", hn) self.m(hn.last, "Franklin", hn) self.m(hn.nickname, "Ben", hn) - + def test_nickname_in_parenthesis_with_comma_and_suffix(self): hn = HumanName("Franklin, Benjamin (Ben), Jr.") self.m(hn.first, "Benjamin", hn) @@ -1399,7 +1398,7 @@ def test_nickname_in_parenthesis_with_comma_and_suffix(self): self.m(hn.last, "Franklin", hn) self.m(hn.suffix, "Jr.", hn) self.m(hn.nickname, "Ben", hn) - + def test_nickname_in_single_quotes(self): hn = HumanName("Benjamin 'Ben' Franklin") self.m(hn.first, "Benjamin", hn) @@ -1413,28 +1412,28 @@ def test_nickname_in_double_quotes(self): self.m(hn.middle, "", hn) self.m(hn.last, "Franklin", hn) self.m(hn.nickname, "Ben", hn) - + def test_single_quotes_on_first_name_not_treated_as_nickname(self): hn = HumanName("Brian Andrew O'connor") self.m(hn.first, "Brian", hn) self.m(hn.middle, "Andrew", hn) self.m(hn.last, "O'connor", hn) self.m(hn.nickname, "", hn) - + def test_single_quotes_on_both_name_not_treated_as_nickname(self): hn = HumanName("La'tanya O'connor") self.m(hn.first, "La'tanya", hn) self.m(hn.middle, "", hn) self.m(hn.last, "O'connor", hn) self.m(hn.nickname, "", hn) - + def test_single_quotes_on_end_of_last_name_not_treated_as_nickname(self): hn = HumanName("Mari' Aube'") self.m(hn.first, "Mari'", hn) self.m(hn.middle, "", hn) self.m(hn.last, "Aube'", hn) self.m(hn.nickname, "", hn) - + def test_okina_inside_name_not_treated_as_nickname(self): hn = HumanName("Harrieta Keōpūolani Nāhiʻenaʻena") self.m(hn.first, "Harrieta", hn) @@ -1492,7 +1491,6 @@ def test_nickname_and_last_name_with_title(self): self.m(hn.nickname, "Rick", hn) - # class MaidenNameTestCase(HumanNameTestBase): # # def test_parenthesis_and_quotes_together(self): @@ -1542,12 +1540,12 @@ def test_prefix(self): hn = HumanName("Juan del Sur") self.m(hn.first, "Juan", hn) self.m(hn.last, "del Sur", hn) - + def test_prefix_with_period(self): hn = HumanName("Jill St. John") self.m(hn.first, "Jill", hn) self.m(hn.last, "St. John", hn) - + def test_prefix_before_two_part_last_name(self): hn = HumanName("pennie von bergen wessels") self.m(hn.first, "pennie", hn) @@ -1641,7 +1639,7 @@ def test_comma_three_conjunctions(self): class SuffixesTestCase(HumanNameTestBase): - + def test_suffix(self): hn = HumanName("Joe Franklin Jr") self.m(hn.first, "Joe", hn) @@ -1716,13 +1714,13 @@ def test_phd_conflict(self): self.m(hn.first, "Adolph", hn) self.m(hn.last, "D", hn) - # http://en.wikipedia.org/wiki/Ma_(surname) + def test_potential_suffix_that_is_also_last_name(self): hn = HumanName("Jack Ma") self.m(hn.first, "Jack", hn) self.m(hn.last, "Ma", hn) - + def test_potential_suffix_that_is_also_last_name_comma(self): hn = HumanName("Ma, Jack") self.m(hn.first, "Jack", hn) @@ -1820,27 +1818,27 @@ def test_chained_title_first_name_title_is_initials(self): self.m(hn.first, "Marc", hn) self.m(hn.middle, "Thomas", hn) self.m(hn.last, "Treadwell", hn) - + def test_conflict_with_chained_title_first_name_initial(self): hn = HumanName("U. S. Grant") self.m(hn.first, "U.", hn) self.m(hn.middle, "S.", hn) self.m(hn.last, "Grant", hn) - + def test_chained_title_first_name_initial_with_no_period(self): hn = HumanName("US Magistrate Judge T Michael Putnam") self.m(hn.title, "US Magistrate Judge", hn) self.m(hn.first, "T", hn) self.m(hn.middle, "Michael", hn) self.m(hn.last, "Putnam", hn) - + def test_chained_hyphenated_title(self): hn = HumanName("US Magistrate-Judge Elizabeth E Campbell") self.m(hn.title, "US Magistrate-Judge", hn) self.m(hn.first, "Elizabeth", hn) self.m(hn.middle, "E", hn) self.m(hn.last, "Campbell", hn) - + def test_chained_hyphenated_title_with_comma_suffix(self): hn = HumanName("Mag-Judge Harwell G Davis, III") self.m(hn.title, "Mag-Judge", hn) @@ -1883,7 +1881,7 @@ def test_title_with_last_initial_is_suffix(self): self.m(hn.title, "King", hn) self.m(hn.first, "John", hn) self.m(hn.last, "V.", hn) - + def test_initials_also_suffix(self): hn = HumanName("Smith, J.R.") self.m(hn.first, "J.R.", hn) @@ -2062,10 +2060,10 @@ def test_capitalize_prefix_clash_on_first_name(self): class HumanNameOutputFormatTests(HumanNameTestBase): - + def test_formatting_init_argument(self): hn = HumanName("Rev John A. Kenneth Doe III (Kenny)", - string_format="TEST1") + string_format="TEST1") self.assertEqual(u(hn), "TEST1") def test_formatting_constants_attribute(self): @@ -2160,7 +2158,7 @@ def test_formating_of_nicknames_in_middle(self): self.assertEqual(u(hn), "Rev John (Kenny) A. Kenneth Doe III") hn.nickname = '' self.assertEqual(u(hn), "Rev John A. Kenneth Doe III") - + def test_remove_emojis(self): hn = HumanName("Sam Smith 😊") self.m(hn.first, "Sam", hn) @@ -2184,6 +2182,90 @@ def test_keep_emojis(self): # test cleanup +class InitialsTestCase(HumanNameTestBase): + def test_initials(self): + hn = HumanName("Andrew Boris Petersen") + self.m(hn.initials(), "A. B. P.", hn) + self.m(hn.initials(exclude_last_name=True), "A. B.", hn) + self.m(hn.initials(exclude_middle_name=True), "A. P.", hn) + self.m(hn.initials(exclude_first_name=True), "B. P.", hn) + + def test_initials_complex_name(self): + hn = HumanName("Doe, John A. Kenneth, Jr.") + self.m(hn.initials(), "J. A. K. D.", hn) + self.m(hn.initials(exclude_last_name=True), "J. A. K.", hn) + self.m(hn.initials(exclude_middle_name=True), "J. D.", hn) + self.m(hn.initials(exclude_first_name=True), "A. K. D.", hn) + + def test_initials_list(self): + hn = HumanName("Andrew Boris Petersen") + self.m(hn.initials_list(), ["A", "B", "P"], hn) + self.m(hn.initials_list(exclude_last_name=True), ["A", "B"], hn) + self.m(hn.initials_list(exclude_middle_name=True), ["A", "P"], hn) + self.m(hn.initials_list(exclude_first_name=True), ["B", "P"], hn) + + def test_initials_list_complex_name(self): + hn = HumanName("Doe, John A. Kenneth, Jr.") + self.m(hn.initials_list(), ["J", "A", "K", "D"], hn) + self.m(hn.initials_list(exclude_last_name=True), ["J", "A", "K"], hn) + self.m(hn.initials_list(exclude_middle_name=True), ["J", "D"], hn) + self.m(hn.initials_list(exclude_first_name=True), ["A", "K", "D"], hn) + + def test_initials_configuration(self): + hn = HumanName("Doe, John A. Kenneth, Jr.") + from nameparser.config import CONSTANTS + + CONSTANTS.force_exclude_last_name_initial = True + self.m(hn.initials(), "J. A. K.", hn) + self.m(hn.initials(exclude_last_name=True), "J. A. K.", hn) + self.m(hn.initials(exclude_middle_name=True), "J.", hn) + self.m(hn.initials(exclude_first_name=True), "A. K.", hn) + CONSTANTS.force_exclude_last_name_initial = False + + CONSTANTS.force_exclude_middle_name_initial = True + self.m(hn.initials(), "J. D.", hn) + self.m(hn.initials(exclude_last_name=True), "J.", hn) + self.m(hn.initials(exclude_middle_name=True), "J. D.", hn) + self.m(hn.initials(exclude_first_name=True), "D.", hn) + CONSTANTS.force_exclude_middle_name_initial = False + + CONSTANTS.force_exclude_first_name_initial = True + self.m(hn.initials(), "A. K. D.", hn) + self.m(hn.initials(exclude_last_name=True), "A. K.", hn) + self.m(hn.initials(exclude_middle_name=True), "D.", hn) + self.m(hn.initials(exclude_first_name=True), "A. K. D.", hn) + CONSTANTS.force_exclude_first_name_initial = False + + CONSTANTS.initials_delimiter = '' + self.m(hn.initials(), "J A K D", hn) + CONSTANTS.initials_delimiter = '.' + + def test_initials_configuration_list(self): + hn = HumanName("Doe, John A. Kenneth, Jr.") + from nameparser.config import CONSTANTS + + CONSTANTS.force_exclude_last_name_initial = True + self.m(hn.initials_list(), ["J", "A", "K"], hn) + self.m(hn.initials_list(exclude_last_name=True), ["J", "A", "K"], hn) + self.m(hn.initials_list(exclude_middle_name=True), ["J"], hn) + self.m(hn.initials_list(exclude_first_name=True), ["A", "K"], hn) + CONSTANTS.force_exclude_last_name_initial = False + + CONSTANTS.force_exclude_middle_name_initial = True + self.m(hn.initials_list(), ["J", "D"], hn) + self.m(hn.initials_list(exclude_last_name=True), ["J"], hn) + self.m(hn.initials_list(exclude_middle_name=True), ["J", "D"], hn) + self.m(hn.initials_list(exclude_first_name=True), ["D"], hn) + CONSTANTS.force_exclude_middle_name_initial = False + + CONSTANTS.force_exclude_first_name_initial = True + self.m(hn.initials_list(), ["A", "K", "D"], hn) + self.m(hn.initials_list(exclude_last_name=True), ["A", "K"], hn) + self.m(hn.initials_list(exclude_middle_name=True), ["D"], hn) + self.m(hn.initials_list(exclude_first_name=True), ["A", "K", "D"], hn) + CONSTANTS.force_exclude_first_name_initial = False + + TEST_NAMES = ( "John Doe", "John Doe, Jr.", @@ -2359,7 +2441,7 @@ def test_keep_emojis(self): "U.S. District Judge Marc Thomas Treadwell", "Dra. Andréia da Silva", "Srta. Andréia da Silva", - + ) From b9be118b89bdd71feca8e379e7db0a7fd8d23e17 Mon Sep 17 00:00:00 2001 From: Rink Stiekema Date: Thu, 21 Oct 2021 12:16:38 +0200 Subject: [PATCH 46/99] Removed unnecessary comma --- nameparser/parser.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/nameparser/parser.py b/nameparser/parser.py index 6455629..7b4c2e9 100644 --- a/nameparser/parser.py +++ b/nameparser/parser.py @@ -213,7 +213,7 @@ def initials_list(self, exclude_last_name=False, exclude_middle_name=False, excl return initials_list - def initials(self, exclude_last_name=False, exclude_middle_name=False, exclude_first_name=False, ): + def initials(self, exclude_last_name=False, exclude_middle_name=False, exclude_first_name=False): """ Return period-delimited initials of the first, middle and optionally last name. From 19e1893058988c49f302cf01eb946ffb05f6a1a0 Mon Sep 17 00:00:00 2001 From: Rink Stiekema Date: Fri, 22 Oct 2021 16:59:29 +0200 Subject: [PATCH 47/99] Use string formatting for initials --- docs/usage.rst | 40 ++++++++++----- nameparser/config/__init__.py | 80 +++++------------------------ nameparser/parser.py | 42 +++++++-------- tests.py | 97 ++++++++++++----------------------- 4 files changed, 91 insertions(+), 168 deletions(-) diff --git a/docs/usage.rst b/docs/usage.rst index 01beb48..e6e7c40 100644 --- a/docs/usage.rst +++ b/docs/usage.rst @@ -181,24 +181,36 @@ Initials Support The HumanName class can try to get the correct representation of initials. Initials can be tricky as different format usages exist. -If you want to exclude on of the name parts from the initials, you can use one of the following boolean parameters: -`exclude_last_name`, `exclude_middle_name` or `exclude_first_name` +If you want to exclude on of the name parts from the initials, you can use the initials format by chainging +:py:attr:`~nameparser.config.Constants.initials_format` +Three attributes exist for the format, `first`, `middle` and `last`. + +.. doctest:: initials format + + >>> from nameparser.config import CONSTANTS + >>> CONSTANTS.initials_format = "{first} {middle}" + >>> HumanName("Doe, John A. Kenneth, Jr.").initials() + 'J. A. K.' + >>> HumanName("Doe, John A. Kenneth, Jr.", initials_format="{last}, {first}).initials() + 'D., J.' -You can also force the behavior using the CONSTANTS: -:py:attr:`~nameparser.config.Constants.force_exclude_last_name` -:py:attr:`~nameparser.config.Constants.force_exclude_middle_name` -:py:attr:`~nameparser.config.Constants.force_exclude_first_name` Furthermore, the delimiter for the string output can be set through: :py:attr:`~nameparser.config.Constants.initials_delimiter` -.. doctest:: initials +.. doctest:: initials delimiter + + >>> HumanName("Doe, John A. Kenneth, Jr.", initials_delimiter=";").initials() + "J; A; K;" + >>> from nameparser.config import CONSTANTS + >>> CONSTANTS.initials_delimiter = "." + >>> HumanName("Doe, John A. Kenneth, Jr.", initials_format="{first}{middle}{last}).initials() + "J.A.K.D." + +If you want to receive a list representation of the initials, yo ucan use :py:meth:`~nameparser.HumanName.initials_list`. +This function is unaffected by :py:attr:`~nameparser.config.Constants.initials_format` - >>> name = HumanName("Doe, John A. Kenneth, Jr.") - >>> name.initials() - 'J. A. K. D.' - >>> name.initials(exclude_last_name) - 'J. A. K.' - >>> name.initials_list(exclude_middle_name): - ['J', 'D'] +.. doctest:: list format + >>> HumanName("Doe, John A. Kenneth, Jr.", initials_delimiter=";").initials_list() + ["J", "A", "K", "D"] diff --git a/nameparser/config/__init__.py b/nameparser/config/__init__.py index 488c899..7b2baef 100644 --- a/nameparser/config/__init__.py +++ b/nameparser/config/__init__.py @@ -172,6 +172,18 @@ class Constants(object): """ The default string format use for all new `HumanName` instances. """ + + initials_format = "{first} {middle} {last}" + """ + The default initials format used for all new `HumanName` instances. + """ + + initials_delimiter = "." + """ + The default initials delimiter used for all new `HumanName` instances. + Will be used to add a delimiter between each initial. + """ + empty_attribute_default = '' """ Default return value for empty attributes. @@ -203,23 +215,6 @@ class Constants(object): """ - initials_delimiter = '.' - """" - Determines how the initials from :py:meth:`~nameparser.parser.HumanName.initials` are seperated. - - .. doctest:: - - >>> from nameparser.config import CONSTANTS - >>> HumanName('Shirley Maclaine').initials() - 'S. M.' - >>> CONSTANTS.initials_delimiter = '' - >>> HumanName('Shirley Maclaine').initials() - 'S M' - >>> CONSTANTS.initials_delimiter = '-' - >>> HumanName('Shirley Maclaine').initials() - 'S- M-' - """ - force_mixed_case_capitalization = False """ If set, forces the capitalization of mixed case strings when @@ -236,57 +231,6 @@ class Constants(object): """ - force_exclude_last_name_initial = False - """ - If True, forces the last name to be excluded in the initials when - :py:meth:`~nameparser.parser.HumanName.initials` or - :py:meth:`~nameparser.parser.HumanName.initials_list` is called. - - .. doctest:: - - >>> from nameparser.config import CONSTANTS - >>> CONSTANTS.force_exclude_last_name_initial = True - >>> name = HumanName('Shirley Ashley Maclaine') - >>> name.initials() - 'S. A.' - >>> name.initials_list() - ['S', 'A'] - """ - - force_exclude_middle_name_initial = False - """ - If True, forces the middle name to be included in the initials when - :py:meth:`~nameparser.parser.HumanName.initials` or - :py:meth:`~nameparser.parser.HumanName.initials_list` is called. - - .. doctest:: - - >>> from nameparser.config import CONSTANTS - >>> CONSTANTS.force_exclude_middle_name_initial = True - >>> name = HumanName('Shirley Ashley Maclaine') - >>> name.initials() - 'S. M.' - >>> name.initials_list() - ['S', 'M'] - """ - - force_exclude_first_name_initial = False - """ - If True, forces the first name to be included in the initials when - :py:meth:`~nameparser.parser.HumanName.initials` or - :py:meth:`~nameparser.parser.HumanName.initials_list` is called. - - .. doctest:: - - >>> from nameparser.config import CONSTANTS - >>> CONSTANTS.force_exclude_first_name_initial = True - >>> name = HumanName('Shirley Ashley Maclaine') - >>> name.initials() - 'A. M.' - >>> name.initials_list() - ['A', 'M'] - """ - def __init__(self, prefixes=PREFIXES, suffix_acronyms=SUFFIX_ACRONYMS, diff --git a/nameparser/parser.py b/nameparser/parser.py index 7b4c2e9..52ad6bd 100644 --- a/nameparser/parser.py +++ b/nameparser/parser.py @@ -53,6 +53,8 @@ class HumanName(object): `per-instance config `_. :param str encoding: string representing the encoding of your input :param str string_format: python string formatting + :param str initials_format: python initials string formatting + :param str initials_delimter: string delimiter for initials """ C = CONSTANTS @@ -74,13 +76,15 @@ class HumanName(object): _full_name = '' def __init__(self, full_name="", constants=CONSTANTS, encoding=DEFAULT_ENCODING, - string_format=None): + string_format=None, initials_format=None, initials_delimiter=None): self.C = constants if type(self.C) is not type(CONSTANTS): self.C = Constants() self.encoding = encoding self.string_format = string_format or self.C.string_format + self.initials_format = initials_format or self.C.initials_format + self.initials_delimiter = initials_delimiter or self.C.initials_delimiter # full_name setter triggers the parse self.full_name = full_name @@ -184,36 +188,27 @@ def as_dict(self, include_empty=True): d[m] = val return d - def initials_list(self, exclude_last_name=False, exclude_middle_name=False, exclude_first_name=False): + def initials_list(self): """ - Return period-delimited initials of the first, middle and optionally last name. - - :param bool exclude_last_name: Exclude the last name as part of the initials - :param bool exclude_middle_name: Exclude the middle name as part of the initials - :param bool exclude_first_name: Exclude the first name as part of the initials - :rtype: str + Returns the initials as a list .. doctest:: >>> name = HumanName("Sir Bob Andrew Dole") >>> name.initials() ["B", "A", "D"] - >>> name.initials(False) - ["B", "A"] + >>> name = HumanName("J. Doe") + >>> name.initials() + ["J", "D"] """ initials_list = [] - if not self.C.force_exclude_first_name_initial and not exclude_first_name: - initials_list = [name[0] for name in self.first_list if len(name)] - - if not self.C.force_exclude_middle_name_initial and not exclude_middle_name: - initials_list += [name[0] for name in self.middle_list if len(name)] - - if not self.C.force_exclude_last_name_initial and not exclude_last_name: - initials_list += [name[0] for name in self.last_list if len(name)] + initials_list = [name[0] for name in self.first_list if len(name)] + initials_list += [name[0] for name in self.middle_list if len(name)] + initials_list += [name[0] for name in self.last_list if len(name)] return initials_list - def initials(self, exclude_last_name=False, exclude_middle_name=False, exclude_first_name=False): + def initials(self): """ Return period-delimited initials of the first, middle and optionally last name. @@ -228,9 +223,14 @@ def initials(self, exclude_last_name=False, exclude_middle_name=False, exclude_f >>> name.initials(False) "B. A." """ - initials_list = self.initials_list(exclude_last_name, exclude_middle_name, exclude_first_name) - return " ".join([initial + self.C.initials_delimiter for initial in initials_list]) or self.C.empty_attribute_default + initials_dict = { + "first": (self.initials_delimiter + " ").join([name[0] for name in self.first_list if len(name)]) + self.initials_delimiter, + "middle": (self.initials_delimiter + " ").join([name[0] for name in self.middle_list if len(name)]) + self.initials_delimiter, + "last": (self.initials_delimiter + " ").join([name[0] for name in self.last_list if len(name)]) + self.initials_delimiter + } + + return self.initials_format.format(**initials_dict) @property def has_own_config(self): diff --git a/tests.py b/tests.py index 6bcb99d..0c884e5 100644 --- a/tests.py +++ b/tests.py @@ -2186,84 +2186,51 @@ class InitialsTestCase(HumanNameTestBase): def test_initials(self): hn = HumanName("Andrew Boris Petersen") self.m(hn.initials(), "A. B. P.", hn) - self.m(hn.initials(exclude_last_name=True), "A. B.", hn) - self.m(hn.initials(exclude_middle_name=True), "A. P.", hn) - self.m(hn.initials(exclude_first_name=True), "B. P.", hn) def test_initials_complex_name(self): hn = HumanName("Doe, John A. Kenneth, Jr.") self.m(hn.initials(), "J. A. K. D.", hn) - self.m(hn.initials(exclude_last_name=True), "J. A. K.", hn) - self.m(hn.initials(exclude_middle_name=True), "J. D.", hn) - self.m(hn.initials(exclude_first_name=True), "A. K. D.", hn) - def test_initials_list(self): - hn = HumanName("Andrew Boris Petersen") - self.m(hn.initials_list(), ["A", "B", "P"], hn) - self.m(hn.initials_list(exclude_last_name=True), ["A", "B"], hn) - self.m(hn.initials_list(exclude_middle_name=True), ["A", "P"], hn) - self.m(hn.initials_list(exclude_first_name=True), ["B", "P"], hn) + def test_initials_format(self): + hn = HumanName("Doe, John A. Kenneth, Jr.", initials_format="{first} {middle}") + self.m(hn.initials(), "J. A. K.", hn) + hn = HumanName("Doe, John A. Kenneth, Jr.", initials_format="{first} {last}") + self.m(hn.initials(), "J. D.", hn) + hn = HumanName("Doe, John A. Kenneth, Jr.", initials_format="{middle} {last}") + self.m(hn.initials(), "A. K. D.", hn) + hn = HumanName("Doe, John A. Kenneth, Jr.", initials_format="{first}, {last}") + self.m(hn.initials(), "J., D.", hn) - def test_initials_list_complex_name(self): + def test_initials_format_constants(self): + from nameparser.config import CONSTANTS + orig_format = CONSTANTS.initials_format + CONSTANTS.initials_format = "{first} {last}" hn = HumanName("Doe, John A. Kenneth, Jr.") - self.m(hn.initials_list(), ["J", "A", "K", "D"], hn) - self.m(hn.initials_list(exclude_last_name=True), ["J", "A", "K"], hn) - self.m(hn.initials_list(exclude_middle_name=True), ["J", "D"], hn) - self.m(hn.initials_list(exclude_first_name=True), ["A", "K", "D"], hn) - - def test_initials_configuration(self): + self.m(hn.initials(), "J. D.", hn) + CONSTANTS.initials_format = "{first} {last}" hn = HumanName("Doe, John A. Kenneth, Jr.") - from nameparser.config import CONSTANTS + self.m(hn.initials(), "J. D.", hn) + CONSTANTS.initials_format = orig_format - CONSTANTS.force_exclude_last_name_initial = True - self.m(hn.initials(), "J. A. K.", hn) - self.m(hn.initials(exclude_last_name=True), "J. A. K.", hn) - self.m(hn.initials(exclude_middle_name=True), "J.", hn) - self.m(hn.initials(exclude_first_name=True), "A. K.", hn) - CONSTANTS.force_exclude_last_name_initial = False + def test_initials_delimiter(self): + hn = HumanName("Doe, John A. Kenneth, Jr.", initials_delimiter=";") + self.m(hn.initials(), "J; A; K; D;", hn) - CONSTANTS.force_exclude_middle_name_initial = True - self.m(hn.initials(), "J. D.", hn) - self.m(hn.initials(exclude_last_name=True), "J.", hn) - self.m(hn.initials(exclude_middle_name=True), "J. D.", hn) - self.m(hn.initials(exclude_first_name=True), "D.", hn) - CONSTANTS.force_exclude_middle_name_initial = False - - CONSTANTS.force_exclude_first_name_initial = True - self.m(hn.initials(), "A. K. D.", hn) - self.m(hn.initials(exclude_last_name=True), "A. K.", hn) - self.m(hn.initials(exclude_middle_name=True), "D.", hn) - self.m(hn.initials(exclude_first_name=True), "A. K. D.", hn) - CONSTANTS.force_exclude_first_name_initial = False + def test_initials_delimiter_constants(self): + from nameparser.config import CONSTANTS + orig_delimiter = CONSTANTS.initials_delimiter + CONSTANTS.initials_delimiter = ";" + hn = HumanName("Doe, John A. Kenneth, Jr.") + self.m(hn.initials(), "J; A; K; D;", hn) + CONSTANTS.initials_delimiter = orig_delimiter - CONSTANTS.initials_delimiter = '' - self.m(hn.initials(), "J A K D", hn) - CONSTANTS.initials_delimiter = '.' + def test_initials_list(self): + hn = HumanName("Andrew Boris Petersen") + self.m(hn.initials_list(), ["A", "B", "P"], hn) - def test_initials_configuration_list(self): + def test_initials_list_complex_name(self): hn = HumanName("Doe, John A. Kenneth, Jr.") - from nameparser.config import CONSTANTS - - CONSTANTS.force_exclude_last_name_initial = True - self.m(hn.initials_list(), ["J", "A", "K"], hn) - self.m(hn.initials_list(exclude_last_name=True), ["J", "A", "K"], hn) - self.m(hn.initials_list(exclude_middle_name=True), ["J"], hn) - self.m(hn.initials_list(exclude_first_name=True), ["A", "K"], hn) - CONSTANTS.force_exclude_last_name_initial = False - - CONSTANTS.force_exclude_middle_name_initial = True - self.m(hn.initials_list(), ["J", "D"], hn) - self.m(hn.initials_list(exclude_last_name=True), ["J"], hn) - self.m(hn.initials_list(exclude_middle_name=True), ["J", "D"], hn) - self.m(hn.initials_list(exclude_first_name=True), ["D"], hn) - CONSTANTS.force_exclude_middle_name_initial = False - - CONSTANTS.force_exclude_first_name_initial = True - self.m(hn.initials_list(), ["A", "K", "D"], hn) - self.m(hn.initials_list(exclude_last_name=True), ["A", "K"], hn) - self.m(hn.initials_list(exclude_middle_name=True), ["D"], hn) - self.m(hn.initials_list(exclude_first_name=True), ["A", "K", "D"], hn) - CONSTANTS.force_exclude_first_name_initial = False + self.m(hn.initials_list(), ["J", "A", "K", "D"], hn) TEST_NAMES = ( From 22f4d0cff7984c135881d5f56a0a882472140fc7 Mon Sep 17 00:00:00 2001 From: Rink Stiekema Date: Tue, 26 Oct 2021 14:19:37 +0200 Subject: [PATCH 48/99] Handle missing middlename case for initials and collapse whitespace --- nameparser/parser.py | 16 ++++++++++++---- tests.py | 22 +++++++++++++++++----- 2 files changed, 29 insertions(+), 9 deletions(-) diff --git a/nameparser/parser.py b/nameparser/parser.py index 52ad6bd..77c8217 100644 --- a/nameparser/parser.py +++ b/nameparser/parser.py @@ -224,13 +224,21 @@ def initials(self): "B. A." """ + first_initials_list = [name[0] for name in self.first_list] + middle_initials_list = [name[0] for name in self.middle_list] + last_initials_list = [name[0] for name in self.last_list] + initials_dict = { - "first": (self.initials_delimiter + " ").join([name[0] for name in self.first_list if len(name)]) + self.initials_delimiter, - "middle": (self.initials_delimiter + " ").join([name[0] for name in self.middle_list if len(name)]) + self.initials_delimiter, - "last": (self.initials_delimiter + " ").join([name[0] for name in self.last_list if len(name)]) + self.initials_delimiter + "first": (self.initials_delimiter + " ").join(first_initials_list) + self.initials_delimiter + if len(first_initials_list) else self.C.empty_attribute_default, + "middle": (self.initials_delimiter + " ").join(middle_initials_list) + self.initials_delimiter + if len(middle_initials_list) else self.C.empty_attribute_default, + "last": (self.initials_delimiter + " ").join(last_initials_list) + self.initials_delimiter + if len(last_initials_list) else self.C.empty_attribute_default } - return self.initials_format.format(**initials_dict) + _s = self.initials_format.format(**initials_dict) + return self.collapse_whitespace(_s) @property def has_own_config(self): diff --git a/tests.py b/tests.py index 0c884e5..f075cb6 100644 --- a/tests.py +++ b/tests.py @@ -2187,6 +2187,18 @@ def test_initials(self): hn = HumanName("Andrew Boris Petersen") self.m(hn.initials(), "A. B. P.", hn) + def test_initials_simple_name(self): + hn = HumanName("John Doe") + self.m(hn.initials(), "J. D.", hn) + hn = HumanName("John Doe", initials_format="{first} {last}") + self.m(hn.initials(), "J. D.", hn) + hn = HumanName("John Doe", initials_format="{last}") + self.m(hn.initials(), "D.", hn) + hn = HumanName("John Doe", initials_format="{first}") + self.m(hn.initials(), "J.", hn) + hn = HumanName("John Doe", initials_format="{middle}") + self.m(hn.initials(), "", hn) + def test_initials_complex_name(self): hn = HumanName("Doe, John A. Kenneth, Jr.") self.m(hn.initials(), "J. A. K. D.", hn) @@ -2203,14 +2215,14 @@ def test_initials_format(self): def test_initials_format_constants(self): from nameparser.config import CONSTANTS - orig_format = CONSTANTS.initials_format + _orig = CONSTANTS.initials_format CONSTANTS.initials_format = "{first} {last}" hn = HumanName("Doe, John A. Kenneth, Jr.") self.m(hn.initials(), "J. D.", hn) CONSTANTS.initials_format = "{first} {last}" hn = HumanName("Doe, John A. Kenneth, Jr.") - self.m(hn.initials(), "J. D.", hn) - CONSTANTS.initials_format = orig_format + self.m(hn.initials(), "J. D.", hn) + CONSTANTS.initials_format = _orig def test_initials_delimiter(self): hn = HumanName("Doe, John A. Kenneth, Jr.", initials_delimiter=";") @@ -2218,11 +2230,11 @@ def test_initials_delimiter(self): def test_initials_delimiter_constants(self): from nameparser.config import CONSTANTS - orig_delimiter = CONSTANTS.initials_delimiter + _orig = CONSTANTS.initials_delimiter CONSTANTS.initials_delimiter = ";" hn = HumanName("Doe, John A. Kenneth, Jr.") self.m(hn.initials(), "J; A; K; D;", hn) - CONSTANTS.initials_delimiter = orig_delimiter + CONSTANTS.initials_delimiter = _orig def test_initials_list(self): hn = HumanName("Andrew Boris Petersen") From 606f77c1b435db8a5f56530b47db25aad34f8308 Mon Sep 17 00:00:00 2001 From: Rink Stiekema Date: Wed, 27 Oct 2021 13:04:01 +0200 Subject: [PATCH 49/99] Filter conjunctions and prefixes from initials --- nameparser/parser.py | 29 +++++++++++++++++------------ tests.py | 1 + 2 files changed, 18 insertions(+), 12 deletions(-) diff --git a/nameparser/parser.py b/nameparser/parser.py index 77c8217..f068f5d 100644 --- a/nameparser/parser.py +++ b/nameparser/parser.py @@ -188,6 +188,12 @@ def as_dict(self, include_empty=True): d[m] = val return d + def process_initial(self, name_part): + """ + Name parts may include prefixes or conjuctions. This function filters these from the name. + """ + return " ".join([split for split in name_part.split(" ") if len(split) and not (self.is_prefix(split) or self.is_conjunction(split))])[0] + def initials_list(self): """ Returns the initials as a list @@ -195,18 +201,16 @@ def initials_list(self): .. doctest:: >>> name = HumanName("Sir Bob Andrew Dole") - >>> name.initials() + >>> name.initials_list() ["B", "A", "D"] >>> name = HumanName("J. Doe") - >>> name.initials() + >>> name.initials_list() ["J", "D"] """ - initials_list = [] - initials_list = [name[0] for name in self.first_list if len(name)] - initials_list += [name[0] for name in self.middle_list if len(name)] - initials_list += [name[0] for name in self.last_list if len(name)] - - return initials_list + first_initials_list = [self.__process_initial__(name) for name in self.first_list if name] + middle_initials_list = [self.__process_initial__(name) for name in self.middle_list if name] + last_initials_list = [self.__process_initial__(name) for name in self.last_list if name] + return first_initials_list + middle_initials_list + last_initials_list def initials(self): """ @@ -220,13 +224,14 @@ def initials(self): >>> name = HumanName("Sir Bob Andrew Dole") >>> name.initials() "B. A. D." - >>> name.initials(False) + >>> name = HumanName("Sir Bob Andrew Dole", initials_format="{first} {middle}") + >>> name.initials() "B. A." """ - first_initials_list = [name[0] for name in self.first_list] - middle_initials_list = [name[0] for name in self.middle_list] - last_initials_list = [name[0] for name in self.last_list] + first_initials_list = [self.__process_initial__(name) for name in self.first_list if name] + middle_initials_list = [self.__process_initial__(name) for name in self.middle_list if name] + last_initials_list = [self.__process_initial__(name) for name in self.last_list if name] initials_dict = { "first": (self.initials_delimiter + " ").join(first_initials_list) + self.initials_delimiter diff --git a/tests.py b/tests.py index f075cb6..4e14b1d 100644 --- a/tests.py +++ b/tests.py @@ -2472,6 +2472,7 @@ def test_variations_of_TEST_NAMES(self): print((repr(hn_instance))) hn_instance.capitalize() print((repr(hn_instance))) + print("Initials: " + hn_instance.initials()) else: print("-"*80) print("Running tests") From fd3e8470477bd38fa76e3d6dcecb23e7a78faa71 Mon Sep 17 00:00:00 2001 From: Rink Stiekema Date: Mon, 8 Nov 2021 16:27:48 +0100 Subject: [PATCH 50/99] Rename parse_initial to __parse_initial__ --- nameparser/parser.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/nameparser/parser.py b/nameparser/parser.py index f068f5d..bbde276 100644 --- a/nameparser/parser.py +++ b/nameparser/parser.py @@ -188,7 +188,7 @@ def as_dict(self, include_empty=True): d[m] = val return d - def process_initial(self, name_part): + def __process_initial__(self, name_part): """ Name parts may include prefixes or conjuctions. This function filters these from the name. """ From 377bea2a1525fc9b22055fd5976f440185b08a00 Mon Sep 17 00:00:00 2001 From: Rink Stiekema Date: Thu, 11 Nov 2021 16:30:05 +0100 Subject: [PATCH 51/99] Check if name_part exists --- nameparser/parser.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/nameparser/parser.py b/nameparser/parser.py index bbde276..bd93e81 100644 --- a/nameparser/parser.py +++ b/nameparser/parser.py @@ -192,7 +192,8 @@ def __process_initial__(self, name_part): """ Name parts may include prefixes or conjuctions. This function filters these from the name. """ - return " ".join([split for split in name_part.split(" ") if len(split) and not (self.is_prefix(split) or self.is_conjunction(split))])[0] + parsed = " ".join([split for split in name_part.split(" ") if len(split) and not (self.is_prefix(split) or self.is_conjunction(split))]) + return parsed[0] if len(parsed) else "" def initials_list(self): """ From b4fca52465902ad4b1134f8d612f6a287b84717f Mon Sep 17 00:00:00 2001 From: Derek Gulbranson Date: Mon, 3 Jan 2022 12:06:44 -0800 Subject: [PATCH 52/99] Update usage.rst update documentation --- docs/usage.rst | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/usage.rst b/docs/usage.rst index e6e7c40..7fbe274 100644 --- a/docs/usage.rst +++ b/docs/usage.rst @@ -181,7 +181,7 @@ Initials Support The HumanName class can try to get the correct representation of initials. Initials can be tricky as different format usages exist. -If you want to exclude on of the name parts from the initials, you can use the initials format by chainging +To exclude any of the name parts from the initials, change the initials format string: :py:attr:`~nameparser.config.Constants.initials_format` Three attributes exist for the format, `first`, `middle` and `last`. @@ -207,7 +207,7 @@ Furthermore, the delimiter for the string output can be set through: >>> HumanName("Doe, John A. Kenneth, Jr.", initials_format="{first}{middle}{last}).initials() "J.A.K.D." -If you want to receive a list representation of the initials, yo ucan use :py:meth:`~nameparser.HumanName.initials_list`. +To get a list representation of the initials, use :py:meth:`~nameparser.HumanName.initials_list`. This function is unaffected by :py:attr:`~nameparser.config.Constants.initials_format` .. doctest:: list format From 3623394debbda9deb6d93aee9e2bb429db9d2912 Mon Sep 17 00:00:00 2001 From: Derek Gulbranson Date: Mon, 3 Jan 2022 12:07:02 -0800 Subject: [PATCH 53/99] Update dev-requirements.txt prune old dev requirements --- dev-requirements.txt | 4 ---- 1 file changed, 4 deletions(-) diff --git a/dev-requirements.txt b/dev-requirements.txt index 8aab0b6..edd07b3 100644 --- a/dev-requirements.txt +++ b/dev-requirements.txt @@ -1,6 +1,2 @@ -ipdb -nose>=1.3.7 -coverage>=4.0.3 dill>=0.2.5 -twine Sphinx From 0e9d7e64fb9231c5572bddcc7c3c12a3681398b3 Mon Sep 17 00:00:00 2001 From: Derek Gulbranson Date: Mon, 3 Jan 2022 12:08:14 -0800 Subject: [PATCH 54/99] Create python-package.yml create workflow for testing package on different versions of python --- .github/workflows/python-package.yml | 40 ++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 .github/workflows/python-package.yml diff --git a/.github/workflows/python-package.yml b/.github/workflows/python-package.yml new file mode 100644 index 0000000..5c7731e --- /dev/null +++ b/.github/workflows/python-package.yml @@ -0,0 +1,40 @@ +# This workflow will install Python dependencies, run tests and lint with a variety of Python versions +# For more information see: https://help.github.com/actions/language-and-framework-guides/using-python-with-github-actions + +name: Python package + +on: + push: + branches: [ master ] + pull_request: + branches: [ master ] + +jobs: + build: + + runs-on: ubuntu-latest + strategy: + fail-fast: false + matrix: + python-version: ["3.8", "3.9", "3.10"] + + steps: + - uses: actions/checkout@v2 + - name: Set up Python ${{ matrix.python-version }} + uses: actions/setup-python@v2 + with: + python-version: ${{ matrix.python-version }} + - name: Install dependencies + run: | + python -m pip install --upgrade pip + python -m pip install flake8 pytest + if [ -f dev-requirements.txt ]; then pip install -r dev-requirements.txt; fi + - name: Lint with flake8 + run: | + # stop the build if there are Python syntax errors or undefined names + flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics + # exit-zero treats all errors as warnings. The GitHub editor is 127 chars wide + flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics + - name: Test with pytest + run: | + pytest From fd78ec6e20380454424730f15aa51683f9da1e45 Mon Sep 17 00:00:00 2001 From: Derek Gulbranson Date: Mon, 3 Jan 2022 12:27:06 -0800 Subject: [PATCH 55/99] bump to version 1.1.0 --- docs/release_log.rst | 2 ++ nameparser/__init__.py | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/docs/release_log.rst b/docs/release_log.rst index 38e76e4..91d4068 100644 --- a/docs/release_log.rst +++ b/docs/release_log.rst @@ -1,5 +1,7 @@ Release Log =========== +* 1.1.0 - January 3, 2022 + - Add initials support (#128) * 1.0.6 - February 8, 2020 - Fix Python 3.8 syntax error (#104) * 1.0.5 - Dec 12, 2019 diff --git a/nameparser/__init__.py b/nameparser/__init__.py index 6c898ba..a9ee753 100644 --- a/nameparser/__init__.py +++ b/nameparser/__init__.py @@ -1,4 +1,4 @@ -VERSION = (1, 0, 6) +VERSION = (1, 1, 0) __version__ = '.'.join(map(str, VERSION)) __author__ = "Derek Gulbranson" __author_email__ = 'derek73@gmail.com' From 3f4c623ea934a0c5d6dd9cfdc1e79329c7bea20d Mon Sep 17 00:00:00 2001 From: Derek Gulbranson Date: Mon, 3 Jan 2022 12:29:13 -0800 Subject: [PATCH 56/99] don't lint --- .github/workflows/python-package.yml | 6 ------ 1 file changed, 6 deletions(-) diff --git a/.github/workflows/python-package.yml b/.github/workflows/python-package.yml index 5c7731e..11aaa6b 100644 --- a/.github/workflows/python-package.yml +++ b/.github/workflows/python-package.yml @@ -29,12 +29,6 @@ jobs: python -m pip install --upgrade pip python -m pip install flake8 pytest if [ -f dev-requirements.txt ]; then pip install -r dev-requirements.txt; fi - - name: Lint with flake8 - run: | - # stop the build if there are Python syntax errors or undefined names - flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics - # exit-zero treats all errors as warnings. The GitHub editor is 127 chars wide - flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics - name: Test with pytest run: | pytest From f401d9ce40a8d5dd81879e7eaa718701820a482e Mon Sep 17 00:00:00 2001 From: Derek Gulbranson Date: Mon, 3 Jan 2022 12:31:12 -0800 Subject: [PATCH 57/99] run tests --- .github/workflows/python-package.yml | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/.github/workflows/python-package.yml b/.github/workflows/python-package.yml index 11aaa6b..e6f2be7 100644 --- a/.github/workflows/python-package.yml +++ b/.github/workflows/python-package.yml @@ -27,8 +27,7 @@ jobs: - name: Install dependencies run: | python -m pip install --upgrade pip - python -m pip install flake8 pytest if [ -f dev-requirements.txt ]; then pip install -r dev-requirements.txt; fi - - name: Test with pytest + - name: Run Tests run: | - pytest + python tests.py From 95a20cdca787211de7dbf50348afa813e7789223 Mon Sep 17 00:00:00 2001 From: Derek Gulbranson Date: Mon, 3 Jan 2022 12:36:21 -0800 Subject: [PATCH 58/99] run tests back to python 3.5 --- .github/workflows/python-package.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/python-package.yml b/.github/workflows/python-package.yml index e6f2be7..721acaa 100644 --- a/.github/workflows/python-package.yml +++ b/.github/workflows/python-package.yml @@ -1,7 +1,7 @@ # This workflow will install Python dependencies, run tests and lint with a variety of Python versions # For more information see: https://help.github.com/actions/language-and-framework-guides/using-python-with-github-actions -name: Python package +name: Test the Python package on: push: @@ -16,7 +16,7 @@ jobs: strategy: fail-fast: false matrix: - python-version: ["3.8", "3.9", "3.10"] + python-version: ["3.5", "3.6", "3.7", "3.8", "3.9", "3.10"] steps: - uses: actions/checkout@v2 From 2a54e19e6b90aa8b8b49a907d3cff3f44e6918e4 Mon Sep 17 00:00:00 2001 From: Derek Gulbranson Date: Mon, 3 Jan 2022 12:48:08 -0800 Subject: [PATCH 59/99] add publish workflow --- .github/workflows/python-publish.yml | 36 ++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 .github/workflows/python-publish.yml diff --git a/.github/workflows/python-publish.yml b/.github/workflows/python-publish.yml new file mode 100644 index 0000000..b6b3cb1 --- /dev/null +++ b/.github/workflows/python-publish.yml @@ -0,0 +1,36 @@ +# This workflow will upload a Python Package using Twine when a release is created +# For more information see: https://help.github.com/en/actions/language-and-framework-guides/using-python-with-github-actions#publishing-to-package-registries + +# This workflow uses actions that are not certified by GitHub. +# They are provided by a third-party and are governed by +# separate terms of service, privacy policy, and support +# documentation. + +name: Upload Published Python Package + +on: + release: + types: [published] + +jobs: + deploy: + + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v2 + - name: Set up Python + uses: actions/setup-python@v2 + with: + python-version: '3.x' + - name: Install dependencies + run: | + python -m pip install --upgrade pip + pip install build + - name: Build package + run: python -m build + - name: Publish package + uses: pypa/gh-action-pypi-publish@27b31702a0e7fc50959f5ad993c78deac1bdfc29 + with: + user: __token__ + password: ${{ secrets.PYPI_API_TOKEN }} From 4421e7b506606ee60e24d97116c095071ca3d826 Mon Sep 17 00:00:00 2001 From: Derek Gulbranson Date: Mon, 3 Jan 2022 13:14:33 -0800 Subject: [PATCH 60/99] remove duplicate titles these titles are already present --- nameparser/config/titles.py | 3 --- 1 file changed, 3 deletions(-) diff --git a/nameparser/config/titles.py b/nameparser/config/titles.py index 91a9ac3..aa709b0 100644 --- a/nameparser/config/titles.py +++ b/nameparser/config/titles.py @@ -280,7 +280,6 @@ 'expert', 'fadm', 'family', - 'father', 'federal', 'field', 'film', @@ -318,8 +317,6 @@ 'high', 'highness', 'his', - 'his eminence', - 'his eminence metropolitan', 'historian', 'historicus', 'historien', From 1fce4857b8d0288d9a5b64ab8ec42d2ec0a4d35f Mon Sep 17 00:00:00 2001 From: Derek Gulbranson Date: Mon, 3 Jan 2022 13:26:07 -0800 Subject: [PATCH 61/99] remove duplicates, add tests --- nameparser/config/prefixes.py | 1 - tests.py | 15 +++++++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/nameparser/config/prefixes.py b/nameparser/config/prefixes.py index 99bc9f4..d4356ce 100644 --- a/nameparser/config/prefixes.py +++ b/nameparser/config/prefixes.py @@ -42,7 +42,6 @@ 'ste', 'van', 'vander', - 'van der', 'vel', 'von', 'vom', diff --git a/tests.py b/tests.py index 4e14b1d..d2d433e 100644 --- a/tests.py +++ b/tests.py @@ -1979,6 +1979,21 @@ def test_title_with_periods_lastname_comma(self): self.m(hn.first, "John", hn) self.m(hn.last, "Doe", hn) + def test_mac_with_spaces(self): + hn = HumanName("Jane Mac Beth") + self.m(hn.first, "Jane", hn) + self.m(hn.last, "Mac Beth", hn) + + def test_mac_as_first_name(self): + hn = HumanName("Mac Miller") + self.m(hn.first, "Mac", hn) + self.m(hn.last, "Miller", hn) + + def test_multiple_prefixes(self): + hn = HumanName("Mike van der Velt") + self.m(hn.first, "Mike", hn) + self.m(hn.last, "van der Velt", hn) + class HumanNameCapitalizationTestCase(HumanNameTestBase): def test_capitalization_exception_for_III(self): From 0f8fcaf735e71e4f4ac2e2ae83d3827b60cbed35 Mon Sep 17 00:00:00 2001 From: Derek Gulbranson Date: Mon, 3 Jan 2022 14:14:19 -0800 Subject: [PATCH 62/99] don't crash if regexes not defined test can override all of the constants --- nameparser/parser.py | 26 ++++++++++++-------- tests.py | 56 +++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 71 insertions(+), 11 deletions(-) diff --git a/nameparser/parser.py b/nameparser/parser.py index b6afd09..b4eb677 100644 --- a/nameparser/parser.py +++ b/nameparser/parser.py @@ -2,6 +2,7 @@ from __future__ import unicode_literals import sys +import re from operator import itemgetter from itertools import groupby @@ -467,11 +468,14 @@ def post_process(self): self.handle_capitalization() def fix_phd(self): - _re = self.C.regexes.phd - match = _re.search(self._full_name) - if match: - self.suffix_list.append(match.group(1)) - self._full_name = _re.sub('', self._full_name) + try: + _re = self.C.regexes.phd + match = _re.search(self._full_name) + if match: + self.suffix_list.append(match.group(1)) + self._full_name = _re.sub('', self._full_name) + except AttributeError: + pass def parse_nicknames(self): """ @@ -485,10 +489,12 @@ def parse_nicknames(self): Loops through 3 :py:data:`~nameparser.config.regexes.REGEXES`; `quoted_word`, `double_quotes` and `parenthesis`. """ - - re_quoted_word = self.C.regexes.quoted_word - re_double_quotes = self.C.regexes.double_quotes - re_parenthesis = self.C.regexes.parenthesis + + empty_re = re.compile("") + + re_quoted_word = self.C.regexes.quoted_word or empty_re + re_double_quotes = self.C.regexes.double_quotes or empty_re + re_parenthesis = self.C.regexes.parenthesis or empty_re for _re in (re_quoted_word, re_double_quotes, re_parenthesis): if _re.search(self._full_name): @@ -704,7 +710,7 @@ def parse_pieces(self, parts, additional_parts_count=0): # constants so they get parsed correctly later for part in output: # if this part has a period not at the beginning or end - if self.C.regexes.period_not_at_end.match(part): + if self.C.regexes.period_not_at_end and self.C.regexes.period_not_at_end.match(part): # split on periods, any of the split pieces titles or suffixes? # ("Lt.Gov.") period_chunks = part.split(".") diff --git a/tests.py b/tests.py index d2d433e..039918f 100644 --- a/tests.py +++ b/tests.py @@ -20,6 +20,7 @@ """ import logging +import re try: import dill except ImportError: @@ -27,7 +28,7 @@ from nameparser import HumanName from nameparser.util import u -from nameparser.config import Constants +from nameparser.config import Constants, TupleManager log = logging.getLogger('HumanName') @@ -199,6 +200,59 @@ def test_surnames_attribute(self): hn = HumanName("John Edgar Casey Williams III") self.m(hn.surnames, "Edgar Casey Williams", hn) + def test_override_constants(self): + C = Constants() + hn = HumanName(constants=C) + self.assertTrue(hn.C is C) + + def test_override_regex(self): + var = TupleManager([("spaces", re.compile(r"\s+", re.U)),]) + C = Constants(regexes=var) + hn = HumanName(constants=C) + self.assertTrue(hn.C.regexes == var) + + def test_override_titles(self): + var = ["abc","def"] + C = Constants(titles=var) + hn = HumanName(constants=C) + self.assertTrue(sorted(hn.C.titles) == sorted(var)) + + def test_override_first_name_titles(self): + var = ["abc","def"] + C = Constants(first_name_titles=var) + hn = HumanName(constants=C) + self.assertTrue(sorted(hn.C.first_name_titles) == sorted(var)) + + def test_override_prefixes(self): + var = ["abc","def"] + C = Constants(prefixes=var) + hn = HumanName(constants=C) + self.assertTrue(sorted(hn.C.prefixes) == sorted(var)) + + def test_override_suffix_acronyms(self): + var = ["abc","def"] + C = Constants(suffix_acronyms=var) + hn = HumanName(constants=C) + self.assertTrue(sorted(hn.C.suffix_acronyms) == sorted(var)) + + def test_override_suffix_not_acronyms(self): + var = ["abc","def"] + C = Constants(suffix_not_acronyms=var) + hn = HumanName(constants=C) + self.assertTrue(sorted(hn.C.suffix_not_acronyms) == sorted(var)) + + def test_override_conjunctions(self): + var = ["abc","def"] + C = Constants(conjunctions=var) + hn = HumanName(constants=C) + self.assertTrue(sorted(hn.C.conjunctions) == sorted(var)) + + def test_override_capitalization_exceptions(self): + var = TupleManager([("spaces", re.compile(r"\s+", re.U)),]) + C = Constants(capitalization_exceptions=var) + hn = HumanName(constants=C) + self.assertTrue(hn.C.capitalization_exceptions == var) + class FirstNameHandlingTests(HumanNameTestBase): def test_first_name(self): From 322d5e6523fedfa8fdaf815d785dd52dd8ef81b0 Mon Sep 17 00:00:00 2001 From: Derek Gulbranson Date: Mon, 3 Jan 2022 14:14:30 -0800 Subject: [PATCH 63/99] Update release_log.rst --- docs/release_log.rst | 1 + 1 file changed, 1 insertion(+) diff --git a/docs/release_log.rst b/docs/release_log.rst index 91d4068..57b7bf5 100644 --- a/docs/release_log.rst +++ b/docs/release_log.rst @@ -2,6 +2,7 @@ Release Log =========== * 1.1.0 - January 3, 2022 - Add initials support (#128) + - Add more titles and prefixes (#120, #127, #128, #119) * 1.0.6 - February 8, 2020 - Fix Python 3.8 syntax error (#104) * 1.0.5 - Dec 12, 2019 From 2a3edaef456e96cacf96f9d8041dd951773e86b1 Mon Sep 17 00:00:00 2001 From: Derek Gulbranson Date: Mon, 3 Jan 2022 15:01:12 -0800 Subject: [PATCH 64/99] fix #126, don't count as prefix when first name update initials processing to only exclude conjunctions and prefixes when it is not a first name. --- nameparser/parser.py | 18 ++++++++++++++---- tests.py | 15 +++++++++++++++ 2 files changed, 29 insertions(+), 4 deletions(-) diff --git a/nameparser/parser.py b/nameparser/parser.py index b4eb677..bbb5e39 100644 --- a/nameparser/parser.py +++ b/nameparser/parser.py @@ -191,10 +191,17 @@ def as_dict(self, include_empty=True): def __process_initial__(self, name_part): """ - Name parts may include prefixes or conjuctions. This function filters these from the name. - """ - parsed = " ".join([split for split in name_part.split(" ") if len(split) and not (self.is_prefix(split) or self.is_conjunction(split))]) - return parsed[0] if len(parsed) else "" + Name parts may include prefixes or conjuctions. This function filters these from the name unless it is + a first name, since first names cannot be conjunctions or prefixes. + """ + parts = name_part.split(" ") + parsed = "" + if len(parts) and not (name_part == 'first' and (self.is_prefix(parts) or self.is_conjunction(parts))): + parsed = " ".join(parts) + if len(parsed) > 0: + return parsed[0] + else: + return self.C.empty_attribute_default def initials_list(self): """ @@ -855,6 +862,9 @@ def join_on_conjunctions(self, pieces, additional_parts_count=0): # join everything after the prefix until the next prefix or suffix try: + if i == 0 and total_length >= 1: + # If it's the first piece and there are more than 1 rootnames, assume it's a first name + continue next_prefix = next(iter(filter(self.is_prefix, pieces[i + 1:]))) j = pieces.index(next_prefix) if j == i + 1: diff --git a/tests.py b/tests.py index 039918f..7b04e54 100644 --- a/tests.py +++ b/tests.py @@ -1605,6 +1605,17 @@ def test_prefix_before_two_part_last_name(self): self.m(hn.first, "pennie", hn) self.m(hn.last, "von bergen wessels", hn) + def test_prefix_is_first_name(self): + hn = HumanName("Van Johnson") + self.m(hn.first, "Van", hn) + self.m(hn.last, "Johnson", hn) + + def test_prefix_is_first_name_with_middle_name(self): + hn = HumanName("Van Jeremy Johnson") + self.m(hn.first, "Van", hn) + self.m(hn.middle, "Jeremy", hn) + self.m(hn.last, "Johnson", hn) + def test_prefix_before_two_part_last_name_with_suffix(self): hn = HumanName("pennie von bergen wessels III") self.m(hn.first, "pennie", hn) @@ -2313,6 +2324,10 @@ def test_initials_list_complex_name(self): hn = HumanName("Doe, John A. Kenneth, Jr.") self.m(hn.initials_list(), ["J", "A", "K", "D"], hn) + def test_initials_with_prefix_firstname(self): + hn = HumanName("Van Jeremy Johnson") + self.m(hn.initials_list(), ["V", "J", "J"], hn) + TEST_NAMES = ( "John Doe", From 7cae3e0962bb6b9f1bb6b75d5a234200456c5eb7 Mon Sep 17 00:00:00 2001 From: Derek Gulbranson Date: Mon, 3 Jan 2022 15:03:10 -0800 Subject: [PATCH 65/99] fix #117, add baroness title --- nameparser/config/titles.py | 1 + 1 file changed, 1 insertion(+) diff --git a/nameparser/config/titles.py b/nameparser/config/titles.py index aa709b0..28f14ba 100644 --- a/nameparser/config/titles.py +++ b/nameparser/config/titles.py @@ -117,6 +117,7 @@ 'banner', 'bard', 'baron', + 'baroness', 'barrister', 'baseball', 'bearer', From 0c037cb3d9b05b09d8deb40d069a68a03853d301 Mon Sep 17 00:00:00 2001 From: Derek Gulbranson Date: Mon, 3 Jan 2022 15:09:11 -0800 Subject: [PATCH 66/99] fix #114 add mx title --- nameparser/config/titles.py | 1 + 1 file changed, 1 insertion(+) diff --git a/nameparser/config/titles.py b/nameparser/config/titles.py index 28f14ba..e082bb8 100644 --- a/nameparser/config/titles.py +++ b/nameparser/config/titles.py @@ -430,6 +430,7 @@ 'murshid', 'musician', 'musicologist', + 'mx', 'mystery', 'nanny', 'narrator', From b3aee01edee5c4449e722e04a9ba4a06b05cb2ff Mon Sep 17 00:00:00 2001 From: Derek Gulbranson Date: Mon, 3 Jan 2022 15:10:29 -0800 Subject: [PATCH 67/99] fix #116, add cppm suffix --- nameparser/config/suffixes.py | 1 + 1 file changed, 1 insertion(+) diff --git a/nameparser/config/suffixes.py b/nameparser/config/suffixes.py index 9765b92..804f2b5 100644 --- a/nameparser/config/suffixes.py +++ b/nameparser/config/suffixes.py @@ -239,6 +239,7 @@ 'cpm', 'cpo', 'cpp', + 'cppm', 'cprc', 'cpre', 'cprp', From 5c5b3b6a2bb37ed15f4ae2fd07e0a767f8a74d54 Mon Sep 17 00:00:00 2001 From: Derek Gulbranson Date: Mon, 3 Jan 2022 15:13:53 -0800 Subject: [PATCH 68/99] fix #102, add de' to prefixes --- nameparser/config/prefixes.py | 1 + 1 file changed, 1 insertion(+) diff --git a/nameparser/config/prefixes.py b/nameparser/config/prefixes.py index d4356ce..1573baf 100644 --- a/nameparser/config/prefixes.py +++ b/nameparser/config/prefixes.py @@ -17,6 +17,7 @@ 'da', 'dal', 'de', + 'de\'', 'degli', 'dei', 'del', From 1d41f78ddcb8f02983138129e54a06df3dccdbbb Mon Sep 17 00:00:00 2001 From: Derek Gulbranson Date: Mon, 3 Jan 2022 15:30:16 -0800 Subject: [PATCH 69/99] fix #125, remove duke from titles --- nameparser/config/titles.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/nameparser/config/titles.py b/nameparser/config/titles.py index e082bb8..04746bc 100644 --- a/nameparser/config/titles.py +++ b/nameparser/config/titles.py @@ -252,7 +252,7 @@ 'druid', 'drummer', 'duchesse', - 'duke', + # 'duke', # a common first name 'dutchess', 'ecologist', 'economist', From 108298fbc2f2e87b05bd1b25d0ef276e11b9c854 Mon Sep 17 00:00:00 2001 From: Derek Gulbranson Date: Mon, 3 Jan 2022 15:32:42 -0800 Subject: [PATCH 70/99] duke is no longer a title --- tests.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests.py b/tests.py index 7b04e54..4d5cb33 100644 --- a/tests.py +++ b/tests.py @@ -1847,8 +1847,8 @@ def test_last_name_is_also_title_no_comma(self): self.m(hn.suffix, "Jr.", hn) def test_last_name_is_also_title_with_comma(self): - hn = HumanName("Duke Martin Luther King, Jr.") - self.m(hn.title, "Duke", hn) + hn = HumanName("Dr Martin Luther King, Jr.") + self.m(hn.title, "Dr", hn) self.m(hn.first, "Martin", hn) self.m(hn.middle, "Luther", hn) self.m(hn.last, "King", hn) From c240265194f2713ece710a80d60e00eeca3a2e48 Mon Sep 17 00:00:00 2001 From: Derek Gulbranson Date: Mon, 3 Jan 2022 20:26:26 -0800 Subject: [PATCH 71/99] fix #123, "al" prefix doesn't seem to conflict with the first name "Al" --- nameparser/config/prefixes.py | 1 + 1 file changed, 1 insertion(+) diff --git a/nameparser/config/prefixes.py b/nameparser/config/prefixes.py index 1573baf..0334f83 100644 --- a/nameparser/config/prefixes.py +++ b/nameparser/config/prefixes.py @@ -12,6 +12,7 @@ #: correct parsing of the last name "von bergen wessels". PREFIXES = set([ 'abu', + 'al', 'bin', 'bon', 'da', From 9101de032294a5d1f23dad3fe08bc6f6460afe22 Mon Sep 17 00:00:00 2001 From: Derek Gulbranson Date: Mon, 3 Jan 2022 20:27:51 -0800 Subject: [PATCH 72/99] fix initials for prefixes on first names Also make is_suffix, is_prefix and is_conjunction support lists --- nameparser/parser.py | 43 ++++++++++++++++++++++++++++++------------- tests.py | 16 ++++++++++++++++ 2 files changed, 46 insertions(+), 13 deletions(-) diff --git a/nameparser/parser.py b/nameparser/parser.py index bbb5e39..35f4135 100644 --- a/nameparser/parser.py +++ b/nameparser/parser.py @@ -189,17 +189,19 @@ def as_dict(self, include_empty=True): d[m] = val return d - def __process_initial__(self, name_part): + def __process_initial__(self, name_part, firstname=False): """ Name parts may include prefixes or conjuctions. This function filters these from the name unless it is a first name, since first names cannot be conjunctions or prefixes. """ parts = name_part.split(" ") - parsed = "" - if len(parts) and not (name_part == 'first' and (self.is_prefix(parts) or self.is_conjunction(parts))): - parsed = " ".join(parts) - if len(parsed) > 0: - return parsed[0] + initials = [] + if len(parts) and isinstance(parts, list): + for part in parts: + if not (self.is_prefix(part) or self.is_conjunction(part)) or firstname == True: + initials.append(part[0]) + if len(initials) > 0: + return " ".join(initials) else: return self.C.empty_attribute_default @@ -216,7 +218,7 @@ def initials_list(self): >>> name.initials_list() ["J", "D"] """ - first_initials_list = [self.__process_initial__(name) for name in self.first_list if name] + first_initials_list = [self.__process_initial__(name, True) for name in self.first_list if name] middle_initials_list = [self.__process_initial__(name) for name in self.middle_list if name] last_initials_list = [self.__process_initial__(name) for name in self.last_list if name] return first_initials_list + middle_initials_list + last_initials_list @@ -238,7 +240,7 @@ def initials(self): "B. A." """ - first_initials_list = [self.__process_initial__(name) for name in self.first_list if name] + first_initials_list = [self.__process_initial__(name, True) for name in self.first_list if name] middle_initials_list = [self.__process_initial__(name) for name in self.middle_list if name] last_initials_list = [self.__process_initial__(name) for name in self.last_list if name] @@ -378,14 +380,24 @@ def is_title(self, value): def is_conjunction(self, piece): """Is in the conjunctions set and not :py:func:`is_an_initial()`.""" - return piece.lower() in self.C.conjunctions and not self.is_an_initial(piece) + if isinstance(piece, list): + for item in piece: + if self.is_conjunction(item): + return True + else: + return piece.lower() in self.C.conjunctions and not self.is_an_initial(piece) def is_prefix(self, piece): """ Lowercase and no periods version of piece is in the :py:data:`~nameparser.config.prefixes.PREFIXES` set. """ - return lc(piece) in self.C.prefixes + if isinstance(piece, list): + for item in piece: + if self.is_prefix(item): + return True + else: + return lc(piece) in self.C.prefixes def is_roman_numeral(self, value): """ @@ -403,9 +415,14 @@ def is_suffix(self, piece): `C.suffix_acronyms`. """ # suffixes may have periods inside them like "M.D." - return ((lc(piece).replace('.', '') in self.C.suffix_acronyms) - or (lc(piece) in self.C.suffix_not_acronyms)) \ - and not self.is_an_initial(piece) + if isinstance(piece, list): + for piece in pieces: + if self.is_suffix(piece): + return True + else: + return ((lc(piece).replace('.', '') in self.C.suffix_acronyms) + or (lc(piece) in self.C.suffix_not_acronyms)) \ + and not self.is_an_initial(piece) def are_suffixes(self, pieces): """Return True if all pieces are suffixes.""" diff --git a/tests.py b/tests.py index 4d5cb33..91917a4 100644 --- a/tests.py +++ b/tests.py @@ -200,6 +200,18 @@ def test_surnames_attribute(self): hn = HumanName("John Edgar Casey Williams III") self.m(hn.surnames, "Edgar Casey Williams", hn) + def test_is_prefix_with_list(self): + hn = HumanName() + items = ['firstname', 'lastname', 'del'] + self.assertTrue(hn.is_prefix(items)) + self.assertTrue(hn.is_prefix(items[1:])) + + def test_is_conjunction_with_list(self): + hn = HumanName() + items = ['firstname', 'lastname', 'and'] + self.assertTrue(hn.is_conjunction(items)) + self.assertTrue(hn.is_conjunction(items[1:])) + def test_override_constants(self): C = Constants() hn = HumanName(constants=C) @@ -2328,6 +2340,10 @@ def test_initials_with_prefix_firstname(self): hn = HumanName("Van Jeremy Johnson") self.m(hn.initials_list(), ["V", "J", "J"], hn) + def test_initials_with_prefix(self): + hn = HumanName("Alex van Johnson") + self.m(hn.initials_list(), ["A", "J"], hn) + TEST_NAMES = ( "John Doe", From c6f94a1be9a9c8a9122b453a2ac5bc1ccb6ef5b5 Mon Sep 17 00:00:00 2001 From: Derek Gulbranson Date: Mon, 3 Jan 2022 20:50:17 -0800 Subject: [PATCH 73/99] update badge --- README.rst | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/README.rst b/README.rst index b347593..ee0c152 100644 --- a/README.rst +++ b/README.rst @@ -136,8 +136,7 @@ https://github.com/derek73/python-nameparser .. _click here to propose changes to the titles: https://github.com/derek73/python-nameparser/edit/master/nameparser/config/titles.py -.. |Build Status| image:: https://travis-ci.org/derek73/python-nameparser.svg?branch=master - :target: https://travis-ci.org/derek73/python-nameparser +.. [![Tests](https://github.com/derek73/python-nameparser/actions/workflows/python-package.yml/badge.svg)](https://github.com/derek73/python-nameparser/actions/workflows/python-package.yml) .. |PyPI| image:: https://img.shields.io/pypi/v/nameparser.svg :target: https://pypi.org/project/nameparser/ .. |Documentation| image:: https://readthedocs.org/projects/nameparser/badge/?version=latest From 3700dec4d0a95533a7e9cbc9f814b542ad5255d3 Mon Sep 17 00:00:00 2001 From: Derek Gulbranson Date: Mon, 3 Jan 2022 20:52:38 -0800 Subject: [PATCH 74/99] update badge --- README.rst | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.rst b/README.rst index ee0c152..effc51b 100644 --- a/README.rst +++ b/README.rst @@ -135,8 +135,8 @@ https://github.com/derek73/python-nameparser .. _Start a New Issue: https://github.com/derek73/python-nameparser/issues .. _click here to propose changes to the titles: https://github.com/derek73/python-nameparser/edit/master/nameparser/config/titles.py - -.. [![Tests](https://github.com/derek73/python-nameparser/actions/workflows/python-package.yml/badge.svg)](https://github.com/derek73/python-nameparser/actions/workflows/python-package.yml) +.. |Build Status| image:: https://github.com/derek73/python-nameparser/actions/workflows/python-package.yml/badge.svg + :target: https://github.com/derek73/python-nameparser/actions/workflows/python-package.yml .. |PyPI| image:: https://img.shields.io/pypi/v/nameparser.svg :target: https://pypi.org/project/nameparser/ .. |Documentation| image:: https://readthedocs.org/projects/nameparser/badge/?version=latest From e9aef6a7ae050f63efadf447e88b5964420392ad Mon Sep 17 00:00:00 2001 From: Derek Gulbranson Date: Mon, 3 Jan 2022 20:59:09 -0800 Subject: [PATCH 75/99] Update README.rst --- README.rst | 1 + 1 file changed, 1 insertion(+) diff --git a/README.rst b/README.rst index effc51b..11db547 100644 --- a/README.rst +++ b/README.rst @@ -13,6 +13,7 @@ individual components. * hn.suffix * hn.nickname * hn.surnames *(middle + last)* +* hn.initials *first initial of each name part+ Supported Name Structures ~~~~~~~~~~~~~~~~~~~~~~~~~ From da1bbc8ee1b1d82e0ff23c13c6165c3e75f9f381 Mon Sep 17 00:00:00 2001 From: huangwf0119 <73567665+huangwf0119@users.noreply.github.com> Date: Fri, 28 Jan 2022 15:59:41 +0800 Subject: [PATCH 76/99] Fix one bug in HumanName.is_suffix --- nameparser/parser.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/nameparser/parser.py b/nameparser/parser.py index 35f4135..bff64e2 100644 --- a/nameparser/parser.py +++ b/nameparser/parser.py @@ -416,8 +416,8 @@ def is_suffix(self, piece): """ # suffixes may have periods inside them like "M.D." if isinstance(piece, list): - for piece in pieces: - if self.is_suffix(piece): + for item in piece: + if self.is_suffix(item): return True else: return ((lc(piece).replace('.', '') in self.C.suffix_acronyms) From 993f7aa3c89d65165dc4b3095a2d32a2ffccb182 Mon Sep 17 00:00:00 2001 From: Derek Gulbranson Date: Fri, 28 Jan 2022 01:24:04 -0800 Subject: [PATCH 77/99] version 1.1.1 --- docs/release_log.rst | 2 ++ nameparser/__init__.py | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/docs/release_log.rst b/docs/release_log.rst index 57b7bf5..f89b8a7 100644 --- a/docs/release_log.rst +++ b/docs/release_log.rst @@ -1,5 +1,7 @@ Release Log =========== +* 1.1.1 - January 28, 2022 + - Fix bug in is_suffix handling of lists (#128) * 1.1.0 - January 3, 2022 - Add initials support (#128) - Add more titles and prefixes (#120, #127, #128, #119) diff --git a/nameparser/__init__.py b/nameparser/__init__.py index a9ee753..6439529 100644 --- a/nameparser/__init__.py +++ b/nameparser/__init__.py @@ -1,4 +1,4 @@ -VERSION = (1, 1, 0) +VERSION = (1, 1, 1) __version__ = '.'.join(map(str, VERSION)) __author__ = "Derek Gulbranson" __author_email__ = 'derek73@gmail.com' From c44a281e671fec49cf66d8ccc3bea28099f0653f Mon Sep 17 00:00:00 2001 From: Derek Gulbranson Date: Fri, 28 Jan 2022 01:25:34 -0800 Subject: [PATCH 78/99] typo --- docs/release_log.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/release_log.rst b/docs/release_log.rst index f89b8a7..954d992 100644 --- a/docs/release_log.rst +++ b/docs/release_log.rst @@ -1,7 +1,7 @@ Release Log =========== * 1.1.1 - January 28, 2022 - - Fix bug in is_suffix handling of lists (#128) + - Fix bug in is_suffix handling of lists (#129) * 1.1.0 - January 3, 2022 - Add initials support (#128) - Add more titles and prefixes (#120, #127, #128, #119) From 265d2b3b9611b41060ac550c85911be95ed7ca5b Mon Sep 17 00:00:00 2001 From: Derek Gulbranson Date: Fri, 28 Jan 2022 18:26:30 -0800 Subject: [PATCH 79/99] fix rst formatting error --- README.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.rst b/README.rst index 11db547..eebde5b 100644 --- a/README.rst +++ b/README.rst @@ -13,7 +13,7 @@ individual components. * hn.suffix * hn.nickname * hn.surnames *(middle + last)* -* hn.initials *first initial of each name part+ +* hn.initials *(first initial of each name part)* Supported Name Structures ~~~~~~~~~~~~~~~~~~~~~~~~~ From 95dc27b36c84e1d76b1ee5f7d74c815c0134fb86 Mon Sep 17 00:00:00 2001 From: Derek Gulbranson Date: Fri, 28 Jan 2022 18:45:07 -0800 Subject: [PATCH 80/99] add content type for readme --- setup.py | 1 + 1 file changed, 1 insertion(+) diff --git a/setup.py b/setup.py index ba0cc5a..2067716 100755 --- a/setup.py +++ b/setup.py @@ -15,6 +15,7 @@ def read(fname): packages = ['nameparser','nameparser.config'], description = 'A simple Python module for parsing human names into their individual components.', long_description = README, + long_description_content_type = "text/x-rst", version = nameparser.__version__, url = nameparser.__url__, author = nameparser.__author__, From 75fba9b3eada82151fccc8ed1f9f957b28d7a90e Mon Sep 17 00:00:00 2001 From: Derek Gulbranson Date: Fri, 28 Jan 2022 18:48:12 -0800 Subject: [PATCH 81/99] check package can build too --- .github/workflows/python-package.yml | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.github/workflows/python-package.yml b/.github/workflows/python-package.yml index 721acaa..d47e569 100644 --- a/.github/workflows/python-package.yml +++ b/.github/workflows/python-package.yml @@ -27,7 +27,10 @@ jobs: - name: Install dependencies run: | python -m pip install --upgrade pip + pip install twine if [ -f dev-requirements.txt ]; then pip install -r dev-requirements.txt; fi - name: Run Tests run: | python tests.py + python setup.py sdist bdist_wheel + twine check dist/* From 3077ad5efdc696649cf5cc6371cbdc6608feebe8 Mon Sep 17 00:00:00 2001 From: Derek Gulbranson Date: Fri, 28 Jan 2022 18:52:59 -0800 Subject: [PATCH 82/99] only build source dist --- .github/workflows/python-package.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/python-package.yml b/.github/workflows/python-package.yml index d47e569..ea0bfa7 100644 --- a/.github/workflows/python-package.yml +++ b/.github/workflows/python-package.yml @@ -32,5 +32,5 @@ jobs: - name: Run Tests run: | python tests.py - python setup.py sdist bdist_wheel + python setup.py sdist twine check dist/* From f1dff67cbf13697dad6cfd07c27da251b9030f03 Mon Sep 17 00:00:00 2001 From: Derek Gulbranson Date: Fri, 28 Jan 2022 19:31:45 -0800 Subject: [PATCH 83/99] silence error about duplicate index --- docs/modules.rst | 1 + 1 file changed, 1 insertion(+) diff --git a/docs/modules.rst b/docs/modules.rst index eaf3240..2056330 100644 --- a/docs/modules.rst +++ b/docs/modules.rst @@ -7,6 +7,7 @@ HumanName.parser .. py:module:: nameparser.parser .. py:class:: HumanName + :noindex: .. autoclass:: HumanName :members: From dc53c0e64276540d87a930420a4eb123cdb5c961 Mon Sep 17 00:00:00 2001 From: Derek Gulbranson Date: Fri, 28 Jan 2022 19:35:31 -0800 Subject: [PATCH 84/99] test that the docs build with sphinx --- .github/workflows/python-package.yml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.github/workflows/python-package.yml b/.github/workflows/python-package.yml index ea0bfa7..5ad4ce0 100644 --- a/.github/workflows/python-package.yml +++ b/.github/workflows/python-package.yml @@ -28,9 +28,11 @@ jobs: run: | python -m pip install --upgrade pip pip install twine + pip install sphinx if [ -f dev-requirements.txt ]; then pip install -r dev-requirements.txt; fi - name: Run Tests run: | python tests.py python setup.py sdist twine check dist/* + sphinx-build -b html docs dist/docs From 8b73ff9e0aed23285f451cfa7091e47e9835a608 Mon Sep 17 00:00:00 2001 From: Derek Gulbranson Date: Fri, 28 Jan 2022 19:40:34 -0800 Subject: [PATCH 85/99] use pip from python binary --- .github/workflows/python-package.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/python-package.yml b/.github/workflows/python-package.yml index 5ad4ce0..cf60638 100644 --- a/.github/workflows/python-package.yml +++ b/.github/workflows/python-package.yml @@ -27,8 +27,8 @@ jobs: - name: Install dependencies run: | python -m pip install --upgrade pip - pip install twine - pip install sphinx + python -m pip install twine + python -m pip install sphinx if [ -f dev-requirements.txt ]; then pip install -r dev-requirements.txt; fi - name: Run Tests run: | From 8144083eac5a7871101bafcd6ba289d402ca30f8 Mon Sep 17 00:00:00 2001 From: Edward Betts Date: Tue, 1 Feb 2022 15:27:33 +0000 Subject: [PATCH 86/99] Correct a spelling mistake --- nameparser/parser.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/nameparser/parser.py b/nameparser/parser.py index bff64e2..5e3f32f 100644 --- a/nameparser/parser.py +++ b/nameparser/parser.py @@ -191,7 +191,7 @@ def as_dict(self, include_empty=True): def __process_initial__(self, name_part, firstname=False): """ - Name parts may include prefixes or conjuctions. This function filters these from the name unless it is + Name parts may include prefixes or conjunctions. This function filters these from the name unless it is a first name, since first names cannot be conjunctions or prefixes. """ parts = name_part.split(" ") From 42292eb4b9a026afbb81f47b647a03a079b71f39 Mon Sep 17 00:00:00 2001 From: Derek Gulbranson Date: Mon, 7 Feb 2022 19:26:13 -0800 Subject: [PATCH 87/99] don't test is_title() if there's a first name --- nameparser/parser.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/nameparser/parser.py b/nameparser/parser.py index bff64e2..edeedc5 100644 --- a/nameparser/parser.py +++ b/nameparser/parser.py @@ -641,9 +641,9 @@ def parse_full_name(self): except IndexError: nxt = None - if self.is_title(piece) \ + if not self.first \ and (nxt or len(pieces) == 1) \ - and not self.first: + and self.is_title(piece): self.title_list.append(piece) continue if not self.first: From 5b1b88da5e6962d1adb9ab56a09928a7250bf6e4 Mon Sep 17 00:00:00 2001 From: Derek Gulbranson Date: Mon, 7 Feb 2022 19:27:39 -0800 Subject: [PATCH 88/99] don't test is_title() if there's a first name --- nameparser/parser.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/nameparser/parser.py b/nameparser/parser.py index d34af4e..162b044 100644 --- a/nameparser/parser.py +++ b/nameparser/parser.py @@ -681,9 +681,9 @@ def parse_full_name(self): except IndexError: nxt = None - if self.is_title(piece) \ + if not self.first \ and (nxt or len(post_comma_pieces) == 1) \ - and not self.first: + and self.is_title(piece): self.title_list.append(piece) continue if not self.first: From ab8e5b51c9da92dabdab47fbf12b9487818464d1 Mon Sep 17 00:00:00 2001 From: Derek Gulbranson Date: Mon, 7 Feb 2022 19:31:50 -0800 Subject: [PATCH 89/99] don't test is_title() if there's a first name --- nameparser/parser.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/nameparser/parser.py b/nameparser/parser.py index 162b044..2ee146c 100644 --- a/nameparser/parser.py +++ b/nameparser/parser.py @@ -591,9 +591,9 @@ def parse_full_name(self): nxt = None # title must have a next piece, unless it's just a title - if self.is_title(piece) \ + if not self.first \ and (nxt or p_len == 1) \ - and not self.first: + and self.is_title(piece): self.title_list.append(piece) continue if not self.first: From caec4ab12cb1d55205d3eb82d2b35ad7174ac901 Mon Sep 17 00:00:00 2001 From: Pavel T Date: Thu, 29 Sep 2022 11:47:39 -0400 Subject: [PATCH 90/99] fix HumanName repr for names with single quotes eg `HumanName("O'NEILL")` --- nameparser/parser.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/nameparser/parser.py b/nameparser/parser.py index 2ee146c..5d15fe7 100644 --- a/nameparser/parser.py +++ b/nameparser/parser.py @@ -150,7 +150,7 @@ def __repr__(self): if self.unparsable: _string = "<%(class)s : [ Unparsable ] >" % {'class': self.__class__.__name__, } else: - _string = "<%(class)s : [\n\ttitle: '%(title)s' \n\tfirst: '%(first)s' \n\tmiddle: '%(middle)s' \n\tlast: '%(last)s' \n\tsuffix: '%(suffix)s'\n\tnickname: '%(nickname)s'\n]>" % { + _string = "<%(class)s : [\n\ttitle: %(title)r \n\tfirst: %(first)r \n\tmiddle: %(middle)r \n\tlast: %(last)r \n\tsuffix: %(suffix)r\n\tnickname: %(nickname)r\n]>" % { 'class': self.__class__.__name__, 'title': self.title or '', 'first': self.first or '', From 593d2c44d36d09e24f32ee8a3ce4492320b05092 Mon Sep 17 00:00:00 2001 From: moomoohk Date: Wed, 5 Oct 2022 22:45:34 +0300 Subject: [PATCH 91/99] Make HumanName objects hashable --- nameparser/parser.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/nameparser/parser.py b/nameparser/parser.py index 2ee146c..bb19fb6 100644 --- a/nameparser/parser.py +++ b/nameparser/parser.py @@ -141,6 +141,9 @@ def __unicode__(self): return self.collapse_whitespace(_s).strip(', ') return " ".join(self) + def __hash__(self): + return hash(str(self)) + def __str__(self): if sys.version_info[0] >= 3: return self.__unicode__() From 3ae1f2a7100ed4f7c29b06b682665ff3a9494688 Mon Sep 17 00:00:00 2001 From: moomoohk Date: Wed, 5 Oct 2022 22:51:40 +0300 Subject: [PATCH 92/99] Update python-package.yml --- .github/workflows/python-package.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/python-package.yml b/.github/workflows/python-package.yml index cf60638..98c83f8 100644 --- a/.github/workflows/python-package.yml +++ b/.github/workflows/python-package.yml @@ -4,6 +4,7 @@ name: Test the Python package on: + workflow_dispatch: push: branches: [ master ] pull_request: From 0033f390be4d7779aaef5acfbf519dd2d8fe1d71 Mon Sep 17 00:00:00 2001 From: Derek Gulbranson Date: Sun, 13 Nov 2022 18:45:39 -0800 Subject: [PATCH 93/99] add support for name parts in the constructor, fix #140 --- docs/release_log.rst | 2 ++ nameparser/__init__.py | 2 +- nameparser/parser.py | 28 ++++++++++++++++++++++++---- tests.py | 39 ++++++++++++++++++++++++++++++++++++++- 4 files changed, 65 insertions(+), 6 deletions(-) diff --git a/docs/release_log.rst b/docs/release_log.rst index 954d992..6430d05 100644 --- a/docs/release_log.rst +++ b/docs/release_log.rst @@ -1,5 +1,7 @@ Release Log =========== +* 1.1.2 - November 13, 2022 + - Add support for attributes in constructor (#140) * 1.1.1 - January 28, 2022 - Fix bug in is_suffix handling of lists (#129) * 1.1.0 - January 3, 2022 diff --git a/nameparser/__init__.py b/nameparser/__init__.py index 6439529..eb595d6 100644 --- a/nameparser/__init__.py +++ b/nameparser/__init__.py @@ -1,4 +1,4 @@ -VERSION = (1, 1, 1) +VERSION = (1, 1, 2) __version__ = '.'.join(map(str, VERSION)) __author__ = "Derek Gulbranson" __author_email__ = 'derek73@gmail.com' diff --git a/nameparser/parser.py b/nameparser/parser.py index a9874b3..c35f55e 100644 --- a/nameparser/parser.py +++ b/nameparser/parser.py @@ -36,7 +36,10 @@ class HumanName(object): Instantiation assigns to ``full_name``, and assignment to :py:attr:`full_name` triggers :py:func:`parse_full_name`. After parsing the - name, these instance attributes are available. + name, these instance attributes are available. Alternatively, you can pass + any of the instance attributes to the constructor method and skip the parsing + process. If any of the the instance attributes are passed to the constructor + as keywords, :py:func:`parse_full_name` will not be performed. **HumanName Instance Attributes** @@ -56,6 +59,12 @@ class HumanName(object): :param str string_format: python string formatting :param str initials_format: python initials string formatting :param str initials_delimter: string delimiter for initials + :param str first: first name + :param str middle: middle name + :param str last: last name + :param str title: The title or prenominal + :param str suffix: The suffix or postnominal + :param str nickname: Nicknames """ C = CONSTANTS @@ -77,7 +86,9 @@ class HumanName(object): _full_name = '' def __init__(self, full_name="", constants=CONSTANTS, encoding=DEFAULT_ENCODING, - string_format=None, initials_format=None, initials_delimiter=None): + string_format=None, initials_format=None, initials_delimiter=None, + first=None, middle=None, last=None, title=None, suffix=None, + nickname=None): self.C = constants if type(self.C) is not type(CONSTANTS): self.C = Constants() @@ -86,8 +97,17 @@ def __init__(self, full_name="", constants=CONSTANTS, encoding=DEFAULT_ENCODING, self.string_format = string_format or self.C.string_format self.initials_format = initials_format or self.C.initials_format self.initials_delimiter = initials_delimiter or self.C.initials_delimiter - # full_name setter triggers the parse - self.full_name = full_name + if (first or middle or last or title or suffix or nickname): + self.first = first + self.middle = middle + self.last = last + self.title = title + self.suffix = suffix + self.nickname = nickname + self.unparsable = False + else: + # full_name setter triggers the parse + self.full_name = full_name def __iter__(self): return self diff --git a/tests.py b/tests.py index 91917a4..5eb1c72 100644 --- a/tests.py +++ b/tests.py @@ -2343,7 +2343,44 @@ def test_initials_with_prefix_firstname(self): def test_initials_with_prefix(self): hn = HumanName("Alex van Johnson") self.m(hn.initials_list(), ["A", "J"], hn) - + + def test_constructor_first(self): + hn = HumanName(first="TheName") + self.assertFalse(hn.unparsable) + self.m(hn.first, "TheName", hn) + + def test_constructor_middle(self): + hn = HumanName(middle="TheName") + self.assertFalse(hn.unparsable) + self.m(hn.middle, "TheName", hn) + + def test_constructor_last(self): + hn = HumanName(last="TheName") + self.assertFalse(hn.unparsable) + self.m(hn.last, "TheName", hn) + + def test_constructor_title(self): + hn = HumanName(title="TheName") + self.assertFalse(hn.unparsable) + self.m(hn.title, "TheName", hn) + + def test_constructor_suffix(self): + hn = HumanName(suffix="TheName") + self.assertFalse(hn.unparsable) + self.m(hn.suffix, "TheName", hn) + + def test_constructor_nickname(self): + hn = HumanName(nickname="TheName") + self.assertFalse(hn.unparsable) + self.m(hn.nickname, "TheName", hn) + + def test_constructor_multiple(self): + hn = HumanName(first="TheName", last="lastname", title="mytitle", full_name="donotparse") + self.assertFalse(hn.unparsable) + self.m(hn.first, "TheName", hn) + self.m(hn.last, "lastname", hn) + self.m(hn.title, "mytitle", hn) + TEST_NAMES = ( "John Doe", From c2d07184489ae733ff7dcb6c3e395cf682e7bb5b Mon Sep 17 00:00:00 2001 From: Derek Gulbranson Date: Sun, 13 Nov 2022 18:59:30 -0800 Subject: [PATCH 94/99] update release notes and resources --- docs/release_log.rst | 2 ++ docs/resources.rst | 4 ++++ 2 files changed, 6 insertions(+) diff --git a/docs/release_log.rst b/docs/release_log.rst index 6430d05..a6d6aa4 100644 --- a/docs/release_log.rst +++ b/docs/release_log.rst @@ -2,6 +2,8 @@ Release Log =========== * 1.1.2 - November 13, 2022 - Add support for attributes in constructor (#140) + - Make HumanName instances hashable (#138) + - Update repr for names with single quotes (#137) * 1.1.1 - January 28, 2022 - Fix bug in is_suffix handling of lists (#129) * 1.1.0 - January 3, 2022 diff --git a/docs/resources.rst b/docs/resources.rst index 6cc28e8..8934aae 100644 --- a/docs/resources.rst +++ b/docs/resources.rst @@ -7,6 +7,8 @@ Naming Practices and Resources * Wikipedia_Anthroponymy_ * Wikipedia_Naming_conventions_ * Wikipedia_List_Of_Titles_ + * Tussenvoegsel_ + * Family_Name_Affixes_ .. _US_Census_Surname_Data_2000: https://www.census.gov/data/developers/data-sets/surnames/2000.html .. _US_Social_Security_Administration_Baby_Names_Index: https://www.ssa.gov/oact/babynames/limits.html @@ -14,3 +16,5 @@ Naming Practices and Resources .. _Wikipedia_Anthroponymy: https://en.wikipedia.org/wiki/Anthroponymy .. _Wikipedia_Naming_conventions: http://en.wikipedia.org/wiki/Wikipedia:Naming_conventions_(people) .. _Wikipedia_List_Of_Titles: https://en.wikipedia.org/wiki/Title +.. _Tussenvoegsel: https://en.wikipedia.org/wiki/Tussenvoegsel +.. _Family_Name_Affixes : https://en.wikipedia.org/wiki/List_of_family_name_affixes From 89851f43ef9c09a1459fa87d42a3a2e47816a31e Mon Sep 17 00:00:00 2001 From: Evgeny Liskovets Date: Thu, 14 Sep 2023 12:36:50 -0400 Subject: [PATCH 95/99] Fix case when we have two same prefixes in the name --- nameparser/parser.py | 12 ++++++------ tests.py | 10 +++++++--- 2 files changed, 13 insertions(+), 9 deletions(-) diff --git a/nameparser/parser.py b/nameparser/parser.py index c35f55e..50607f6 100644 --- a/nameparser/parser.py +++ b/nameparser/parser.py @@ -36,10 +36,10 @@ class HumanName(object): Instantiation assigns to ``full_name``, and assignment to :py:attr:`full_name` triggers :py:func:`parse_full_name`. After parsing the - name, these instance attributes are available. Alternatively, you can pass + name, these instance attributes are available. Alternatively, you can pass any of the instance attributes to the constructor method and skip the parsing - process. If any of the the instance attributes are passed to the constructor - as keywords, :py:func:`parse_full_name` will not be performed. + process. If any of the the instance attributes are passed to the constructor + as keywords, :py:func:`parse_full_name` will not be performed. **HumanName Instance Attributes** @@ -536,9 +536,9 @@ def parse_nicknames(self): Loops through 3 :py:data:`~nameparser.config.regexes.REGEXES`; `quoted_word`, `double_quotes` and `parenthesis`. """ - + empty_re = re.compile("") - + re_quoted_word = self.C.regexes.quoted_word or empty_re re_double_quotes = self.C.regexes.double_quotes or empty_re re_parenthesis = self.C.regexes.parenthesis or empty_re @@ -906,7 +906,7 @@ def join_on_conjunctions(self, pieces, additional_parts_count=0): # If it's the first piece and there are more than 1 rootnames, assume it's a first name continue next_prefix = next(iter(filter(self.is_prefix, pieces[i + 1:]))) - j = pieces.index(next_prefix) + j = pieces.index(next_prefix, i+1) if j == i + 1: # if there are two prefixes in sequence, join to the following piece j += 1 diff --git a/tests.py b/tests.py index 5eb1c72..2760991 100644 --- a/tests.py +++ b/tests.py @@ -2071,6 +2071,10 @@ def test_multiple_prefixes(self): self.m(hn.first, "Mike", hn) self.m(hn.last, "van der Velt", hn) + def test_prefix_as_fist_name(self): + hh = HumanName("Van Ma Van") + self.m(hh.first, "Van Ma", hh) + self.m(hh.last, "Van", hh) class HumanNameCapitalizationTestCase(HumanNameTestBase): def test_capitalization_exception_for_III(self): @@ -2343,12 +2347,12 @@ def test_initials_with_prefix_firstname(self): def test_initials_with_prefix(self): hn = HumanName("Alex van Johnson") self.m(hn.initials_list(), ["A", "J"], hn) - + def test_constructor_first(self): hn = HumanName(first="TheName") self.assertFalse(hn.unparsable) self.m(hn.first, "TheName", hn) - + def test_constructor_middle(self): hn = HumanName(middle="TheName") self.assertFalse(hn.unparsable) @@ -2380,7 +2384,7 @@ def test_constructor_multiple(self): self.m(hn.first, "TheName", hn) self.m(hn.last, "lastname", hn) self.m(hn.title, "mytitle", hn) - + TEST_NAMES = ( "John Doe", From 609be71e72205b6c0719d8eb55386df2218daee0 Mon Sep 17 00:00:00 2001 From: Evgeny Liskovets Date: Thu, 14 Sep 2023 12:45:54 -0400 Subject: [PATCH 96/99] Rename test with better description --- nameparser/parser.py | 2 +- tests.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/nameparser/parser.py b/nameparser/parser.py index 50607f6..a5eb352 100644 --- a/nameparser/parser.py +++ b/nameparser/parser.py @@ -906,7 +906,7 @@ def join_on_conjunctions(self, pieces, additional_parts_count=0): # If it's the first piece and there are more than 1 rootnames, assume it's a first name continue next_prefix = next(iter(filter(self.is_prefix, pieces[i + 1:]))) - j = pieces.index(next_prefix, i+1) + j = pieces.index(next_prefix, i + 1) if j == i + 1: # if there are two prefixes in sequence, join to the following piece j += 1 diff --git a/tests.py b/tests.py index 2760991..be407cc 100644 --- a/tests.py +++ b/tests.py @@ -2071,7 +2071,7 @@ def test_multiple_prefixes(self): self.m(hn.first, "Mike", hn) self.m(hn.last, "van der Velt", hn) - def test_prefix_as_fist_name(self): + def test_2_same_prefixes_in_the_name(self): hh = HumanName("Van Ma Van") self.m(hh.first, "Van Ma", hh) self.m(hh.last, "Van", hh) From ed322da1eb6cfb571118453e0398a9db3b03484f Mon Sep 17 00:00:00 2001 From: Evgeny Liskovets Date: Thu, 14 Sep 2023 18:29:11 -0400 Subject: [PATCH 97/99] Change test --- tests.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/tests.py b/tests.py index be407cc..2cdd526 100644 --- a/tests.py +++ b/tests.py @@ -2072,9 +2072,10 @@ def test_multiple_prefixes(self): self.m(hn.last, "van der Velt", hn) def test_2_same_prefixes_in_the_name(self): - hh = HumanName("Van Ma Van") - self.m(hh.first, "Van Ma", hh) - self.m(hh.last, "Van", hh) + hh = HumanName("Vincent van Gogh van Beethoven") + self.m(hh.first, "Vincent", hh) + self.m(hh.middle, "van Gogh", hh) + self.m(hh.last, "van Beethoven", hh) class HumanNameCapitalizationTestCase(HumanNameTestBase): def test_capitalization_exception_for_III(self): From 42a3b7b8ceba9d5d84329970dbbaa3dcb4ce28f2 Mon Sep 17 00:00:00 2001 From: Derek Gulbranson Date: Thu, 14 Sep 2023 21:28:33 -0700 Subject: [PATCH 98/99] Update to current python versions --- .github/workflows/python-package.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/python-package.yml b/.github/workflows/python-package.yml index 98c83f8..0cc23c2 100644 --- a/.github/workflows/python-package.yml +++ b/.github/workflows/python-package.yml @@ -17,7 +17,7 @@ jobs: strategy: fail-fast: false matrix: - python-version: ["3.5", "3.6", "3.7", "3.8", "3.9", "3.10"] + python-version: ["3.7", "3.8", "3.9", "3.10", "3.11"] steps: - uses: actions/checkout@v2 From 759a1316f2fda4395714f36d777fd014dcdd51b0 Mon Sep 17 00:00:00 2001 From: Derek Gulbranson Date: Wed, 20 Sep 2023 17:05:34 -0700 Subject: [PATCH 99/99] v1.1.3 update version and release notes --- docs/release_log.rst | 2 ++ nameparser/__init__.py | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/docs/release_log.rst b/docs/release_log.rst index a6d6aa4..a0ab7ee 100644 --- a/docs/release_log.rst +++ b/docs/release_log.rst @@ -1,5 +1,7 @@ Release Log =========== +* 1.1.3 - September 20, 2023 + - Fix case when we have two same prefixes in the name ()#147) * 1.1.2 - November 13, 2022 - Add support for attributes in constructor (#140) - Make HumanName instances hashable (#138) diff --git a/nameparser/__init__.py b/nameparser/__init__.py index eb595d6..ab914e9 100644 --- a/nameparser/__init__.py +++ b/nameparser/__init__.py @@ -1,4 +1,4 @@ -VERSION = (1, 1, 2) +VERSION = (1, 1, 3) __version__ = '.'.join(map(str, VERSION)) __author__ = "Derek Gulbranson" __author_email__ = 'derek73@gmail.com'