Linguistic Structure/Constituency Parsing

Lesson 6.12,666 words

Constituency Parsing

A constituency parse groups a sentence into nested phrases described by a context-free grammar. We build the CFG formalism, read the phrase structure of English off a treebank, confront the structural ambiguity that makes parsing hard, convert to Chomsky normal form, and then solve it with CKY — the dynamic-programming chart that fills a triangular table bottom-up.

╌╌╌╌

Some groups of words behave as a single unit. In I'd like to fly on September seventeenth from Atlanta to Denver, the phrase on September seventeenth can slide to the front of the sentence, to the very end, or into the middle, and the result is still English — but you cannot move its words one at a time. The whole phrase moves together or not at all. That is the empirical fact behind syntactic constituency: words cluster into phrases, phrases nest inside larger phrases, and this hierarchy governs where words may appear, how they agree, and what a sentence means.1

Constituency parsing is the task of recovering that hierarchy — mapping a flat string of words to a nested tree of phrases. It underlies grammar checking (a sentence that will not parse is often ungrammatical), and it feeds semantic analysis and question answering: to answer Which flights to Denver depart before the Seattle flight? a system must know that to Denver modifies flights, not depart, and that which flights to Denver is the subject.2 This lesson develops the grammar that describes constituents, the ambiguity that makes recovering them hard, and the dynamic-programming algorithm that recovers them anyway.

Context-free grammars

The standard formal system for constituent structure is the context-free grammar, or CFG (equivalently, a phrase-structure grammar).3 A CFG is built from rules (productions), each expressing how a symbol may be rewritten as an ordered sequence of symbols. The symbols split into two classes: terminals, which are the actual words (the, flight), and nonterminals, which are abstractions over them (NP, VP, Noun). Each rule has a single nonterminal on the left of the arrow and a string of terminals and nonterminals on the right:

The rules with a single terminal on the right (Det → a) are the lexicon; the rest are the grammar proper. Reading the arrow as rewrite the left symbol with the right string, a CFG generates strings. Start from NP, rewrite it to Det Nominal, rewrite Nominal to Noun, and rewrite the parts of speech to words: the string a flight has been derived from NP. That sequence of rewrites is a derivation, and it is naturally drawn as a parse tree with the start symbol at the root and the words at the leaves.

A parse tree for "a flight": the derivation NP -> Det Nominal -> Det Noun -> a flight, drawn with the start symbol NP at the root and words at the leaves. NP immediately dominates Det and Nom.

We say NP dominates every node beneath it, and immediately dominates its direct children Det and Nom. Formally, a CFG is a 4-tuple : a set of nonterminals, a disjoint set of terminals, a set of rules with and , and a designated start symbol.4 The language that generates is the set of terminal strings derivable from :

where is the reflexive-transitive closure of directly derives. A string in is grammatical; one outside it is ungrammatical.

Adding a few rules turns this into a grammar for small sentences. A sentence S is a noun phrase followed by a verb phrase; a verb phrase is a verb followed by assorted complements; a prepositional phrase is a preposition followed by a noun phrase:

With this grammar (Jurafsky and Martin call it ), the sentence I prefer a morning flight has a full derivation from S, shown as a tree.

Parse tree for "I prefer a morning flight" under the grammar L0. The top rule is S -> NP VP; the recursive rule Nominal -> Nominal Noun stacks "morning" onto "flight".

The same tree has a flat bracketed notation, which packs the structure onto one line by matching brackets and labelling each with its nonterminal:

text
[S [NP [Pro I]] [VP [V prefer] [NP [Det a] [Nom [N morning] [Nom [N flight]]]]]]

Grammatical phenomena, briefly

The phrase structure of English has enough regularity to fill a chapter; a few patterns recur throughout and motivate the design choices later.5

  • NP structure. A noun phrase has a head noun with modifiers before it (determiners, numbers, adjectives — the first non-stop flight) and after it (prepositional phrases, relative clauses — flights from Denver that serve dinner). Post-head modifiers are recursive: Nominal → Nominal PP and Nominal → Nominal Noun stack indefinitely, the same recursion the tree above exhibits.
  • VP structure and subcategorization. A verb phrase pairs a verb with complements, but not every verb accepts every complement. find requires a direct object (VP → Verb NP), disappear takes none, and want accepts either an NP or an infinitival VP. The set of complements a verb licenses is its subcategorization frame; treating it carelessly forces the grammar to multiply verb classes and rules.6
  • Agreement. The subject and main verb must match in person and number: this flight serves but not *this flight serve. A pure CFG can only enforce this by doubling every affected rule (a singular copy and a plural copy), one of the redundancies that pushes practitioners toward lexicalized grammars.
  • Coordination. Like constituents conjoin into a larger constituent of the same type: NP → NP and NP, VP → VP and VP. The general schema is X → X and X. Conjoinability is itself a test for constituency.
  • Long-distance dependencies. In What flights do you have from Burbank?, the wh-phrase what flights is the object of have, yet sits far from it at the front of the sentence. These fronted arguments — in wh-questions, relative clauses, and topicalization — strain a formalism whose rules are local, and treebanks mark them with special empty nodes (below).

Treebanks

A grammar robust enough to parse any sentence lets us build a corpus in which every sentence is paired with its parse tree — a treebank.7 The Penn Treebank is the canonical example, with hand-corrected parses of the Brown, Switchboard, ATIS, and Wall Street Journal corpora. Trees are stored in LISP-style parenthesized notation; the ATIS sentence The flight should arrive at eleven a.m. tomorrow is bracketed as:

text
(S (NP-SBJ (DT The) (NN flight))   (VP (MD should)       (VP (VB arrive)           (PP-TMP (IN at) (NP (CD eleven) (RB a.m.)))           (NP-TMP (NN tomorrow)))))

Two features matter downstream. First, a treebank is a grammar: read the rules off the trees and you recover a CFG for the corpus. The Wall Street Journal portion (a million words) yields about 17,500 distinct rule types, many of them very flat and very long — there are roughly 4,500 rules just for expanding VP, including monsters like VP → VBP PP PP PP PP PP ADVP PP. Second, Penn Treebank marks long-distance dependencies with traces: an empty -NONE- node holds the position where a moved constituent belongs, co-indexed with the constituent's surface location, so a parser can recover which argument goes with which predicate.

A Penn Treebank tree for a Brown-corpus sentence, "That cold, empty sky was full of fire and light." The -PRD tag marks a non-VP predicate (here an adjective phrase) and -SBJ marks the surface subject.

Structural ambiguity

The reason parsing is hard is that one grammar routinely assigns many trees to one sentence — structural ambiguity.8 Groucho Marx's line I shot an elephant in my pajamas is the textbook case. The prepositional phrase in my pajamas can attach low, inside the noun phrase headed by elephant (the elephant is wearing the pajamas — the joke), or high, inside the verb phrase headed by shot (the shooting happened in the pajamas). Both are licensed by the same grammar.

Two parses of "I shot an elephant in my pajamas." Left: the PP attaches inside the NP (the elephant is in the pajamas). Right: the PP attaches to the VP (the shooting happens in the pajamas). This is PP-attachment ambiguity.

This is attachment ambiguity: a constituent can hang from the tree at more than one place. PP-attachment is the most common form, and it compounds — a sentence with several prepositional phrases has a number of parses that grows combinatorially.

A second flavor is coordination ambiguity, where a conjunction leaves the grouping unclear. old men and women can mean old men and women (everyone is old) or old men and women (only the men are old). The two readings correspond to different trees.

Coordination ambiguity in "old men and women." Left: the adjective scopes over the whole coordination, so both men and women are old. Right: the adjective scopes over "men" only.

Real sentences pile these up. Many grammatically valid parses are semantically absurd, and enumerating them naively is exponential. The engineering response comes in two parts: a dynamic-programming algorithm that represents all parses compactly, and a scoring model that picks the right one. We take them in that order.

Chomsky normal form

The parsing algorithm below needs the grammar in a restricted shape. A CFG is in Chomsky normal form (CNF) if it is -free and every rule has one of exactly two shapes: (two nonterminals) or (one terminal).9 CNF grammars are binary branching: above the part-of-speech level, every node has exactly two children. That property is what makes the chart triangular and the algorithm cubic.

Any CFG can be converted to a weakly equivalent CNF grammar — one generating the same string set — by repairing three kinds of offending rule.10

  • Terminals mixed with nonterminals on the right (INF-VP → to VP): introduce a dummy nonterminal covering the terminal, giving INF-VP → TO VP and TO → to.
  • Unit productions — a single nonterminal on the right (A → B): follow each chain and, for every non-unit rule , add , then discard the unit rules. This flattens the grammar and promotes terminals higher in the tree.
  • Right-hand sides longer than two (A → B C D): split off the leftmost pair under a fresh nonterminal, A → X1 D and X1 → B C, and iterate until every rule is binary. So S → Aux NP VP becomes S → X1 VP and X1 → Aux NP.
Converting the ternary rule A -> B C D to Chomsky normal form. A fresh nonterminal X1 absorbs the leftmost pair B C, leaving two binary rules that derive the same strings.

The conversion has a side benefit: binarizing a family of flat rules like VP → VBD NP, VP → VBD NP PP, VP → VBD NP PP PP, ... can be replaced by the compact pair VP → VBD NP and VP → VP PP, generating the same infinite family with two rules.

The CKY algorithm

CKY (Cocke-Kasami-Younger) is the standard dynamic-programming parser.11 It is the same idea as minimum edit distance and Viterbi: fill a table of subproblem solutions bottom-up so that each cell is computed from cells already filled. Here a subproblem is which nonterminals can span this stretch of the input, and the context-free property is what makes it a dynamic program — once a constituent is found over a span, it can be reused in any larger derivation without being reanalyzed.

The chart

Index the fenceposts between words from to , so the input Book the flight through Houston is read as Book the flight through Houston . A cell holds the set of nonterminals that can span positions through . Only the upper triangle of the matrix is used, since ; the cell covers the whole sentence, and the sentence is grammatical exactly when .

Because the grammar is in CNF, any constituent spanning was built from a rule where spans and spans for some split point with . Geometrically, the left child lies along row to the left of , and the right child lies down column beneath it. Filling means scanning every split , checking whether any grammar rule combines a nonterminal from with one from .

Filling cell [i,j] in the CKY chart. For each split point k, the left child comes from cell [i,k] (along row i) and the right child from cell [k,j] (down column j); a rule A -> B C combining them adds A to [i,j].

The recognizer

The order of filling matters: to have both children ready, process columns left to right and, within a column, rows bottom to top. The base case fills the diagonal with the parts of speech of word ; then each higher cell combines pairs of lower cells.

Algorithm:CKY-Parse(words,grammar)\textsc{CKY-Parse}(\textit{words}, \textit{grammar}) — fill the constituency chart
  1. 1
    input: sentence words[1..n]\textit{words}[1..n], grammar in Chomsky normal form
  2. 2
    table\textit{table} \gets empty (n+1)×(n+1)(n{+}1) \times (n{+}1) table
  3. 3
    for j=1j = 1 to nn do
  4. 4
    for each AA such that Awords[j]grammarA \rightarrow \textit{words}[j] \in \textit{grammar} do
  5. 5
    add AA to table[j1,j]\textit{table}[j{-}1, j]
    part-of-speech base case
  6. 6
    for i=j2i = j{-}2 downto 00 do
  7. 7
    for k=i+1k = i{+}1 to j1j{-}1 do
  8. 8
    for each ABCgrammarA \rightarrow B\,C \in \textit{grammar} with Btable[i,k]B \in \textit{table}[i,k] and Ctable[k,j]C \in \textit{table}[k,j] do
  9. 9
    add AA to table[i,j]\textit{table}[i,j]
    constituent over span (i, j)
  10. 10
    return table\textit{table}

Three nested loops over positions (, , ) times a scan of the grammar give time — cubic in the sentence length, linear in the grammar size. As written this is a recognizer: it reports grammaticality by testing but does not return trees. Two changes make it a parser: attach to each table entry back-pointers to the two cells it was built from, and allow multiple copies of a nonterminal (one per way of deriving it). The filled table then encodes every parse; a single tree is read off by picking an from and recursively following its back-pointers.

A worked example

Take the grammar in CNF, whose lexicon includes Book as Verb, Noun, Nominal, VP, and S; the as Det; flight as Noun, Nominal; through as Preposition; and Houston as NP, ProperNoun. Relevant binary rules: S → NP VP, S → VP PP, VP → Verb NP, VP → VP PP, NP → Det Nominal, Nominal → Nominal PP, PP → Preposition NP, plus S → X2 PP and VP → X2 PP with X2 → Verb NP. Parse Book the flight through Houston ().

Trace the fill span-length by span-length; the algorithm processes column left to right and, within a column, row from the diagonal upward, so every child cell is ready before its parent.

Length 1 (the diagonal). Each word's parts of speech come straight from the lexicon, filling :

Length 2. Each cell has a single split point. (Book the) needs a rule combining something in with Det in — none exists, so . (the flight): Det in and Nominal in fire NP → Det Nominal, so NP ∈ [1,3]. (flight through): no rule takes a Nominal then a Preposition, so it stays empty. (through Houston): Preposition in meets NP in under PP → Preposition NP, giving PP ∈ [3,5].

Length 3. Now two split points must each be tried. (flight through Houston): at split , Nominal in and PP in fire Nominal → Nominal PP, so Nominal ∈ [2,5]. (Book the flight): at , the Verb in and NP in fire both VP → Verb NP and its binarized helper X2 → Verb NP, and the resulting VP also satisfies S → VP chains, so .

Length 4. (the flight through Houston): at , Det in and Nominal in fire NP → Det Nominal, so NP ∈ [1,5].

Length 5 (the top cell). spans the whole sentence, with four possible splits . Two of them succeed and produce three S derivations:

  • : Verb in with NP in via X2 → Verb NP, and through Houston already folded into the NP — this is the low PP-attachment (the flight through Houston).
  • : VP/X2 in with PP in via VP → VP PP and via S → X2 PP — the high PP-attachment (the booking happens through Houston), reached two ways.

So holds {S, VP, X2} with three distinct back-pointer sets for S.

The CKY chart for "Book the flight through Houston" filling by span length: length-1 diagonal from the lexicon, then length 2, 3, 4, and finally the top cell [0,5] with three S derivations (the PP-attachment ambiguity). Empty cells are shown blank.

The same chart, drawn in the conventional upper-triangular layout with the diagonal along the bottom, is below; the top-right cell carries the three-way ambiguity as three back-pointer sets.

Completed CKY chart for "Book the flight through Houston." Each cell [i,j] lists the nonterminals spanning words i..j. The top-right cell [0,5] holds three S's, one per parse. Empty cells are omitted.

The three S entries in are the three readings: the prepositional phrase through Houston modifies the flight, or it modifies the booking event via VP → VP PP, or it fills the second argument slot recovered through VP → X2 PP (the binarized form of the original VP → Verb NP PP). CKY has produced all of them in one cubic-time pass and stored them in a table of shared subtrees — this reuse of solved subproblems is dynamic programming exactly as in the algorithms course, the same optimal-substructure argument that underlies edit distance and Viterbi, specialized to the triangular chart of spans.

Where this continues

We have the recognition machinery. A context-free grammar describes how words group into nested phrases; the phrase structure of English fills it out; a treebank turns those trees into a grammar automatically; structural ambiguity — PP-attachment, coordination — is why one grammar assigns many trees to one sentence; and Chomsky normal form binarizes the grammar so the CKY chart can fill a triangular table bottom-up in cubic time, storing every parse compactly.

CKY hands back all parses but does not say which is right. Scoring trees to pick the correct one — probabilistic CFGs and neural span parsers — evaluating a parser against a treebank with PARSEVAL, and the cheaper shallow parsing that skips the full tree continue in CKY scoring, evaluation, and shallow parsing.

Footnotes

  1. Jurafsky & Martin, Speech and Language Processing (3rd ed.), §12.1 — Constituency: the evidence that groups of words act as units (appearing in the same environments, moving together as preposed/postposed phrases).
  2. Jurafsky & Martin, Ch. 13 — Constituency Parsing, chapter opening: parsing as assigning syntactic structure, and its use in grammar checking, semantic analysis, and question answering.
  3. Jurafsky & Martin, §12.2 — Context-Free Grammars: rules and productions, terminals and nonterminals, the lexicon, derivations, and parse trees.
  4. Jurafsky & Martin, §12.2.1 — Formal Definition of Context-Free Grammar: the 4-tuple , direct derivation, and the language generated from the start symbol.
  5. Jurafsky & Martin, §12.3 — Some Grammar Rules for English: sentence-level constructions, the noun phrase, the verb phrase, and coordination.
  6. Jurafsky & Martin, §12.3.4 — The Verb Phrase: transitive vs. intransitive verbs, subcategorization frames, and complements as logical arguments of the verb.
  7. Jurafsky & Martin, §12.4 — Treebanks: the Penn Treebank, LISP-style bracketed notation, traces / -NONE- nodes for long-distance dependencies, and treebanks read as grammars.
  8. Jurafsky & Martin, §13.1 — Ambiguity: structural ambiguity, PP-attachment (I shot an elephant in my pajamas), and coordination ambiguity (old men and women).
  9. Jurafsky & Martin, §12.5 — Grammar Equivalence and Normal Form: Chomsky normal form as -free grammars with rules or , and their binary-branching trees.
  10. Jurafsky & Martin, §13.2.1 — Conversion to Chomsky Normal Form: handling terminals mixed with nonterminals, unit productions, and right-hand sides longer than two.
  11. Jurafsky & Martin, §13.2 — CKY Parsing: the dynamic-programming chart, fenceposts, the fill order, the recognizer of Fig. 13.5, and its extension to a parser with back-pointers; worked on Book the flight through Houston.

╌╌ END ╌╌