Search for plural words in line and make it singular words

515 Views Asked by At

I want to find if the line consists plural words. If so, I want to change those words to singular words.

For example:

file1.txt

That bananas is yellow. They does taste good.

Expected_output.txt

That banana is yellow. They do taste good.

please help me.

I have tried using .re to delete 's' from the words. But it deletes every 's' in the file. I want to delete only 's' that is at the end of word. For example, 'sacks'. I want 'sack', but I got 'ack'. This is what I have tried.

with open('file1.txt') as file1:
    file1 = file1.read()
test = re.sub('s', ' ', file1)
with open('file1.txt', 'w') as out:
    out.writelines(test)
1

There are 1 best solutions below

0
On

You basically have 2 options: nltk library (more complex) or python package with pattern. Neat might be:

from pattern.text.en import singularize

plurals = ['caresses', 'flies', 'dies', 'mules', 'geese', 'mice', 'bars', 'foos',
           'families', 'dogs', 'child', 'wolves']

singles = [singularize(plural) for plural in plurals]
print(singles)

Check more here.