Skip to main content

A simple Python module for parsing human names into their individual components.

Project description

Build Status PyPI PyPI version Documentation License Downloads Codecov

A simple Python (3.10+) module for parsing human names into their individual components.

  • hn.title

  • hn.first

  • hn.middle

  • hn.last

  • hn.suffix

  • hn.nickname

  • hn.maiden

  • hn.surnames (middle + last)

  • hn.given_names (first + middle)

  • hn.initials (first initial of each name part)

  • hn.last_base (last, minus any prefixes)

  • hn.last_prefixes (leading last-name particles, e.g. “van der”)

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.

  1. Title Firstname “Nickname” Middle Middle Lastname Suffix

  2. Lastname [Suffix], Title Firstname (Nickname) Middle Middle[,] Suffix [, Suffix]

  3. Title Firstname M Lastname [Suffix], Suffix [Suffix] [, Suffix]

How It Works

The parser works in two layers.

A vocabulary layer recognizes name pieces by what they are, using configurable sets of known words: titles (“Dr.”), suffixes (“III”, “PhD”), last-name prefixes (“de la”), conjunctions (“y”, “&”), and delimited nicknames (“Doc”). Titles and conjunctions chain together to handle complex titles like “Asst Secretary of State”; prefixes join forward so “de la Vega” stays one last name. This layer doesn’t care where in the string a word appears — and it’s the layer you customize, by adding or removing entries in the sets to fit your dataset.

A positional layer then assigns everything the vocabulary layer didn’t claim, based purely on where it sits: the first unclaimed word is the first name, the last one is the last name, and anything between them is a middle name. There is no semantic understanding — “Dr” is a title when it comes before a name and a suffix when it comes after (“pre-nominal” and “post-nominal” would probably be better names) — and no attempt to correct mistakes in the input.

It attempts the best guess that can be made with a simple, deterministic, rule-based approach — no statistical models or machine learning; the same input always parses the same way. The positional layer assumes Western name order (given name first), so the main use case is English and other languages that share that structure. It can also try to correct the capitalization of names that are all upper- or lowercase. 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+https://github.com/derek73/python-nameparser.git

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'
    maiden: ''
]>
>>> name.last
'de la Vega'
>>> name.as_dict()
{'title': 'Dr.', 'first': 'Juan', 'middle': 'Q. Xavier', 'last': 'de la Vega', 'suffix': 'III', 'nickname': 'Doc Vega', 'maiden': ''}
>>> str(name)
'Dr. Juan Q. Xavier de la Vega III (Doc Vega)'
>>> name.string_format = "{first} {last}"
>>> str(name)
'Juan de la Vega'

Because the positional layer has no semantic understanding, position is everything:

>>> name = HumanName("1 & 2, 3 4 5, Mr.")
>>> name
<HumanName : [
    title: ''
    first: '3'
    middle: '4 5'
    last: '1 & 2'
    suffix: 'Mr.'
    nickname: ''
    maiden: ''
]>

Customization

Your project may need some adjustment for your dataset. Most customization is vocabulary — customizing the configured pre-defined sets of titles, prefixes, etc. that the vocabulary layer matches against. You can also do your own pre- or post-processing, or subclass the HumanName class for deeper changes. 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

https://github.com/derek73/python-nameparser

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

nameparser-1.3.1.tar.gz (95.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

nameparser-1.3.1-py3-none-any.whl (46.5 kB view details)

Uploaded Python 3

File details

Details for the file nameparser-1.3.1.tar.gz.

File metadata

  • Download URL: nameparser-1.3.1.tar.gz
  • Upload date:
  • Size: 95.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for nameparser-1.3.1.tar.gz
Algorithm Hash digest
SHA256 482d866f97a15be23477c1bccaa3e2fc1ee1d879440f1f1607912f914f68bdcc
MD5 a4b0d949ff35caec6a46005c5056a83c
BLAKE2b-256 145a1c03a5fb03a35fbef912378e4e8c75a07c281182f876401ead254b38a6fa

See more details on using hashes here.

Provenance

The following attestation bundles were made for nameparser-1.3.1.tar.gz:

Publisher: python-publish.yml on derek73/python-nameparser

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file nameparser-1.3.1-py3-none-any.whl.

File metadata

  • Download URL: nameparser-1.3.1-py3-none-any.whl
  • Upload date:
  • Size: 46.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for nameparser-1.3.1-py3-none-any.whl
Algorithm Hash digest
SHA256 923ecb91d56968efb806bdf167fe0360c00bafbe26eccdd312cf0e45b405e731
MD5 52c65217b812c426c62593f2eb3ef1be
BLAKE2b-256 91a4d62f59d2a829c3d406bed0c748a8077066f7a92b67ddaa6cf0120219199d

See more details on using hashes here.

Provenance

The following attestation bundles were made for nameparser-1.3.1-py3-none-any.whl:

Publisher: python-publish.yml on derek73/python-nameparser

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page