Extract Word from Synset using Wordnet in NLTK 3.0

duhaime picture duhaime · Dec 17, 2014 · Viewed 25.3k times · Source

Some time ago, someone on SO asked how to retrieve a list of words for a given synset using NLTK's wordnet wrapper. Here is one of the suggested responses:

for synset in wn.synsets('dog'):
    print synset.lemmas[0].name

Running this code with NLTK 3.0 yields TypeError: 'instancemethod' object is not subscriptable.

I tried each of the previously-proposed solutions (each of the solutions described on the page linked above), but each throws an error. I therefore wanted to ask: Is it possible to print the words for a list of synsets with NLTK 3.0? I would be thankful for any advice others can offer on this question.

Answer

πόδας ὠκύς picture πόδας ὠκύς · Dec 17, 2014

WordNet works fine in NLTK 3.0. You are just accessing the lemmas (and names) in the wrong way. Try this instead:

>>> import nltk
>>> nltk.__version__
'3.0.0'
>>> from nltk.corpus import wordnet as wn
>>> for synset in wn.synsets('dog'):
    for lemma in synset.lemmas():
        print lemma.name()


dog
domestic_dog
Canis_familiaris
frump
dog
dog
cad
bounder
blackguard
...

synset.lemmas is a method and does not have a __getitem__() method (and so is not subscriptable).