Reputation:
I want to input a string, tokenize it, and compare each word with a specific word (in this code, the word is 'play'). I have the code
from nltk.tokenize import word_tokenize
txt = "bat ball cocaine golf football cake leg hand me you her she he dog cat drug"
x = word_tokenize(txt)
from nltk.corpus import wordnet
for i in range (10):
syn = wordnet.synsets(x[i])[0]
print ("Synset name : ", syn.name())
w1 = wordnet.synset('play.n.01')
w2 = wordnet.synset(syn)
print(w1.wup_similarity(w2))
i = i +1
This gives an error:
AttributeError Traceback (most recent call last)
<ipython-input-127-a30645977ba6> in <module>()
13
14 w1 = wordnet.synset('play.n.01')
---> 15 w2 = wordnet.synset(syn)
16 print(w1.wup_similarity(w2))
17 i = i +1
help
Upvotes: 0
Views: 73
Reputation: 140
You passed the wrong argument to the function.
Instead, you should've passed syn.name()
Use this w2 = wordnet.synset(syn.name())
With this correction, IndexError: list index out of range
raises on 10th iteration.
Try this to solve the problem
syn = wordnet.synsets(x[i])
if syn:
syn = syn[0]
else:
continue
Upvotes: 0