鍍金池/ 教程/ Python/ 語料訪問
文本翻譯
提取URL地址
處理PDF
塊分類
搜索和匹配
大寫轉(zhuǎn)換
提取電子郵件地址
字符串的不變性
文本處理狀態(tài)機(jī)
雙字母組
閱讀RSS提要
單詞替換
WordNet接口
重新格式化段落
標(biāo)記單詞
向后讀取文件
塊和裂口
美化打印數(shù)字
拼寫檢查
將二進(jìn)制轉(zhuǎn)換為ASCII
文本分類
文字換行
頻率分布
字符串作為文件
約束搜索
詞干算法
符號(hào)化
同義詞和反義詞
過濾重復(fù)的字詞
刪除停用詞
Python文本處理教程
文字摘要
段落計(jì)數(shù)令牌
語料訪問
文字改寫
文本處理簡(jiǎn)介
處理Word文檔
Python文本處理開發(fā)環(huán)境
排序行

語料訪問

Corpora是一個(gè)展示多個(gè)文本文檔集合的組。 單個(gè)集合稱為語料庫(kù)。 其中一個(gè)著名的語料庫(kù)是古騰堡語料庫(kù),其中包含大約25,000本免費(fèi)電子書,由 http://www.gutenberg.org/ 托管。 在下面的例子中,只訪問語料庫(kù)中那些文件的名稱,這些文件是純文本,以.txt結(jié)尾的文件名。

from nltk.corpus import gutenberg
fields = gutenberg.fileids()

print(fields)

執(zhí)行上面示例代碼,得到以下結(jié)果 -

[austen-emma.txt', austen-persuasion.txt', austen-sense.txt', bible-kjv.txt', 
blake-poems.txt', bryant-stories.txt', burgess-busterbrown.txt',
carroll-alice.txt', chesterton-ball.txt', chesterton-brown.txt', 
chesterton-thursday.txt', edgeworth-parents.txt', melville-moby_dick.txt',
milton-paradise.txt', shakespeare-caesar.txt', shakespeare-hamlet.txt',
shakespeare-macbeth.txt', whitman-leaves.txt']

訪問原始文本

可以使用sent_tokenize函數(shù)從這些文件中訪問原始文本,該函數(shù)也可以在nltk中使用。 在下面的例子中,將檢索blake-poen文本的前兩段。

from nltk.tokenize import sent_tokenize
from nltk.corpus import gutenberg

sample = gutenberg.raw("blake-poems.txt")

token = sent_tokenize(sample)

for para in range(2):
    print(token[para])

當(dāng)運(yùn)行上面的程序時(shí),我們得到以下輸出 -

[Poems by William Blake 1789]


SONGS OF INNOCENCE AND OF EXPERIENCE
and THE BOOK of THEL


 SONGS OF INNOCENCE


 INTRODUCTION

 Piping down the valleys wild,
   Piping songs of pleasant glee,
 On a cloud I saw a child,
   And he laughing said to me:

 "Pipe a song about a Lamb!"
So I piped with merry cheer.

上一篇:處理PDF下一篇:詞干算法