问题
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