【问题标题】:Why does NLTK WordNet fail finding simple words?为什么 NLTK WordNet 找不到简单的单词?
【发布时间】:2023-03-30 11:02:01
【问题描述】:

我想编写一个简单的函数,通过 NLTK 来查看 WordNet 中是否“存在”这个词。

def is_known(word):
    """return True if this word "exists" in WordNet
       (or at least in nltk.corpus.stopwords)."""
    if word.lower() in nltk.corpus.stopwords.words('english'):
        return True
    synset = wn.synsets(word)
    if len(synset) == 0:
        return False
    else:
        return True

为什么像could, since, without, although 这样的词会返回 False?它们没有出现在 WordNet 中吗?有没有更好的方法来找出 WN 中是否存在一个单词(使用 NLTK)?

我的第一次尝试是消除“停用词”,即像 to, if, when, then, I, you 这样的词,但我仍然找不到非常常见的词(如 could)。

【问题讨论】:

  • 当它是一个停用词时,你为什么返回 True?
  • 那只是试图忽略这些词。但我注意到并非所有常用词都是停用词。

标签: python nltk wordnet


【解决方案1】:

WordNet 不包含这些词或类似的词。有关解释,请参阅WordNet docs 中的以下内容:

Q. Why is WordNet missing: of, an, the, and, about, above, because, etc.
A. WordNet only contains "open-class words": nouns, verbs, adjectives, and adverbs. Thus, excluded words include determiners, prepositions, pronouns, conjunctions, and particles.

您也不会在 WordNet 的在线版本中找到这类词。

【讨论】:

    【解决方案2】:

    您可以尝试提取 wordnet 中的所有引理,然后检查该列表:

    from nltk.corpus import wordnet as wn
    from itertools import chain
    all_lemmas = set(chain(*[i.lemma_names for i in wn.all_synsets()]))
    
    def in_wordnet(word):
      return True if word in all_lemmas else False
    
    print in_wordnet('can')
    print in_wordnet('could')
    

    [出]:

    True
    False
    

    请注意,wordnet 包含引理而不是单词。另请注意,单词/引理可以是多义词,而不是真正的包含词,例如

    I can foo bar.The water can is heavy

    【讨论】:

    • in_wordnet 给我的结果与 is_known 相同,但速度很慢(当然不是函数本身)
    猜你喜欢
    • 2023-03-11
    • 2016-11-09
    • 1970-01-01
    • 2016-02-10
    • 2015-08-16
    • 1970-01-01
    • 2013-07-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多