- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Python Program to Convert Singular to Plural
In this article, we will learn a Python Program to Convert Singular to Plural.
Assume you are given a singular word and we must convert it into plural using the various python methods.
Methods Used
The following are the various methods to accomplish this task −
Using the regex module
Using NLTK and Pattern-en Packages
Using the Textblob module
Using inflect module
Method 1: Using the regex module
The regex module in python searches for a string or group of strings based on a specified pattern. In case it doesn't already exist in your Python you must install it, this module often comes pre-installed with Python.
Example
The following program returns the plural of the given word using the regex module by specifying the appropriate regex patterns −
# importing re(regex) module from re import * # input word to be pluralized inputWord = "plant" print("The plural of the word {", inputWord, "} is:") # checking whether the input word is ending with s,x,z or is # ending with ah, eh, ih, oh, uh, dh, gh, kh, ph, rh, th # with the regex pattern if search('[sxz]$', inputWord) or search('[^aeioudgkprt]h$', inputWord): # If it is true, then get the pluraof the inputWord by adding "es" in end print(sub('$', 'es', inputWord)) # checking whether the input word is ending with ay,ey,iy,oy,uy # with the other regex pattern elif search('[aeiou]y$', inputWord): # If it is true, then get the plural # of the word by removing 'y' from the end and adding ies to end print(sub('y$', 'ies', inputWord)) # Else add it just "s" to the word at the end to make it plural else: print(inputWord + 's')
Output
On executing, the above program will generate the following output −
The plural of the word { plant } is: plants
But, using this regex method is not very efficient for obtaining plurals because it correctly pluralizes some words while failing to do so in other instances. As a result, we shall search for more effective techniques to determine the word's plural.
Method 2: Using NLTK and Pattern-en Packages
NLTK module
The NLTK module creates Python applications to work with human language data and offers simple interfaces to corpora and lexical resources.
Pattern Module
A pattern module is an open-source Python module that is used to carry out various natural language processing operations. This module can be used for a variety of tasks, including text processing and data mining.
Install the pattern module using the below command
pip install pattern
Download the required nltk data using the below code −
# importing nltk module import nltk nltk.download('omw-1.4') # downloading the NLTK data to run the en function of the package module nltk.download('popular')
Example
The following program returns the plural of the given word using the NLTK and Pattern-en Packages −
# importing pluralize from pattern-en module from pattern.en import pluralize # passing the word to be pluralized as an argument to the # pluralize() function to make the word plural print(pluralize('lion'))
Output
On executing, the above program will generate the following output −
lions
Method 3:Using the Textblob module
The Textblob Python module is used to process textual data and is referred to as Textblob module. This is one of the simplest modules for problems involving natural language processing.
The following command can be used to download this module. You also need to install the textblob module's corpora function in addition to the texblob module.
Install the textblob module using the below command −
pip install textblob
Example
The following program returns the plural of the given word using the Textblob module −
# importing TextBlob function from textblob module from textblob import TextBlob # passing the word to be pluralized as an argument to the TextBlob() function blobWord = TextBlob('flower') # printing the plural word of the given blob word using the pluralize() function print(blobWord.words.pluralize())
Output
On executing, the above program will generate the following output −
['flowers']
Method 4: Using inflect module
Inflect module
The Inflect module in Python is used to create plural nouns, singular nouns, ordinals, and indefinite articles, as well as to convert numbers into words. The following command can be used to install this module.
Installation −
pip install inflect
Algorithm (Steps)
Following are the Algorithm/steps to be followed to perform the desired task. −
Use the import keyword to import inflect module.
Set the engine/source of the inflect module using the engine() function
Use the pluralize() function to get the plural of the given word by passing the input word as an argument to it and print the resultant word.
The singular noun is converted into a plural noun by the plural() function, which functions very appropriately.
Example
The following program returns the plural of the given word using the inflect module −
# importing inflect module import inflect m = inflect.engine() # input word to be pluralized word = 'dog' # Pass the word to be pluralized as an argument to the plural() # function to make the word plural. print("The plural form of 'dog' is: ", m.plural(word))
Output
On executing, the above program will generate the following output −
The plural form of 'dog' is: dogs
Conclusion
In this article, we covered four different methods for changing the given word from singular to plural. We learned how to use the pip command to install a module. Additionally, we learned how to download data from nltk.