Comparing and Managing Names Using name-tools module in Python


The name-tools module is a python library which provides us the tools to work with the names. These are used in many applications likely data cleaning, text processing and Natural Language Processing etc. It has a several functions for comparing and managing the names.

Installing name-tools

Before working with the name-tools, we need to install it in the python environment. The following is the process for installing the name-tools.

pip install name-tools

After executing the above code, if you see the text below, then the installation is considered successful.

Collecting name-toolsNote: you may need to restart the kernel to use updated packages.
  Downloading name_tools-0.1.7.tar.gz (8.1 kB)
  Preparing metadata (setup.py): started
  Preparing metadata (setup.py): finished with status 'done'
Building wheels for collected packages: name-tools
  Building wheel for name-tools (setup.py): started
  Building wheel for name-tools (setup.py): finished with status 'done'
  Created wheel for name-tools: filename=name_tools-0.1.7-py3-none-any.whl size=9525 sha256=3bce0771237183de499b9532c0402143d736ac303b5188c472f581e458fdff3c
  Stored in directory: c:\users\test\appdata\local\pip\cache\wheels\68\95\21\48e449be873df8448fb0f40f8f66114e54c1f6a0229c15a967
Successfully built name-tools
Installing collected packages: name-tools
Successfully installed name-tools-0.1.7

In the name-tools module, we have different methods available which can be used to compare and manage the names. Let’s see them one by one.

The split() Method

The split() method is used to split the given name into 4 parts namely prefix, first name, last name and suffixes. It takes the name as the input argument.

Example

In the following example we are trying to split the contents of the string "The Tutorialspoint learning platform" into 3 parts i.e. using the split() function.

import name_tools
name = "The Tutorialspoint learning platform"
splitted_name = name_tools.split(name)
print(splitted_name)

Output

The following is the output of the split() function of the name_tools.

('The', 'Tutorialspoint learning', 'platform', '')

Example

Let’s see one more example to understand the split() method of the name_tools module.

import name_tools
name = "Dr.Abdul kalam"
splitted_name = name_tools.split(name)
print(splitted_name)

Output

The following is the output of the split() function of the name_tools.

('Dr.', 'Abdul', 'kalam', '')

The canonicalize() Method

canonicalize() method returns the name within the canonical format, which removes the extra spaces and capitalizes the prefix, first name and suffix.

Example

When we pass the input name to the canonicalize() function then it returns the extra spaces and capitalizes the name.

import name_tools
name = "  WILLIAM SHAKESPEARE   "
canonical_name = name_tools.canonicalize(name)
print(canonical_name)

Output

The following is the output of the canonicalize() function of the name_tools module.

William Shakespeare

The match() Method

The match() method checks whether two names are similar or not and returns the score of similarity between them. It takes two strings as arguments.

Example

If we pass two separate names as the input arguments to the match() function, then the similarity check of both inputs is performed and the similarity score is returned.

import name_tools
name1 = "Tutorialspoint"
name2 = "Tutorial"
score = name_tools.match(name1,name2)
print(score)

Output

Following is the output of the match() function, which gives the similarity score.

0.0

Example

This is another example to get the similarity score of the two input names using the match() function.

import name_tools
name1 = "Python language"
name2 = "Java language"
score = name_tools.match(name1,name2)
print(score)

Output

Following is the output of the match() function, which gives the similarity score, when we run the above code.

0.6000000000000001

Updated on: 07-Aug-2023

45 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements