Skip to content
Related Articles

Related Articles

Comparing and Managing Names Using name-tools module in Python

View Discussion
Improve Article
Save Article
  • Last Updated : 04 Jul, 2021
View Discussion
Improve Article
Save Article

While working with documents, we can have problems in which we need to work with Names. This article discusses the name-tools library which can help to perform this task. It can be used to manage and compare names. It currently includes English and Western Style names.

Installation:

Use the below command to install the name-tools library:

pip install name_tools

Example 1: Working with split()

The split(), is used to divide the name into 4 parts namely, prefix ( Mr, Dr, etc) , first name, middle name, and suffix (Jr., II, etc).

Python3




import name_tools
  
# examples to split name
name1 = name_tools.split("Mr. Sandeep Jain Sr.")
print(name1)
  
# examples to split name
name2 = name_tools.split("Mr. Manjeet Singh")
print(name2)
  
# examples to split name
name3 = name_tools.split("Dr. Robert D III")
print(name3)

Output : 

('Mr.', 'Sandeep', 'Jain', 'Sr.')
('Mr.', 'Manjeet', 'Singh', '')
('Dr.', 'Robert', 'D', 'III')

Example 2: Working with canonicalize()

This function converts the name to its standard format: ‘Prefixes First Last, Suffixes’. The spaces are escaped and words are capitalized.

Python3




import name_tools
  
print("Demonstrating Canonicalize : ")
# examples to canonicalize name
# returns String
name1 = name_tools.canonicalize("mr. sandeep jain, sr.")
print(name1)
  
# examples to canonicalize name
# returns String
name2 = name_tools.canonicalize("mr. manjeet Singh")
print(name2)
  
# examples to canonicalize name
# returns String
name3 = name_tools.canonicalize("dr.  robert d 3")
print(name3)

Output : 

Mr. Sandeep Jain, Sr.
Mr. Manjeet Singh
Dr. Robert D 3

Example 3: Comparing names 

Comparison of names is done using match(), which takes 2 names and compares to factors such as word orders, initials, honorifics, and titles. Giving each factor a specific weight.

Python3




import name_tools
  
print("Demonstrating Matching names : ")
  
# examples to match name
# returns percentage similarity
match1 = name_tools.match("Mr Sandeep", "Sandeep")
print(match1)
  
match2 = name_tools.match("Mr. Sandeep Jain", "Dr. Sandeep Jain")
print(match2)
  
match3 = name_tools.match("Mr. Jain Sandeep", "Mr. Sandeep Jain")
print(match3)

Output : 

0.95
0.8999999999999999
0.0

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!