We have an interesting topic in English grammar called “singular and plurals.” Let us examine how they can be implemented using Python
We all are aware that, singular refers to an object that has a single number or quantity. For instance, “a book” or “a pen.” Plurals denote objects in groups or a large number of objects. For example, “pens” or “books” etc.
Here, we’ll see how to implement this concept in Python.
In English, there are a few rules for converting singular to plural:
- A singular noun can be made plural by adding “s” at the end.
- Words that end in “sh, s, x, z” can be made plural by adding “es” at the end.
- A singular word that ends in a consonant and then y can be made plural by removing the “y” and adding “ies.”
There may be some exceptions to the above rules. However, we will stick to the rules that were given above.
To perform this task we use the re module
re module:
A regular expression is a name given to the re-package.
In Python, this package is used to manipulate strings. Moreover, it can be used to determine whether we are searching for a specific search pattern in a string. In other words, suppose we need to identify the occurrence of “p” in “pythonprograms”, we can utilize the re-package for such searches.
Program to Pluralize a Given Word in Python
Approach:
- Import regex(re) module using the import keyword
- Create a function say pluralize_word which accepts a word(noun) as an argument and returns the pluralized word
- Check if the given word matches with the rules using the search() function in the regex module and if the conditional statement
- If it is true then substitute ‘es’ to the word passed to make it a plural using the sub() function of the regex module
- If it is true then substitute ‘es’ to the end of the word passed.
- If it is true then substitute ‘ies’ with ‘y’ to the word passed to make it a plural using the sub() function of the regex module
- Else just add ‘s’ to the word passed.
- Give the list as static input and store it in a variable.
- Iterate in the list of words using the For loop
- pass the iterator word as an argument to the pluralize_word function(It returns the pluralized word) and print it
- The Exit of the Program.
Below is the implementation:
# Import regex(re) module using the import keyword import re # Create a function say pluralize_word which accepts a word(noun) as an argument and returns the pluralized word def pluralize_word(word): # Check if the given word matches with the rules using the search() function in regex module and if conditional statement if re.search('[sxz]$', word): # If it is true then substitute 'es' to the word passed to make it a plural using sub() function of regex module return re.sub('$', 'es', word) elif re.search('[^aeioudgkprt]h$', word): # If it is true then substitue 'es' to the end of the word passed. return re.sub('$', 'es', word) elif re.search('[aeiou]y$', word): # If it is true then substitute 'ies' with 'y' to the word passed to make it a plural using sub() function of regex module return re.sub('y$', 'ies', word) else: # Else just add 's' to the word passed. return word + 's' # Give the list as static input and store it in a variable. gvn_lst = ["cat", "fox", "flower", "cap"] #Iterate in the list of words using the For loop for word in gvn_lst: #pass the iterator word as an argument to the pluralize_word function(It returns the pluralized word) and print it print(word, '-->', pluralize_word(word))
Output:
cat --> cats fox --> foxes flower --> flowers cap --> caps