2016-05-15 4 views
0

私はnltkの作業を開始しました。私は形容詞を渡し、wordnetから最初のsynsetを抽出し、その反意語とともに印刷する機能を生成しようとしています。彼女は私のコードです:Wordnet synset - 奇妙なリストのインデックスが範囲外であるエラー

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 

インタープリタが「範囲外のリスト」例外をスローすることを除いて、ほぼあります。私は、存在しないリストの位置を呼び出すことができないことを知っています。私は、このエラーは、そうしようとすると発生することを知っています。しかし、私は明示的にこれについてでテストしているので、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 

答えて

1

ほとんどの場合、wn.synsets(placementItem, 'a')はあなたの空のリストが返されました。これは、placementItemがwordnetにない場合に発生します。

したがって、iterationSet[0]を実行すると、範囲外の例外がスローされます。代わりに、あなたがあるためにあなたの小切手を変更することができます。

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

代わりの

if iterationSet[0]: 
    print(... 
関連する問題