780 likes | 799 Views
Chart Parsing and Augmenting Grammars. CSE-391: Artificial Intelligence University of Pennsylvania Matt Huenerfauth March 2005. Homework 6. Due Friday, April 1, 2005. (Two weeks.) You will write/modify some grammar rules. You will enact some parsing algorithms by hand.
E N D
Chart Parsing and Augmenting Grammars CSE-391: Artificial IntelligenceUniversity of Pennsylvania Matt Huenerfauth March 2005
Homework 6 • Due Friday, April 1, 2005. (Two weeks.) • You will write/modify some grammar rules. • You will enact some parsing algorithms by hand. • You will use the chart-parsing Python code from the textbook (we’ll learn about chart parsing today).
Last time: Our grammar S NP VP VP V VP V NP NP pronoun NP noun V verb NP det adj noun V aux verb NP det noun • NON-TERMINAL vs. terminal…
Last time: Two Parsing Approaches • Top Down Parsing • Start with the top-level rule for S. • Blindly try all possible rules to build the sub pieces of the original S rule. Recurse. • When you back up, forget structures you built. • Bottom Up Parsing • Start by considering the part of speech of all the words. Build all the possible ‘local’ pieces of structure, and slowly build larger pieces.
Last time: Top-down parse[The, old, can, can, hold, the, water] S → NP VP Consider NP… NP → pronoun? pronoun? fail NP → noun? noun? fail NP → det adj noun? det? the adj?old noun? Can Succeed. Succeed. What about the VP?
Last time: Top-down parse[can, hold, the, water] Consider VP… V → verb? verb? fail V → aux verb? aux? can verb? hold succeed succeed fail[the, water]
Bottom Up Parsing • To illustrate bottom-up parsing, we will draw a chart diagram. • For a sentence with k words, we will start with k+1 nodes in the diagram. • We put edges between adjacent nodes labeled with input words & part of speech. the old can can hold the water auxverbnoun auxverbnoun det adj noun det noun verb verb noun
Drawing an edge… • As we apply grammar rules to build a parse, we draw more edges: • An edge has a starting and a stopping point. This represents what input words it covers. • An edge has a label. In the bottom-up algorithm, it looks like this: VP V NP • This means we just found a V and a NP, and we have combined them to build VP. • We’ll see an example of this on the next slide.
NP det noun NP det noun V aux verb V aux verb NP det adj noun VP V NP VP V NP VP V NP VP VP NP S NP VP NP noun NP noun NP noun NP noun NP noun V verb V verb V verb V verb Bottom Up Parsing the old can can hold the water auxverbnoun auxverbnoun det adj noun det noun verb verb noun
NP det noun V aux verb NP det adj noun VP VP NP S NP VP Bottom Up Parsing the old can can hold the water auxverbnoun auxverbnoun det adj noun det noun verb verb noun
Three Parsing Algorithms • Top Down Parsing • Tries lots of rules, does a lot of backtracking. • Has to re-build structures that it forgets about. • Feels a lot like uninformed search. • Bottom Up Parsing • Starts with the small pieces of structure, but it builds everything it can. Don’t need all of it. • Most of it not used in the final complete parse. • Top-Down Chart Parsing
(Top Down) Chart Parsing • Advantages: • Only build those pieces of structure that we actually need to parse the entire sentence. • Remember pieces of structure that we build in a chart so that if we have to backtrack, we don’t have to build those sub-structures all over again.
Add one more rule… • The first thing we do is add one more rule to our grammar. Our goal will be to produce a parse of the whole sentence that produces an S’: S’ S • So, this will be the first rule we’ll try to process with the algorithm…
the can can fall det auxverbnoun auxverbnoun verbnoun A New Kind of Edge • Instead of just drawing edges in our chart diagram for complete structures that we find, now we’ll also add edges that represent incomplete structures that we want to find… • For example, an edge labeled: VP V • NP could be used to represent a point in the algorithm where we really want to build a VP, and we have already found a V but have not yet found a NP to complete it. • Why would we want such a thing? We’ll see…
How to Start Our goal is to build an edge covering the whole sentence labeled S’. So, we first add an edge that covers no words at the start of the sentence. Since we haven’t found any S yet in order to fulfill the S’ rule, then we have to put the dot in front of the S. So, now our mission is to find an S in this sentence so that we can produce an S’. In other words, when we add edges to the chart that contain dots, then we know we are looking for the thing to the rightof the dot. the can can fall det auxverbnoun auxverbnoun verbnoun S’•S
The Algorithm • When we add an edge to the diagram, we give it a blue label. After we “process” the edge, then the label turns black. We will process edges in the order they are added to the diagram. • Process edges like a “queue” (breadth-first feel). Could also use “stack” but this is less traditional. We really want all parses, not just one. • How to we “process” an edge? It depends on where the dot is. • Before a NON-TERMINAL? • Before a terminal? • At the end?
PREDICT future rules… • If the dot is before a NON-TERMINAL. • We look up all the rules in the grammar that can produce that NON-TERMINAL. • We add all of these rules to the diagram as edges which cover no words. • We put a dot in front of the first item after the arrow. • Remember: All new edges have blue labels.
Dot before NON-TERMINAL:“Predict” the can can fall det auxverbnoun auxverbnoun verbnoun S’ • S (1)
Dot before NON-TERMINAL:“Predict” the can can fall det auxverbnoun auxverbnoun verbnoun S’ • SS • NP VP (1)(2)
Dot before NON-TERMINAL:“Predict” the can can fall det auxverbnoun auxverbnoun verbnoun S’ • SS • NP VP (1)(2)
Dot before NON-TERMINAL:“Predict” the can can fall det auxverbnoun auxverbnoun verbnoun S’ • SS • NP VPNP •proNP •nounNP •det nounNP •det adj noun (1) (2) (3) (4) (5) (6)
Dot before NON-TERMINAL:“Predict” the can can fall det auxverbnoun auxverbnoun verbnoun S’ • SS • NP VPNP • proNP •nounNP •det nounNP •det adj noun (1) (2) (3) (4) (5) (6)
SCAN input words… • When the dot is before a terminal… • Check the next word of input to see if any of its parts of speech matches the terminal symbol. • If there is a match, then we:spread the rule to the right and we move the dot to the right as well.
Dot before terminal: “Scan” the can can fall det auxverbnoun auxverbnoun verbnoun S’ • SS • NP VPNP •proNP •nounNP •det nounNP •det adj noun (1) (2) (3) (4) (5) (6)
Dot before terminal: “Scan” the can can fall det auxverbnoun auxverbnoun verbnoun S’ • SS • NP VPNP • proNP •nounNP •det nounNP •det adj noun (1) (2) (3) (4) (5) (6)
Dot before terminal: “Scan” the can can fall det auxverbnoun auxverbnoun verbnoun S’ • SS • NP VPNP • proNP • nounNP •det nounNP •det adj noun (1) (2) (3) (4) (5) (6)
Dot before terminal: “Scan” (7) NP det •noun the can can fall det auxverbnoun auxverbnoun verbnoun S’ • SS • NP VPNP • proNP • nounNP •det nounNP •det adj noun (1) (2) (3) (4) (5) (6)
Dot before terminal: “Scan” (7) NP det •noun the can can fall det auxverbnoun auxverbnoun verbnoun S’ • SS • NP VPNP • proNP • nounNP • det nounNP •det adj noun (1) (2) (3) (4) (5) (6)
Dot before terminal: “Scan” (7) (8) NP det •noun NP det •adj noun the can can fall det auxverbnoun auxverbnoun verbnoun S’ • SS • NP VPNP • proNP • nounNP • det nounNP •det adj noun (1) (2) (3) (4) (5) (6)
Dot before terminal: “Scan” (7) (8) NP det •noun NP det •adj noun the can can fall det auxverbnoun auxverbnoun verbnoun S’ • SS • NP VPNP • proNP • nounNP • det nounNP • det adj noun (1) (2) (3) (4) (5) (6)
Dot before terminal: “Scan” (9) NP det noun • (7) (8) NP det •noun NP det •adj noun the can can fall det auxverbnoun auxverbnoun verbnoun S’ • SS • NP VPNP • proNP • nounNP • det nounNP • det adj noun (1) (2) (3) (4) (5) (6)
Dot before terminal: “Scan” (9) NP det noun • (7) (8) NP det • noun NP det •adj noun the can can fall det auxverbnoun auxverbnoun verbnoun S’ • SS • NP VPNP • proNP • nounNP • det nounNP • det adj noun (1) (2) (3) (4) (5) (6)
Dot before terminal: “Scan” (9) NP det noun • (7) (8) NP det • noun NP det • adj noun the can can fall det auxverbnoun auxverbnoun verbnoun S’ • SS • NP VPNP • proNP • nounNP • det nounNP • det adj noun (1) (2) (3) (4) (5) (6)
Edge Extension • When the dot is at the end of the label… • Assume current edge is labeled: X … • • Look at all of the edges that ended where the current edge began. • If any of these preceding edges have a label of the form: … … • X … • Then spread this preceding edge across the width of the current edge, and label it:… … X • … (move the dot to the right)
Dot at end: “Edge Extension” (9) NP det noun • (7) (8) NP det • noun NP det • adj noun the can can fall det auxverbnoun auxverbnoun verbnoun S’ • SS • NP VPNP • proNP • nounNP • det nounNP • det adj noun (1) (2) (3) (4) (5) (6)
Dot at end: “Edge Extension” (9) NP det noun • (7) (8) NP det • noun NP det • adj noun the can can fall det auxverbnoun auxverbnoun verbnoun S’ • SS • NP VPNP • proNP • nounNP • det nounNP • det adj noun (1) (2) (3) (4) (5) (6)
Dot at end: “Edge Extension” NP det noun •S NP • VP (9) (10) (7) (8) NP det • noun NP det • adj noun the can can fall det auxverbnoun auxverbnoun verbnoun S’ • SS • NP VPNP • proNP • nounNP • det nounNP • det adj noun (1) (2) (3) (4) (5) (6)
Dot at end: “Edge Extension” NP det noun •S NP • VP (9) (10) (7) (8) NP det • noun NP det • adj noun the can can fall det auxverbnoun auxverbnoun verbnoun S’ • SS • NP VPNP • proNP • nounNP • det nounNP • det adj noun (1) (2) (3) (4) (5) (6)
Dot at end: “Edge Extension” NP det noun •S NP • VP (9) (10) (7) (8) NP det • noun NP det • adj noun the can can fall det auxverbnoun auxverbnoun verbnoun S’ • SS • NP VPNP • proNP • nounNP • det nounNP • det adj noun (1) (2) (3) (4) (5) (6)
Remember… • Process one edge at a time in the order they were added to the diagram. (queue) • If the dot is just before: • a NON-TERMINAL:Then “predict.” (grab rules from the grammar) • a terminal:Then “scan.” (compare to POS of next word) • the end of the label:Then “edge extend.” (check preceding edges) • Don’t add an edge with same exact label and same exact span as a previous one.
NP det noun •S NP • VP (9) (10) (7) (8) NP det • noun NP det • adj noun the can can fall det auxverbnoun auxverbnoun verbnoun S’ • SS • NP VPNP • proNP • nounNP • det nounNP • det adj noun (1) (2) (3) (4) (5) (6)
NP det noun •S NP • VP (9) (10) (7) (8) NP det • noun NP det • adj noun the can can fall det auxverbnoun auxverbnoun verbnoun S’ • SS • NP VPNP • proNP • nounNP • det nounNP • det adj noun (11)(12) VP • VVP • V NP (1) (2) (3) (4) (5) (6)
NP det noun •S NP • VP (9) (10) (7) (8) NP det • noun NP det • adj noun the can can fall det auxverbnoun auxverbnoun verbnoun S’ • SS • NP VPNP • proNP • nounNP • det nounNP • det adj noun (11)(12) VP • VVP • V NP (1) (2) (3) (4) (5) (6)
NP det noun •S NP • VP (9) (10) (7) (8) NP det • noun NP det • adj noun the can can fall det auxverbnoun auxverbnoun verbnoun S’ • SS • NP VPNP • proNP • nounNP • det nounNP • det adj noun (11)(12)(13) (14) VP • VVP • V NPV •verbV •aux verb (1) (2) (3) (4) (5) (6)
NP det noun •S NP • VP (9) (10) (7) (8) NP det • noun NP det • adj noun the can can fall det auxverbnoun auxverbnoun verbnoun S’ • SS • NP VPNP • proNP • nounNP • det nounNP • det adj noun (11)(12)(13) (14) VP • VVP • V NPV •verbV •aux verb (1) (2) (3) (4) (5) (6)
NP det noun •S NP • VP (9) (10) (7) (8) NP det • noun NP det • adj noun the can can fall det auxverbnoun auxverbnoun verbnoun S’ • SS • NP VPNP • proNP • nounNP • det nounNP • det adj noun (11)(12)(13) (14) VP • VVP • V NPV •verbV •aux verb (1) (2) (3) (4) (5) (6)
NP det noun •S NP • VP (9) (10) (7) (8) NP det • noun V verb • (15) NP det • adj noun the can can fall det auxverbnoun auxverbnoun verbnoun S’ • SS • NP VPNP • proNP • nounNP • det nounNP • det adj noun (11)(12)(13) (14) VP • VVP • V NPV •verbV •aux verb (1) (2) (3) (4) (5) (6)
NP det noun •S NP • VP (9) (10) (7) (8) NP det • noun V verb • (15) NP det • adj noun the can can fall det auxverbnoun auxverbnoun verbnoun S’ • SS • NP VPNP • proNP • nounNP • det nounNP • det adj noun (11)(12)(13) (14) VP • VVP • V NPV • verbV •aux verb (1) (2) (3) (4) (5) (6)
NP det noun •S NP • VP (9) (10) (7) (8) NP det • noun V verb •V aux • verb (15)(16) NP det • adj noun the can can fall det auxverbnoun auxverbnoun verbnoun S’ • SS • NP VPNP • proNP • nounNP • det nounNP • det adj noun (11)(12)(13) (14) VP • VVP • V NPV • verbV •aux verb (1) (2) (3) (4) (5) (6)
NP det noun •S NP • VP (9) (10) (7) (8) NP det • noun V verb •V aux • verb (15)(16) NP det • adj noun the can can fall det auxverbnoun auxverbnoun verbnoun S’ • SS • NP VPNP • proNP • nounNP • det nounNP • det adj noun (11)(12)(13) (14) VP • VVP • V NPV • verbV • aux verb (1) (2) (3) (4) (5) (6)