简体   繁体   English

在一行中搜索复数单词并使其成为单数单词

[英]Search for plural words in line and make it singular words

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文件1.txt

That bananas is yellow.那个香蕉是黄色的。 They does taste good.它们的味道确实不错。

Expected_output.txt 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.我尝试使用.re 从单词中删除“s”。 But it deletes every 's' in the file.但它会删除文件中的每个“s”。 I want to delete only 's' that is at the end of word.我只想删除单词末尾的's'。 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)

You basically have 2 options: nltk library (more complex) or python package with pattern .您基本上有 2 个选项: nltk库(更复杂)或 python package 与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 . 在这里查看更多。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM