Context FreeGrammars
Reading: Chap 12-13, Jurafsky & Martin
This slide set was adapted from J. Martin and Rada Mihalcea
Slide 1
Syntax
Syntax = rules describing how words can connect to each other
* that and after year last I saw you yesterday colorless green ideas sleep furiously
• the kind of implicit knowledge of your native language that you had mastered by the time you were 3 or 4 years old without explicit instruction
• not necessarily the type of rules you were later taught in school.
Slide 1
Syntax
Why should you care?Grammar checkersQuestion answering Information extractionMachine translation
Slide 1
04/22/23
Speech and Language Processing - Jurafsky and Martin
4
Constituency
The basic idea here is that groups of words within utterances can be shown to act as single units.
And in a given language, these units form coherent classes that can be be shown to behave in similar ways
Slide 1
04/22/23
Speech and Language Processing - Jurafsky and Martin 5
Constituency
For example, it makes sense to the say that the following are all noun phrases in English...
The person I ate dinner with yesterdayThe car that I drove in college
Slide 1
04/22/23
Speech and Language Processing - Jurafsky and Martin
6
Grammars and Constituency
However, it isn’t easy or obvious how we come up with the right set of constituents and the rules that govern how they combine...
That’s why there are so many different theories of grammar and competing analyses of the same data.
The approach to grammar, and the analyses, adopted here are very generic (and don’t correspond to any modern linguistic theory of grammar).
Slide 1
04/22/23
Speech and Language Processing - Jurafsky and Martin
7
Context-Free Grammars
Context-free grammars (CFGs)Also known as
Phrase structure grammarsBackus-Naur form
Consist ofRules TerminalsNon-terminals
Slide 1
04/22/23
Speech and Language Processing - Jurafsky and Martin
8
Context-Free Grammars
TerminalsWe’ll take these to be words
Non-TerminalsThe constituents in a language
Like noun phrase, verb phrase and sentenceRules
Rules are equations that consist of a single non-terminal on the left and any number of terminals and non-terminals on the right.
Slide 1
CFG Example
S -> NP VPNP -> Det NOMINALNOMINAL -> NounVP -> VerbDet -> aNoun -> flightVerb -> left
these rules are defined independent of the context where they might occur -> CFG
9
Slide 1
CFGs
S -> NP VPThis says that there are units called S, NP, and VP in this
languageThat an S consists of an NP followed immediately by a VPDoesn’t say that that’s the only kind of SNor does it say that this is the only place that NPs and VPs occur
GenerativityYou can view these rules as either analysis or synthesis
machinesGenerate strings in the languageReject strings not in the languageImpose structures (trees) on strings in the language
10
Slide 1
Parsing
Parsing is the process of taking a string and a grammar and returning a (many) parse tree(s) for that string
Other optionsRegular languages (expressions)
Too weak – not expressive enoughContext-sensitive
Too powerful – parsing is not efficient
11
Slide 1
Context?
The notion of context in CFGs is not the same as the ordinary meaning of the word context in language.
All it really means is that the non-terminal on the left-hand side of a rule is out there all by itselfA -> B CMeans that I can rewrite an A as a B followed by a C regardless
of the context in which A is found
12
Slide 1
Key Constituents (English)
SentencesNoun phrasesVerb phrasesPrepositional phrases
13
Slide 1
04/22/23
Speech and Language Processing - Jurafsky and Martin
14
Some NP Rules
Here are some rules for our noun phrases
Together, these describe two kinds of NPs.One that consists of a determiner followed by a nominalAnd another that says that proper names are NPs.The third rule illustrates two things
An explicit disjunctionTwo kinds of nominals
A recursive definitionSame non-terminal on the right and left-side of the
rule
Slide 1
04/22/23
Speech and Language Processing - Jurafsky and Martin
15
Grammar
Slide 1
04/22/23
Speech and Language Processing - Jurafsky and Martin
16
Derivations
A derivation is a sequence of rules applied to a string that accounts for that stringCovers all the elements in the
stringCovers only the elements in the
string
Slide 1
Practice: Another Recursion Example
The following where the non-terminal on the left also appears somewhere on the right (directly).NP -> NP PP [[The flight] [to Boston]]VP -> VP PP [[departed Miami] [at noon]]
17
Slide 1
Recursion Example
flights from DenverFlights from Denver to MiamiFlights from Denver to Miami in FebruaryFlights from Denver to Miami in February on a FridayFlights from Denver to Miami in February on a Friday under
$300Flights from Denver to Miami in February on a Friday under
$300 with lunch
18
Slide 1
04/22/23
Speech and Language Processing - Jurafsky and Martin
19
Sentence Types
Declaratives: A plane left.
S NP VPImperatives: Leave!
S VPYes-No Questions: Did the plane leave?
S Aux NP VPWH Questions: When did the plane leave?
S WH-NP Aux NP VP
Slide 1
04/22/23
Speech and Language Processing - Jurafsky and Martin
20
Noun Phrases
Let’s consider the following rule in more detail...NP Det Nominal
Consider the derivation for the following exampleAll the morning flights from Denver to Tampa leaving before 10
Slide 1
21
Noun Phrases
Slide 1
04/22/23
Speech and Language Processing - Jurafsky and Martin
22
NP Structure
Clearly this NP is really about flights. That’s the central criticial noun in this NP. It is the head.
We can dissect this kind of NP into the stuff that can come before the head, and the stuff that can come after it.
Slide 1
04/22/23
Speech and Language Processing - Jurafsky and Martin
23
Determiners
Noun phrases can start with determiners...Determiners can be
Simple lexical items: the, this, a, an, etc.A car
Or simple possessivesJohn’s car
Or complex recursive versions of thatJohn’s sister’s husband’s son’s car
Slide 1
04/22/23
Speech and Language Processing - Jurafsky and Martin
24
Nominals
Contains the head and any pre- and post- modifiers of the head.Pre-
Quantifiers, cardinals, ordinals...Three cars
Adjectiveslarge cars
Note: there are ordering constraintsThree large cars?large three cars
Slide 1
04/22/23
Speech and Language Processing - Jurafsky and Martin
25
Postmodifiers
Three kindsPrepositional phrases
From SeattleNon-finite clauses
Arriving before noonRelative clauses
That serve breakfastRecursive rules to handle these
Nominal Nominal PPNominal Nominal GerundVPNominal Nominal RelClause
Slide 1
Agreement
This dogThose dogs
This dog eatsThose dogs eat
*This dogs*Those dog
*This dog eat*Those dogs eats
26
Slide 1
04/22/23
Speech and Language Processing - Jurafsky and Martin
27
Verb Phrases
English VPs consist of a head verb along with 0 or more following constituents which we’ll call arguments.
Slide 1
Subcategorization
Sneeze: John sneezedFind: Please find [a flight to NY]NPGive: Give [me]NP[a cheaper fare]NPHelp: Can you help [me]NP[with a flight]PPPrefer: I prefer [to leave earlier]TO-VPTold: I was told [United has a flight]S
…
*John sneezed the book*I prefer United has a flight*Give with a flight
Subcat expresses the constraints that a predicate places on the number and type of the argument it wants to take
28
Slide 1
Overgeneration
The various rules for VPs overgenerate.They permit the presence of strings containing verbs and
arguments that don’t go togetherFor exampleVP -> V NP therefore
Sneezed the book is a VP since “sneeze” is a verb and “the book” is a valid NP
In lecture: go over the grammar for assignment 3
Slide 1
04/22/23
30
Possible CFG Solution
Possible solution for agreement.Can use the same trick for all the verb/VP classes.
(Like propositionalizing a first-order knowledge base – the KB gets very large, but the inference algorithms are very efficient)
SgS -> SgNP SgVPPlS -> PlNp PlVPSgNP -> SgDet SgNomPlNP -> PlDet PlNomPlVP -> PlV NPSgVP ->SgV Np…
Slide 1
Movement
• Core example (no movement yet)– [[My travel agent]NP [booked [the flight]NP]VP]S
• I.e. “book” is a straightforward transitive verb. It expects a single NP arg within the VP as an argument, and a single NP arg as the subject.
Slide 1
Movement
• What about?– Which flight do you want me to have the travel agent
book?
• The direct object argument to “book” isn’t appearing in the right place. It is in fact a long way from where its supposed to appear.
• And note that it’s separated from its verb by 2 other verbs.
32
Slide 1
Formally…
To put all previous discussions/examples in a formal definition for CFG:
A context free grammar has four parameters:
1. A set of non-terminal symbols N2. A set of terminal symbols T3. A set of production rules P, each of the form A a, where
A is a non-terminal, and a is a string of symbols from the infinite set of strings (T N)*
4. A designated start symbol S
Slide 1
Grammar equivalence and normal form
Strong equivalence:– two grammars are strongly equivalent if:
• they generate/accept the same set of strings• they assign the same phrase structure to each sentence
– two grammars are weakly equivalent if:• they generate/accept the same set of strings• they do not assign the same phrase structure to each sentence
Normal form – Restrict the form of productions– Chomsky Normal Form (CNF)– Right hand side of the productions has either one or two terminals or
non-terminals– e.g. A -> BC A -> a– Any grammar can be translated into a weakly equivalent CNF– A -> B C D <=> A-> B X X -> C D
34
Slide 1
04/22/23
Speech and Language Processing - Jurafsky and Martin
35
Treebanks
Treebanks are corpora in which each sentence has been paired with a parse tree (presumably the right one).
These are generally created By first parsing the collection with an automatic
parserAnd then having human annotators correct each
parse as necessary.This generally requires detailed annotation guidelines that provide a POS tagset, a grammar and instructions for how to deal with particular grammatical constructions.
Slide 1
04/22/23
36
Penn Treebank
Penn TreeBank is a widely used treebank.
Most well known is the Wall Street Journal section of the Penn TreeBank.
1 M words from the 1987-1989 Wall Street Journal.
Slide 1
04/22/23
Speech and Language Processing - Jurafsky and Martin
37
Treebank Grammars
Treebanks implicitly define a grammar for the language covered in the treebank.
Simply take the local rules that make up the sub-trees in all the trees in the collection and you have a grammar.
Not complete, but if you have decent size corpus, you’ll have a grammar with decent coverage.
Slide 1
04/22/23
38
Treebank Grammars
Such grammars tend to be very flat due to the fact that they tend to avoid recursion.
For example, the Penn Treebank has 4500 different rules for VPs. Among them...
Slide 1
04/22/23
Speech and Language Processing - Jurafsky and Martin
39
Heads in Trees
Finding heads in treebank trees is a task that arises frequently in many applications.Particularly important in statistical parsing
We can visualize this task by annotating the nodes of a parse tree with the heads of each corresponding node.
Slide 1
04/22/23
40
Lexically Decorated Tree
Slide 1
04/22/23
Speech and Language Processing - Jurafsky and Martin
41
Head Finding
The standard way to do head finding is to use a simple set of tree traversal rules specific to each non-terminal in the grammar.
Slide 1
04/22/23
Speech and Language Processing - Jurafsky and Martin
42
Noun Phrases
Slide 1
04/22/23
Speech and Language Processing - Jurafsky and Martin
43
Treebank Uses
Treebanks (and headfinding) are particularly critical to the development of statistical parsersChapter 14
Also valuable to Corpus Linguistics Investigating the empirical details of various constructions in a
given language
Slide 1
04/22/23
Speech and Language Processing - Jurafsky and Martin
44
Dependency Grammars
In CFG-style phrase-structure grammars the main focus is on constituents.
But it turns out you can get a lot done with just binary relations among the words in an utterance.
In a dependency grammar framework, a parse is a tree where the nodes stand for the words in an utteranceThe links between the words represent dependency relations
between pairs of words.Relations may be typed (labeled), or not.
Slide 1
04/22/23
Speech and Language Processing - Jurafsky and Martin
45
Dependency Relations
Slide 1
04/22/23
46
Dependency Parse
They hid the letter on the shelf
See the Stanford parser on line
Slide 1
04/22/23
Speech and Language Processing - Jurafsky and Martin
47
Dependency Parsing
The dependency approach has a number of advantages over full phrase-structure parsing.Deals well with free word order languages where the constituent
structure is quite fluidParsing is much faster than CFG-bases parsersDependency structure often captures the syntactic relations
needed by later applicationsCFG-based approaches often extract this same information
from trees anyway.
Slide 1
04/22/23
Speech and Language Processing - Jurafsky and Martin
48
Dependency Parsing
There are two modern approaches to dependency parsingOptimization-based approaches that search a space of trees for
the tree that best matches some criteriaShift-reduce approaches that greedily take actions based on the
current word and state.
Slide 1
04/22/23
49
Summary
Context-free grammars can be used to model various facts about the syntax of a language.
When paired with parsers, such grammars consititute a critical component in many applications.
Constituency is a key phenomena easily captured with CFG rules.But agreement and subcategorization do pose
significant problemsTreebanks pair sentences in corpus with their corresponding trees.