Finding the synonyms for words in wordnet

萝らか妹 提交于 2019-12-08 12:58:11

问题


I try to use Wordnet as a thesarus, so I have a list of words and I need to collect for every word its synonyms. I tried this

from nltk.corpus import wordnet as wn
for i,j in enumerate(wn.synsets('dog')):
    print (j.lemma_names)

This code gives the following output

<bound method Synset.lemma_names of Synset('dog.n.01')>
<bound method Synset.lemma_names of Synset('frump.n.01')>
<bound method Synset.lemma_names of Synset('dog.n.03')>
<bound method Synset.lemma_names of Synset('cad.n.01')>
<bound method Synset.lemma_names of Synset('frank.n.02')>
<bound method Synset.lemma_names of Synset('pawl.n.01')>
<bound method Synset.lemma_names of Synset('andiron.n.01')>
<bound method Synset.lemma_names of Synset('chase.v.01')>

But I want to collect in a list only the synonyms, so the output will be like this

['frump', 'cad', 'frank', 'pawl', 'andiron', 'chase']


回答1:


As your output indicates, lemma_names is a method rather than a property. Blow code works as you expected:

from nltk.corpus import wordnet as wn
result = [st.lemma_names()[0] for st in wn.synsets('dog')]
print(result)

The output is:

[u'dog', u'frump', u'dog', u'cad', u'frank', u'pawl', u'andiron', u'chase']

Please note that the items in the list are of Unicode string. This is why you see the leading u in the output.



来源:https://stackoverflow.com/questions/36634000/finding-the-synonyms-for-words-in-wordnet

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!