Python WordNet 接口


WordNet 是英语词典,类似于传统的词库 NLTK 包括英语 WordNet。我们可以将其作为获取词义、用法示例和定义的参考。相似词的集合称为引理。 WordNet 中的单词是有组织的,节点和边表示单词文本,边表示单词之间的关系。下面我们将看到如何使用 WordNet 模块。

所有引理


from nltk.corpus import wordnet as wn
res=wn.synset('locomotive.n.01').lemma_names()
print res

当我们运行上面的程序时,我们得到如下输出:

[u'locomotive', u'engine', u'locomotive_engine', u'railway_locomotive']

词定义


使用定义函数可以得到一个词的字典定义。它描述了我们在普通字典中可以找到的单词的含义。

from nltk.corpus import wordnet as wn
resdef = wn.synset('ocean.n.01').definition()
print resdef

当我们运行上面的程序时,我们得到如下输出:

a large body of water constituting a principal part of the hydrosphere

使用示例


我们可以使用 示例() 功能。

from nltk.corpus import wordnet as wn
res_exm = wn.synset('good.n.01').examples()
print res_exm

当我们运行上述程序时,我们得到以下输出:

['for your own good', "what's the good of worrying?"]

反义词


使用反义词函数获取所有相反的词。

from nltk.corpus import wordnet as wn
# get all the antonyms
res_a = wn.lemma('horizontal.a.01.horizontal').antonyms()
print res_a

当我们运行上述程序时,我们得到以下输出:

[Lemma('inclined.a.02.inclined'), Lemma('vertical.a.01.vertical')]