0

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)
goodKarma
  • 1
  • 2
  • 1
    What have you tried, and what exactly is the problem with it? – jonrsharpe Jan 26 '21 at 17:51
  • 1
    Please [edit] the question to provide a [mre]. – jonrsharpe Jan 26 '21 at 18:01
  • 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', ' ', file2) with open('file1.txt', 'w') as out: out.writelines(test) – goodKarma Jan 26 '21 at 18:06

1 Answers1

0

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.

Leemosh
  • 883
  • 6
  • 19