【发布时间】: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
【问题讨论】: