【问题标题】:Search for plural words in line and make it singular words在一行中搜索复数单词并使其成为单数单词
【发布时间】:2021-01-26 17:50:43
【问题描述】:

我想查找该行是否包含复数词。如果是这样,我想把这些词改成单数词。

例如:

file1.txt

那个香蕉是黄色的。它们的味道确实不错。

预期输出.txt

那个香蕉是黄色的。它们的味道确实不错。

请帮帮我。

我尝试使用 .re 从单词中删除“s”。但它会删除文件中的每个“s”。我只想删除单词末尾的's'。例如,“麻袋”。我想要“解雇”,但我得到了“确认”。 这是我尝试过的。

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

【问题讨论】:

  • 你尝试过什么,它到底有什么问题?
  • 请edit提供minimal reproducible example的问题。
  • 我尝试使用 .re 从单词中删除“s”。但它会删除文件中的每个“s”。我只想删除单词末尾的's'。例如,“麻袋”。我想要“解雇”,但我得到了“确认”。这是我尝试过的。使用 open('file1.txt') 作为 file1: file1 = file1.read() test = re.sub('s', ' ', file2) with open('file1.txt', 'w') 作为输出: out.writelines(test)

标签: python singular plural


【解决方案1】:

您基本上有 2 个选项:nltk 库(更复杂)或带有 pattern 的 python 包。整洁可能是:

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)

查看更多here。

【讨论】:

    猜你喜欢
    • 2018-04-03
    • 1970-01-01
    • 2018-03-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多