Skip to main content

A simple class that parses names into their constituent parts

Project description

Name Parser

This package is really a single class written as yet another solution to an already-solved problem. While writing a Django app to create estate planning documents for the law firm at which I work, I had to decide how to handle client names. While the documents use names in a natural order, referring to clients in typical given name, middle name, last name order, conventional indexing and sorting of names (in English usage, anyway) for administrative purposes necessitates a last name, first name sorting. The obvious and perfectly serviceable solution is to collect the parts of clients' names in discrete fields: a given name (or first name) field, middle name field, last name field. The parts can be easily concantenated to use names in a natural order but can just as easily be sorted by last name (or any other name part). This is the approach taken by a software suite we use in our real estate practice area and it works great. However, I find it much easier simply to enter names in a natural order. Rather than type "John" in a first name field, tab to a middle name field and type "Mark", tab to a last name field and type "Smith", and finally tab to a suffix field and type "Jr.", I'd much rather just type John Mark Smith, Jr. and get on with it. This class allows me to do just that and yet sort the names any way I'd like.

Installation

>>> python -m pip install simple_name_parser

Usage

The package contains a single class, NameParser(self, lang="es"). Typical usage begins by creating an instance of NameParser. There are actually two parsers available, one for English language names and another for Spanish language names (which are typically sorted by the paternal surname which appears next to last in the natural written order). The English parser is set as the default. For the Spanish parser, set lang to "es".

>>> from simple_name_parser import NameParser 

>>> english_parser = NameParser(lang="en")
>>> spanish_parser = NameParser(lang="es")

The NameParser class uses 2 methods:

parse_name(name: str) takes a string containing a name in natural order as an argument and returns a named tuple.

>>> english_name = "John Mark Smith, Jr."
>>> english_parser.parse_name(english_name)
Name(given_name='John', middle_name='Mark', surname='Smith', suffix='Jr.')
>>> spanish_name = "Carlos Alberto García López"
>>> spanish_parser.parse_name(spanish_name)
Name(given_name='Carlos', middle_name='Alberto', surname='García López', suffix='')

sort_names(names: list, sort_key: str="surname") takes two arguments: a list of strings containing names and a string containing a sort_key of "given_name", "middle_name", or "surname", with "surname" being the default.

>>> english_names = ["John Mark Smith", "Alfred E. Neumann", "Charles Emerson Winchester, III", "Florence Nightingale"]
>>> english_parser.sort_names(english_names, "surname")
['Alfred E. Neumann', 'Florence Nightingale', 'John Mark Smith', 'Charles Emerson Winchester, III']
>>> english_parser.sort_names(english_names, "given_name")
['Alfred E. Neumann', 'Charles Emerson Winchester, III', 'Florence Nightingale', 'John Mark Smith']
>>> spanish_names = ["Carlos Alberto García López", "María Elena Fernández Pérez", "Javier Eduardo Martínez Sánchez", "Sofía Isabel Rodríguez Díaz"]
>>> spanish_parser.sort_names(spanish_names, "surname")
['María Elena Fernández Pérez', 'Carlos Alberto García López', 'Javier Eduardo Martínez Sánchez', 'Sofía Isabel Rodríguez Díaz']

Honestly, the sort_names() method is really kind of superfluous as it's just a simple lambda sort method that's easily reproducible with just a little more code than it takes to call the method. I added it just to make my life a little easier.

This package is very, very beta. Neither parser (especially the Spanish parser) has been extensively tested, particularly in edge cases. At this point, it serves my (limited) purpose. More refinements to come ...

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

simple_name_parser-0.0.1.tar.gz (3.8 kB view details)

Uploaded Source

Built Distribution

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

simple_name_parser-0.0.1-py3-none-any.whl (4.0 kB view details)

Uploaded Python 3

File details

Details for the file simple_name_parser-0.0.1.tar.gz.

File metadata

  • Download URL: simple_name_parser-0.0.1.tar.gz
  • Upload date:
  • Size: 3.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.13.1

File hashes

Hashes for simple_name_parser-0.0.1.tar.gz
Algorithm Hash digest
SHA256 84b9e8d15a91f5f27369aa4f1af691f4e1bec7bda2d38b9a06ec510d933a0b11
MD5 606f7d3eba6e05ab10dbe77112cd0325
BLAKE2b-256 f1013f4d6e50aee34ef763ff6aeadc768ed58af1904ec5693853685ec6b0a94b

See more details on using hashes here.

File details

Details for the file simple_name_parser-0.0.1-py3-none-any.whl.

File metadata

File hashes

Hashes for simple_name_parser-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 03c1a1ea9a832d76eb86289fb87798fb3798b2580096f6597ffc4365874dcb4b
MD5 194acf7dec7e190d7cf0bcf4f3aed8af
BLAKE2b-256 51d5cc4b6b78577c43ff40f873d6ec068c417eb7201cc534aa0b805f0f12b593

See more details on using hashes here.

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