Top Banner
1 Probabilistic Context Free Grammar (PCFG) 자연어처리
29

Probabilistic Context Free Grammar (PCFG)leeck/NLP/PCFG.pdf · 2019-11-21 · 3 Probabilistic Context Free Grammar (PCFG) •A PCFG is a probabilistic version of a CFG –Each production

Mar 20, 2020

Download

Documents

dariahiddleston
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Probabilistic Context Free Grammar (PCFG)leeck/NLP/PCFG.pdf · 2019-11-21 · 3 Probabilistic Context Free Grammar (PCFG) •A PCFG is a probabilistic version of a CFG –Each production

11

Probabilistic Context Free Grammar(PCFG)

자연어처리

Page 2: Probabilistic Context Free Grammar (PCFG)leeck/NLP/PCFG.pdf · 2019-11-21 · 3 Probabilistic Context Free Grammar (PCFG) •A PCFG is a probabilistic version of a CFG –Each production

Statistical Parsing

• Statistical parsing

– Parse tree에확률값을부여

– Syntactic ambiguity를해결

– Parse tree로구성된 tree-bank로부터Supervised learning을이용

2

Page 3: Probabilistic Context Free Grammar (PCFG)leeck/NLP/PCFG.pdf · 2019-11-21 · 3 Probabilistic Context Free Grammar (PCFG) •A PCFG is a probabilistic version of a CFG –Each production

3

Probabilistic Context Free Grammar(PCFG)

• A PCFG is a probabilistic version of a CFG

– Each production has a probability.

– 동일한Non-terminal로부터생성하는모든규칙들의확률의합은 1

• S → NP VP 0.8

• S → Aux NP VP 0.1

• S → VP 0.1

+ 1

Page 4: Probabilistic Context Free Grammar (PCFG)leeck/NLP/PCFG.pdf · 2019-11-21 · 3 Probabilistic Context Free Grammar (PCFG) •A PCFG is a probabilistic version of a CFG –Each production

Simple PCFG for ATIS English

S → NP VP S → Aux NP VP S → VP NP → PronounNP → Proper-NounNP → Det NominalNominal → NounNominal → Nominal NounNominal → Nominal PPVP → VerbVP → Verb NPVP → VP PPPP → Prep NP

Grammar0.80.10.10.20.20.60.30.20.50.20.50.31.0

Prob

+

+

+

+

1.0

1.0

1.0

1.0

Det → the | a | that | this0.6 0.2 0.1 0.1

Noun → book | flight | meal | money0.1 0.5 0.2 0.2

Verb → book | include | prefer0.5 0.2 0.3

Pronoun → I | he | she | me0.5 0.1 0.1 0.3

Proper-Noun → Houston | NWA0.8 0.2

Aux → does1.0

Prep → from | to | on | near | through0.25 0.25 0.1 0.2 0.2

Lexicon

Page 5: Probabilistic Context Free Grammar (PCFG)leeck/NLP/PCFG.pdf · 2019-11-21 · 3 Probabilistic Context Free Grammar (PCFG) •A PCFG is a probabilistic version of a CFG –Each production

5

Sentence Probability

• Assume productions for each node are chosen independently.

• Probability of derivation is the product of the probabilities of its productions.

P(D1) = 0.1 x 0.5 x 0.5 x 0.6 x 0.6 x

0.5 x 0.3 x 1.0 x 0.2 x 0.2 x

0.5 x 0.8= 0.0000216

D1S

VP

Verb NP

Det Nominal

Nominal PP

book

Prep NP

through

Houston

Proper-Noun

the

flight

Noun

0.5

0.50.6

0.6 0.5

1.0

0.2

0.3

0.5 0.2

0.8

0.1

Page 6: Probabilistic Context Free Grammar (PCFG)leeck/NLP/PCFG.pdf · 2019-11-21 · 3 Probabilistic Context Free Grammar (PCFG) •A PCFG is a probabilistic version of a CFG –Each production

Syntactic Disambiguation

• Resolve ambiguity by picking most probable parse tree.

66

D2

VP

Verb NP

Det Nominalbook

Prep NP

through

Houston

Proper-Noun

the

flight

Noun

0.5

0.50.6

0.61.0

0.20.3

0.5 0.2

0.8

S

VP

0.1

PP

0.3

P(D2) = 0.1 x 0.3 x 0.5 x 0.6 x 0.5 x

0.6 x 0.3 x 1.0 x 0.5 x 0.2 x

0.2 x 0.8= 0.00001296

Page 7: Probabilistic Context Free Grammar (PCFG)leeck/NLP/PCFG.pdf · 2019-11-21 · 3 Probabilistic Context Free Grammar (PCFG) •A PCFG is a probabilistic version of a CFG –Each production

Sentence Probability

• Probability of a sentence is the sum of the probabilities of all of its derivations.

7

P(“book the flight through Houston”) =

P(D1) + P(D2) = 0.0000216 + 0.00001296

= 0.00003456

Page 8: Probabilistic Context Free Grammar (PCFG)leeck/NLP/PCFG.pdf · 2019-11-21 · 3 Probabilistic Context Free Grammar (PCFG) •A PCFG is a probabilistic version of a CFG –Each production

8

Three Useful PCFG Tasks

• Observation likelihood: To classify and order sentences.

• Most likely derivation: To determine the most likely parse tree for a sentence.

• Maximum likelihood training: To train a PCFG to fit empirical training data.

Page 9: Probabilistic Context Free Grammar (PCFG)leeck/NLP/PCFG.pdf · 2019-11-21 · 3 Probabilistic Context Free Grammar (PCFG) •A PCFG is a probabilistic version of a CFG –Each production

PCFG: Most Likely Derivation

• There is an analog to the Viterbi algorithm to efficiently determine the most probable derivation (parse tree) for a sentence.

S → NP VP

S → VP

NP → Det A N

NP → NP PP

NP → PropN

A → ε

A → Adj A

PP → Prep NP

VP → V NP

VP → VP PP

0.9

0.1

0.5

0.3

0.2

0.6

0.4

1.0

0.7

0.3

English

PCFG

Parser

John liked the dog in the pen.S

NP VP

John V NP PP

liked the dog in the penX

Page 10: Probabilistic Context Free Grammar (PCFG)leeck/NLP/PCFG.pdf · 2019-11-21 · 3 Probabilistic Context Free Grammar (PCFG) •A PCFG is a probabilistic version of a CFG –Each production

10

PCFG: Most Likely Derivation

• There is an analog to the Viterbi algorithm to efficiently determine the most probable derivation (parse tree) for a sentence.

S → NP VP

S → VP

NP → Det A N

NP → NP PP

NP → PropN

A → ε

A → Adj A

PP → Prep NP

VP → V NP

VP → VP PP

0.9

0.1

0.5

0.3

0.2

0.6

0.4

1.0

0.7

0.3

English

PCFG

Parser

John liked the dog in the pen.

S

NP VP

John V NP

liked the dog in the pen

Page 11: Probabilistic Context Free Grammar (PCFG)leeck/NLP/PCFG.pdf · 2019-11-21 · 3 Probabilistic Context Free Grammar (PCFG) •A PCFG is a probabilistic version of a CFG –Each production

Probabilistic CYK

• CYK can be modified for PCFG parsing by including in each cell a probability for each non-terminal.

• Cell[i,j] must retain the most probablederivation of each constituent (non-terminal) covering words i +1 through j together with its associated probability.

• When transforming the grammar to CNF (Chomsky Normal Form), must set production probabilities to preserve the probability of derivations.

Page 12: Probabilistic Context Free Grammar (PCFG)leeck/NLP/PCFG.pdf · 2019-11-21 · 3 Probabilistic Context Free Grammar (PCFG) •A PCFG is a probabilistic version of a CFG –Each production

Probabilistic Grammar Conversion

S → NP VPS → Aux NP VP

S → VP

NP → Pronoun

NP → Proper-Noun

NP → Det NominalNominal → Noun

Nominal → Nominal NounNominal → Nominal PPVP → Verb

VP → Verb NPVP → VP PPPP → Prep NP

Original Grammar Chomsky Normal Form

S → NP VPS → X1 VPX1 → Aux NPS → book | include | prefer

0.01 0.004 0.006S → Verb NPS → VP PPNP → I | he | she | me

0.1 0.02 0.02 0.06NP → Houston | NWA

0.16 .04NP → Det NominalNominal → book | flight | meal | money

0.03 0.15 0.06 0.06Nominal → Nominal NounNominal → Nominal PPVP → book | include | prefer

0.1 0.04 0.06VP → Verb NPVP → VP PPPP → Prep NP

0.80.1

0.1

0.2

0.2

0.60.3

0.20.50.2

0.50.31.0

0.80.11.0

0.050.03

0.6

0.20.5

0.50.31.0

Page 13: Probabilistic Context Free Grammar (PCFG)leeck/NLP/PCFG.pdf · 2019-11-21 · 3 Probabilistic Context Free Grammar (PCFG) •A PCFG is a probabilistic version of a CFG –Each production

Probabilistic CYK Parser

13

Book the flight through Houston

S :.01, VP:.1,

Verb:.5

Nominal:.03

Noun:.1

Det:.6

Nominal:.15

Noun:.5

None

NP:.6*.6*.15

=.054

Page 14: Probabilistic Context Free Grammar (PCFG)leeck/NLP/PCFG.pdf · 2019-11-21 · 3 Probabilistic Context Free Grammar (PCFG) •A PCFG is a probabilistic version of a CFG –Each production

Probabilistic CYK Parser

14

Book the flight through Houston

S :.01, VP:.1,

Verb:.5

Nominal:.03

Noun:.1

Det:.6

Nominal:.15

Noun:.5

None

NP:.6*.6*.15

=.054

VP:.5*.5*.054

=.0135

Page 15: Probabilistic Context Free Grammar (PCFG)leeck/NLP/PCFG.pdf · 2019-11-21 · 3 Probabilistic Context Free Grammar (PCFG) •A PCFG is a probabilistic version of a CFG –Each production

Probabilistic CYK Parser

15

Book the flight through Houston

S :.01, VP:.1,

Verb:.5

Nominal:.03

Noun:.1

Det:.6

Nominal:.15

Noun:.5

None

NP:.6*.6*.15

=.054

VP:.5*.5*.054

=.0135

S:.05*.5*.054

=.00135

Page 16: Probabilistic Context Free Grammar (PCFG)leeck/NLP/PCFG.pdf · 2019-11-21 · 3 Probabilistic Context Free Grammar (PCFG) •A PCFG is a probabilistic version of a CFG –Each production

Probabilistic CYK Parser

16

Book the flight through Houston

S :.01, VP:.1,

Verb:.5

Nominal:.03

Noun:.1

Det:.6

Nominal:.15

Noun:.5

None

NP:.6*.6*.15

=.054

VP:.5*.5*.054

=.0135

S:.05*.5*.054

=.00135

None

None

None

Prep:.2

Page 17: Probabilistic Context Free Grammar (PCFG)leeck/NLP/PCFG.pdf · 2019-11-21 · 3 Probabilistic Context Free Grammar (PCFG) •A PCFG is a probabilistic version of a CFG –Each production

Probabilistic CYK Parser

17

Book the flight through Houston

S :.01, VP:.1,

Verb:.5

Nominal:.03

Noun:.1

Det:.6

Nominal:.15

Noun:.5

None

NP:.6*.6*.15

=.054

VP:.5*.5*.054

=.0135

S:.05*.5*.054

=.00135

None

None

None

Prep:.2

NP:.16

PropNoun:.8

PP:1.0*.2*.16

=.032

Page 18: Probabilistic Context Free Grammar (PCFG)leeck/NLP/PCFG.pdf · 2019-11-21 · 3 Probabilistic Context Free Grammar (PCFG) •A PCFG is a probabilistic version of a CFG –Each production

Probabilistic CYK Parser

18

Book the flight through Houston

S :.01, VP:.1,

Verb:.5

Nominal:.03

Noun:.1

Det:.6

Nominal:.15

Noun:.5

None

NP:.6*.6*.15

=.054

VP:.5*.5*.054

=.0135

S:.05*.5*.054

=.00135

None

None

None

Prep:.2

NP:.16

PropNoun:.8

PP:1.0*.2*.16

=.032

Nominal:

.5*.15*.032

=.0024

Page 19: Probabilistic Context Free Grammar (PCFG)leeck/NLP/PCFG.pdf · 2019-11-21 · 3 Probabilistic Context Free Grammar (PCFG) •A PCFG is a probabilistic version of a CFG –Each production

Probabilistic CYK Parser

19

Book the flight through Houston

S :.01, VP:.1,

Verb:.5

Nominal:.03

Noun:.1

Det:.6

Nominal:.15

Noun:.5

None

NP:.6*.6*.15

=.054

VP:.5*.5*.054

=.0135

S:.05*.5*.054

=.00135

None

None

None

Prep:.2

NP:.16

PropNoun:.8

PP:1.0*.2*.16

=.032

Nominal:

.5*.15*.032

=.0024

NP:.6*.6*

.0024

=.000864

Page 20: Probabilistic Context Free Grammar (PCFG)leeck/NLP/PCFG.pdf · 2019-11-21 · 3 Probabilistic Context Free Grammar (PCFG) •A PCFG is a probabilistic version of a CFG –Each production

Probabilistic CYK Parser

20

Book the flight through Houston

S :.01, VP:.1,

Verb:.5

Nominal:.03

Noun:.1

Det:.6

Nominal:.15

Noun:.5

None

NP:.6*.6*.15

=.054

VP:.5*.5*.054

=.0135

S:.05*.5*.054

=.00135

None

None

None

Prep:.2

NP:.16

PropNoun:.8

PP:1.0*.2*.16

=.032

Nominal:

.5*.15*.032

=.0024

NP:.6*.6*

.0024

=.000864

S:.05*.5*

.000864

=.0000216

Page 21: Probabilistic Context Free Grammar (PCFG)leeck/NLP/PCFG.pdf · 2019-11-21 · 3 Probabilistic Context Free Grammar (PCFG) •A PCFG is a probabilistic version of a CFG –Each production

Probabilistic CYK Parser

21

Book the flight through Houston

S :.01, VP:.1,

Verb:.5

Nominal:.03

Noun:.1

Det:.6

Nominal:.15

Noun:.5

None

NP:.6*.6*.15

=.054

VP:.5*.5*.054

=.0135

S:.05*.5*.054

=.00135

None

None

None

Prep:.2

NP:.16

PropNoun:.8

PP:1.0*.2*.16

=.032

Nominal:

.5*.15*.032

=.0024

NP:.6*.6*

.0024

=.000864

S:.0000216

S:.03*.0135*

.032

=.00001296

Page 22: Probabilistic Context Free Grammar (PCFG)leeck/NLP/PCFG.pdf · 2019-11-21 · 3 Probabilistic Context Free Grammar (PCFG) •A PCFG is a probabilistic version of a CFG –Each production

Probabilistic CYK Parser

22

Book the flight through Houston

S :.01, VP:.1,

Verb:.5

Nominal:.03

Noun:.1

Det:.6

Nominal:.15

Noun:.5

None

NP:.6*.6*.15

=.054

VP:.5*.5*.054

=.0135

S:.05*.5*.054

=.00135

None

None

None

Prep:.2

NP:.16

PropNoun:.8

PP:1.0*.2*.16

=.032

Nominal:

.5*.15*.032

=.0024

NP:.6*.6*

.0024

=.000864

S:.0000216Pick most probable

parse, i.e. take max to

combine probabilities

of multiple derivations

of each constituent in

each cell.

Page 23: Probabilistic Context Free Grammar (PCFG)leeck/NLP/PCFG.pdf · 2019-11-21 · 3 Probabilistic Context Free Grammar (PCFG) •A PCFG is a probabilistic version of a CFG –Each production

23

Vanilla PCFG Limitations

• Since probabilities of productions do not rely on specific words or concepts, only general structural disambiguation is possible– e.g. prefer to attach PPs to Nominals.

• Consequently, vanilla PCFGs cannot resolve syntactic ambiguities that require semantics to resolve– e.g. ate with fork vs. meatballs.

• In order to work well, PCFGs must be lexicalized, i.e. productions must be specialized to specific words by including their head-word in their LHS non-terminals (e.g. VP-ate).

Page 24: Probabilistic Context Free Grammar (PCFG)leeck/NLP/PCFG.pdf · 2019-11-21 · 3 Probabilistic Context Free Grammar (PCFG) •A PCFG is a probabilistic version of a CFG –Each production

Probabilistic CFG (PCFG)

S NP VP 1.0

VP V NP 0.7

VP VP PP 0.3

PP P NP 1.0

P with 1.0

V saw 1.0

NP NP PP 0.4

NP astronomers 0.1

NP ears 0.18

NP saw 0.04

NP stars 0.18

NP telescope 0.1

S NP VP

VP V NP

VP VP PP

PP P NP

P with

V saw

NP NP PP

NP astronomers

NP ears

NP saw

NP stars

NP telescope

- PCFG

- CFG

Page 25: Probabilistic Context Free Grammar (PCFG)leeck/NLP/PCFG.pdf · 2019-11-21 · 3 Probabilistic Context Free Grammar (PCFG) •A PCFG is a probabilistic version of a CFG –Each production

PCFG example

Page 26: Probabilistic Context Free Grammar (PCFG)leeck/NLP/PCFG.pdf · 2019-11-21 · 3 Probabilistic Context Free Grammar (PCFG) •A PCFG is a probabilistic version of a CFG –Each production

Repeated work…

Page 27: Probabilistic Context Free Grammar (PCFG)leeck/NLP/PCFG.pdf · 2019-11-21 · 3 Probabilistic Context Free Grammar (PCFG) •A PCFG is a probabilistic version of a CFG –Each production

Parsing PCFG: CYK

• CYK (Cocke, Younger, and Kasami) 알고리즘– Dynamic Programing: O(|P|*n3)

– C[i][j][Z] = Probability, B[i][j][Z] = back pointer

for i = 1 … n-1

for j = i+1 … n

for k = i … j-1

for Z X Y in P

v = C[i][k][X] * C[k+1][j][Y] * p(Z X Y)

if v > C[i][j][Z]

C[i][j][Z] = v

B[i][j][Z] = {(i,k,X), (k+1,j,Y)}

Page 28: Probabilistic Context Free Grammar (PCFG)leeck/NLP/PCFG.pdf · 2019-11-21 · 3 Probabilistic Context Free Grammar (PCFG) •A PCFG is a probabilistic version of a CFG –Each production

Lexicalization

• 성능향상을위해 O(|P|*n5)

Page 29: Probabilistic Context Free Grammar (PCFG)leeck/NLP/PCFG.pdf · 2019-11-21 · 3 Probabilistic Context Free Grammar (PCFG) •A PCFG is a probabilistic version of a CFG –Each production

Re-ranking

• 성능향상을위해 O(k*|P|*n3)

– PCFG를이용 k-best parse tree 생성

– Perceptron, SVM 등을이용하여 re-ranking