Home:ALL Converter>input a string and compare each word with a given word using NLP Python

input a string and compare each word with a given word using NLP Python

Ask Time:2020-01-23T02:26:00         Author:Khabbab Zakaria

Json Formatter

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

Author:Khabbab Zakaria,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/59866105/input-a-string-and-compare-each-word-with-a-given-word-using-nlp-python
yy