📜  Python -Corpora访问

📅  最后修改于: 2020-11-06 06:19:16             🧑  作者: Mango


Corpora是一个小组,提出了多个文本文档集合。单个集合称为语料库。这样的著名语料库就是古腾堡语料库,其中包含约25,000本免费电子书,托管在http://www.gutenberg.org/。在下面的示例中,我们仅访问语料库中那些纯文本(文件名以.txt结尾)的文件的名称。

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

print(fields)

当我们运行上面的程序时,我们得到以下输出-

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

访问原始文本

我们可以使用send_tokenize函数访问这些文件中的原始文本,该函数在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])

当我们运行上面的程序时,我们得到以下输出-

[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.