Python – 访问语料库

Python – 访问语料库

语料库是一个包含多个文本文档集合的组。一个文本文档集合称为“语料库”。其中一个著名的语料库是 Gutenberg 语料库,包含约 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']

访问原始文本

我们可以使用 sent_tokenize 函数从这些文件中访问原始文本,该函数在 nltk 中也可用。在以下示例中,我们检索 blake poems 文本的前两段。

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.

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程