As just mentioned, a text corpus is a large body of text. Many corpora are designed to
contain a careful balance of material in one or more genres. We examined some small
text collections in Chapter 1, such as the speeches known as the US Presidential Inau
gural Addresses. This particular corpus actually contains dozens of individual texts—
one per address—but for convenience we glued them end-to-end and treated them as
a single text. Chapter 1 also used various predefined texts that we accessed by typing
from book import *. However, since we want to be able to work with other texts, this
section examines a variety of text corpora. We’ll see how to select individual texts, and
how to work with them.
Accessing Text Corporaand Lexical Resources
Practical work in Natural Language Processing typically uses large bodies of linguistic
data, or corpora. The goal of this chapter is to answer the following questions:
- What are some useful text corpora and lexical resources, and how can we access
them with Python? - Which Python constructs are most helpful for this work?
- How do we avoid repeating ourselves when writing Python code?
Accessing Text Corpora
As just mentioned, a text corpus is a large body of text. Many corpora are designed to
contain a careful balance of material in one or more genres. We examined some small
text collections in Chapter 1, such as the speeches known as the US Presidential Inau
gural Addresses. This particular corpus actually contains dozens of individual texts—
one per address—but for convenience we glued them end-to-end and treated them as
a single text. Chapter 1 also used various predefined texts that we accessed by typing
from book import *. However, since we want to be able to work with other texts, this
section examines a variety of text corpora. We’ll see how to select individual texts, and
how to work with them.
Counting Vocabulary
The most obvious fact about texts that emerges from the preceding examples is that
they differ in the vocabulary they use. In this section, we will see how to use the com
puter to count the words in a text in a variety of useful ways. As before, you will jump
right in and experiment with the Python interpreter, even though you may not have
studied Python systematically yet. Test your understanding by modifying the examples,
and trying the exercises at the end of the chapter.
Searching Text
There are many ways to examine the context of a text apart from simply reading it. A
concordance view shows us every occurrence of a given word, together with some
context. Here we look up the word monstrous in Moby Dick by entering text1 followed
by a period, then the term concordance, and then placing “monstrous” in parentheses:
Getting Started with NLTK
Before going further you should install NLTK, downloadable for free from http://www
.nltk.org/. Follow the instructions there to download the version required for your
platform.
Getting Started with Python
One of the friendly things about Python is that it allows you to type directly into the
interactive interpreter—the program that will be running your Python programs. You
can access the Python interpreter using a simple graphical interface called the In
teractive DeveLopment Environment (IDLE). On a Mac you can find this under Ap
plications→MacPython, and on Windows under All Programs→Python. Under Unix
you can run Python from the shell by typing idle (if this is not installed, try typing
python). The interpreter will print a blurb about your Python version; simply check that
you are running Python 2.4 or 2.5
1.1 Computing with Language: Texts and Words
We’re all very familiar with text, since we read and write it every day. Here we will treat
text as raw data for the programs we write, programs that manipulate and analyze it in
a variety of interesting ways. But before we can do this, we have to get started with the
Python interpreter.
