Python 在 NLTK 3.0 中使用 Wordnet 从 Synset 中提取单词
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27517924/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Extract Word from Synset using Wordnet in NLTK 3.0
提问by duhaime
Some time ago, someone on SO asked how to retrieve a list of words for a given synsetusing NLTK's wordnet wrapper. Here is one of the suggested responses:
前段时间,SO 上有人询问如何使用 NLTK 的 wordnet 包装器检索给定同义词集的单词列表。以下是建议的回应之一:
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
.
使用 NLTK 3.0 运行此代码会产生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.
我尝试了之前提出的每个解决方案(上面链接的页面中描述的每个解决方案),但每个都引发错误。因此,我想问:是否可以使用 NLTK 3.0 打印同义词列表的单词?对于其他人可以就这个问题提供的任何建议,我将不胜感激。
采纳答案by π?δα? ?κ??
WordNet works fine in NLTK 3.0. You are just accessing the lemmas (and names) in the wrong way. Try this instead:
WordNet 在 NLTK 3.0 中运行良好。您只是以错误的方式访问引理(和名称)。试试这个:
>>> 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).
synset.lemmas
是一种方法并且没有__getitem__()
方法(因此不可下标)。
回答by alvas
Use:
用:
wn.synset('dog.n.1').name()
instead of:
代替:
wn.synset('dog.n.1').name
because NLTK changed Synset properties to get functions instead. see https://github.com/nltk/nltk/commit/ba8ab7e23ea2b8d61029484098fd62d5986acd9c
因为 NLTK 更改了 Synset 属性以获取函数。见https://github.com/nltk/nltk/commit/ba8ab7e23ea2b8d61029484098fd62d5986acd9c
This is a good list of changes to NLTK's API to suit py3.x: https://github.com/nltk/nltk/wiki/Porting-your-code-to-NLTK-3.0
这是一个很好的 NLTK API 更改列表以适应 py3.x:https: //github.com/nltk/nltk/wiki/Porting-your-code-to-NLTK-3.0
回答by Francis Bond
You can also go directly to the lemma names with lemma_names()
:
您还可以使用以下命令直接转到引理名称lemma_names()
:
>>> wordnet.synset('dog.n.1').lemma_names()
['dog', 'domestic_dog', 'Canis_familiaris']
And it works for multiple languages
它适用于多种语言
>>>> wordnet.synset('dog.n.1').lemma_names(lang='jpn')
['イヌ', 'ドッグ', '洋犬', '犬', '飼犬', '飼い犬']