NLTK学习笔记(二):文本、语料资源和WordNet汇总
目錄
- 語料庫基本函數表
- 文本語料庫分類
- 常見語料庫及其用法
- 載入自定義語料庫
- 詞典資源
- 停用詞語料庫
- WordNet面向語義的英語字典
- 語義相似度
語料庫基本函數表
| fileids() | 語料庫中的文件 |
| fileids([categories]) | 對應分類中的語料庫文件 |
| categories() | 語料庫的分類 |
| categories([fileids]) | 文件對應的語料庫分類 |
| raw(fileids=[f1,f2..],categories=[c1,c2...]) | 對應文件和分類中原始內容。參數可以式空 |
| words(fileids=[f1,f2..],categories=[c1,c2...]) | 對應文件和分類的詞匯。參數可以空 |
| sents() | sents(fileids=[f1,f2..],categories=[c1,c2...]) |
| abspath(fileid) | 文件在磁盤的位置 |
| encoding(fileid) | 文件的編碼 |
| open(fileid) | 打開文件流 |
| root() | 本地語料庫corpus的位置 |
| readme() | README文件的內容 |
文本語料庫分類
常見語料庫及其用法
注意nltk.Text(string) 返回類似text1的Text對象
古藤堡語料庫
包含36000本電子書,可以在這里下載
from nltk.corpus import gutenberg print(gutenberg.fileids())emma= gutenberg.words('austen-emma.txt') print(gutenberg.raw('austen-emma.txt')) emma = nltk.Text(emma)# print(emma[:10])網絡&&聊天體
網絡text主要是非正式文學,論壇交流,劇本,評論等。聊天文本是根據聊天室劃分的(文件名包括 日期、聊天室、帖子數量),被劃分為15個大文件。
#網絡體:webtext from nltk.corpus import webtext for fileid in webtext.fileids():print(fileid,webtext.raw(fileid)[:50]) [out] firefox.txt Cookie Manager: "Don't allow sites that set remove grail.txt SCENE 1: [wind] [clop clop clop] KING ARTHUR: Who overheard.txt White guy: So, do you have any plans for this even pirates.txt PIRATES OF THE CARRIBEAN: DEAD MAN'S CHEST, by Ted singles.txt 25 SEXY MALE, seeks attrac older single lady, for wine.txt Lovely delicate, fragrant Rhone wine. Polished lea #聊天體:nps_chat from nltk.corpus import nps_chat chatroom = nps_chat.posts('10-19-20s_706posts.xml') chatroom[123:125] [out] [['i','do',"n't",'want','hot','pics','of','a','female',',','I','can','look','in','a','mirror','.'],['hi', 'U64']]布朗語料庫
百萬詞級語料庫,沒啥好說的。按照文本分類,如新聞、社論等。
from nltk.corpus import brown print(brown.categories()) print(brown.fileids())因為這個語料庫是研究文本間系統性差異的資源,所以可以來比較一下不同文本中情態動詞的用法。
import nltk from nltk.corpus import brown news = brown.words(categories='news') fdist = nltk.FreqDist([w.lower() for w in news]) modals= ['can','could','may','might','must','will'] for m in modals:print(m,':',fdist[m])路透社語料庫
新聞文檔,分為“訓練”和“測試”兩組。便于及其進行訓練和測試。命名就是'test/number'和'training/number'
from nltk.corpus import reuters print(reuters.fileids()) print(reuters.categories())就職演說語料庫
感覺這算是美國特色吧。因為命名采用'year-name.txt'的格式,我們可以提取出來時間維度,并且做個折線圖來統計特定詞匯的出現頻率(不同年代中)
from nltk.corpus import inaugural print(list(f[:4]for f in inaugural.fileids())) #下面體現American和citizen隨時間推移使用情況 cfd = nltk.ConditionalFreqDist(\(target,fileid[:4])\for fileid in inaugural.fileids()\for w in inaugural.words(fileid)\for target in ['america','citizen']\if w.lower().startswith(target)) cfd.plot()感受一下效果圖(附截圖)
載入自定義語料庫
如果想操作自己的語料庫,并且使用之前的方法,那么,需要PlaintextCorpusReader 函數來載入他們,這個函數參數有兩個,第一個是根目錄,第二個是子文件(可以使用正則表達式進行匹配)
from nltk.corpus import PlaintextCorpusReader root = r'C:\Users\Asura-Dong\Desktop\tem\dict' wordlist = PlaintextCorpusReader(root,'.*')#匹配所有文件 print(wordlist.fileids()) print(wordlist.words('tem1.txt')) 輸出結果: ['README', 'tem1.txt'] ['hello', 'world']詞典資源
詞典:包括詞性和注釋信息。
停用詞語料庫
stopwords即是,遺憾的是沒有中文停用詞
from nltk.corpus import stopwords #定義一個計算func計算不在停用詞列表中的比例的函數 def content(text):stopwords_eng = stopwords.words('english')content = [w for w in text if w.lower() and w not in stopwords_eng]return len(content)/len(text) print(content(nltk.corpus.reuters.words()))名字詞典
就兩部分組成,男性和女性的英文名字。這里我們研究一下最后一個名字最后一個字母和性別的關系
names = nltk.corpus.names print(names.fileids()) male = names.words('male.txt') female = names.words('female.txt') cfd = nltk.ConditionalFreqDist((fileid,name[-1]) for fileid in names.fileids() for name in names.words(fileid)) cfd.plot()(附截圖)
發音詞典
這個更神奇,竟然是為了發音合成準備的。以后通讀這本書后,也想想怎么遷移到中文上。
引入 nltk.corpus.cmudict 后,我們可以得到它音素的長度,由此可以找到押韻的詞語。
s = ['N','IHO','K','S'] entries = nltk.corpus.cmudict.entries() print('Example:',entries[0]) word_list = [word for word,pron in entries if pron[-4:]==s] print(word_list)在因素表中,我們會發現數字:1,2,0。分別代表著:主重音、次重音、無重音。
這里我們可以定義一個function,找到具有特定重音模式的詞匯。
WordNet面向語義的英語字典
最后必須說一下這個字典。WordNet是由Princeton 大學的心理學家,語言學家和計算機工程師聯合設計的一種基于認知語言學的英語詞典。它不是光把單詞以字母順序排列,而且按照單詞的意義組成一個“單詞的網絡”。
引入和同義詞
motorcar和automobile是同義詞,可以借助wordnet來研究。
from nltk.corpus import wordnet as wn wn.synsets('motorcar')結果是:[Synset('car.n.01')]。說明motorcar 只有一個 可能的含義。car.n.01被稱為“同義 詞集 ”。我們可以通過wn.synset('car.n.01').lemma_names 來查看當前同義詞集的其他詞 (car這個單詞就有很多個同義詞集了) 。wn.synset('car.n.01').examples 和wn.synset('car.n.01').definition 可以分別查看定義和例子(但是Python3里面不可以。)
而類似car.n.01.car這樣的處于下一級的稱之為詞條。
對于詞條級別的obj,可以看下面的操作。
上位詞、下位詞、反義詞
上位詞(hypernym),指概念上外延更廣的主題詞。 例如:”花”是”鮮花”的上位詞,”植物”是”花”的上位詞,”音樂”是”mp3”的上位詞。反過來就是下位詞了。
上位詞和下位詞通過hyponyms() 和 root_hypernyms() 來訪問。
motorcar = wn.synset('car.n.01').hyponyms()#下位詞 car = wn.synset('car.n.01').root_hypernyms()反義詞就通過antonyms() 來訪問
其他詞集關系
之前是從上->到下,或者反過來。更重要的是從整體->局部,或者反過來。如大樹和樹冠、樹干的關系,這些是part_meronyms() 。而大樹集合就成了森林,member_holonyms() 。而樹的實質是心材和邊材組成,即substance_meronyms()。
語義相似度
當兩個單詞有相同的上位詞(在詞樹中尋找),而若上位詞恰好屬于較低層,那么它們會有一定密切聯系。
right = wn.synset('right_whale.n.01') orca = wn.synset('orca.n.01') print(right.lowest_common_hypernyms(orca))當然,類似于樹的結構中總是有神的,可以通過min_depth() 來查看一個synset的最小深度。基于這些,我們可以在0-1的范圍內返回相似度。對于上面的代碼,查看相似度:right.path_similarity(orca)。
這些數字大小意義不大。但是當鯨魚和鯨魚、鯨魚和小說來比較時,數字是減小的。對比著看大小,還是有意義的。
轉載于:https://www.cnblogs.com/AsuraDong/p/6958046.html
總結
以上是生活随笔為你收集整理的NLTK学习笔记(二):文本、语料资源和WordNet汇总的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python学习之--安装IDE(ecl
- 下一篇: 如何在vue里面正确的引用 jquery