In this article, we will see how to pluralize a given word using Inflect module of Python.
Various ways to pluralize a given word
- Using regex
- Using NLTK and Pattern-en package
- Using Textblob
- Using inflect
Method 1: Using regex to pluralize a given word in Python
Regex Python module which finds a string or set of strings using a certain pattern is known as the regex module. This module usually comes preinstalled with Python, but in case it doesn’t exist in your Python.
Stepwise Implementation:
Step 1: First of all, import the re library.
from re import *
Step 2: Now, define the word and declare it in a variable.
word="#Define the word"
Step 3: Then, run an if-else loop to check if the string contains certain alphabets in end and replace them with certain characters to make it plural. In this step, we check if a word is ending with s,x, or z or ends with ‘ah’, ‘eh’, ‘ih’, ‘oh’, ‘uh’, ‘dh’, ‘gh’, ‘kh’, ‘ph’, ‘rh’, ‘th’, and then add es in end to make the word plural.
if search('[sxz]$', word) or search('[^aeioudgkprt]h$', word): print( sub('$', 'es', word))
Step 4: Next, we check if the word ends with ‘ay’, ‘ey’, ‘iy’, ‘oy’, or ‘uy’. If yes, then we remove y from the word and replace it with ‘ies’ in end to make it plural.
elif search('[aeiou]y$', word): print( sub('y$', 'ies', word))
Step 5: Finally, for the rest of the left cases, we add s, in the end, to make it plural.
else: print( word + 's')
Example:
Python
# Python program to pluralize a # given word using regex module # Import the libraries from re import * # Declare the word in a variable word = "apple" # Check if word is ending with s,x,z or is # ending with ah, eh, ih, oh,uh,dh,gh,kh,ph,rh,th if search( '[sxz]$' , word) or search( '[^aeioudgkprt]h$' , word): # Make it plural by adding es in end print (sub( '$' , 'es' , word)) # Check if word is ending with ay,ey,iy,oy,uy elif search( '[aeiou]y$' , word): # Make it plural by removing y from end adding ies to end print (sub( 'y$' , 'ies' , word)) # In all the other cases else : # Make the plural of word by adding s in end print (word + 's' ) |
Output:
apples
This way is not too efficient for getting plural of words as it does plural of certain words appropriately, while it fails in some cases too. Hence, we will look for more efficient ways to find a plural of the word.
Method 2: Using NLTK and Pattern-en package
Modules Required:
- NLTK: The module which builds Python programs to work with human language data and provides easy-to-use interfaces to corpora and lexical resources is known as the NLTK module.
- Pattern: An open-source module of Python which is used to perform various natural language processing tasks is known as a pattern module. This module can be used for text processing, data mining, and various other operations.
Stepwise Implementation:
Step 1: First of all, import the module, i.e., NLTK.
import nltk
Step 2: Now, you need to download the NLTK data for running the en function of the package module. This is just a one-time step. You don’t need to do it in each program.
nltk.download('popular')
Step 3: Then, import the plural function of pattern.en module.
from pattern.en import pluralize
Step 4: Finally, make the word plural by using pluralize() function and printing the output.
print (pluralize('#Define the word'))
Example:
Python
# Python program to pluralize a given # word using pattern-en package # Import the NLTK module from pattern.en import pluralize import nltk # Installing NLTK data to import # and run en module of pattern nltk.download( 'popular' ) # Importing the pattern en module # Define the word and make it plural # by using pluralize() function print (pluralize( 'child' )) |
Output:
children
Method 3: Using textblob to pluralize a given word in Python
Textblob Python module which is used for processing textual data is known as the Textblob module. It is one of the easiest modules to perform natural language processing tasks. This module can be downloaded using the following command. Along with textblob module, you also need to install the corpora function of textblob module.
Stepwise Implementation:
Step 1: First of all, import the library textblob.
from textblob import TextBlob
Step 2: Now, define the word and store it in a variable.
blob = TextBlob('#Define the word')
Step 3: Finally, make the word plural by using words. pluralize() function and print the result.
print(blob.words.pluralize())
Example:
Python
# Python program to pluralize a given # word using textblob module # Importing the libraries from textblob import TextBlob # Define the word and store in variable blob_word = TextBlob( 'cat' ) # Make the word plural by using pluralize() function print (blob_word.words.pluralize()) |
Output:
[cats]
Method 4: Using Inflect to pluralize a given word in Python
Inflect Python module, that is not only used to generate plurals, singular nouns, ordinals, and indefinite articles but also used to convert numbers to words is known as Inflect module. This module can be installed through the following command:
Stepwise Implementation:
Step 1: First of all, import the libraries required, i.e., inflect.
import inflect
Step 2: Now, declare a method of the import module.
p = inflect.engine()
Step 3: Finally, print the plural of the string by using the plural() function. The plural() function works appropriately by converting the singular noun to a plural noun.
print("Plural of string: ", p.plural('#string-text'))
Examples:
Example 1:
In this example, we have defined the string text, i.e., ‘child‘ in a variable a, and then print the result by using the plural() function of inflect module.
Python
# Program to pluralize a given # word in Python # Import the libraries import inflect # Declare method of inflect module p = inflect.engine() # Define the string and store it in variable a = 'child' # Print the plural of the string defined print ( "Plural of child: " , p.plural(a)) |
Output:
children
Example 2:
In this example, we have taken the string input as ‘apple‘ to see if the plural() function works appropriately or not.
Python
# Program to pluralize a given # word in Python # Import the libraries import inflect # Declare method of inflect module p = inflect.engine() # Print the plural of the word apple print ( "Plural of apple: " , p.plural( 'apple' )) |
Output:
Plural of apple: apple
Example 3:
In this example, we have taken the string input as ‘tooth‘ to see if the plural() function works appropriately or not.
Python
# Program to pluralize a given # word in Python # Import the libraries import inflect # Declare method of inflect module p = inflect.engine() # Print the plural of the word tooth print ( "Plural of tooth: " , p.plural( 'tooth' )) |
Output:
Plural of tooth: teeth
Example 4:
In this example, we have taken the string input as ‘watch‘ to see if the plural() function works appropriately or not.
Python
# Program to pluralize a given # word in Python # Import the libraries import inflect # Declare method of inflect module p = inflect.engine() # Print the plural of the word watch print ( "Plural of watch: " , p.plural( 'watch' )) |
Output:
Plural of watch: watches
Example 5:
In this example, we have made the program dynamic, by taking the input from the user in a variable. Then, we converted that variable to plural.
Python
# Program to pluralize a given # word in Python # Import the libraries import inflect # Declare method of inflect module p = inflect.engine() # Make program dynamic by taking input from user word = input ( "Enter a word: " ) print ( "Plural of entered word: " , p.plural(word)) |
Output:
Plural of entered word: oxen