- Python - 文字處理
- Python - 文字處理簡介
- Python - 文字處理環境
- Python - 字串不可變性
- Python - 排序行
- Python - 段落重新格式化
- Python - 統計段落中的詞元
- Python - 二進位制 ASCII 轉換
- Python - 字串作為檔案
- Python - 反向檔案讀取
- Python - 過濾重複詞
- Python - 從文字中提取電子郵件
- Python - 從文字中提取 URL
- Python - 美化列印
- Python - 文字處理狀態機
- Python - 首字母大寫和翻譯
- Python - 分詞
- Python - 刪除停用詞
- Python - 同義詞和反義詞
- Python - 文字翻譯
- Python - 替換單詞
- Python - 拼寫檢查
- Python - WordNet 介面
- Python - 語料庫訪問
- Python - 詞性標註
- Python - 組塊和組塊間隙
- Python - 組塊分類
- Python - 文字分類
- Python - 二元語法
- Python - 處理 PDF
- Python - 處理 Word 文件
- Python - 讀取 RSS Feed
- Python - 情感分析
- Python - 搜尋和匹配
- Python - 文字整理
- Python - 文字換行
- Python - 頻率分佈
- Python - 文字摘要
- Python - 詞幹提取演算法
- Python - 受限搜尋
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')]
廣告