目录

Python - WordNet Interface

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

所有的Lemmas

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

用法示例

我们可以使用exmaples()函数获得显示单词的一些用法示例的示例句子。

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')]
↑回到顶部↑
WIKI教程 @2018