在 NLTK 3.0 中使用 Wordnet 从 Synset 中提取单词 [英] Extract Word from Synset using Wordnet in NLTK 3.0

查看:34
本文介绍了在 NLTK 3.0 中使用 Wordnet 从 Synset 中提取单词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

前段时间,SO 上有人问 如何使用 NLTK 的 wordnet 包装器检索给定同义词集的单词列表.以下是建议的回应之一:

 用于 wn.synsets('dog') 中的同义词集:打印 synset.lemmas[0].name

使用 NLTK 3.0 运行此代码会产生 TypeError: 'instancemethod' object is not subscriptable.

我尝试了之前提出的每个解决方案(上面链接的页面中描述的每个解决方案),但每个都引发错误.因此,我想问:是否可以使用 NLTK 3.0 打印同义词列表的单词?如果其他人可以就此问题提供任何建议,我将不胜感激.

解决方案

WordNet 在 NLTK 3.0 中运行良好.您只是以错误的方式访问引理(和名称).试试这个:

<预><代码>>>>导入 nltk>>>nltk.__version__'3.0.0'>>>从 nltk.corpus 导入 wordnet as wn>>>对于 wn.synsets('dog') 中的同义词集:对于 synset.lemmas() 中的引理:打印 lemma.name()狗家养狗Canis_familiaris脾气暴躁狗狗CAD边界浑蛋...

synset.lemmas 是一种方法,没有 __getitem__() 方法(因此不可下标).

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.

解决方案

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).

这篇关于在 NLTK 3.0 中使用 Wordnet 从 Synset 中提取单词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆