nameparser 1.2.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- nameparser/__init__.py +7 -0
- nameparser/_version.py +2 -0
- nameparser/config/__init__.py +288 -0
- nameparser/config/capitalization.py +10 -0
- nameparser/config/conjunctions.py +15 -0
- nameparser/config/prefixes.py +47 -0
- nameparser/config/regexes.py +28 -0
- nameparser/config/suffixes.py +654 -0
- nameparser/config/titles.py +635 -0
- nameparser/parser.py +995 -0
- nameparser/py.typed +0 -0
- nameparser/util.py +17 -0
- nameparser-1.2.0.dist-info/METADATA +174 -0
- nameparser-1.2.0.dist-info/RECORD +18 -0
- nameparser-1.2.0.dist-info/WHEEL +5 -0
- nameparser-1.2.0.dist-info/licenses/AUTHORS +1 -0
- nameparser-1.2.0.dist-info/licenses/LICENSE +16 -0
- nameparser-1.2.0.dist-info/top_level.txt +1 -0
nameparser/__init__.py
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
from nameparser._version import VERSION as VERSION
|
|
2
|
+
from nameparser._version import __version__ as __version__
|
|
3
|
+
from nameparser.parser import HumanName as HumanName
|
|
4
|
+
__author__ = "Derek Gulbranson"
|
|
5
|
+
__author_email__ = 'derek73@gmail.com'
|
|
6
|
+
__license__ = "LGPL"
|
|
7
|
+
__url__ = "https://github.com/derek73/python-nameparser"
|
nameparser/_version.py
ADDED
|
@@ -0,0 +1,288 @@
|
|
|
1
|
+
"""
|
|
2
|
+
The :py:mod:`nameparser.config` module manages the configuration of the
|
|
3
|
+
nameparser.
|
|
4
|
+
|
|
5
|
+
A module-level instance of :py:class:`~nameparser.config.Constants` is created
|
|
6
|
+
and used by default for all HumanName instances. You can adjust the entire module's
|
|
7
|
+
configuration by importing this instance and changing it.
|
|
8
|
+
|
|
9
|
+
::
|
|
10
|
+
|
|
11
|
+
>>> from nameparser.config import CONSTANTS
|
|
12
|
+
>>> CONSTANTS.titles.remove('hon').add('chemistry','dean') # doctest: +ELLIPSIS
|
|
13
|
+
SetManager({'msgt', ..., 'adjutant'})
|
|
14
|
+
|
|
15
|
+
You can also adjust the configuration of individual instances by passing
|
|
16
|
+
``None`` as the second argument upon instantiation.
|
|
17
|
+
|
|
18
|
+
::
|
|
19
|
+
|
|
20
|
+
>>> from nameparser import HumanName
|
|
21
|
+
>>> hn = HumanName("Dean Robert Johns", None)
|
|
22
|
+
>>> hn.C.titles.add('dean') # doctest: +ELLIPSIS
|
|
23
|
+
SetManager({'msgt', ..., 'adjutant'})
|
|
24
|
+
>>> hn.parse_full_name() # need to run this again after config changes
|
|
25
|
+
|
|
26
|
+
**Potential Gotcha**: If you do not pass ``None`` as the second argument,
|
|
27
|
+
``hn.C`` will be a reference to the module config, possibly yielding
|
|
28
|
+
unexpected results. See `Customizing the Parser <customize.html>`_.
|
|
29
|
+
"""
|
|
30
|
+
import re
|
|
31
|
+
import sys
|
|
32
|
+
from collections.abc import Iterable, Iterator, Mapping, Set
|
|
33
|
+
from typing import Any, TypeVar
|
|
34
|
+
|
|
35
|
+
if sys.version_info >= (3, 11):
|
|
36
|
+
from typing import Self
|
|
37
|
+
else:
|
|
38
|
+
from typing_extensions import Self
|
|
39
|
+
|
|
40
|
+
from nameparser.util import lc
|
|
41
|
+
from nameparser.config.prefixes import PREFIXES
|
|
42
|
+
from nameparser.config.capitalization import CAPITALIZATION_EXCEPTIONS
|
|
43
|
+
from nameparser.config.conjunctions import CONJUNCTIONS
|
|
44
|
+
from nameparser.config.suffixes import SUFFIX_ACRONYMS
|
|
45
|
+
from nameparser.config.suffixes import SUFFIX_NOT_ACRONYMS
|
|
46
|
+
from nameparser.config.titles import TITLES
|
|
47
|
+
from nameparser.config.titles import FIRST_NAME_TITLES
|
|
48
|
+
from nameparser.config.regexes import EMPTY_REGEX, REGEXES
|
|
49
|
+
|
|
50
|
+
DEFAULT_ENCODING = 'UTF-8'
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
class SetManager(Set):
|
|
54
|
+
'''
|
|
55
|
+
Easily add and remove config variables per module or instance. Subclass of
|
|
56
|
+
``collections.abc.Set``.
|
|
57
|
+
|
|
58
|
+
Only special functionality beyond that provided by set() is
|
|
59
|
+
to normalize constants for comparison (lower case, no periods)
|
|
60
|
+
when they are add()ed and remove()d and allow passing multiple
|
|
61
|
+
string arguments to the :py:func:`add()` and :py:func:`remove()` methods.
|
|
62
|
+
|
|
63
|
+
'''
|
|
64
|
+
|
|
65
|
+
def __init__(self, elements: Iterable[str]) -> None:
|
|
66
|
+
self.elements = set(elements)
|
|
67
|
+
|
|
68
|
+
def __call__(self) -> Set[str]:
|
|
69
|
+
return self.elements
|
|
70
|
+
|
|
71
|
+
def __repr__(self) -> str:
|
|
72
|
+
return "SetManager({})".format(self.elements) # used for docs
|
|
73
|
+
|
|
74
|
+
def __iter__(self) -> Iterator[str]:
|
|
75
|
+
return iter(self.elements)
|
|
76
|
+
|
|
77
|
+
def __contains__(self, value: object) -> bool:
|
|
78
|
+
return value in self.elements
|
|
79
|
+
|
|
80
|
+
def __len__(self) -> int:
|
|
81
|
+
return len(self.elements)
|
|
82
|
+
|
|
83
|
+
def add_with_encoding(self, s: str, encoding: str | None = None) -> None:
|
|
84
|
+
"""
|
|
85
|
+
Add the lower case and no-period version of the string to the set. Pass an
|
|
86
|
+
explicit `encoding` parameter to specify the encoding of binary strings that
|
|
87
|
+
are not DEFAULT_ENCODING (UTF-8).
|
|
88
|
+
"""
|
|
89
|
+
stdin_encoding = None
|
|
90
|
+
if sys.stdin:
|
|
91
|
+
stdin_encoding = sys.stdin.encoding
|
|
92
|
+
encoding = encoding or stdin_encoding or DEFAULT_ENCODING
|
|
93
|
+
if isinstance(s, bytes):
|
|
94
|
+
s = s.decode(encoding)
|
|
95
|
+
self.elements.add(lc(s))
|
|
96
|
+
|
|
97
|
+
def add(self, *strings: str) -> Self:
|
|
98
|
+
"""
|
|
99
|
+
Add the lower case and no-period version of the string arguments to the set.
|
|
100
|
+
Can pass a list of strings. Returns ``self`` for chaining.
|
|
101
|
+
"""
|
|
102
|
+
for s in strings:
|
|
103
|
+
self.add_with_encoding(s)
|
|
104
|
+
|
|
105
|
+
return self
|
|
106
|
+
|
|
107
|
+
def remove(self, *strings: str) -> Self:
|
|
108
|
+
"""
|
|
109
|
+
Remove the lower case and no-period version of the string arguments from the set.
|
|
110
|
+
Returns ``self`` for chaining.
|
|
111
|
+
"""
|
|
112
|
+
for s in strings:
|
|
113
|
+
if (lower := lc(s)) in self.elements:
|
|
114
|
+
self.elements.remove(lower)
|
|
115
|
+
|
|
116
|
+
return self
|
|
117
|
+
|
|
118
|
+
|
|
119
|
+
T = TypeVar('T')
|
|
120
|
+
|
|
121
|
+
|
|
122
|
+
class TupleManager(dict[str, T]):
|
|
123
|
+
'''
|
|
124
|
+
A dictionary with dot.notation access. Subclass of ``dict``. Makes the tuple constants
|
|
125
|
+
more friendly.
|
|
126
|
+
'''
|
|
127
|
+
|
|
128
|
+
def __getattr__(self, attr: str) -> T | None:
|
|
129
|
+
return self.get(attr)
|
|
130
|
+
|
|
131
|
+
__setattr__ = dict.__setitem__
|
|
132
|
+
__delattr__ = dict.__delitem__
|
|
133
|
+
|
|
134
|
+
def __getstate__(self) -> Mapping[str, T]:
|
|
135
|
+
return dict(self)
|
|
136
|
+
|
|
137
|
+
def __setstate__(self, state: Mapping[str, T]) -> None:
|
|
138
|
+
self.update(state)
|
|
139
|
+
|
|
140
|
+
def __reduce__(self) -> tuple[type, tuple[()], Mapping[str, T]]:
|
|
141
|
+
return (TupleManager, (), self.__getstate__())
|
|
142
|
+
|
|
143
|
+
|
|
144
|
+
class RegexTupleManager(TupleManager[re.Pattern[str]]):
|
|
145
|
+
def __getattr__(self, attr: str) -> re.Pattern[str]:
|
|
146
|
+
return self.get(attr, EMPTY_REGEX)
|
|
147
|
+
|
|
148
|
+
|
|
149
|
+
class Constants:
|
|
150
|
+
"""
|
|
151
|
+
An instance of this class hold all of the configuration constants for the parser.
|
|
152
|
+
|
|
153
|
+
:param set prefixes:
|
|
154
|
+
:py:attr:`prefixes` wrapped with :py:class:`SetManager`.
|
|
155
|
+
:param set titles:
|
|
156
|
+
:py:attr:`titles` wrapped with :py:class:`SetManager`.
|
|
157
|
+
:param set first_name_titles:
|
|
158
|
+
:py:attr:`~titles.FIRST_NAME_TITLES` wrapped with :py:class:`SetManager`.
|
|
159
|
+
:param set suffix_acronyms:
|
|
160
|
+
:py:attr:`~suffixes.SUFFIX_ACRONYMS` wrapped with :py:class:`SetManager`.
|
|
161
|
+
:param set suffix_not_acronyms:
|
|
162
|
+
:py:attr:`~suffixes.SUFFIX_NOT_ACRONYMS` wrapped with :py:class:`SetManager`.
|
|
163
|
+
:param set conjunctions:
|
|
164
|
+
:py:attr:`conjunctions` wrapped with :py:class:`SetManager`.
|
|
165
|
+
:type capitalization_exceptions: tuple or dict
|
|
166
|
+
:param capitalization_exceptions:
|
|
167
|
+
:py:attr:`~capitalization.CAPITALIZATION_EXCEPTIONS` wrapped with :py:class:`TupleManager`.
|
|
168
|
+
:type regexes: tuple or dict
|
|
169
|
+
:param regexes:
|
|
170
|
+
:py:attr:`regexes` wrapped with :py:class:`TupleManager`.
|
|
171
|
+
"""
|
|
172
|
+
|
|
173
|
+
prefixes: SetManager
|
|
174
|
+
suffix_acronyms: SetManager
|
|
175
|
+
suffix_not_acronyms: SetManager
|
|
176
|
+
titles: SetManager
|
|
177
|
+
first_name_titles: SetManager
|
|
178
|
+
conjunctions: SetManager
|
|
179
|
+
capitalization_exceptions: TupleManager[str]
|
|
180
|
+
regexes: RegexTupleManager
|
|
181
|
+
|
|
182
|
+
_pst: Set[str] | None
|
|
183
|
+
|
|
184
|
+
string_format = "{title} {first} {middle} {last} {suffix} ({nickname})"
|
|
185
|
+
"""
|
|
186
|
+
The default string format use for all new `HumanName` instances.
|
|
187
|
+
"""
|
|
188
|
+
|
|
189
|
+
initials_format = "{first} {middle} {last}"
|
|
190
|
+
"""
|
|
191
|
+
The default initials format used for all new `HumanName` instances.
|
|
192
|
+
"""
|
|
193
|
+
|
|
194
|
+
initials_delimiter = "."
|
|
195
|
+
"""
|
|
196
|
+
The default initials delimiter used for all new `HumanName` instances.
|
|
197
|
+
Will be used to add a delimiter between each initial.
|
|
198
|
+
"""
|
|
199
|
+
|
|
200
|
+
empty_attribute_default = ''
|
|
201
|
+
"""
|
|
202
|
+
Default return value for empty attributes.
|
|
203
|
+
|
|
204
|
+
.. doctest::
|
|
205
|
+
|
|
206
|
+
>>> from nameparser.config import CONSTANTS
|
|
207
|
+
>>> CONSTANTS.empty_attribute_default = None
|
|
208
|
+
>>> name = HumanName("John Doe")
|
|
209
|
+
>>> name.title
|
|
210
|
+
None
|
|
211
|
+
>>>name.first
|
|
212
|
+
'John'
|
|
213
|
+
|
|
214
|
+
"""
|
|
215
|
+
|
|
216
|
+
capitalize_name = False
|
|
217
|
+
"""
|
|
218
|
+
If set, applies :py:meth:`~nameparser.parser.HumanName.capitalize` to
|
|
219
|
+
:py:class:`~nameparser.parser.HumanName` instance.
|
|
220
|
+
|
|
221
|
+
.. doctest::
|
|
222
|
+
|
|
223
|
+
>>> from nameparser.config import CONSTANTS
|
|
224
|
+
>>> CONSTANTS.capitalize_name = True
|
|
225
|
+
>>> name = HumanName("bob v. de la macdole-eisenhower phd")
|
|
226
|
+
>>> str(name)
|
|
227
|
+
'Bob V. de la MacDole-Eisenhower Ph.D.'
|
|
228
|
+
|
|
229
|
+
"""
|
|
230
|
+
|
|
231
|
+
force_mixed_case_capitalization = False
|
|
232
|
+
"""
|
|
233
|
+
If set, forces the capitalization of mixed case strings when
|
|
234
|
+
:py:meth:`~nameparser.parser.HumanName.capitalize` is called.
|
|
235
|
+
|
|
236
|
+
.. doctest::
|
|
237
|
+
|
|
238
|
+
>>> from nameparser.config import CONSTANTS
|
|
239
|
+
>>> CONSTANTS.force_mixed_case_capitalization = True
|
|
240
|
+
>>> name = HumanName('Shirley Maclaine')
|
|
241
|
+
>>> name.capitalize()
|
|
242
|
+
>>> str(name)
|
|
243
|
+
'Shirley MacLaine'
|
|
244
|
+
|
|
245
|
+
"""
|
|
246
|
+
|
|
247
|
+
def __init__(self,
|
|
248
|
+
prefixes: Iterable[str] = PREFIXES,
|
|
249
|
+
suffix_acronyms: Iterable[str] = SUFFIX_ACRONYMS,
|
|
250
|
+
suffix_not_acronyms: Iterable[str] = SUFFIX_NOT_ACRONYMS,
|
|
251
|
+
titles: Iterable[str] = TITLES,
|
|
252
|
+
first_name_titles: Iterable[str] = FIRST_NAME_TITLES,
|
|
253
|
+
conjunctions: Iterable[str] = CONJUNCTIONS,
|
|
254
|
+
capitalization_exceptions: TupleManager[str] | Iterable[tuple[str, str]] = CAPITALIZATION_EXCEPTIONS,
|
|
255
|
+
regexes: RegexTupleManager | TupleManager[re.Pattern[str]] | Iterable[tuple[str, re.Pattern[str]]] = REGEXES
|
|
256
|
+
) -> None:
|
|
257
|
+
self.prefixes = SetManager(prefixes)
|
|
258
|
+
self.suffix_acronyms = SetManager(suffix_acronyms)
|
|
259
|
+
self.suffix_not_acronyms = SetManager(suffix_not_acronyms)
|
|
260
|
+
self.titles = SetManager(titles)
|
|
261
|
+
self.first_name_titles = SetManager(first_name_titles)
|
|
262
|
+
self.conjunctions = SetManager(conjunctions)
|
|
263
|
+
self.capitalization_exceptions = TupleManager(capitalization_exceptions)
|
|
264
|
+
self.regexes = RegexTupleManager(regexes)
|
|
265
|
+
self._pst = None
|
|
266
|
+
|
|
267
|
+
@property
|
|
268
|
+
def suffixes_prefixes_titles(self) -> Set[str]:
|
|
269
|
+
if not self._pst:
|
|
270
|
+
self._pst = self.prefixes | self.suffix_acronyms | self.suffix_not_acronyms | self.titles
|
|
271
|
+
return self._pst
|
|
272
|
+
|
|
273
|
+
def __repr__(self) -> str:
|
|
274
|
+
return "<Constants() instance>"
|
|
275
|
+
|
|
276
|
+
def __setstate__(self, state: Mapping[str, Any]) -> None:
|
|
277
|
+
Constants.__init__(self, state)
|
|
278
|
+
|
|
279
|
+
def __getstate__(self) -> Mapping[str, Any]:
|
|
280
|
+
attrs = [x for x in dir(self) if not x.startswith('_')]
|
|
281
|
+
return dict([(a, getattr(self, a)) for a in attrs])
|
|
282
|
+
|
|
283
|
+
|
|
284
|
+
#: A module-level instance of the :py:class:`Constants()` class.
|
|
285
|
+
#: Provides a common instance for the module to share
|
|
286
|
+
#: to easily adjust configuration for the entire module.
|
|
287
|
+
#: See `Customizing the Parser with Your Own Configuration <customize.html>`_.
|
|
288
|
+
CONSTANTS = Constants()
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
CONJUNCTIONS = set([
|
|
2
|
+
'&',
|
|
3
|
+
'and',
|
|
4
|
+
'et',
|
|
5
|
+
'e',
|
|
6
|
+
'of',
|
|
7
|
+
'the',
|
|
8
|
+
'und',
|
|
9
|
+
'y',
|
|
10
|
+
])
|
|
11
|
+
"""
|
|
12
|
+
Pieces that should join to their neighboring pieces, e.g. "and", "y" and "&".
|
|
13
|
+
"of" and "the" are also include to facilitate joining multiple titles,
|
|
14
|
+
e.g. "President of the United States".
|
|
15
|
+
"""
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
#: Name pieces that appear before a last name. Prefixes join to the piece
|
|
2
|
+
#: that follows them to make one new piece. They can be chained together, e.g
|
|
3
|
+
#: "von der" and "de la". Because they only appear in middle or last names,
|
|
4
|
+
#: they also signify that all following name pieces should be in the same name
|
|
5
|
+
#: part, for example, "von" will be joined to all following pieces that are not
|
|
6
|
+
#: prefixes or suffixes, allowing recognition of double last names when they
|
|
7
|
+
#: appear after a prefixes. So in "pennie von bergen wessels MD", "von" will
|
|
8
|
+
#: join with all following name pieces until the suffix "MD", resulting in the
|
|
9
|
+
#: correct parsing of the last name "von bergen wessels".
|
|
10
|
+
PREFIXES = set([
|
|
11
|
+
'abu',
|
|
12
|
+
'al',
|
|
13
|
+
'bin',
|
|
14
|
+
'bon',
|
|
15
|
+
'da',
|
|
16
|
+
'dal',
|
|
17
|
+
'de',
|
|
18
|
+
'de\'',
|
|
19
|
+
'degli',
|
|
20
|
+
'dei',
|
|
21
|
+
'del',
|
|
22
|
+
'dela',
|
|
23
|
+
'della',
|
|
24
|
+
'delle',
|
|
25
|
+
'delli',
|
|
26
|
+
'dello',
|
|
27
|
+
'der',
|
|
28
|
+
'di',
|
|
29
|
+
'dí',
|
|
30
|
+
'do',
|
|
31
|
+
'dos',
|
|
32
|
+
'du',
|
|
33
|
+
'ibn',
|
|
34
|
+
'la',
|
|
35
|
+
'le',
|
|
36
|
+
'mac',
|
|
37
|
+
'mc',
|
|
38
|
+
'san',
|
|
39
|
+
'santa',
|
|
40
|
+
'st',
|
|
41
|
+
'ste',
|
|
42
|
+
'van',
|
|
43
|
+
'vander',
|
|
44
|
+
'vel',
|
|
45
|
+
'von',
|
|
46
|
+
'vom',
|
|
47
|
+
])
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import re
|
|
2
|
+
|
|
3
|
+
# emoji regex from https://stackoverflow.com/questions/26568722/remove-unicode-emoji-using-re-in-python
|
|
4
|
+
re_emoji = re.compile('[' # lgtm[py/overly-large-range]
|
|
5
|
+
'\U0001F300-\U0001F64F'
|
|
6
|
+
'\U0001F680-\U0001F6FF'
|
|
7
|
+
'\u2600-\u26FF\u2700-\u27BF]+',
|
|
8
|
+
re.UNICODE)
|
|
9
|
+
|
|
10
|
+
EMPTY_REGEX = re.compile('')
|
|
11
|
+
|
|
12
|
+
REGEXES = set([
|
|
13
|
+
("spaces", re.compile(r"\s+", re.U)),
|
|
14
|
+
("word", re.compile(r"(\w|\.)+", re.U)),
|
|
15
|
+
("mac", re.compile(r'^(ma?c)(\w{2,})', re.I | re.U)),
|
|
16
|
+
("initial", re.compile(r'^(\w\.|[A-Z])?$', re.U)),
|
|
17
|
+
("quoted_word", re.compile(r'(?<!\w)\'([^\s]*?)\'(?!\w)', re.U)),
|
|
18
|
+
("double_quotes", re.compile(r'\"(.*?)\"', re.U)),
|
|
19
|
+
("parenthesis", re.compile(r'\((.*?)\)', re.U)),
|
|
20
|
+
("roman_numeral", re.compile(r'^(X|IX|IV|V?I{0,3})$', re.I | re.U)),
|
|
21
|
+
("no_vowels",re.compile(r'^[^aeyiuo]+$', re.I | re.U)),
|
|
22
|
+
("period_not_at_end",re.compile(r'.*\..+$', re.I | re.U)),
|
|
23
|
+
("emoji",re_emoji),
|
|
24
|
+
("phd", re.compile(r'\s(ph\.?\s+d\.?)', re.I | re.U)),
|
|
25
|
+
])
|
|
26
|
+
"""
|
|
27
|
+
All regular expressions used by the parser are precompiled and stored in the config.
|
|
28
|
+
"""
|