WordNet介面


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