Taaltheorie en Taalverwerking Remko Scha

Download Report

Transcript Taaltheorie en Taalverwerking Remko Scha

Bachelor-opleiding Kunstmatige Intelligentie
Taaltheorie en Taalverwerking
Remko Scha
Taaltheorie en Taalverwerking
Week 2:
Herschrijfgrammatica's
De Chomsky-hiërarchie
Het formalistisch perspectief op taal:
Mathematische methodes voor het definiëren van
verzamelingen strings.
Vorige week:
Reguliere Expressies
&
Eindige Automaten
Het formalistisch perspectief op taal:
Mathematische methodes voor het definiëren van
verzamelingen strings.
Vorige week:
Reguliere Expressies
Deze week:
Herschrijfgrammatica's
≈
Eindige Automaten
Herschrijfgrammatica's
Jurafsky & Martin:
Context-Free/Formal Grammars for English
(Ed. 1: Ch. 9. Ed. 2: Ch. 12)
Language and Complexity
(Ed. 1: Ch. 12. Ed. 2: Ch. 16)
S
NP
article
the
Engelse zin met woordsoorten
en "oppervlakte-structuur"
noun
wumpus
VP
copula
is
adjective
dead
Herschrijfgrammatica die
deze zin genereert
S
NP
S  NP VP
VP
NP  article noun
VP  copula adjective
article  the
noun  wumpus
copula  is
adjective  dead
article
the
noun
wumpus
copula
is
adjective
dead
Herschrijfgrammatica die
nog meer zinnen genereert
S
S  NP VP
NP
NP  article noun
NP  John
VP  copula adjective
VP  verb
verb  walks
article  the
article  a
noun  wumpus
noun  boy
copula  is
copula  was
adjective  dead
adjective  happy
article
the
VP
noun
wumpus
a
boy
John
copula
is
was
adjective
dead
happy
walks
Generatie-proces
S
Generatie-proces
S
S  NP VP
S
S  NP VP
NP
VP
S
S  NP VP
NP  article noun
NP
VP
S
S  NP VP
NP
VP
NP  article noun
article
noun
S  NP VP
NP  article noun
article  the
S
NP
VP
article
the
noun
S  NP VP
NP  article noun
article  the
S
noun  boy
NP
VP
article
the
noun
boy
S  NP VP
NP  article noun
article  the
S
noun  boy
VP  verb
NP
VP
article
the
noun
boy
S  NP VP
NP  article noun
article  the
S
noun  boy
VP  verb
NP
VP
verb  walks
article
the
noun
boy
verb
walks
S  NP VP
NP  article noun
article  the
S
noun  boy
VP  walks
NP
article
the
VP
noun
boy
verb
walks
Dit generatie-proces kan beschouwd worden als een serie
string-herschrijvingen beginnend met S.
S  NP VP
NP  article noun
article  the
noun  boy
VP  walks
herschrijf-proces:
(1)
(2)
(3)
(4)
(5)
(6)
S
NP
article noun
the noun
the
boy
the
boy
VP
VP
VP
VP
walks
Een verzameling herschrijfregels is een formele
grammatica (een “herschrijfgrammatica”).
De taal gedefinieerd door zo'n grammatica is de
verzameling van woordsequenties die d.m.v. van een
serie van opeenvolgende herschrijvingen afgeleid
kunnen worden uit het startsymbool S.
Herschrijfgrammatica:
4-tupel <N, , P, S>
N: eindige verzameling non-terminale symbolen
(b.v.: {S, NP, VP, noun, article, ...})
: eindige verzameling terminale symbolen
(b.v.: {the, a, boy, wumpus, ...})
N=
S: startsymbool;
SN
P: eindige verzameling herschrijfregels { , .....}
  (N  )*,   (N  )*
[Notatie: A* = de verzameling van strings die uitsluitend bestaan uit elementen van A
= de monoïde gegenereerd door A ]
Herschrijfgrammatica's in soorten:
de Chomsky-hiërarchie
Herschrijfgrammatica's in soorten
A  N; x  ; , , 
Type 0: Onbeperkte herschrijfgrammatica
Herschrijfregels  met  ≠ .
Type 1: Contextgevoelige Grammatica
Herschrijfregels: A   met
Type 2: Contextvrije Grammatica
Herschrijfregels: A   .
Type 3: Reguliere Grammatica
Herschrijfregels: A  x B of A  x .
≠.
 (N  )*
Herschrijfgrammatica's in soorten
A  N; x  ; , , 
Type 0: Onbeperkte herschrijfgrammatica
Herschrijfregels  met  ≠ .
Type 1: Contextgevoelige Grammatica
Herschrijfregels: A   met
Type 2: Contextvrije Grammatica
Herschrijfregels: A   .
Type 3: Reguliere Grammatica
Herschrijfregels: A  x B of A  x .
≠.
 (N  )*
Herschrijfgrammatica's in soorten
A  N; x  ; , , 
Type 0: Onbeperkte herschrijfgrammatica
Herschrijfregels  met  ≠ .
Type 1: Contextgevoelige Grammatica
Herschrijfregels: A   met  ≠  .
Oftewel: Herschrijfregels  met || ≤ ||.
Type 2: Contextvrije Grammatica
Herschrijfregels: A   .
Type 3: Reguliere Grammatica
Herschrijfregels: A  x B of A  x .
 (N  )*
Herschrijfgrammatica's in soorten
A  N; x  ; , , 
Type 0: Onbeperkte herschrijfgrammatica
Herschrijfregels  met  ≠ .
Type 1: Contextgevoelige Grammatica
Herschrijfregels: A   met
Oftewel: Herschrijfregels  met || ≤ || .
Type 2: Contextvrije Grammatica
Herschrijfregels: A   .
Type 3: Reguliere Grammatica
Herschrijfregels: A  x B of A  x .
≠.
 (N  )*
Herschrijfgrammatica's in soorten
A  N; x  ; , , 
 (N  )*
Type 0: Onbeperkte herschrijfgrammatica
Herschrijfregels  met  ≠ .
Type 1: Contextgevoelige Grammatica
Herschrijfregels: A   met
≠.
Oftewel: Herschrijfregels  met || ≤ || .
Type 2: Contextvrije Grammatica
Herschrijfregels: A   .
Type 3: Reguliere Grammatica
Herschrijfregels: A  x B of A  x . (Rechts-lineair)
Of: Herschrijfregels: A  Bx of A  x . (Links-lineair)
De Chomsky-hiërarchie
Type 0: Recursief Opsombare Talen ("Turing-Equivalent").
Type 1: Contextgevoelige Talen.
Type 2: Contextvrije Talen.
Type 3: Reguliere Talen.
De Chomsky-hiërarchie
Type 0: Recursief Opsombare Talen ("Turing-Equivalent").
Type 1: Contextgevoelige Talen.
Type 2: Contextvrije Talen.
Type 3: Reguliere Talen.
Dit is een subsumptie-hiërarchie:
Type 0 omvat Type 1 omvat Type 2 omvat Type 3.
The Chomsky Hierarchy
Type 0: Recursively enumerable (Turing-equivalent)
Type 1: Context-Sensitive
Type 2: Context-Free
Type 3: Regular Grammar / FSA / Reg. Expr.
FSA ≈ Reguliere grammatica
J & M: "Schapentaal":
Rechts-lineaire grammatica:
S  Q0
Q0  b Q1
Q1  a Q2
Q2  a Q3
Q3  a Q3
Q3  !
baa(a)*!
Niet-Reguliere Talen
Sommige talen kunnen niet herkend/gegenereerd
worden door een eindige-toestands-automaat.
Voorbeeld: L = { anbn | n  N }
= { , ab, aabb, aaabbb, ...}
• Intuïtie: de FSA kan niet "onthouden" hoeveel a's
hij gegenereerd heeft.
Niet-Reguliere Talen
Sommige talen kunnen niet herkend/gegenereerd
worden door een eindige-toestands-automaat.
Voorbeeld: L = { anbn | n  N }
= { , ab, aabb, aaabbb, ...}
• Intuïtie: de FSA kan niet "onthouden" hoeveel a's
hij gegenereerd heeft.
• Formeel bewijs: m.b.v. de "pompstelling"
Het idee achter de Pompstelling
qj
q1
qk
Beschouw een accepterende FSA met k toestanden.
Om een string ter lengte pk te accepteren, moet er
een j zijn die meer dan 1 keer bezocht wordt
(pad: q1,…,qj,…,qj,…, qk).
Het idee achter de Pompstelling
qj
q1
qk
Het pad van qj naar qj kun je net zo
vaak doorlopen als je wilt.
Pompstelling (Pumping Lemma)
voor oneindige reguliere talen
Voor elke oneindige reguliere taal L is er ‘n integer N,
zodanig dat voor elke string w  L met lengte |w| ≥ N
geldt dat w = xyz, zodanig dat
y ≠ 
|xy| ≤ N, en
x yn z  L voor elke n ≥ 0
Intuïtie: Gegeven een FSA met N+1 toestanden en een string w die langer is dan N;
dan is er een niet-lege cyclus (“loop”) nodig voor het genereren van de eerste N
elementen van w.
Pompstelling (Pumping Lemma)
voor oneindige reguliere talen
Voor elke oneindige reguliere taal L is er ‘n integer N,
zodanig dat voor elke string w  L met lengte |w| ≥ N
geldt dat w = xyz, zodanig dat
y ≠ 
|xy| ≤ N, en
x yn z  L voor elke n ≥ 0
Pompstelling – Zwakkere Versie
Jurafsky & Martin
Voor elke oneindige reguliere taal L is er ‘n string w  L,
zodanig dat
w = xyz,
y ≠  en
x yn z  L voor elke n ≥ 0
Pompstelling
voor oneindige reguliere talen
De sterkere versie van de pompstelling is nuttig omdat je er meer mee kunt bewijzen.
Maar voor sommige eenvoudige gevallen volstaat de zwakke versie die Jurafsky & Martin
presenteren.
B.v. de vraag: is de taal L
= {anbn | n0} een reguliere taal?
anbn
Stel dat L = {anbn | n0} een reguliere taal is.
anbn
Stel dat L = {anbn | n0} een reguliere taal is.
Dan is er een string apbp die geschreven kan worden
als xyz (met y ≠ ), zodanig dat xyiz  L voor alle i0 .
anbn
Stel dat L = {anbn | n0} een reguliere taal is.
Dan is er een string apbp die geschreven kan worden
als xyz (met y ≠ ), zodanig dat xyiz  L voor alle i0 .
Wat is y voor 'n string?
Drie mogelijkheden:
1) y = ak, dus xyyz = ap+kbp  L; klopt niet!
2) y = bk, dus xyyz = apbk+p  L; klopt niet!
3) y bevat a's en b's,
dus in xyyz komen a's na b's voor; klopt niet!
anbn
Stel dat L = {anbn | n0} een reguliere taal is.
Dan is er een string apbp die geschreven kan worden
als xyz (met y ≠ ), zodanig dat xyiz  L voor alle i0 .
Wat is y voor 'n string?
Drie mogelijkheden:
1) y = ak, dus xyyz = ap+kbp  L; klopt niet!
2) y = bk, dus xyyz = apbk+p  L; klopt niet!
3) y bevat a's en b's,
dus in xyyz komen a's na b's voor; klopt niet!
Contradictie.
Dus: anbn is niet regulier
anbn
L = {anbn | n0} kan wel gegenereerd worden door een
contextvrije grammatica:
SaSb
S
Opgaven van deze week:
Reguliere talen en contextvrije talen.
• Gegeven een contextvrij grammaticaatje, beschrijf
welke verzameling strings erdoor gegenereerd wordt.
• Gegeven een karakterisering van een verzameling strings,
bewijs dat het wel of niet een reguliere taal is.
http://iaaa.nl/TTTV/
Line of Reasoning
Proof by contradiction:
• Assume that L is regular
• Hence, there is a DFA M that recognizes L
• For strings of length  |Q| the DFA M has
to ‘repeat itself’
• Show that M will accept strings outside L
• Conclude that the assumption was wrong
Note that we use the simple DFA, not the
more elaborate (but equivalent) NFA or GNFA
Formal Proof of Pumping Lemma
Let M = (Q,,,q1,F) with Q = {q1,…,qp}
Let s = s1…snL(M) with |s| = n  p
Computational path of M on s is the
sequence r1,…,rn+1  Qn+1 with
r1 = q1, rn+1F and rt+1= (rt,st) for 1tn
Because n+1  p+1, there are two states
such that rj = rk (with j<k and k  p+1)
Let x = s1…sj–1, y = sj…sk–1, and z = sk…sn+1
x takes M from q1=r1 to rj, y takes M from rj to rj,
and z takes M from rj to rn+1F
As a result: xyiz takes M from q1 to rn+1F (i  0)
Formal Proof of Pumping Lemma
Let M = (Q,,,q1,F) with Q = {q1,…,qp}
Let s = s1…snL(M) with |s| = n  p
Computational path of M on s is the
sequence r1,…,rn+1  Qn+1 with
r1 = q1, rn+1F and rt+1= (rt,st) for 1tn
Because n+1  p+1, there are two terms
such that rj = rk (with j<k and k  p+1)
Let x = s1…sj–1, y = sj…sk–1, and z = sk…sn+1
x takes M from q1=r1 to rj, y takes M from rj to rj,
|y|  1 and |xy|  p
and z takes M from rj to rn+1F
As a result: xyiz takes M from q1 to rn+1F (i  0)
x yi z  L(M) for every i{0,1,2,…}
F = { ww | w{0,1}* } (Ex. 1.40)
Let p be the pumping length, and take s = 0p10p1
Let s = xyz = 0p10p1 with condition 3) |xy|p
Only one option: y=0k, with xyyz = 0p+k10p1  F
Without 3) this would have been a pain.
Intersecting Regular
Languages
Let C = { w | # of 0s in w equals # of 1s in w}
Problem: If xyzC with yC, then xyizC
Idea: If C is regular and F is regular, then
the intersection CF has to be regular as well
Solution: Assume that C is regular
Take the regular F = { 0n1m | n,mN}, then
for the intersection: CF = { 0n1n | nN }
But we know that CF is not regular
Conclusion: C is not regular
Pumping Down E = { 0i1j | ij }
Problem: ‘pumping up’ s=0p1p with y=0k gives
xyyz = 0p+k1p, xy3z = 0p+2k1p, which are all in E
(hence do not give contradictions)
Solution: pump down to xz = 0p–k1p.
Overall for s = xyz = 0p1p (with |xy|p):
y=0k, hence xz = 0p–k1p  E
Contradiction: E is not regular