【问题标题】:How to check if given word is in plural or singular form?如何检查给定的单词是复数形式还是单数形式?
【发布时间】:2012-08-30 22:09:17
【问题描述】:

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

【问题讨论】:

标签: python nlp


【解决方案1】:

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

【讨论】:

  • some_boolean_value is False 是反模式,请改用not some_boolean_value
  • 我刚刚编写了代码来使用这种方法来检查一个单词是否是单数,但它在第一个单词上失败了。这个词是“fitness”,singular_noun() 函数返回“fitnes”。所以要小心。
  • 所有格也有问题:singular_noun() 函数将年份变成年份'
  • 尝试一些以s结尾的单数词。喜欢asparagus
【解决方案2】:

查看inflect 0.2.4 库。

0.2.4

正确生成复数、单数名词、序数、不定 文章;将数字转换为单词

【讨论】:

  • @Ashwini:inflect.py 可以生成复数,但它可以检测复数吗?
【解决方案3】:

您不会说您的问题是孤立的单词还是英语句子上下文中的单词。

例如“thesheep”可以是单数也可以是复数。然而:

羊在地里

是单数和

羊在地里

是复数。

对于后者,您需要一个词性标注器,它将识别句子中名词的角色。有很多免费的和商业的,维基百科有an excellent list。 NLTK 可能是 Python 的自然选择。

如果你只有孤立的词,你能做的最好的就是参考许多字典(例如 Wordnet 表示名词的单复数形式)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-05-29
    • 2012-10-26
    • 1970-01-01
    • 2014-04-30
    • 1970-01-01
    • 2019-03-24
    • 1970-01-01
    相关资源
    最近更新 更多