A simple Python module for parsing human names into their individual components.
Project description
A simple Python (3.2+ & 2.6+) module for parsing human names into their individual components.
hn.title
hn.first
hn.middle
hn.last
hn.suffix
hn.nickname
hn.surnames (middle + last)
hn.initials (first initial of each name part)
Supported Name Structures
The supported name structure is generally “Title First Middle Last Suffix”, where all pieces are optional. Comma-separated format like “Last, First” is also supported.
Title Firstname “Nickname” Middle Middle Lastname Suffix
Lastname [Suffix], Title Firstname (Nickname) Middle Middle[,] Suffix [, Suffix]
Title Firstname M Lastname [Suffix], Suffix [Suffix] [, Suffix]
Instantiating the HumanName class with a string splits on commas and then spaces, classifying name parts based on placement in the string and matches against known name pieces like titles and suffixes.
It correctly handles some common conjunctions and special prefixes to last names like “del”. Titles and conjunctions can be chained together to handle complex titles like “Asst Secretary of State”. It can also try to correct capitalization of names that are all upper- or lowercase names.
It attempts the best guess that can be made with a simple, rule-based approach. Its main use case is English and it is not likely to be useful for languages that do not conform to the supported name structure. It’s not perfect, but it gets you pretty far.
Installation
pip install nameparser
If you want to try out the latest code from GitHub you can install with pip using the command below.
pip install -e git+git://github.com/derek73/python-nameparser.git#egg=nameparser
If you need to handle lists of names, check out namesparser, a compliment to this module that handles multiple names in a string.
Quick Start Example
>>> from nameparser import HumanName >>> name = HumanName("Dr. Juan Q. Xavier de la Vega III (Doc Vega)") >>> name <HumanName : [ title: 'Dr.' first: 'Juan' middle: 'Q. Xavier' last: 'de la Vega' suffix: 'III' nickname: 'Doc Vega' ]> >>> name.last 'de la Vega' >>> name.as_dict() {'last': 'de la Vega', 'suffix': 'III', 'title': 'Dr.', 'middle': 'Q. Xavier', 'nickname': 'Doc Vega', 'first': 'Juan'} >>> str(name) 'Dr. Juan Q. Xavier de la Vega III (Doc Vega)' >>> name.string_format = "{first} {last}" >>> str(name) 'Juan de la Vega'
The parser does not attempt to correct mistakes in the input. It mostly just splits on white space and puts things in buckets based on their position in the string. This also means the difference between ‘title’ and ‘suffix’ is positional, not semantic. “Dr” is a title when it comes before the name and a suffix when it comes after. (“Pre-nominal” and “post-nominal” would probably be better names.)
>>> name = HumanName("1 & 2, 3 4 5, Mr.") >>> name <HumanName : [ title: '' first: '3' middle: '4 5' last: '1 & 2' suffix: 'Mr.' nickname: '' ]>
Customization
Your project may need some adjustment for your dataset. You can do this in your own pre- or post-processing, by customizing the configured pre-defined sets of titles, prefixes, etc., or by subclassing the HumanName class. See the full documentation for more information.
Full documentation
Contributing
If you come across name piece that you think should be in the default config, you’re probably right. Start a New Issue and we can get them added.
Please let me know if there are ways this library could be structured to make it easier for you to use in your projects. Read CONTRIBUTING.md for more info on running the tests and contributing to the project.
GitHub Project
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file nameparser-1.1.3.tar.gz
.
File metadata
- Download URL: nameparser-1.1.3.tar.gz
- Upload date:
- Size: 36.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.9.18
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | aa2400ad71ccf8070675b40311a257c934659f91854b154e1ba6c264761c049d |
|
MD5 | 778b2ff3eb0251979f55206ef4514075 |
|
BLAKE2b-256 | eef6418a49f9e7a6d98f938e712b1356eaff2247bc1ce679a851ac0d02022330 |
File details
Details for the file nameparser-1.1.3-py2.py3-none-any.whl
.
File metadata
- Download URL: nameparser-1.1.3-py2.py3-none-any.whl
- Upload date:
- Size: 24.7 kB
- Tags: Python 2, Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.9.18
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 08ccda98681d59751c82052d52f185bc52f99d43e87d46b85c015a9096ecfa66 |
|
MD5 | 19965639dffc70c32a0d557a8bf26812 |
|
BLAKE2b-256 | 5c41dd01b1f0ccffe618efe2d66ecde6964b0fada9d0f4891542675a9e722498 |