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

用法示例

我們可以使用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?"]

反義詞

使用 antonym 函式獲取所有反義詞。

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')]
廣告

© . All rights reserved.