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']
词义定义
可以使用 definition 函数获取一个词的字典定义。 它描述了我们可以在普通词典中找到的单词的含义。
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
使用 Examples
我们可以使用 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?"]
反义词
使用 antonyms 函数得到所有反义词。
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')]