Context- Free Parsing with CKY. October 16, 2014

Similar documents
Natural Language Processing. Lecture 13: More on CFG Parsing

CKY & Earley Parsing. Ling 571 Deep Processing Techniques for NLP January 13, 2016

Parsing with CFGs L445 / L545 / B659. Dept. of Linguistics, Indiana University Spring Parsing with CFGs. Direction of processing

Parsing with CFGs. Direction of processing. Top-down. Bottom-up. Left-corner parsing. Chart parsing CYK. Earley 1 / 46.

Review. Earley Algorithm Chapter Left Recursion. Left-Recursion. Rule Ordering. Rule Ordering

Parsing with Context-Free Grammars

In this chapter, we explore the parsing problem, which encompasses several questions, including:

Parsing with Context-Free Grammars

Introduction to Computational Linguistics

CMPT-825 Natural Language Processing. Why are parsing algorithms important?

Natural Language Processing CS Lecture 06. Razvan C. Bunescu School of Electrical Engineering and Computer Science

Handout 8: Computation & Hierarchical parsing II. Compute initial state set S 0 Compute initial state set S 0

Parsing. Based on presentations from Chris Manning s course on Statistical Parsing (Stanford)

Context-Free Parsing: CKY & Earley Algorithms and Probabilistic Parsing

A* Search. 1 Dijkstra Shortest Path

LECTURER: BURCU CAN Spring

Grammar formalisms Tree Adjoining Grammar: Formal Properties, Parsing. Part I. Formal Properties of TAG. Outline: Formal Properties of TAG

CS 6120/CS4120: Natural Language Processing

Harvard CS 121 and CSCI E-207 Lecture 12: General Context-Free Recognition

CISC4090: Theory of Computation

Even More on Dynamic Programming

Syntactical analysis. Syntactical analysis. Syntactical analysis. Syntactical analysis

Outline. CS21 Decidability and Tractability. Machine view of FA. Machine view of FA. Machine view of FA. Machine view of FA.

Statistical Machine Translation

Top-Down Parsing and Intro to Bottom-Up Parsing

CS 301. Lecture 18 Decidable languages. Stephen Checkoway. April 2, 2018

Tree Adjoining Grammars

Syntactic Analysis. Top-Down Parsing

1 Rules in a Feature Grammar

Parsing. Unger s Parser. Laura Kallmeyer. Winter 2016/17. Heinrich-Heine-Universität Düsseldorf 1 / 21

MA/CSSE 474 Theory of Computation

Administrivia. Test I during class on 10 March. Bottom-Up Parsing. Lecture An Introductory Example

Probabilistic Context-free Grammars

Parsing Algorithms. CS 4447/CS Stephen Watt University of Western Ontario

Decoding and Inference with Syntactic Translation Models

Computing if a token can follow

Computational Models - Lecture 4

Parsing. Unger s Parser. Introduction (1) Unger s parser [Grune and Jacobs, 2008] is a CFG parser that is

Context-Free Parsing: CKY & Earley Algorithms and Probabilistic Parsing

Context Free Grammars

A parsing technique for TRG languages

CS 545 Lecture XVI: Parsing

CSCI 1010 Models of Computa3on. Lecture 17 Parsing Context-Free Languages

Computational Models - Lecture 4 1

Finite Automata Theory and Formal Languages TMV027/DIT321 LP4 2018

Parsing. Weighted Deductive Parsing. Laura Kallmeyer. Winter 2017/18. Heinrich-Heine-Universität Düsseldorf 1 / 26

Context-Free Parsing: CKY & Earley Algorithms and Probabilistic Parsing

Introduction to Bottom-Up Parsing

Computational Models - Lecture 5 1

CISC 4090 Theory of Computation

CSE 105 THEORY OF COMPUTATION

Soft Inference and Posterior Marginals. September 19, 2013

Parsing. Probabilistic CFG (PCFG) Laura Kallmeyer. Winter 2017/18. Heinrich-Heine-Universität Düsseldorf 1 / 22

Notes for Comp 497 (Comp 454) Week 10 4/5/05

CSE 105 THEORY OF COMPUTATION

EXAM. CS331 Compiler Design Spring Please read all instructions, including these, carefully

Syntax Analysis Part I

Introduction to Bottom-Up Parsing

CMSC 330: Organization of Programming Languages. Pushdown Automata Parsing

Foundations of Informatics: a Bridging Course

Follow sets. LL(1) Parsing Table

Introduction to Bottom-Up Parsing

Section 1 (closed-book) Total points 30

Predictive parsing as a specific subclass of recursive descent parsing complexity comparisons with general parsing

Harvard CS 121 and CSCI E-207 Lecture 10: CFLs: PDAs, Closure Properties, and Non-CFLs

CSE 355 Test 2, Fall 2016

Context Free Grammars: Introduction

Probabilistic Context-Free Grammars. Michael Collins, Columbia University

CS481F01 Prelim 2 Solutions

Introduction to Bottom-Up Parsing

Intro to Theory of Computation

Lecture VII Part 2: Syntactic Analysis Bottom-up Parsing: LR Parsing. Prof. Bodik CS Berkley University 1

Advanced Natural Language Processing Syntactic Parsing

Syntax Analysis Part I

Parsing. Left-Corner Parsing. Laura Kallmeyer. Winter 2017/18. Heinrich-Heine-Universität Düsseldorf 1 / 17

CISC 4090 Theory of Computation

Properties of Context-Free Languages

CYK Algorithm for Parsing General Context-Free Grammars

Context Free Grammars: Introduction. Context Free Grammars: Simplifying CFGs

Pushdown Automata (2015/11/23)

Chapter 14 (Partially) Unsupervised Parsing

NPDA, CFG equivalence

Harvard CS 121 and CSCI E-207 Lecture 10: Ambiguity, Pushdown Automata

Sequentially Indexed Grammars

Note: In any grammar here, the meaning and usage of P (productions) is equivalent to R (rules).

Parser Combinators, (Simply) Indexed Grammars, Natural Language Parsing

CSCI Compiler Construction

Languages. Languages. An Example Grammar. Grammars. Suppose we have an alphabet V. Then we can write:

Syntax Analysis Part I. Position of a Parser in the Compiler Model. The Parser. Chapter 4

CSE302: Compiler Design

CS5371 Theory of Computation. Lecture 7: Automata Theory V (CFG, CFL, CNF)

Harvard CS 121 and CSCI E-207 Lecture 9: Regular Languages Wrap-Up, Context-Free Grammars

CS20a: summary (Oct 24, 2002)

From EBNF to PEG. Roman R. Redziejowski. Concurrency, Specification and Programming Berlin 2012

Computability Theory

Probabilistic Context Free Grammars

Maschinelle Sprachverarbeitung

Einführung in die Computerlinguistik

Automata & languages. A primer on the Theory of Computation. Laurent Vanbever. ETH Zürich (D-ITET) October,

Part 3 out of 5. Automata & languages. A primer on the Theory of Computation. Last week, we learned about closure and equivalence of regular languages

Transcription:

Context- Free Parsing with CKY October 16, 2014

Lecture Plan Parsing as Logical DeducBon Defining the CFG recognibon problem BoHom up vs. top down Quick review of Chomsky normal form The CKY algorithm CKY as dynamic programming Complexity analysis

Algorithms for CFGs Given a CFG G and a string s: RecogniBon: Is s L(G)? Equivalently, find some derivabon that proves s is in G's language. Parsing: What are (all of) G's derivabons of s? What is the correct derivabon of s under G? The same core algorithms actually provide solubons to all of these!

Important DisBncBon DeterminisBc grammars give much faster recognibon and parsing algorithms! LR languages are parseable in linear Bme; you may have learned about these in a course on programming languages or compilers. For us, the best known algorithms are O( N n 2.376 ), but these are not pracbcal. Today you will learn an O( N 3 n 3 ) solubon.

Parsing Strategies

Parsing as Search S top- down bohom- up? x1 x2... xn Trees break into pieces (parbal trees), which can be used to define a search space.

Top- Down Parsing (Recursive Descent) (S) s = Book that flight

Top- Down Parsing (Recursive Descent) (S) (S (NP) (VP)) (S Aux (NP) (VP)) (S (VP)) x = Book that flight

Top- Down Parsing (Recursive Descent) (S) (S (NP) (VP)) (S Aux (NP) (VP)) (S (VP)) (S (NP Pronoun) (VP)) (S (NP ProperNoun) (VP)) (S (NP Det Nominal) (VP)) x = Book that flight

Top- Down Parsing (Recursive Descent) (S) (S (NP) (VP)) (S Aux (NP) (VP)) (S (VP)) (S (NP Pronoun) (VP)) (S (NP ProperNoun) (VP)) (S (NP Det Nominal) (VP)) (S Aux (NP Pronoun) (VP)) (S Aux (NP ProperNoun) (VP)) (S Aux (NP Det Nominal) (VP)) x = Book that flight

Top- Down Parsing (Recursive Descent) (S) (S (NP) (VP)) (S Aux (NP) (VP)) (S (VP)) (S (NP Pronoun) (VP)) (S (NP ProperNoun) (VP)) (S (NP Det Nominal) (VP)) (S Aux (NP Pronoun) (VP)) (S Aux (NP ProperNoun) (VP)) (S Aux (NP Det Nominal) (VP)) (S (VP (VP) (PP))) (S (VP Verb)) (S (VP Verb (NP))) (S (VP Verb (NP) (PP))) (S (VP Verb (PP))) x = Book that flight

Top- Down Parsing (Recursive Descent) Never wastes Bme exploring ungrammabcal trees! Inefficiency: most search states (parbal trees) could never lead to a derivabon of our sentence. One other problem...

Lef Recursion! (S) (S (VP)) (S (VP (VP) (PP))) (S (VP (VP (VP) (PP)) (PP))) (S (VP (VP (VP (VP) (PP)) (PP)) (PP))) (S (VP (VP (VP (VP (VP) (PP)) (PP)) (PP)) (PP))) (S (VP (VP (VP (VP (VP (VP) (PP)) (PP)) (PP)) (PP)) (PP))) x = Book that flight

Top- Down RecogniBon Don't need to store the tree! Can collapse states that funcbon the same way. Store unexpanded nonterminals (in sequence) only, along with the number of words covered so far. Start state: (S, 0) Scan: If you're at (xj+1 β, j), you can get to (β, j + 1). Predict: If you're at (Zβ, j) and Z γ, you can get to (γβ, j). Final ( success ) state: (ε, n) This should remind you of genera-ng from a CFG.

BoHom- Up Parsing book that flight

BoHom- Up Parsing (Verb book) (Det that) (Noun flight) (Noun book) (Det that) (Noun flight) book that flight

BoHom- Up Parsing (Nominal (Noun book)) (Det that) (Nominal (Noun flight)) (Verb book) (Det that) (Noun flight) (Noun book) (Det that) (Noun flight) book that flight

BoHom- Up Parsing (Verb book) (Det that) (Nominal (Noun flight)) (Nominal (Noun book)) (Det that) (Nominal (Noun flight)) (Verb book) (Det that) (Noun flight) (Noun book) (Det that) (Noun flight) book that flight

BoHom- Up Parsing Nominal (Noun book)) (NP (Det that) (Nominal (Noun flight))) (Verb book) (Det that) (Nominal (Noun flight)) (Nominal (Noun book)) (Det that) (Nominal (Noun flight)) (Verb book) (Det that) (Noun flight) (Noun book) (Det that) (Noun flight) book that flight

BoHom- Up Parsing Never generates trees that are inconsistent with the sentence. Generates parbal trees that have no hope of geong to S.

Shif- Reduce (A BoHom- Up Method) Don't need to store the tree! Can collapse states that funcbon the same way. Store only the nodes that are parentless. Start state: (ε, 0) Shi/: If you're at (α, j), you can get to (αxj+1, j + 1). Reduce: If you're at (αγ, j) and Z γ, you can get to (αz, j). Final ( success ) state: (S, n)

ImplemenBng 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!

Parsing as Search Basically, you need a strategy for deciding what to pursue next (how to priori-ze the agenda). Examples: depth- first, breadth- first, bohom- up, top- down, lef- to- right, right- to- lef,... The problems: Memory! Ambiguity leads to lots of bad paths In a depth- first model, this results in lots of wasted effort in backtracking. One solubon: greedy parsing. Only works if you have a really smart agenda!

Backtracking

Ambiguity Redux A sentence may have many parses. Even if a sentence has only one parse, finding it may be difficult, because there are many misleading paths you could follow. BoHom- up: fragments that can never have a home in any S Top- down: fragments that never get you to x What to do when there are many parses... how to choose? Return them all?

So Far... Two ways to think of parsing as search Problems: Lef- recursion (in top- down parsers) Efficiency (backtracking, memory) Ambiguity

CKY Kasami, 1965 Younger, 1967 Cocke and Schwartz, 1970

Chomsky Normal Form G = (Σ, N, S, R) Σ : Vocabulary of terminal symbols N: set of nonterminal symbols S N: special start symbol R: ProducBon rules of the form X α where X N (a nonterminal symbol) and α N 2 Σ

CKY Schema Data structure: chart. In each cell, store the possible phrase types. Visit each cell once. Start with narrow spans, progress to longer ones. book the flight through Chicago

CKY Example boo k the flight through Chicago

CKY Schema book Noun Verb VP Nominal S S, VP, X2 S, VP Det NP NP the Nominal Noun Nominal flight Prep PP through ProperN NP Chicago

CKY Algorithm (RecogniBon) input: x, G (in CNF) output: true iff x is in L(G) initialize all cells of C to for i = 1... n C[i-1, i] = {A A xi} for l = 2... n for i = 0... n - l k = i + l for j = i + 1... k 1 for each X YZ s.t. Y C[i, j] & Z C[j, k] C[i, k] = C[i, k] {X} return true if S C[0, n]

Visualizing CKY X 1 5 Y 1 5 Z 1 5 1 2 3 n

Visualizing CKY How do we fill in C(0,2)? 1 2 3 n

Visualizing CKY How do we fill in C(0,2)? Put together C(0,1) and C(1,2). 1 2 3 n

Visualizing CKY How do we fill in C(0,3)? 1 2 3 n

Visualizing CKY How do we fill in C(0,3)? One way 1 2 3 n

Visualizing CKY How do we fill in C(0,3)? One way Another way. 1 2 3 n

Visualizing CKY How do we fill in C(0,n)? 1 2 3 n

Visualizing CKY How do we fill in C(0,n)? n - 1 ways! 1 2 3 n

Ordering in CKY 1 2 3 n

CKY Algorithm for CF RecogniBon input: x, G (in CNF) output: true iff x is in L(G) initialize all cells of C to for i = 1... n C[i-1, i] = {A A xi} for l = 2... n for i = 0... n - l k = i + l for j = i + 1... k 1 for each X YZ s.t. Y C[i, j] & Z C[j, k] C[i, k] = C[i, k] {X} return true if S C[0, n]

CKY Algorithm input: x, G (in CNF) output: true iff x is in L(G) initialize all cells of C to for i = 1... n for each A s.t. A xi C[A, i-1, i] = 1 for l = 2... n for i = 0... n - l k = i + l for j = i + 1... k 1 for each X YZ C[X, i, k] = C[X, i, k] (C[Y, i, j] C[Z, j, k]) return C[S, 0, n]

CKY OperaBons given... for every add to - A x i A C[i - 1, i] Y C[i, j] Z C[j, k] X Y Z X C[i, k]

Visualizing CKY Operations Y Z O(n) B x i B O(n 3 ) X Y Z k X j j i i-1 i k i size of N nonterminal start end length of sentence length of sentence

CKY Algorithm AsymptoBcs Space: O(n 2 ) RunBme: O(n 3 ) Both also depend on grammar size

Parsing as Logical DeducBon

(Weighted) Logic Programming Parsing algorithms can be expressed as (weighted) logic programs I 1 I 2 I k I Shieber, Schabes, & Pereira (1995). Principles and ImplementaBon of DeducBve Parsing. J. Logic Programming

(Weighted) Logic Programming Parsing algorithms can be expressed as (weighted) logic programs I 1 I 2 I k I I 1 : w 1 I 2 : w 2 I k : w k I : w N k i=1 w i w, Shieber, Schabes, & Pereira (1995). Principles and ImplementaBon of DeducBve Parsing. J. Logic Programming

Understanding Logic Programs Antecedents I 1 I 2 I k I Consequent Side condibons

Understanding Logic Programs Items I Axioms A I Inference rules Goals G I I 1 I 2 I k I

Item form [N,N 1, N 1 ] CKY Algorithm

CKY Algorithm Item form [N,N 1, N 1 ] Goals [S, 1, x + 1]

CKY Algorithm Item form [N,N 1, N 1 ] Goals [S, 1, x + 1] Axioms [X, i, i + 1] (X! x i ) 2 N

CKY Algorithm Item form [N,N 1, N 1 ] Goals [S, 1, x + 1] Axioms [X, i, i + 1] (X! x i ) 2 N Inference rules [X, i, k] [Z, i, j] [Y,k,j] (Z! XY) 2 R

Top- Down Parsing Item form [(N [ ), N] Goals [", x ] Axioms [S, 0] Inference rules [x j+1,j] [,j + 1] [Z,j] [,j] (Z! ) 2 R Scan Predict

CKY Algorithm: Discussion Top- down or bohom- up? Parsing as deducbon: visual notabon Horn clauses, used to prove x L(G).

Dynamic Programming Examples: WFSA best path, Viterbi, Earley, CKY Solve sub- problems, store results in a table. Reuse work you ve done already (runbme) Pack sub- problems efficiently (memory) For parsing, many sub- parses can be used repeatedly in many larger sub- parses! Don t build the same sub- parse more than once.

DP ExecuBon Model Clever ordering to ensure items are built exactly once Viterbi: lef to right CKY: narrow to wide MemoizaBon Agenda DP logic execubon model

ImplemenBng 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!

CKY as Search Agenda = [{ [A, i 1, i] A x i } while(agenda not empty) [N, i, j] = pop from Agenda if [N, i, j] = [S, 0, n] return true else: for each visited [Y, j, k] and X N Y push [X, i, k] onto Agenda for each visited [Z, k, i] and X Z N push [X, k, i] onto Agenda return false

What Else? So far, this only does recogni-on. Can we recover parse trees? (Need to store some more informabon.) All trees Best tree