我正在使用NLTK Wordnet提供阿拉伯语。当我运行以下代码时:
# -*- coding: UTF-8 -*-
from nltk.corpus import wordnet as wn
print wn.synsets('bank')[0].lemma_names('arb')
print wn.synsets('ضِفَّة', lang='arb')0].hypernyms()0].lemma_names(lang='arb') #PROBLEM HERE
我收到此错误:
Traceback (most recent call last):
File "nltk_wordnet.py", line 7, in <module>
print wn.synsets('ضِفَّة', lang='arb')[0].hypernyms() [0].lemma_names(lang='arb')
IndexError: list index out of range
答案 0 :(得分:0)
您的代码假定单词'ضِفَّة'
将使用synsets
找到概念关系,并且该集合中的第一项将具有上位词。这可能不是真的。您可以在索引之前检查返回的结果是否为空:
word_synsets = wn.synsets('ضِفَّة', lang='arb')
if word_synsets: # False if word_synsets is an empty list
word_hyp = word_synsets[0].hypernyms()
if word_hyp: # False if word_hyp is an empty list
print(word_hyp[0].lemma_names(lang='arb'))
else:
print('first item in ضِفَّة synset has no hypernym(s)')
else:
print('synset for ضِفَّة returned no result')