NLTK學(xué)習(xí)記錄2:使用語料庫和詞匯資源

內(nèi)置的語料庫

import nltk
nltk.corpus.gutenberg.fileids()  #Gutenberg語料庫的全部文件id
emma = nltk.corpus.gutenberg.words('austen-emma.txt')  #打開一個Gutenberg的文本文件
len(emma)

或者

from nltk.corpus import gutenberg
gutenberg.fileids()
emma = gutenberg.words('austen-emma.txt')

從語料庫中提取文本信息

for fileid in gutenberg.fileids():
  num_chars = len(gutenberg.raw(fileid))
  num_words = len(gutenberg.words(fileid))
  num_sents = len(gutenberg.sents(fileid))
  num_vocab = len(set(w.lower() for w in gutenberg.words(fileid)))

再例如Brown語料庫

from nltk.corpus import brown
brown.categories()
brown.words(categories='news')
brown.words(fileids=['cg22'])
brown.sents(categories=['news', 'editorial', 'reviews'])

cfd = nltk.ConditionalFreqDist(  (genre, word)  for genre in brown.categories()  for word in brown.words(categories=genre))
genres = ['news', 'religion', 'hobbies', 'science_fiction', 'romance', 'humor']
modals = ['can', 'could', 'may', 'might', 'must', 'will']
cfd.tabulate(conditions=genres, samples=modals)

內(nèi)置語料庫基本函數(shù)

#Example  Description
fileids()  #the files of the corpus
fileids([categories])  #the files of the corpus corresponding to these categories
categories()  #the categories of the corpus
categories([fileids])  #the categories of the corpus corresponding to these files
raw() #the raw content of the corpus
raw(fileids=[f1,f2,f3]) #the raw content of the specified files
raw(categories=[c1,c2]) #the raw content of the specified categories
words() #the words of the whole corpus
words(fileids=[f1,f2,f3]) #the words of the specified fileids
words(categories=[c1,c2]) #the words of the specified categories
sents() #the sentences of the whole corpus
sents(fileids=[f1,f2,f3]) #the sentences of the specified fileids
sents(categories=[c1,c2]) #the sentences of the specified categories
abspath(fileid) #the location of the given file on disk
encoding(fileid) #the encoding of the file (if known)
open(fileid) #open a stream for reading the given corpus file
root   #if the path to the root of locally installed corpus
readme() #the contents of the README file of the corpus

載入本地語料庫

from nltk.corpus import PlaintextCorpusReader
corpus_root = '/usr/share/dict'
wordlists = PlaintextCorpusReader(corpus_root, '.*')
wordlists.fileids()
wordlists.words('connectives')

條件頻率分布

頻率分布計數(shù)可觀察事件,例如文本中的單詞的出現(xiàn)。 條件頻率分布需要將每個事件與條件配對。 因此丁恭,不是處理一個單詞序列,我們必須處理一個配對序列:

genre_word = [(genre, word) for genre in ['news', 'romance'] for word in brown.words(categories=genre)]
cfd = nltk.ConditionalFreqDist(genre_word)
cfd['romance'].most_common(20)
cfd['romance']['could']

詞語料

使用詞表

def unusual_words(text):
  text_vocab = set(w.lower() for w in text if w.isalpha())
  english_vocab = set(w.lower() for w in nltk.corpus.words.words())
  unusual = text_vocab - english_vocab 
  return sorted(unusual)

unusual_words(nltk.corpus.gutenberg.words('austen-sense.txt'))

使用停用詞表

from nltk.corpus import stopwords
stopwords.words('english')

使用英文姓名詞表

names = nltk.corpus.names
names.fileids()['female.txt', 'male.txt']
male_names = names.words('male.txt')
female_names = names.words('female.txt')
[w for w in male_names if w in female_names]

使用發(fā)音詞表

entries = nltk.corpus.cmudict.entries()
for entry in entries[42371:42379]:
  print(entry)

使用WordNet

from nltk.corpus import wordnet as wn
wn.synsets('motorcar')   #[Synset('car.n.01')]
wn.synset('car.n.01').lemma_names()  #['car', 'auto', 'automobile', 'machine', 'motorcar']
wn.synset('car.n.01').definition()   #'a motor vehicle with four wheels; usually propelled by an internal combustion engine'
wn.synset('car.n.01').examples()  #['he needs a car to get to work']
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市涂屁,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌灰伟,老刑警劉巖拆又,帶你破解...
    沈念sama閱讀 217,509評論 6 504
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異栏账,居然都是意外死亡帖族,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,806評論 3 394
  • 文/潘曉璐 我一進(jìn)店門挡爵,熙熙樓的掌柜王于貴愁眉苦臉地迎上來竖般,“玉大人,你說我怎么就攤上這事茶鹃』恋瘢” “怎么了艰亮?”我有些...
    開封第一講書人閱讀 163,875評論 0 354
  • 文/不壞的土叔 我叫張陵,是天一觀的道長挣郭。 經(jīng)常有香客問我迄埃,道長,這世上最難降的妖魔是什么兑障? 我笑而不...
    開封第一講書人閱讀 58,441評論 1 293
  • 正文 為了忘掉前任侄非,我火速辦了婚禮,結(jié)果婚禮上旺垒,老公的妹妹穿的比我還像新娘彩库。我一直安慰自己,他們只是感情好先蒋,可當(dāng)我...
    茶點故事閱讀 67,488評論 6 392
  • 文/花漫 我一把揭開白布骇钦。 她就那樣靜靜地躺著,像睡著了一般竞漾。 火紅的嫁衣襯著肌膚如雪眯搭。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,365評論 1 302
  • 那天业岁,我揣著相機(jī)與錄音鳞仙,去河邊找鬼。 笑死笔时,一個胖子當(dāng)著我的面吹牛棍好,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播允耿,決...
    沈念sama閱讀 40,190評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼借笙,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了较锡?” 一聲冷哼從身側(cè)響起业稼,我...
    開封第一講書人閱讀 39,062評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎蚂蕴,沒想到半個月后低散,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,500評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡骡楼,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,706評論 3 335
  • 正文 我和宋清朗相戀三年熔号,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片鸟整。...
    茶點故事閱讀 39,834評論 1 347
  • 序言:一個原本活蹦亂跳的男人離奇死亡跨嘉,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情祠乃,我是刑警寧澤,帶...
    沈念sama閱讀 35,559評論 5 345
  • 正文 年R本政府宣布兑燥,位于F島的核電站亮瓷,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏降瞳。R本人自食惡果不足惜嘱支,卻給世界環(huán)境...
    茶點故事閱讀 41,167評論 3 328
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望挣饥。 院中可真熱鬧除师,春花似錦、人聲如沸扔枫。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,779評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽短荐。三九已至倚舀,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間忍宋,已是汗流浹背痕貌。 一陣腳步聲響...
    開封第一講書人閱讀 32,912評論 1 269
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點兒被人妖公主榨干…… 1. 我叫王不留糠排,地道東北人舵稠。 一個月前我還...
    沈念sama閱讀 47,958評論 2 370
  • 正文 我出身青樓,卻偏偏與公主長得像入宦,于是被迫代替她去往敵國和親哺徊。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 44,779評論 2 354

推薦閱讀更多精彩內(nèi)容