Comparing and Managing Names Using name-tools module in Python
Last Updated :
04 Jul, 2021
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
Similar Reads
Change Object Display Name using __str__ function - Django Models | Python When you create instances of a Django model, by default, they appear in the Django admin interface and elsewhere as "ModelName object (1)" (or a similar format). This can make it hard to identify records, especially when you have many objects.Why Customize Object Display Names?By default, Django doe
2 min read
Compare sequences in Python using dfflib module The dfflib Python module includes various features to evaluate the comparison of sequences, it can be used to compare files, and it can create information about file variations in different formats, including HTML and context and unified diffs. It contains various classes to perform various comparis
5 min read
How to do Fuzzy Matching on Pandas Dataframe Column Using Python? Prerequisite: FuzzyWuzzy In this tutorial, we will learn how to do fuzzy matching on the pandas DataFrame column using Python. Fuzzy matching is a process that lets us identify the matches which are not exact but find a given pattern in our target item. Fuzzy matching is the basis of search engines.
9 min read
Get OS name and version in Python Python programming has several modules that can be used to retrieve information about the current operating system and the version that is running on the system. In this article we will explore How to get the OS name and version in Python.Let us see a simple example to get the OS name and version in
2 min read
Get tag name using Beautifulsoup in Python Prerequisite: Beautifulsoup Installation Name property is provided by Beautiful Soup which is a web scraping framework for Python. Web scraping is the process of extracting data from the website using automated tools to make the process faster. Name object corresponds to the name of an XML or HTML t
1 min read
Python IMDbPY â Getting Person name from searched name In this article we will see how we can get the person name from the searched list of name, we use search_name method to find all the related names.search_name method returns list and each element of list work as a dictionary i.e. they can be queried by giving the key of the data, here key will be na
2 min read