corpora is a group presenting multiple collections of text documents. a single collection is called corpus. one such famous corpus is the gutenberg corpus which contains some 25,000 free electronic books, hosted at http://www.gutenberg.org/. in the below example we access the names of only those files from the corpus which are plain text with filename ending as .txt.
from nltk.corpus import gutenberg fields = gutenberg.fileids() print(fields)
when we run the above program, we get the following output −
[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']
accessing raw text
we can access the raw text from these files using sent_tokenize function which is also available in nltk. in the below example we retrieve the first two paragraphs of the blake poen text.
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])
when we run the above program we get the following output −
[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.