Python - Accès aux corpus

Corpora est un groupe présentant plusieurs collections de documents texte. Une seule collection est appelée corpus. L'un de ces corpus célèbres est le Gutenberg Corpus, qui contient quelque 25 000 livres électroniques gratuits, hébergés sur http://www.gutenberg.org/. Dans l'exemple ci-dessous, nous accédons uniquement aux noms des fichiers du corpus qui sont du texte brut avec un nom de fichier se terminant par .txt.

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

Lorsque nous exécutons le programme ci-dessus, nous obtenons la sortie suivante -

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

Accéder au texte brut

Nous pouvons accéder au texte brut de ces fichiers en utilisant la fonction sent_tokenize qui est également disponible dans nltk. Dans l'exemple ci-dessous, nous récupérons les deux premiers paragraphes du texte 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])

Lorsque nous exécutons le programme ci-dessus, nous obtenons la sortie suivante -

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