我正在使用此代码查找单词的所有上位词
from nltk import wordnet as wn
for synset in wn.wordnet.synsets('green'):
for hypernym in synset.hypernyms():
print synset, hypernym
但这会产生错误:
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
你能提出一些建议吗
答案 0 :(得分:3)
首先确保您已下载wordnet:
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
尝试:
>>> 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') []
或
>>> 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')