gpt4 book ai didi

python - 如何检查给定单词是复数形式还是单数形式?

转载 作者:太空狗 更新时间:2023-10-29 18:19:27 25 4
gpt4 key购买 nike

主题中的问题 - 我正在尝试在 python 中为 Google App Engine 中的应用程序执行此操作。我知道 PyEnchant 库用于自然语言识别,但我不知道我是否可以将它用于我的问题以及如何使用它。

最佳答案

Ashwini 提到了有用的 inflect库,但没有解释如何检查给定单词是复数形式还是单数形式。

如果您知道该词是单数还是复数,您可以使用:

singular_noun(word)

如果单词不是复数,这将返回 False,因此理论上您的单词应该是单数。

当涉及到经典复数、可以是单数或复数的形式时,请注意我的示例中显示的缺点,以及对于一般无法识别的形式它会返回 False 的事实。

import inflect
inflect = inflect.engine()

english_words = ["hat", "hats",
"hero", "heroes",
"cherry", "cherries",
"dish", "dishes",
"stadium", "stadia", "stadiums",
"mitochondrion", "mitochondria",
"sheep", "a sheep", "the sheep",
"whjkjhkjh", "msipelling"]

for en in english_words:
if inflect.singular_noun(en) == False:
print (en, "is singular")
else:
print (en, "is plural")

>>>
hat is singular
hats is plural
hero is singular
heroes is plural
cherry is singular
cherries is plural
dish is singular
dishes is plural
stadium is singular
stadia is singular
stadiums is plural
mitochondrion is singular
mitochondria is singular
sheep is plural
a sheep is plural
the sheep is plural
whjkjhkjh is singular

关于python - 如何检查给定单词是复数形式还是单数形式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12206276/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com