Natural Language Processingdemo.clab.cs.cmu.edu/NLP/S20/files/slides/12-cfparsing.pdfLanguage...

79
Natural Language Processing Lecture 12: Context-Free Parsing

Transcript of Natural Language Processingdemo.clab.cs.cmu.edu/NLP/S20/files/slides/12-cfparsing.pdfLanguage...

Page 1: Natural Language Processingdemo.clab.cs.cmu.edu/NLP/S20/files/slides/12-cfparsing.pdfLanguage Processing - Jurafsky and Martn 58 Earley top-level • As with most dynamic programming

Natural Language Processing

Lecture 12: Context-Free Parsing

Page 2: Natural Language Processingdemo.clab.cs.cmu.edu/NLP/S20/files/slides/12-cfparsing.pdfLanguage Processing - Jurafsky and Martn 58 Earley top-level • As with most dynamic programming

Levels of Linguistic Representation

discourse

semantcs

pragmatcs

lexemes

morphology

orthographyphonology

phonetcs

speech text

anal

ysis

gen

erat

on

most of this class

syntax

Page 3: Natural Language Processingdemo.clab.cs.cmu.edu/NLP/S20/files/slides/12-cfparsing.pdfLanguage Processing - Jurafsky and Martn 58 Earley top-level • As with most dynamic programming

Context-Free Grammars

• Using grammars Recogniton Parsing

• Parsing algorithms Top down Bottom up

• CNF• CKY Algorithm

• Cocke-Younger-Kasami

Page 4: Natural Language Processingdemo.clab.cs.cmu.edu/NLP/S20/files/slides/12-cfparsing.pdfLanguage Processing - Jurafsky and Martn 58 Earley top-level • As with most dynamic programming

Parsing vs Word Matching

• Consider• The student who was taught by Alan Black won the prize

• Who won the prize?• String matching

"Alan Black won the prize.”

• Parsing based• ((The student (who was taught by Alan Black))

won the prize)• “The student won the prize”

Page 5: Natural Language Processingdemo.clab.cs.cmu.edu/NLP/S20/files/slides/12-cfparsing.pdfLanguage Processing - Jurafsky and Martn 58 Earley top-level • As with most dynamic programming

Context-Free Grammars

• Vocabulary of terminal symbols, Σ• Set of nonterminal symbols (a.k.a. variables), N• Special start symbol S N∈• Producton rules of the form X → α where

X N∈α (N Σ)*∈ ∪

Page 6: Natural Language Processingdemo.clab.cs.cmu.edu/NLP/S20/files/slides/12-cfparsing.pdfLanguage Processing - Jurafsky and Martn 58 Earley top-level • As with most dynamic programming

Two Related Problems

• Input: sentence w = (w1, ..., wn) and CFG G• Output (recogniton): true if w Language(∈ G)• Output (parsing): one or more derivatons for w, under G

Page 7: Natural Language Processingdemo.clab.cs.cmu.edu/NLP/S20/files/slides/12-cfparsing.pdfLanguage Processing - Jurafsky and Martn 58 Earley top-level • As with most dynamic programming

Parsing as Search

S

w1 ... ... wn

top-down bottom-up

Page 8: Natural Language Processingdemo.clab.cs.cmu.edu/NLP/S20/files/slides/12-cfparsing.pdfLanguage Processing - Jurafsky and Martn 58 Earley top-level • As with most dynamic programming

Implementng Recognizers as Search

Agenda = { state0 }while(Agenda not empty) s = pop a state from Agenda if s is a success-state return s // valid parse tree else if s is not a failure-state: generate new states from s push new states onto Agenda

return nil // no parse!

Page 9: Natural Language Processingdemo.clab.cs.cmu.edu/NLP/S20/files/slides/12-cfparsing.pdfLanguage Processing - Jurafsky and Martn 58 Earley top-level • As with most dynamic programming

Example Grammar and Lexicon

Page 10: Natural Language Processingdemo.clab.cs.cmu.edu/NLP/S20/files/slides/12-cfparsing.pdfLanguage Processing - Jurafsky and Martn 58 Earley top-level • As with most dynamic programming

Recursive Descent (A Top-Down Parser)

Start state: (S, 0)Scan: From (wj+1 β, j), you can get to (β, j + 1).Predict: If Z → γ, then from (Z β, j), you can get to (γβ, j).Final state: (ε, n)

Page 11: Natural Language Processingdemo.clab.cs.cmu.edu/NLP/S20/files/slides/12-cfparsing.pdfLanguage Processing - Jurafsky and Martn 58 Earley top-level • As with most dynamic programming

Example Grammar and Lexicon

Page 12: Natural Language Processingdemo.clab.cs.cmu.edu/NLP/S20/files/slides/12-cfparsing.pdfLanguage Processing - Jurafsky and Martn 58 Earley top-level • As with most dynamic programming

Shif-Reduce (A Bottom-Up Parser)

• Start state: (ε, 0)• Shif: From (α, j), you can get to (α wj+1, j + 1).• Reduce: If Z → γ, then from (αγ, j) you can get

to (α Z, j).• Final state: (S, n)

Page 13: Natural Language Processingdemo.clab.cs.cmu.edu/NLP/S20/files/slides/12-cfparsing.pdfLanguage Processing - Jurafsky and Martn 58 Earley top-level • As with most dynamic programming

Simple Grammar

• S -> NP VP• VP -> V NP• NP -> John• NP -> Delta• V -> fies

Page 14: Natural Language Processingdemo.clab.cs.cmu.edu/NLP/S20/files/slides/12-cfparsing.pdfLanguage Processing - Jurafsky and Martn 58 Earley top-level • As with most dynamic programming
Page 15: Natural Language Processingdemo.clab.cs.cmu.edu/NLP/S20/files/slides/12-cfparsing.pdfLanguage Processing - Jurafsky and Martn 58 Earley top-level • As with most dynamic programming

Context-Free Grammars in Chomsky Normal Form

• Vocabulary of terminal symbols, Σ• Set of nonterminal symbols (a.k.a. variables), N• Special start symbol S N∈• Producton rules of the form X → α where

X N∈α N,N Σ∈ ∪

Page 16: Natural Language Processingdemo.clab.cs.cmu.edu/NLP/S20/files/slides/12-cfparsing.pdfLanguage Processing - Jurafsky and Martn 58 Earley top-level • As with most dynamic programming

Convert CFGs to CNF

• For each rule X → A B C

• Rewrite as X → A X2 X2 → B C

• Introducing a new non-terminal

Page 17: Natural Language Processingdemo.clab.cs.cmu.edu/NLP/S20/files/slides/12-cfparsing.pdfLanguage Processing - Jurafsky and Martn 58 Earley top-level • As with most dynamic programming
Page 18: Natural Language Processingdemo.clab.cs.cmu.edu/NLP/S20/files/slides/12-cfparsing.pdfLanguage Processing - Jurafsky and Martn 58 Earley top-level • As with most dynamic programming

CKY Algorithm

for i = 1 ... nC[i-1, i] = { V | V → wi }

for ℓ = 2 ... n // widthfor i = 0 ... n - ℓ // lef boundary

k = i + ℓ // right boundary for j = i + 1 ... k – 1 // midpoint

C[i, k] = C[i, k] ∪ { V | V → YZ, Y ∈ C[i, j], Z ∈

C[j, k] }return true if S ∈ C[0, n]

Page 19: Natural Language Processingdemo.clab.cs.cmu.edu/NLP/S20/files/slides/12-cfparsing.pdfLanguage Processing - Jurafsky and Martn 58 Earley top-level • As with most dynamic programming

CKY Algorithm: Chart

book

this

fight

through

Houston

Page 20: Natural Language Processingdemo.clab.cs.cmu.edu/NLP/S20/files/slides/12-cfparsing.pdfLanguage Processing - Jurafsky and Martn 58 Earley top-level • As with most dynamic programming

CKY Algorithm: Chart

Noun

book

this

fight

through

Houston

Page 21: Natural Language Processingdemo.clab.cs.cmu.edu/NLP/S20/files/slides/12-cfparsing.pdfLanguage Processing - Jurafsky and Martn 58 Earley top-level • As with most dynamic programming

CKY Algorithm: Chart

Noun,Verb

book

this

fight

through

Houston

Page 22: Natural Language Processingdemo.clab.cs.cmu.edu/NLP/S20/files/slides/12-cfparsing.pdfLanguage Processing - Jurafsky and Martn 58 Earley top-level • As with most dynamic programming

CKY Algorithm: Chart

Noun,Verb

book Det

this Noun

fight Prep

through PNoun

Houston

Page 23: Natural Language Processingdemo.clab.cs.cmu.edu/NLP/S20/files/slides/12-cfparsing.pdfLanguage Processing - Jurafsky and Martn 58 Earley top-level • As with most dynamic programming

CKY Algorithm: Chart

Noun,Verb

book Det

this Noun

fight Prep

through PNoun, NP

Houston

Page 24: Natural Language Processingdemo.clab.cs.cmu.edu/NLP/S20/files/slides/12-cfparsing.pdfLanguage Processing - Jurafsky and Martn 58 Earley top-level • As with most dynamic programming

CKY Algorithm: Chart

Noun,Verb

-

book Det

this Noun

fight Prep

through PNoun

Houston

Page 25: Natural Language Processingdemo.clab.cs.cmu.edu/NLP/S20/files/slides/12-cfparsing.pdfLanguage Processing - Jurafsky and Martn 58 Earley top-level • As with most dynamic programming

CKY Algorithm: Chart

Noun,Verb

-

book Det NP

this Noun

fight Prep

through PNoun,NP

Houston

Page 26: Natural Language Processingdemo.clab.cs.cmu.edu/NLP/S20/files/slides/12-cfparsing.pdfLanguage Processing - Jurafsky and Martn 58 Earley top-level • As with most dynamic programming

CKY Algorithm: Chart

Noun,Verb

-

book Det NP

this Noun

fight Prep

through PNoun,NP

Houston

Page 27: Natural Language Processingdemo.clab.cs.cmu.edu/NLP/S20/files/slides/12-cfparsing.pdfLanguage Processing - Jurafsky and Martn 58 Earley top-level • As with most dynamic programming

CKY Algorithm: Chart

Noun,Verb

-

book Det NP

this Noun -

fight Prep

through PNoun,NP

Houston

Page 28: Natural Language Processingdemo.clab.cs.cmu.edu/NLP/S20/files/slides/12-cfparsing.pdfLanguage Processing - Jurafsky and Martn 58 Earley top-level • As with most dynamic programming

CKY Algorithm: Chart

Noun,Verb

-

book Det NP -

this Noun -

fight Prep

through PNoun,NP

Houston

Page 29: Natural Language Processingdemo.clab.cs.cmu.edu/NLP/S20/files/slides/12-cfparsing.pdfLanguage Processing - Jurafsky and Martn 58 Earley top-level • As with most dynamic programming

CKY Algorithm: Chart

Noun,Verb

-

book Det NP -

this Noun -

fight Prep PP

through PNoun,NP

Houston

Page 30: Natural Language Processingdemo.clab.cs.cmu.edu/NLP/S20/files/slides/12-cfparsing.pdfLanguage Processing - Jurafsky and Martn 58 Earley top-level • As with most dynamic programming

CKY Algorithm: Chart

Noun,Verb

-

book Det NP -

this Noun - -

fight Prep PP

through PNoun,NP

Houston

Page 31: Natural Language Processingdemo.clab.cs.cmu.edu/NLP/S20/files/slides/12-cfparsing.pdfLanguage Processing - Jurafsky and Martn 58 Earley top-level • As with most dynamic programming

CKY Algorithm: Chart

Noun,Verb

-

book Det NP - NP

this Noun - -

fight Prep PP

through PNoun,NP

Houston

Page 32: Natural Language Processingdemo.clab.cs.cmu.edu/NLP/S20/files/slides/12-cfparsing.pdfLanguage Processing - Jurafsky and Martn 58 Earley top-level • As with most dynamic programming

CKY Algorithm: Chart

Noun,Verb

- VP

book Det NP - NP

this Noun - -

fight Prep PP

through PNoun,NP

Houston

Page 33: Natural Language Processingdemo.clab.cs.cmu.edu/NLP/S20/files/slides/12-cfparsing.pdfLanguage Processing - Jurafsky and Martn 58 Earley top-level • As with most dynamic programming

CKY Algorithm: Chart

Noun,Verb

- VP,S

book Det NP - NP

this Noun - -

fight Prep PP

through PNoun,NP

Houston

Page 34: Natural Language Processingdemo.clab.cs.cmu.edu/NLP/S20/files/slides/12-cfparsing.pdfLanguage Processing - Jurafsky and Martn 58 Earley top-level • As with most dynamic programming

CKY Algorithm: Chart

Noun,Verb

- VP,S -

book Det NP - NP

this Noun - -

fight Prep PP

through PNoun,NP

Houston

Page 35: Natural Language Processingdemo.clab.cs.cmu.edu/NLP/S20/files/slides/12-cfparsing.pdfLanguage Processing - Jurafsky and Martn 58 Earley top-level • As with most dynamic programming

CKY Algorithm: Chart

Noun,Verb

- VP,S - S

book Det NP - NP

this Noun - -

fight Prep PP

through PNoun,NP

Houston

Page 36: Natural Language Processingdemo.clab.cs.cmu.edu/NLP/S20/files/slides/12-cfparsing.pdfLanguage Processing - Jurafsky and Martn 58 Earley top-level • As with most dynamic programming

CKY Algorithm

for i = 1 ... nC[i-1, i] = { V | V → wi }

for ℓ = 2 ... n // widthfor i = 0 ... n - ℓ // lef boundary

k = i + ℓ // right boundary for j = i + 1 ... k – 1 // midpoint

C[i, k] = C[i, k] ∪ { V | V → YZ, Y ∈ C[i, j], Z ∈

C[j, k] }return true if S ∈ C[0, n]

Page 37: Natural Language Processingdemo.clab.cs.cmu.edu/NLP/S20/files/slides/12-cfparsing.pdfLanguage Processing - Jurafsky and Martn 58 Earley top-level • As with most dynamic programming

CKY Equatons

Page 38: Natural Language Processingdemo.clab.cs.cmu.edu/NLP/S20/files/slides/12-cfparsing.pdfLanguage Processing - Jurafsky and Martn 58 Earley top-level • As with most dynamic programming

CKY Complexity

• CKY worst case is O(n^3 . G)• Best is worst case• (Others better in average case)

Page 39: Natural Language Processingdemo.clab.cs.cmu.edu/NLP/S20/files/slides/12-cfparsing.pdfLanguage Processing - Jurafsky and Martn 58 Earley top-level • As with most dynamic programming

Probabilistc/Weighted Parsing

Page 40: Natural Language Processingdemo.clab.cs.cmu.edu/NLP/S20/files/slides/12-cfparsing.pdfLanguage Processing - Jurafsky and Martn 58 Earley top-level • As with most dynamic programming

Example: ambiguous parse

Page 41: Natural Language Processingdemo.clab.cs.cmu.edu/NLP/S20/files/slides/12-cfparsing.pdfLanguage Processing - Jurafsky and Martn 58 Earley top-level • As with most dynamic programming

Probabilistc CFG

Page 42: Natural Language Processingdemo.clab.cs.cmu.edu/NLP/S20/files/slides/12-cfparsing.pdfLanguage Processing - Jurafsky and Martn 58 Earley top-level • As with most dynamic programming

Ambiguous parse w/probabilites

P(lef) = 2.2 *10^-6 P(right) = 6.1 *10^-7

0.05

0.20

0.20

0.20

0.75

0.30

0.60

0.10

0.40

0.05

0.10

0.20 0.20

0.75 0.75

0.30

0.60

0.10 0.40

Page 43: Natural Language Processingdemo.clab.cs.cmu.edu/NLP/S20/files/slides/12-cfparsing.pdfLanguage Processing - Jurafsky and Martn 58 Earley top-level • As with most dynamic programming

Review: Context-Free Grammars

• Vocabulary of terminal symbols, Σ

• Set of nonterminal symbols (a.k.a. variables), N

• Special start symbol S N∈• Producton rules of the form X → α where

X N∈α (N Σ)* ∈ ∪ (in CNF: α N∈ 2 Σ)∪

Page 44: Natural Language Processingdemo.clab.cs.cmu.edu/NLP/S20/files/slides/12-cfparsing.pdfLanguage Processing - Jurafsky and Martn 58 Earley top-level • As with most dynamic programming

Probabilistc Context-Free Grammars

• Vocabulary of terminal symbols, Σ

• Set of nonterminal symbols (a.k.a. variables), N

• Special start symbol S N∈• Producton rules of the form X → α, each with

a positve weight p(X → α), whereX N∈α (N Σ)* ∈ ∪ (in CNF: α N∈ 2 Σ)∪∀X N, ∑∈ α p(X → α) = 1

Page 45: Natural Language Processingdemo.clab.cs.cmu.edu/NLP/S20/files/slides/12-cfparsing.pdfLanguage Processing - Jurafsky and Martn 58 Earley top-level • As with most dynamic programming

CKY Algorithm: Review

for i = 1 ... n

C[i-1, i] = { V | V → wi }

for ℓ = 2 ... n // width

for i = 0 ... n - ℓ // lef boundary

k = i + ℓ // right boundary

for j = i + 1 ... k – 1 // midpoint

C[i, k] = C[i, k] ∪ { V | V → YZ, Y ∈ C[i, j], Z ∈ C[j, k] }

return true if S ∈ C[0, n]

Page 46: Natural Language Processingdemo.clab.cs.cmu.edu/NLP/S20/files/slides/12-cfparsing.pdfLanguage Processing - Jurafsky and Martn 58 Earley top-level • As with most dynamic programming

Weighted CKY Algorithm

for i = 1 ... n, V N∈C[V, i-1, i] = p(V → wi)

for ℓ = 2 ... n // width of span

for i = 0 ... n - ℓ // lef boundary

k = i + ℓ // right boundary

for j = i + 1 ... k – 1 // midpoint

for each binary rule V → YZ

C[V, i, k] = max{ C[V, i, k], C[Y, i, j] × C[Z, j, k] × p(V → YZ) }

return true if S ∈ C[·,0, n]

Page 47: Natural Language Processingdemo.clab.cs.cmu.edu/NLP/S20/files/slides/12-cfparsing.pdfLanguage Processing - Jurafsky and Martn 58 Earley top-level • As with most dynamic programming

CKY Algorithm: Review

Page 48: Natural Language Processingdemo.clab.cs.cmu.edu/NLP/S20/files/slides/12-cfparsing.pdfLanguage Processing - Jurafsky and Martn 58 Earley top-level • As with most dynamic programming

Weighted CKY Algorithm

Page 49: Natural Language Processingdemo.clab.cs.cmu.edu/NLP/S20/files/slides/12-cfparsing.pdfLanguage Processing - Jurafsky and Martn 58 Earley top-level • As with most dynamic programming

P-CKY algorithm from book

Page 50: Natural Language Processingdemo.clab.cs.cmu.edu/NLP/S20/files/slides/12-cfparsing.pdfLanguage Processing - Jurafsky and Martn 58 Earley top-level • As with most dynamic programming
Page 51: Natural Language Processingdemo.clab.cs.cmu.edu/NLP/S20/files/slides/12-cfparsing.pdfLanguage Processing - Jurafsky and Martn 58 Earley top-level • As with most dynamic programming

Parsing as (Weighted) Deducton

Page 52: Natural Language Processingdemo.clab.cs.cmu.edu/NLP/S20/files/slides/12-cfparsing.pdfLanguage Processing - Jurafsky and Martn 58 Earley top-level • As with most dynamic programming

Earley’s Algorithm

Page 53: Natural Language Processingdemo.clab.cs.cmu.edu/NLP/S20/files/slides/12-cfparsing.pdfLanguage Processing - Jurafsky and Martn 58 Earley top-level • As with most dynamic programming
Page 54: Natural Language Processingdemo.clab.cs.cmu.edu/NLP/S20/files/slides/12-cfparsing.pdfLanguage Processing - Jurafsky and Martn 58 Earley top-level • As with most dynamic programming

Example Grammar (same for CKY)

Page 55: Natural Language Processingdemo.clab.cs.cmu.edu/NLP/S20/files/slides/12-cfparsing.pdfLanguage Processing - Jurafsky and Martn 58 Earley top-level • As with most dynamic programming

02/25/2020 Speech and

Language Processing - Jurafsky and Martn

55

Earley Parsing

• Allows arbitrary CFGs• Top-down control• Fills a table (or chart) in a single sweep over

the input– Table is length N+1; N is number of words– Table entries represent

• Completed consttuents and their locatons• In-progress consttuents• Predicted consttuents

Page 56: Natural Language Processingdemo.clab.cs.cmu.edu/NLP/S20/files/slides/12-cfparsing.pdfLanguage Processing - Jurafsky and Martn 58 Earley top-level • As with most dynamic programming

02/25/2020 Speech and

Language Processing - Jurafsky and Martn

56

States

• The table-entries are called states and are represented with dotted-rules.

S . VP A VP is predicted

NP Det . Nominal An NP is in progress

VP V NP . A VP has been found

Page 57: Natural Language Processingdemo.clab.cs.cmu.edu/NLP/S20/files/slides/12-cfparsing.pdfLanguage Processing - Jurafsky and Martn 58 Earley top-level • As with most dynamic programming

02/25/2020 Speech and

Language Processing - Jurafsky and Martn

57

States/Locatons

• S . VP [0,0]

• NP Det .Nominal [1,2]

• VP V NP . [0,3]

A VP is predicted at the start of the sentence

An NP is in progress; the Det goes from 1 to 2

A VP has been found startng at 0 and ending at 3

Page 58: Natural Language Processingdemo.clab.cs.cmu.edu/NLP/S20/files/slides/12-cfparsing.pdfLanguage Processing - Jurafsky and Martn 58 Earley top-level • As with most dynamic programming

02/25/2020 Speech and

Language Processing - Jurafsky and Martn

58

Earley top-level

• As with most dynamic programming approaches, the answer is found by looking in the table in the right place.

• In this case, there should be an S state in the final column that spans from 0 to N and is complete. That is,

S α . [0,N]

• If that’s the case, you’re done.

Page 59: Natural Language Processingdemo.clab.cs.cmu.edu/NLP/S20/files/slides/12-cfparsing.pdfLanguage Processing - Jurafsky and Martn 58 Earley top-level • As with most dynamic programming

02/25/2020 Speech and

Language Processing - Jurafsky and Martn

59

Earley top-level (2)

• So sweep through the table from 0 to N…– New predicted states are created by startng top-

down from S

– New incomplete states are created by advancing existng states as new consttuents are discovered

– New complete states are created in the same way.

Page 60: Natural Language Processingdemo.clab.cs.cmu.edu/NLP/S20/files/slides/12-cfparsing.pdfLanguage Processing - Jurafsky and Martn 58 Earley top-level • As with most dynamic programming

02/25/2020 Speech and

Language Processing - Jurafsky and Martn

60

Earley top-level (3)

• More specifically…1. Predict all the states you can upfront

2. Read a word1. Extend states based on matches

2. Generate new predictons

3. Go to step 2

3. When you’re out of words, look at the chart to see if you have a winner

Page 61: Natural Language Processingdemo.clab.cs.cmu.edu/NLP/S20/files/slides/12-cfparsing.pdfLanguage Processing - Jurafsky and Martn 58 Earley top-level • As with most dynamic programming

Earley code: top-level

Page 62: Natural Language Processingdemo.clab.cs.cmu.edu/NLP/S20/files/slides/12-cfparsing.pdfLanguage Processing - Jurafsky and Martn 58 Earley top-level • As with most dynamic programming

Earley code: 3 main functons

Page 63: Natural Language Processingdemo.clab.cs.cmu.edu/NLP/S20/files/slides/12-cfparsing.pdfLanguage Processing - Jurafsky and Martn 58 Earley top-level • As with most dynamic programming

02/25/2020 Speech and

Language Processing - Jurafsky and Martn

63

Extended Earley Example

• Book that fight

• We should find: an S from 0 to 3 that is a completed state

Page 64: Natural Language Processingdemo.clab.cs.cmu.edu/NLP/S20/files/slides/12-cfparsing.pdfLanguage Processing - Jurafsky and Martn 58 Earley top-level • As with most dynamic programming
Page 65: Natural Language Processingdemo.clab.cs.cmu.edu/NLP/S20/files/slides/12-cfparsing.pdfLanguage Processing - Jurafsky and Martn 58 Earley top-level • As with most dynamic programming
Page 66: Natural Language Processingdemo.clab.cs.cmu.edu/NLP/S20/files/slides/12-cfparsing.pdfLanguage Processing - Jurafsky and Martn 58 Earley top-level • As with most dynamic programming
Page 67: Natural Language Processingdemo.clab.cs.cmu.edu/NLP/S20/files/slides/12-cfparsing.pdfLanguage Processing - Jurafsky and Martn 58 Earley top-level • As with most dynamic programming
Page 68: Natural Language Processingdemo.clab.cs.cmu.edu/NLP/S20/files/slides/12-cfparsing.pdfLanguage Processing - Jurafsky and Martn 58 Earley top-level • As with most dynamic programming
Page 69: Natural Language Processingdemo.clab.cs.cmu.edu/NLP/S20/files/slides/12-cfparsing.pdfLanguage Processing - Jurafsky and Martn 58 Earley top-level • As with most dynamic programming

Earley’s Algorithm in equatons

• We can look at this from the declaratve programming point of view too.

ROOT → • S [0,0] goal:ROOT → S• [0,n]

book the fight through Chicago

Page 70: Natural Language Processingdemo.clab.cs.cmu.edu/NLP/S20/files/slides/12-cfparsing.pdfLanguage Processing - Jurafsky and Martn 58 Earley top-level • As with most dynamic programming

Earley’s Algorithm: PREDICT

Given V → α•Xβ [i, j] and the rule X → γ,create X → •γ [j, j]

ROOT → • S [0,0]S → • VP [0,0]S → • NP VP [0,0]...VP → • V NP [0,0]...NP → • DT N [0,0]...

book the fight through Chicago

ROOT → • S [0,0]S→ VPS → • VP [0,0]

Page 71: Natural Language Processingdemo.clab.cs.cmu.edu/NLP/S20/files/slides/12-cfparsing.pdfLanguage Processing - Jurafsky and Martn 58 Earley top-level • As with most dynamic programming

Earley’s Algorithm: SCAN

Given V → α•Tβ [i, j] and the rule T → wj+1,

create T → wj+1• [j, j+1]

ROOT → • S [0,0]S → • VP [0,0]S → • NP VP [0,0]...VP → • V NP [0,0]...NP → • DT N [0,0]...

V → book• [0, 1]

book the fight through Chicago

VP → • V NP [0,0]V → bookV → book • [0,1]

Page 72: Natural Language Processingdemo.clab.cs.cmu.edu/NLP/S20/files/slides/12-cfparsing.pdfLanguage Processing - Jurafsky and Martn 58 Earley top-level • As with most dynamic programming

Earley’s Algorithm: COMPLETE

Given V → α•Xβ [i, j] and X → γ• [j, k],create V → αX•β [i, k]

ROOT → • S [0,0]S → • VP [0,0]S → • NP VP [0,0]...VP → • V NP [0,0]...NP → • DT N [0,0]...

V → book• [0, 1]VP → V • NP [0,1]

book the fight through Chicago

VP → • V NP [0,0]V → book • [0,1]VP → V • NP [0,1]

Page 73: Natural Language Processingdemo.clab.cs.cmu.edu/NLP/S20/files/slides/12-cfparsing.pdfLanguage Processing - Jurafsky and Martn 58 Earley top-level • As with most dynamic programming
Page 74: Natural Language Processingdemo.clab.cs.cmu.edu/NLP/S20/files/slides/12-cfparsing.pdfLanguage Processing - Jurafsky and Martn 58 Earley top-level • As with most dynamic programming

Thought Questons

• Runtme?– O(n3)

• Memory?– O(n2)

• Can we make it faster?

• Recovering trees?

Page 75: Natural Language Processingdemo.clab.cs.cmu.edu/NLP/S20/files/slides/12-cfparsing.pdfLanguage Processing - Jurafsky and Martn 58 Earley top-level • As with most dynamic programming

Make it an Earley Parser

Page 76: Natural Language Processingdemo.clab.cs.cmu.edu/NLP/S20/files/slides/12-cfparsing.pdfLanguage Processing - Jurafsky and Martn 58 Earley top-level • As with most dynamic programming

Parsing as Search, Again

Page 77: Natural Language Processingdemo.clab.cs.cmu.edu/NLP/S20/files/slides/12-cfparsing.pdfLanguage Processing - Jurafsky and Martn 58 Earley top-level • As with most dynamic programming

Implementng Recognizers as Search

Agenda = { state0 }

while(Agenda not empty) s = pop a state from Agendaif s is a success-state return s // valid parse treeelse if s is not a failure-state: generate new states from spush new states onto Agenda

return nil // no parse!

Page 78: Natural Language Processingdemo.clab.cs.cmu.edu/NLP/S20/files/slides/12-cfparsing.pdfLanguage Processing - Jurafsky and Martn 58 Earley top-level • As with most dynamic programming

Agenda-Based Probabilistc Parsing

Agenda = { (item, value) : inital updates from equatons }// items take the form [X, i, j]; values are reals

while(Agenda not empty) u = pop an update from Agendaif u.item is goal return u.value // valid parse treeelse if u.value -> Chart[u.item] store Chart[u.item] ← u.valueif u.item combines with other Chart items:

generate new updates from u and items stored in Chartpush new updates onto Agenda

return nil // no parse!

Page 79: Natural Language Processingdemo.clab.cs.cmu.edu/NLP/S20/files/slides/12-cfparsing.pdfLanguage Processing - Jurafsky and Martn 58 Earley top-level • As with most dynamic programming

Catalog of CF Parsing Algorithms

• Recogniton/Boolean vs. parsing/probabilistc

• Chomsky normal form/CKY vs. general/Earley’s

• Exhaustve vs. agenda