【问题标题】:Wordnet synset - strange list index out of range ErrorWordnet synset - 奇怪的列表索引超出范围错误
【发布时间】:2016-05-15 14:35:20
【问题描述】:

我开始使用 nltk 并尝试生成一个函数,该函数允许我传入形容词,从 wordnet 中提取第一个同义词集并将其与反义词一起打印。她是我的代码:

def placementOperator(wordItem):
   wordnet_lemmatizer = WordNetLemmatizer()
   placementItem = wordnet_lemmatizer.lemmatize(wordItem,'a')
   print("The placementItem is: " + placementItem)
   iterationSet = wn.synsets(placementItem, 'a')
   if iterationSet[0]:
       print(" This is the SS NAME : " + iterationSet[0].name())
       for j in iterationSet[0].lemmas():
           print("  This is the LEMMAAAA: " + j.name())
           if j.antonyms():
               print("   This is the RElATIONSHIP " + j.name(), j.antonyms()[0].name())
           else: print("    _______> NO ANTONYM!")
   else: pass

我快到了,只是我的解释器抛出了“列表超出范围”异常。我知道我不能调用不存在的列表位置,并且我知道当尝试这样做时会发生此错误。但是,由于我正在使用 if iterationSet[0] 对此进行明确测试,因此我不确定我是如何最终遇到错误的。

任何建议将不胜感激。

她是错误的:

Traceback (most recent call last):
  File "C:/Users/Admin/PycharmProjects/momely/associate/associate.py", line  57, in <module> preProcessor(0)
  File "C:/Users/Admin/PycharmProjects/momely/associate/associate.py", line 54, in preProcessor placementOperator(each_element[0])
   File "C:/Users/Admin/PycharmProjects/momely/associate/associate.py", line 31, in placementOperator if iterationSet[0]:
IndexError: list index out of range

【问题讨论】:

    标签: python nltk wordnet


    【解决方案1】:

    很可能,wn.synsets(placementItem, 'a') 给您返回了一个空列表。如果placementItem 不在wordnet 中,就会发生这种情况。

    因此,当您执行iterationSet[0] 时,它会引发超出范围的异常。相反,您可以将支票更改为:

    if iterationSet:
        print( ....
        ....
    

    而不是

    if iterationSet[0]:
       print(...
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-10-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多