0

我想使用 python 将所有同义词和复数形式的单词转换为单词的基本版本。

例如,婴儿会变成婴儿,婴儿和婴儿也会变成婴儿。

我尝试为根代码创建一个朴素的复数版本,但它的问题是它并不总是正常运行并且无法检测到大量案例。

contents = ["buying", "stalls", "responsibilities"]
for token in contents:
    if token.endswith("ies"):
        token = token.replace('ies','y')
    elif token.endswith('s'):
        token = token[:-1]
    elif token.endswith("ed"):
        token = token[:-2]
    elif token.endswith("ing"):
        token = token[:-3]

print(contents)
4

1 回答 1

1

我以前没有使用过这个库,因此对此持怀疑态度。但是,NodeBox Linguistics 似乎是一组合理的脚本,如果您在 MacOS 上,它们将完全符合您的要求。检查此处的链接:https ://www.nodebox.net/code/index.php/Linguistics

根据他们的文档,您似乎可以使用如下行:

print( en.noun.singular("people") )
>>> person

print( en.verb.infinitive("swimming") )
>>> swim

etc.

除了上面的例子,另一个需要考虑的是自然语言处理库,如NLTK. 我推荐使用外部库的原因是因为英语有很多例外。正如我在评论中提到的,考虑以下词语:class、fling、red、geese 等,这会违反原始问题中提到的规则。

于 2019-07-12T19:41:03.037 回答