PCFG
PCFG
PCFG
Probabilistic Context‐Free
Grammars
Raphael Hoffmann
590AI, Winter 2009
Outline
• PCFGs: Inference and Learning
• Parsing English
• Discriminative CFGs
• Grammar Induction
Image Search for “pcfg”
Live Search
Outline
• PCFGs: Inference and Learning
• Parsing English
• Discriminative CFGs
• Grammar Induction
The velocity of the seismic waves rises to …
Slide based on “Foundations of Statistical Natural Language Processing” by Christopher Manning and Hinrich Schütze
A CFG consists of
• Terminals w 1 , w 2 , . . . , wV
• Nonterminals N 1, N 2 ,...,
Nn
• Start symbol N1
• Rules N i −→ ζ j
where ζ j is a sequence of
terminals and nonterminals
Slide based on “Foundations of Statistical Natural Language Processing” by Christopher Manning and Hinrich Schütze
A (generative) PCFG consists of
• Terminals w 1 , w 2 , . . . , wV
• Nonterminals N 1, N 2 ,...,
Nn
• Start symbol N1
• Rules N i −→ ζ j
where ζ j is a sequence of
terminals and nonterminals
Slide based on “Foundations of Statistical Natural Language Processing” by Christopher Manning and Hinrich Schütze
Notation
sentence: sequence of words w w .. . w 1 2 m
wab :
the subsequence w ... w a b
N i
ab : nonterminal N i
dominates wa . . . wb
Ni
∗
N =⇒ ζ
i
:
repeated derivation from N i gives ζ
Slide based on “Foundations of Statistical Natural Language Processing” by Christopher Manning and Hinrich Schütze
Probability of a Sentence
X
P (w1 n ) = P (w1 n , t)
t
w 1n
Slide based on “Foundations of Statistical Natural Language Processing” by Christopher Manning and Hinrich Schütze
Example
Slide based on “Foundations of Statistical Natural Language Processing” by Christopher Manning and Hinrich Schütze
astronomers saw stars with ears
Slide based on “Foundations of Statistical Natural Language Processing” by Christopher Manning and Hinrich Schütze
astronomers saw stars with ears
Slide based on “Foundations of Statistical Natural Language Processing” by Christopher Manning and Hinrich Schütze
Probabilities
Slide based on “Foundations of Statistical Natural Language Processing” by Christopher Manning and Hinrich Schütze
Assumptions of PCFGs
1. Place invariance (like time invariance in HMMs)
j
∀k P (Nk ( k +c ) −→ is the same
ζ)
Slide based on “Foundations of Statistical Natural Language Processing” by Christopher Manning and Hinrich Schütze
Assumptions of PCFGs
1. Place invariance (like time invariance in HMMs)
j
∀k P (Nk ( k +c ) −→ is the same
ζ)
2. Context free
j
P (Nklj −→ ζ| words outside wk . . . wl ) = P (Nkl −→ ζ)
Slide based on “Foundations of Statistical Natural Language Processing” by Christopher Manning and Hinrich Schütze
Assumptions of PCFGs
1. Place invariance (like time invariance in HMMs)
j
∀k P (Nk ( k +c ) −→ is the same
ζ)
2. Context free
j
P (Nklj −→ ζ| words outside wk . . . wl ) = P (Nkl −→ ζ)
3. Ancestor free
j j
P (Nklj −→ ζ| ancestor nodes of Nkl ) = P (Nkl −→ ζ)
Slide based on “Foundations of Statistical Natural Language Processing” by Christopher Manning and Hinrich Schütze
Some Features of PCFGs
• Partial solution for grammar ambiguity
• Can be learned from positive data alone
(but grammar induction difficult)
• Robustness
(admit everything with low probability)
• Gives a probabilistic language model
• Predictive power better than that for a
HMM
Slide based on “Foundations of Statistical Natural Language Processing” by Christopher Manning and Hinrich Schütze
Some Features of PCFGs
• Not lexicalized (probabilities do not factor in
lexical co‐occurrence)
• PCFG is a worse language model for English
than n‐gram models
• Certain biases: smaller trees more probable
(average WSJ sentence 23 words)
Slide based on “Foundations of Statistical Natural Language Processing” by Christopher Manning and Hinrich Schütze
Inconsistent Distributions
Slide based on “Foundations of Statistical Natural Language Processing” by Christopher Manning and Hinrich Schütze
Questions
Let w 1m be a sentence, G a grammar, t a parse tree.
Slide based on “Foundations of Statistical Natural Language Processing” by Christopher Manning and Hinrich Schütze
Questions
Let w 1m be a sentence, G a grammar, t a parse tree.
Slide based on “Foundations of Statistical Natural Language Processing” by Christopher Manning and Hinrich Schütze
Questions
Let w 1m be a sentence, G a grammar, t a parse tree.
Slide based on “Foundations of Statistical Natural Language Processing” by Christopher Manning and Hinrich Schütze
Chomsky Normal Form
• Any CFG grammar can be represented in CNF
where all rules take the form
N i −→ N j N k
N i −→ wj
Slide based on “Foundations of Statistical Natural Language Processing” by Christopher Manning and Hinrich Schütze
HMMs and PCFGs
• HMMs: distribution over strings of certain length
P
∀n w1n P (w1 n ) = 1
• PCFGs: distribution over strings of language L
P
w ∈ L P (w) = 1
Slide based on “Foundations of Statistical Natural Language Processing” by Christopher Manning and Hinrich Schütze
HMMs and PCFGs
• HMMs: distribution over strings of certain length
P
∀n w1n P (w1 n ) = 1
• PCFGs: distribution over strings of language L
P
w ∈ L P (w) = 1
• Consider
Slide based on “Foundations of Statistical Natural Language Processing” by Christopher Manning and Hinrich Schütze
HMMs and PCFGs
• HMMs: distribution over strings of certain length
P
∀n w1n P (w1 n ) = 1
• PCFGs: distribution over strings of language L
P
w ∈ L P (w) = 1
• Consider
Slide based on “Foundations of Statistical Natural Language Processing” by Christopher Manning and Hinrich Schütze
Inside and Outside Probabilities
• For HMMs we have
Forwards αi (t) = P (w 1 ( t − 1 ) , X t = i)
Backwards βi (t) = P (wtT |Xt = i)
Slide based on “Foundations of Statistical Natural Language Processing” by Christopher Manning and Hinrich Schütze
Inside and Outside Probabilities
• For HMMs we have
Forwards αi (t) = P (w 1 ( t − 1 ) , X t = i)
Backwards βi (t) = P (wtT |Xt = i)
Slide based on “Foundations of Statistical Natural Language Processing” by Christopher Manning and Hinrich Schütze
Inside and Outside Probabilities
Slide based on “Foundations of Statistical Natural Language Processing” by Christopher Manning and Hinrich Schütze
Probability of a sentence
j
Outside α j (p, q) = P 1 ( p − 1 ) , Npq , w (q+1)m |G)
Inside (w P (wpq |Npq j
, G)
β j (p, q) =
P (w1m|G) = β1 (1, m)
X
P (w1m|G) = α j (k, k)P (N j −→ wk )
j
Inside Probabilities
pq |Npq ,
j
β j (p, q) = P G)
(w
• Base case j
β j (k, k) = P (wkk |Nkk , G)
= P (N j
−→ wk|G)
Slide based on “Foundations of Statistical Natural Language Processing” by Christopher Manning and Hinrich Schütze
Inside Probabilities
pq |Npq ,
j
β j (p, q) = P G)
(w
• Base case j
β j (k, k) = P (wkk |Nkk , G)
= P (N j
−→ wk|G)
• Induction
Want to find β j (p, q) for p < q
X qX− 1
βj (p, q) = P (N j −→ N r N s )βr (p, d)βs (d + 1,
r , s d=p
q)
Slide based on “Foundations of Statistical Natural Language Processing” by Christopher Manning and Hinrich Schütze
CYK Algorithm
Slide based on “Foundations of Statistical Natural Language Processing” by Christopher Manning and Hinrich Schütze
CYK Algorithm
Slide based on “Foundations of Statistical Natural Language Processing” by Christopher Manning and Hinrich Schütze
CYK Algorithm
βV P = 0.126 β P P = 0.18
? ? ? ?
β N P = 0.1 βV = 1.0 β N P = 0.18 βP = 1.0 β N P = 0.18
β N P = 0.04
astronomers saw stars with ears
Slide based on “Foundations of Statistical Natural Language Processing” by Christopher Manning and Hinrich Schütze
CYK Algorithm
βS = 0.015876
βV P =
0.015876
βS =
? β N P = 0.01296
0.0126
βV P = 0.126 β P P = 0.18
Slide based on “Foundations of Statistical Natural Language Processing” by Christopher Manning and Hinrich Schütze
CYK Algorithm
Worst case: O(m3r)
m = length of sentence
βS = 0.015876 r = number of rules in grammar
n = number of non‐terminals
βV P =
If we consider all possible CNF rules:
0.015876 O(m3n3)
β S = 0.0126 β N P = 0.01296
βV P = 0.126 β P P = 0.18
Slide based on “Foundations of Statistical Natural Language Processing” by Christopher Manning and Hinrich Schütze
Outside Probabilities
• Compute top‐down (after inside probabilities)
• Base case
α 1 (1, m) = 1
α j (1, m) = 0, for j 6 = 1
• Induction
⎛ ⎞
X Xm
α j (p, q) ⎝ α f (p, e)P ( N f −→ N j N g )βg (q + 1,
= ⎛f , g e = qp+− 11 e)⎠ ⎞
X X
+ ⎝ α f (e, q)P ( N f −→ N g N j )βg (e, p −
f , g e = 1 1) ⎠
Slide based on “Foundations of Statistical Natural Language Processing” by Christopher Manning and Hinrich Schütze
Probability of a node existing
• As with a HMM, we can form a product of the
inside and outside probabilities.
α j (p, q)βj (p, q) = P (w1m , Npqj |
G)
Slide based on “Foundations of Statistical Natural Language Processing” by Christopher Manning and Hinrich Schütze
Probability of a node existing
• As with a HMM, we can form a product of the
inside and outside probabilities.
α j (p, q)βj (p, q) = P (w1m , Npqj |
G)
• Therefore,
X
P (w1 m , Npq |G) = α j (p, q)βj (p,
j q)
Slide based on “Foundations of Statistical Natural Language Processing” by Christopher Manning and Hinrich Schütze
Probability of a node existing
• As with a HMM, we can form a product of the
inside and outside probabilities.
α j (p, q)βj (p, q) = P (w1m , Npqj |
G)
• Therefore,
X
P (w1 m , Npq |G) = α j (p, q)βj (p,
j q)
• Just in the cases of the root node and the
preterminals, we know there will be some
such constituent.
Slide based on “Foundations of Statistical Natural Language Processing” by Christopher Manning and Hinrich Schütze
Training
• If have data
count ˆ j C ( N j −→ ζ)
P (N −→ ζ) = P γ C ( N j −→ γ)
Slide based on “Foundations of Statistical Natural Language Processing” by Christopher Manning and Hinrich Schütze
Training
• If have data
count ˆ j C ( N j −→ ζ)
P (N −→ ζ) = P γ C ( N j −→ γ)
Pˆ (N j −→ N r N s ) = . . .
Pˆ (N j −→ wk) = . . .
end
tw
Slide based on “Foundations of o
Statistical Natural Language Processing” by Christopher Manning and Hinrich Schütze
EM Problems
• Slow: O(m3n3) for each sentence and each
iteration
• Local maxima (Charniak: 300 trials led to 300
different max.)
• In practice, need >3 times more non‐terminals
than are theoretically needed
• No guarantee that learned non‐terminals
correspond to NP, VP, …
Slide based on “Foundations of Statistical Natural Language Processing” by Christopher Manning and Hinrich Schütze
Bracketing helps
Pereira/Schabes ’92:
• Train on sentences:
37% of predicted brackets correct
• Train on sentences + brackets:
90% of predicted brackets correct
Slide based on “Foundations of Statistical Natural Language Processing” by Christopher Manning and Hinrich Schütze
Grammar Induction
• Rules typically selected by linguist
• Automatic induction is very difficult for
context‐free languages
• It is easy to find some form of structure, but
little resemblance to that of linguistics/NLP
Slide based on “Foundations of Statistical Natural Language Processing” by Christopher Manning and Hinrich Schütze
Outline
• PCFGs: Inference and Learning
• Parsing English
• Discriminative CFGs
• Grammar Induction
Parsing for Disambiguation
The post office will hold out discounts and
service concessions as incentives.
Slide based on “Foundations of Statistical Natural Language Processing” by Christopher Manning and Hinrich Schütze
Parsing for Disambiguation
• There are typically many syntactically
possible parses
• Want to find the most likely parses
Slide based on “Foundations of Statistical Natural Language Processing” by Christopher Manning and Hinrich Schütze
Treebanks
• If grammar induction does not work, why not
count expansions in many parse trees?
• Penn Treebank
Slide based on “Foundations of Statistical Natural Language Processing” by Christopher Manning and Hinrich Schütze
PCFG weaknesses
• No Context
– (immediate prior context, speaker, …)
• No Lexicalization
– “VP NP NP” more likely if verb is “hand” or “tell”
– fail to capture lexical dependencies (n‐grams do)
• No Structural Context
– How NP expands depends on position
Slide based on “Foundations of Statistical Natural Language Processing” by Christopher Manning and Hinrich Schütze
PCFG weaknesses
Expansion % as Subj % as Obj
NP −→ PRP 13.7% 2.1%
NP −→ NNP 3.5% 0.9%
NP −→ DT NN 5.6% 4.6%
NP −→ NN 1.4% 2.8%
NP −→ NP SBAR 0.5% 2.6%
NP −→ NP PP 5.6% 14.1%
Expansion % as 1st Obj % as 2nd Obj
NP −→ NNS 7.5% 0.2%
NP −→ PRP 13.4% 0.9%
NP −→ NP PP 12.2% 14.4%
NP −→ DT NN 10.4% 13.3%
NP −→ NNP 4.5% 5.9%
NP −→ NN 3.9% 9.2%
NP −→ JJ NN 1.1% 10.4%
NP −→ NP SBAR 0.3% 5.1%
Slide based on “Foundations of Statistical Natural Language Processing” by Christopher Manning and Hinrich Schütze
Other Approaches
• Challenge: use lexical and structural context,
without too many parameters, sparse data
• Other Grammars
– Probabilistic Left‐Corner Grammars
– Phrase Structure Grammars
– Dependency Grammars
– Probabilistic Tree Substitution Grammars
– History‐based Grammars
Slide based on “Foundations of Statistical Natural Language Processing” by Christopher Manning and Hinrich Schütze
Outline
• PCFGs: Inference and Learning
• Parsing English
• Discriminative CFGs
• Grammar Induction
Generative vs Discriminative
• An HMM (or PCFG) is a generative model
P (y, w)
HMMs Generative
Naïve Bayes
directed models
Slide based on “An introduction to Conditional Random Fields for Relational Learning” by Charles Sutton and Andrew McCallum
Generative and Discriminative Models
General
Sequence Tree Graphs
General
Sequence Graphs
General
Tree Graphs
?
Sequence
• Rule scores
XF
S(N i −→ ζ j , p, q) λ k (N i −→ ζ j )fk (w1 2 .. . m, p, q, N i −→
= k=1 w w ζj )
Slide based on “Learning to Extract Information from Semi‐Structured Text using a Discriminative Context Free Grammar” by Paul Viola and Mukund Narasimhan
Features
XF
S(N i −→ ζ j , p, q) = λk (Ni −→ ζj )fk (w1 2 .. . wm , p, q, i
−→ j
w k=1 N ζ )
• Features can depend on all tokens + span
• Consider feature “AllOnTheSameLine”
Mavis Wood Mavis Wood Products
Products
Slide based on “Learning to Extract Information from Semi‐Structured Text using a Discriminative Context Free Grammar” by Paul Viola and Mukund Narasimhan
Features
XF
S(N i −→ ζ j , p, q) λ k (N i −→ ζ j )fk (w1 2 .. . m, p, q, N i −→
= k=1 w w ζj )
Slide based on “Learning to Extract Information from Semi‐Structured Text using a Discriminative Context Free Grammar” by Paul Viola and Mukund Narasimhan
Example
Slide based on “Learning to Extract Information from Semi‐Structured Text using a Discriminative Context Free Grammar” by Paul Viola and Mukund Narasimhan
Example
Slide based on “Learning to Extract Information from Semi‐Structured Text using a Discriminative Context Free Grammar” by Paul Viola and Mukund Narasimhan
Training
• Train feature weight vector for each rule
• Have labels, but not parse trees;
efficiently create trees by ignoring leaves
Slide based on “Learning to Extract Information from Semi‐Structured Text using a Discriminative Context Free Grammar” by Paul Viola and Mukund Narasimhan
Collins’ Averaged Perceptron
Slide based on “Learning to Extract Information from Semi‐Structured Text using a Discriminative Context Free Grammar” by Paul Viola and Mukund Narasimhan
Results
Slide based on “Learning to Extract Information from Semi‐Structured Text using a Discriminative Context Free Grammar” by Paul Viola and Mukund Narasimhan
Outline
• PCFGs: Inference and Learning
• Parsing English
• Discriminative CFGs
• Grammar Induction
Gold’s Theorem ‘67
“Any formal language which has hierarchical
structure capable of infinite recursion is
unlearnable from positive evidence alone.”
Slide based on “Unsupervised grammar induction with Minimum Description Length” by Roni Katzir
Common Approach
• Minimize total description length
• Simulated Annealing
Slide based on “Unsupervised grammar induction with Minimum Description Length” by Roni Katzir
random_neighbor(G)
• Insert:
• Delete
• New Rule
• Split
• Substitute
Slide based on “Unsupervised grammar induction with Minimum Description Length” by Roni Katzir
Energy
Slide based on “Unsupervised grammar induction with Minimum Description Length” by Roni Katzir
Experiment 1
• Word segmentation by 8‐month old infants
• Vocabulary: pabiku, golatu, daropi, tibudo
• Saffran ’96: use speech synthesizer, no word
breaks, 2 minutes = 180 words
• Infants can distinguish words from non‐
words
• Now try grammar induction (60 words)
Slide based on “Unsupervised grammar induction with Minimum Description Length” by Roni Katzir
Experiment 1
Slide based on “Unsupervised grammar induction with Minimum Description Length” by Roni Katzir
Experiment 2
Slide based on “Unsupervised grammar induction with Minimum Description Length” by Roni Katzir
Experiment 2
• Accurate segmentation
• Inaccurate structural learning
Slide based on “Unsupervised grammar induction with Minimum Description Length” by Roni Katzir
Prototype‐Driven Grammar Induction
• Semi‐supervised approach
• Give only a few dozen prototypical examples
(for NP e.g. determiner noun, pronouns, …)
• On English Penn Treebank: F1 = 65.1
(52% reduction over naïve PCFG induction)
Aria Haghighi and Dan Klein.
Prototype-Driven Grammar Induction.
ACL 2006
Dan Klein and Chris Manning.
A Generative Constituent-Context Model for Improved Grammar Induction.
ACL 2001
That’s it!