103k views
2 votes
Write a program that takes nouns and forms their plurals on the basis of these rules:

a. If noun ends in "y", remove the "y" and add "ies".
b. If noun ends in "s", "ch", or "sh", add "es".
c. In all other cases, just add "s".
Print each noun and its plural. Try the following data:
chair dairy boss circuis fly dog church clue dish

User Szxk
by
8.3k points

1 Answer

4 votes

Answer:

def nounToplural(tex):

import re

text = tex.split()

new = []

for word in text:

if re.findall(r"y\b", word):

word = re.sub(r"y\b", 'ies' ,word)

new.append(word)

elif re.findall(r"ch\b", word):

word = re.sub(r"ch\b", 'ches' ,word)

new.append(word)

elif re.findall(r"sh\b", word):

word = re.sub(r"sh\b", 'shes' ,word)

new.append(word)

elif re.findall(r"s\b", word):

word = re.sub(r"s\b", 'ses' ,word)

new.append(word)

else:

word = word + 's'

new.append(word)

result = []

for i in text:

for j in new:

if new.index(j) == text.index(i):

result.append( (i,j) )

print(result)

str = "chair dairy boss circuis fly dog church clue dish"

nounToplural(str)

Step-by-step explanation:

We made use or Regular Expressions which is simply a special sequence of characters that helps you match or find other strings or sets of strings, by using a specialized syntax held in a pattern.

Firstly, define the function and import the regular expression module, split the text into a list and create an empty list to hold the plural list.

For every word in the list we check with IF statements and regular expressions to find out what they end with.

if re.findall(r"y\b", word):

word = re.sub(r"y\b", 'ies' ,word)

new.append(word)

The above IF block checks if a noun ends with 'y', if it does it replaces the 'y' with 'ies' and ads it to the new list.

If it does not, ot moves to the next IF/ELIF block.

In the ELIF blocks carry out a a similar operation for "s", "ch", or "sh", in order to add "es".

Finally, 's' is added to all other cases using the ELSE block which traps any noun that did not fall under the IF or any of the ELIF blocks.

This last part of the code prints both list side by side and passes them to a new list result which is printed to the screen

result = []

for i in text:

for j in new:

if new.index(j) == text.index(i):

result.append( (i,j) )

print(result)

I called the function and passed the string you provided in your question and attached the result of the code.

Write a program that takes nouns and forms their plurals on the basis of these rules-example-1
User Remee
by
8.3k points