8.1 Some Grammatical Dilemmas
Linguistic Data and Unlimited Possibilities
Previous chapters have shown you how to process and analyze text corpora, and we
have stressed the challenges for NLP in dealing with the vast amount of electronic
language data that is growing daily. Let’s consider this data more closely, and make the
thought experiment that we have a gigantic corpus consisting of everything that has
been either uttered or written in English over, say, the last 50 years. Would we be
justified in calling this corpus “the language of modern English”? There are a number
of reasons why we might answer no. Recall that in Chapter 3, we asked you to search
the Web for instances of the pattern the of. Although it is easy to find examples on the
Web containing this word sequence, such as New man at the of IMG (see http://www
.telegraph.co.uk/sport/2387900/New-man-at-the-of-IMG.html), speakers of English
will say that most such examples are errors, and therefore not part of English after all.
Accordingly, we can argue that “modern English” is not equivalent to the very big set
of word sequences in our imaginary corpus. Speakers of English can make judgments
about these sequences, and will reject some of them as being ungrammatical.
Equally, it is easy to compose a new sentence and have speakers agree that it is perfectly
good English. For example, sentences have an interesting property that they can be
embedded inside larger sentences. Consider the following sentences:
(1) a.Usain Bolt broke the 100m record.
b.The Jamaica Observer reported that Usain Bolt broke the 100m record.
c.Andre said The Jamaica Observer reported that Usain Bolt broke the 100m
d.I think Andre said the Jamaica Observer reported that Usain Bolt broke
the 100m record.
If we replaced whole sentences with the symbol
, we would see patterns like Andre
and I think
. These are templates for taking a sentence and constructing a bigger
sentence. There are other templates we can use, such as
. With a
bit of ingenuity we can construct some really long sentences using these templates.
Here’s an impressive example from a Winnie the Pooh story by A.A. Milne, In Which
Piglet Is Entirely Surrounded by Water:
[You can imagine Piglet’s joy when at last the ship came in sight of him.] In after-years
he liked to think that he had been in Very Great Danger during the Terrible Flood, but
the only danger he had really been in was the last half-hour of his imprisonment, when
Owl, who had just flown up, sat on a branch of his tree to comfort him, and told him a
very long story about an aunt who had once laid a seagull’s egg by mistake, and the story
went on and on, rather like this sentence, until Piglet who was listening out of his window
without much hope, went to sleep quietly and naturally, slipping slowly out of the win-
dow towards the water until he was only hanging on by his toes, at which moment,
292 | | Chapter 8: Analyzing Sentence Structure
luckily, a sudden loud squawk from Owl, which was really part of the story, being what
his aunt said, woke the Piglet up and just gave him time to jerk himself back into safety
and say, “How interesting, and did she?” when—well, you can imagine his joy when at
last he saw the good ship, Brain of Pooh (Captain, C. Robin; 1st Mate, P. Bear) coming
over the sea to rescue him…
This long sentence actually has a simple structure that begins S but S when S. We can
see from this example that language provides us with constructions which seem to allow
us to extend sentences indefinitely. It is also striking that we can understand sentences
of arbitrary length that we’ve never heard before: it’s not hard to concoct an entirely
novel sentence, one that has probably never been used before in the history of the
language, yet all speakers of the language will understand it.
The purpose of a grammar is to give an explicit description of a language. But the way
in which we think of a grammar is closely intertwined with what we consider to be a
language. Is it a large but finite set of observed utterances and written texts? Is it some-
thing more abstract like the implicit knowledge that competent speakers have about
grammatical sentences? Or is it some combination of the two? We won’t take a stand
on this issue, but instead will introduce the main approaches.
In this chapter, we will adopt the formal framework of “generative grammar,” in which
a “language” is considered to be nothing more than an enormous collection of all
grammatical sentences, and a grammar is a formal notation that can be used for “gen-
erating” the members of this set. Grammars use recursive productions of the form
, as we will explore in Section 8.3. In Chapter 10 we will extend this, to
automatically build up the meaning of a sentence out of the meanings of its parts.
A well-known example of ambiguity is shown in (2), from the Groucho Marx movie,
Animal Crackers (1930):
(2) While hunting in Africa, I shot an elephant in my pajamas. How an elephant
got into my pajamas I’ll never know.
Let’s take a closer look at the ambiguity in the phrase: I shot an elephant in my paja-
mas. First we need to define a simple grammar:
>>> groucho_grammar = nltk.parse_cfg("""
... S -> NP VP
... PP -> P NP
... NP -> Det N | Det N PP | 'I'
... VP -> V NP | VP PP
... Det -> 'an' | 'my'
... N -> 'elephant' | 'pajamas'
... V -> 'shot'
... P -> 'in'
8.1 Some Grammatical Dilemmas s | | 293
This grammar permits the sentence to be analyzed in two ways, depending on whether
the prepositional phrase in my pajamas describes the elephant or the shooting event.
>>> parser = nltk.ChartParser(groucho_grammar)
>>> trees = parser.nbest_parse(sent)
>>> for tree in trees:
... print tree
(VP (V shot) (NP (Det an) (N elephant)))
(PP (P in) (NP (Det my) (N pajamas)))))
The program produces two bracketed structures, which we can depict as trees, as
shown in (3):
294 | | Chapter 8: Analyzing Sentence Structure
Notice that there’s no ambiguity concerning the meaning of any of the words; e.g., the
word shot doesn’t refer to the act of using a gun in the first sentence and using a camera
in the second sentence.
Your Turn: Consider the following sentences and see if you can think
of two quite different interpretations: Fighting animals could be danger-
ous. Visiting relatives can be tiresome. Is ambiguity of the individual
words to blame? If not, what is the cause of the ambiguity?
This chapter presents grammars and parsing, as the formal and computational methods
for investigating and modeling the linguistic phenomena we have been discussing. As
we shall see, patterns of well-formedness and ill-formedness in a sequence of words
can be understood with respect to the phrase structure and dependencies. We can
develop formal models of these structures using grammars and parsers. As before, a
key motivation is natural language understanding. How much more of the meaning of
a text can we access when we can reliably recognize the linguistic structures it contains?
Having read in a text, can a program “understand” it enough to be able to answer simple
questions about “what happened” or “who did what to whom”? Also as before, we will
develop simple programs to process annotated corpora and perform useful tasks.
8.2 What’s the Use of Syntax?
We gave an example in Chapter 2 of how to use the frequency information in bigrams
to generate text that seems perfectly acceptable for small sequences of words but rapidly
degenerates into nonsense. Here’s another pair of examples that we created by com-
puting the bigrams over the text of a children’s story, The Adventures of Buster
Brown (included in the Project Gutenberg Selection Corpus):
(4) a.He roared with me the pail slip down his back
b.The worst part and clumsy looking for whoever heard light
You intuitively know that these sequences are “word-salad,” but you probably find it
hard to pin down what’s wrong with them. One benefit of studying grammar is that it
provides a conceptual framework and vocabulary for spelling out these intuitions. Let’s
take a closer look at the sequence the worst part and clumsy looking. This looks like a
coordinate structure, where two phrases are joined by a coordinating conjunction
such as and, but, or or. Here’s an informal (and simplified) statement of how coordi-
nation works syntactically:
Coordinate Structure: if v
are both phrases of grammatical category X, then v
is also a phrase of category X.
8.2 What’s the Use of Syntax? ? | | 295
Here are a couple of examples. In the first, two
s (noun phrases) have been conjoined
to make an
, while in the second, two
s (adjective phrases) have been conjoined to
(5) a.The book’s ending was (NP the worst part and the best part) for me.
b.On land they are (AP slow and clumsy looking).
What we can’t do is conjoin an
, which is why the worst part and clumsy
looking is ungrammatical. Before we can formalize these ideas, we need to understand
the concept of constituent structure.
Constituent structure is based on the observation that words combine with other words
to form units. The evidence that a sequence of words forms such a unit is given by
substitutability—that is, a sequence of words in a well-formed sentence can be replaced
by a shorter sequence without rendering the sentence ill-formed. To clarify this idea,
consider the following sentence:
(6) The little bear saw the fine fat trout in the brook.
The fact that we can substitute He for The little bear indicates that the latter sequence
is a unit. By contrast, we cannot replace little bear saw in the same way. (We use an
asterisk at the start of a sentence to indicate that it is ungrammatical.)
(7) a.He saw the fine fat trout in the brook.
b.*The he the fine fat trout in the brook.
In Figure 8-1, we systematically substitute longer sequences by shorter ones in a way
which preserves grammaticality. Each sequence that forms a unit can in fact be replaced
by a single word, and we end up with just two elements.
Figure 8-1. Substitution of word sequences: Working from the top row, we can replace particular
sequences of words (e.g., the brook) with individual words (e.g., it); repeating this process, we arrive
at a grammatical two-word sentence.
296 | | Chapter 8: Analyzing Sentence Structure
In Figure 8-2, we have added grammatical category labels to the words we saw in the
earlier figure. The labels
stand for noun phrase, verb phrase, and
prepositional phrase, respectively.
If we now strip out the words apart from the topmost row, add an
node, and flip the
figure over, we end up with a standard phrase structure tree, shown in (8). Each node
in this tree (including the words) is called a constituent. The immediate constitu-
As we saw in Section 8.1, sentences can have arbitrary length. Conse-
quently, phrase structure trees can have arbitrary depth. The cascaded
chunk parsers we saw in Section 7.4 can only produce structures of
bounded depth, so chunking methods aren’t applicable here.
Figure 8-2. Substitution of word sequences plus grammatical categories: This diagram reproduces
Figure 8-1 along with grammatical categories corresponding to noun phrases (NP), verb phrases
(VP), prepositional phrases (PP), and nominals (Nom).
8.2 What’s the Use of Syntax? ? | | 297
As we will see in the next section, a grammar specifies how the sentence can be subdi-
vided into its immediate constituents, and how these can be further subdivided until
we reach the level of individual words.
8.3 Context-Free Grammar
A Simple Grammar
Let’s start off by looking at a simple context-free grammar (CFG). By convention,
the lefthand side of the first production is the start-symbol of the grammar, typically
, and all well-formed trees must have this symbol as their root label. In NLTK, context-
free grammars are defined in the
module. In Example 8-1 we define a
grammar and show how to parse a simple sentence admitted by the grammar.
Example 8-1. A simple context-free grammar.
grammar1 = nltk.parse_cfg("""
S -> NP VP
VP -> V NP | V NP PP
PP -> P NP
V -> "saw" | "ate" | "walked"
NP -> "John" | "Mary" | "Bob" | Det N | Det N PP
Det -> "a" | "an" | "the" | "my"
N -> "man" | "dog" | "cat" | "telescope" | "park"
P -> "in" | "on" | "by" | "with"
>>> sent = "Mary saw Bob".split()
>>> rd_parser = nltk.RecursiveDescentParser(grammar1)
>>> for tree in rd_parser.nbest_parse(sent):
... print tree
(S (NP Mary) (VP (V saw) (NP Bob)))
The grammar in Example 8-1 contains productions involving various syntactic cate-
gories, as laid out in Table 8-1. The recursive descent parser used here can also be
inspected via a graphical interface, as illustrated in Figure 8-3; we discuss this parser
in more detail in Section 8.4.
Table 8-1. Syntactic categories
the man walked
saw a park
prepositional phrase e with a telescope
298 | | Chapter 8: Analyzing Sentence Structure
Documents you may be interested
Documents you may be interested