Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

getting hypernyms from wordnet through nltk python

I am using this code to find all hypernyms of a word

from nltk import wordnet as wn
for synset in wn.wordnet.synsets('green'):
   for hypernym in synset.hypernyms():
       print synset, hypernym

but this gives error as:

   File"<stdin>",line 1 in <module>
   File "/usr/local/lib/python 2.7/dist-packages/nltk/corpus/reader/wordnet.py"line 1219 in synset
   lema,pos,synset_index_str=name.lower().rsplit('.',2)
   ValueError: need more than 1 value to unpack

Can you suggest something

like image 602
Niharika Roy Avatar asked May 12 '15 21:05

Niharika Roy


1 Answers

First ensure that you've wordnet downloaded:

alvas@ubi:~$ python
Python 2.7.6 (default, Mar 22 2014, 22:59:56) 
[GCC 4.8.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import nltk
>>> nltk.download('wordnet')
[nltk_data] Downloading package wordnet to /home/alvas/nltk_data...
[nltk_data]   Package wordnet is already up-to-date!
True

Try:

>>> from nltk.corpus import wordnet as wn
>>> for ss in wn.synsets('green'):
...     print ss, ss.hypernyms()
... 
Synset('green.n.01') [Synset('chromatic_color.n.01')]
Synset('park.n.02') [Synset('tract.n.01')]
Synset('green.n.03') []
Synset('green.n.04') [Synset('environmentalist.n.01')]
Synset('green.n.05') []
Synset('green.n.06') [Synset('site.n.01')]
Synset('greens.n.01') [Synset('vegetable.n.01')]
Synset('k.n.07') [Synset('ketamine.n.01')]
Synset('green.v.01') [Synset('discolor.v.03')]
Synset('green.s.01') []
Synset('green.a.02') []
Synset('green.a.03') []
Synset('green.s.04') []
Synset('fleeceable.s.01') []

or

>>> for ss in wn.synsets('green'):
...     for hyper in ss.hypernyms():
...             print ss, hyper
... 
Synset('green.n.01') Synset('chromatic_color.n.01')
Synset('park.n.02') Synset('tract.n.01')
Synset('green.n.04') Synset('environmentalist.n.01')
Synset('green.n.06') Synset('site.n.01')
Synset('greens.n.01') Synset('vegetable.n.01')
Synset('k.n.07') Synset('ketamine.n.01')
Synset('green.v.01') Synset('discolor.v.03')
like image 88
alvas Avatar answered Sep 20 '22 02:09

alvas