Natürlichsprachliche Systeme I - Grammatiken und Parsing mit NLTK

Transcription

Natürlichsprachliche Systeme I - Grammatiken und Parsing mit NLTK
Grammatiken und Parsing mit NLTK
Natürlichsprachliche Systeme I
Grammatiken und Parsing mit NLTK
D. Rösner
Institut für Wissens- und Sprachverarbeitung
Fakultät für Informatik
Otto-von-Guericke Universität Magdeburg
c
WS 2014/15, 14. November 2014, 2010
- 2015 D.Rösner
D. Rösner NSS I 2014/15 . . .
1
Grammatiken und Parsing mit NLTK
Gliederung
1
Grammatiken und Parsing mit NLTK
Probleme
Syntax
CFGs
Dependenz
Valenz
D. Rösner NSS I 2014/15 . . .
2
Grammatiken und Parsing mit NLTK
Probleme
Syntax
CFGs
Dependenz
Valenz
Parsing mit NLTK
Herausforderungen bei Formalisierung natürlicher Sprachen
(cf. [BKL09], Ch. 8.1):
rekursive Strukturen
Beispiel: Sätze können ineinander eingebettet werden
1
2
3
4
Usain Bolt broke the 100m record
The Jamaica Observer reported that Usain Bolt broke the
100m record
Andre said the Jamaica Observer reported that Usain Bolt
broke the 100m record
I think Andre said the Jamaica Observer reported that
Usain Bolt broke the 100m record
abgeleitete Template:
Andre said S.
I think S.
D. Rösner NSS I 2014/15 . . .
4
Grammatiken und Parsing mit NLTK
Probleme
Syntax
CFGs
Dependenz
Valenz
Parsing mit NLTK
Grammatiken (cf. [BKL09], Ch. 8.1):
sollen explizite Beschreibung einer Sprache liefern
Sicht der ’generativen Grammatik’
eine Sprache ist die Sammlung (Menge) aller
grammatischen Sätze
eine Grammatik ist eine formale Notation, mit der die
Elemente dieser Menge erzeugt werden können
D. Rösner NSS I 2014/15 . . .
5
Grammatiken und Parsing mit NLTK
Probleme
Syntax
CFGs
Dependenz
Valenz
Parsing mit NLTK
Mehrdeutigkeit (cf. [BKL09], Ch. 8.1):
While hunting in Africa, I shot an elephant in my pajamas.
How an elephant got into my pajamas I’ll never know.
D. Rösner NSS I 2014/15 . . .
6
Grammatiken und Parsing mit NLTK
Probleme
Syntax
CFGs
Dependenz
Valenz
Parsing mit NLTK
Beispiel: eine kleine kontextfreie Grammatik (cf. [BKL09], Ch.
8.1):
>>>
...
...
...
...
...
...
...
...
...
groucho_grammar = nltk.parse_cfg("""
S -> NP VP
PP -> P NP
NP -> Det N | Det N PP | ’I’
VP -> V NP | VP PP
Det -> ’an’ | ’my’
N -> ’elephant’ | ’pajamas’
V -> ’shot’
P -> ’in’
""")
D. Rösner NSS I 2014/15 . . .
7
Grammatiken und Parsing mit NLTK
Probleme
Syntax
CFGs
Dependenz
Valenz
Parsing mit NLTK
Beispiel: kontextfreie Grammatik (cf. [BKL09], Ch. 8.1):
>>> groucho_grammar
<Grammar with 13 productions>
>>> sent = [’I’, ’shot’, ’an’, ’elephant’, ’in’, ’my’, ’pajamas’]
>>> len(sent)
7
>>> parser = nltk.ChartParser(groucho_grammar)
>>> parser
<nltk.parse.chart.ChartParser object at 0x03578F70>
>>> trees = parser.nbest_parse(sent)
>>> len(trees)
2
D. Rösner NSS I 2014/15 . . .
8
Grammatiken und Parsing mit NLTK
Probleme
Syntax
CFGs
Dependenz
Valenz
Parsing mit NLTK
Beispiel: kontextfreie Grammatik (cf. [BKL09], Ch. 8.1):
>>> for tree in trees:
print tree
(S
(NP I)
(VP
(V shot)
(NP (Det an) (N elephant) (PP (P in) (NP (Det my) (N pajamas))))))
(S
(NP I)
(VP
(VP (V shot) (NP (Det an) (N elephant)))
(PP (P in) (NP (Det my) (N pajamas)))))
>>>
D. Rösner NSS I 2014/15 . . .
9
Grammatiken und Parsing mit NLTK
Probleme
Syntax
CFGs
Dependenz
Valenz
Parsing mit NLTK
Syntax natürlicher Sprachen (cf. [BKL09], Ch. 8.2):
elementare Einsicht: Wörter bilden mit anderen Wörtern
zusammengehörende Einheiten, sog. Konstituenten
Test auf Konstituenten-Eigenschaft durch
Substituierbarkeit
Substituierbarkeit liegt vor,
wenn in einem wohlgeformten Satz eine Sequenz von
Wörtern durch eine kürzere Sequenz ersetzt werden kann,
ohne dass der Satz die Wohlgeformtheit verliert
Beispiel:
The little bear saw the fine fat trout in the brook.
He saw the fine fat trout in the brook.
aber: little bear saw keine Konstituente
*The he the fine fat trout in the brook.
D. Rösner NSS I 2014/15 . . .
11
Grammatiken und Parsing mit NLTK
Probleme
Syntax
CFGs
Dependenz
Valenz
Parsing mit NLTK
Beispiel einer CFG (cf. [BKL09], Ch. 8.3):
import nltk
grammar1 = nltk.parse_cfg("""
S -> NP VP
VP -> V NP | V NP PP
PP -> P NP
V -> "saw" | "ate" | "walked"
NP -> "John" | "Mary" | "Bob" | Det N | Det N PP
Det -> "a" | "an" | "the" | "my"
N -> "man" | "dog" | "cat" | "telescope" | "park"
P -> "in" | "on" | "by" | "with"
""")
D. Rösner NSS I 2014/15 . . .
13
Grammatiken und Parsing mit NLTK
Probleme
Syntax
CFGs
Dependenz
Valenz
Parsing mit NLTK
Beispiel einer CFG (cf. [BKL09], Ch. 8.3):
>>> sent = "Mary saw Bob".split()
>>> sent
[’Mary’, ’saw’, ’Bob’]
>>> rd_parser = nltk.RecursiveDescentParser(grammar1)
>>> rd_parser
<nltk.parse.rd.RecursiveDescentParser object at 0x035FA130>
D. Rösner NSS I 2014/15 . . .
14
Grammatiken und Parsing mit NLTK
Probleme
Syntax
CFGs
Dependenz
Valenz
Parsing mit NLTK
Beispiel einer CFG (cf. [BKL09], Ch. 8.3):
>>> trees = rd_parser.nbest_parse(sent)
>>> trees
[Tree(’S’, [Tree(’NP’, [’Mary’]),
Tree(’VP’, [Tree(’V’, [’saw’]),
Tree(’NP’, [’Bob’])])])]
>>> len(trees)
1
>>> for tree in trees:
print tree
(S (NP Mary) (VP (V saw) (NP Bob)))
D. Rösner NSS I 2014/15 . . .
15
Grammatiken und Parsing mit NLTK
Probleme
Syntax
CFGs
Dependenz
Valenz
Parsing mit NLTK
Beispiel einer CFG (cf. [BKL09], Ch. 8.3):
>>> sent2 = "The dog saw a man in the park".split()
>>> sent2
[’The’, ’dog’, ’saw’, ’a’, ’man’, ’in’, ’the’, ’park’]
>>> trees2 = rd_parser.nbest_parse(sent2)
Traceback (most recent call last):
...
ValueError: Grammar does not cover
some of the input words: "’The’".
D. Rösner NSS I 2014/15 . . .
16
Grammatiken und Parsing mit NLTK
Probleme
Syntax
CFGs
Dependenz
Valenz
Parsing mit NLTK
Beispiel: strukturelle Ambiguität (cf. [BKL09], Ch. 8.3):
>>> sent2 = "the dog saw a man in the park".split()
>>> sent2
[’the’, ’dog’, ’saw’, ’a’, ’man’, ’in’, ’the’, ’park’]
>>> trees2 = rd_parser.nbest_parse(sent2)
>>> for tree in trees2:
print tree
(S
(NP (Det the) (N dog))
(VP
(V saw)
(NP (Det a) (N man) (PP (P in) (NP (Det the) (N park))))))
(S
(NP (Det the) (N dog))
(VP
(V saw)
(NP (Det a) (N man))
(PP (P in) (NP (Det the)
(N park)))))
D. Rösner
NSS I 2014/15 . . .
17
Grammatiken und Parsing mit NLTK
Probleme
Syntax
CFGs
Dependenz
Valenz
Parsing mit NLTK
Beispiel: Grammatik in Datei grammar2.cfg (cf. [BKL09], Ch.
8.3):
S -> NP VP
NP -> Det Nom | PropN
Nom -> Adj Nom | N
VP -> V Adj | V NP | V S | V NP PP
PP -> P NP
PropN -> ’Buster’ | ’Chatterer’ | ’Joe’
Det -> ’the’ | ’a’
N -> ’bear’ | ’squirrel’ | ’tree’ | ’fish’ | ’log’
Adj -> ’angry’ | ’frightened’ | ’little’ | ’tall’
V -> ’chased’ | ’saw’ | ’said’ | ’thought’ | ’was’ | ’put’
P -> ’on’
D. Rösner NSS I 2014/15 . . .
18
Grammatiken und Parsing mit NLTK
Probleme
Syntax
CFGs
Dependenz
Valenz
Parsing mit NLTK
Beispiel: Grammatik in Datei grammar2.cfg (cf. [BKL09], Ch.
8.3):
>>> grammar2 = nltk.data.load(’file:grammar2.cfg’)
>>> grammar2
<Grammar with 31 productions>
>>> for p in grammar2.productions():
print p
S -> NP VP
NP -> Det Nom
NP -> PropN
Nom -> Adj Nom
Nom -> N
VP -> V Adj
VP -> V NP
VP -> V S
VP -> V NP PP
PP -> P NP
PropN -> ’Buster’
PropN -> ’Chatterer’
PropN -> ’Joe’
...
D. Rösner NSS I 2014/15 . . .
19
Grammatiken und Parsing mit NLTK
Probleme
Syntax
CFGs
Dependenz
Valenz
Parsing mit NLTK
Beispiel: Grammatik in Datei grammar2.cfg (cf. [BKL09], Ch.
8.3):
...
Det -> ’the’
Det -> ’a’
N -> ’bear’
N -> ’squirrel’
N -> ’tree’
N -> ’fish’
N -> ’log’
Adj -> ’angry’
Adj -> ’frightened’
Adj -> ’little’
Adj -> ’tall’
V -> ’chased’
V -> ’saw’
V -> ’said’
V -> ’thought’
V -> ’was’
V -> ’put’
P -> ’on’
>>>
D. Rösner NSS I 2014/15 . . .
20
Grammatiken und Parsing mit NLTK
Probleme
Syntax
CFGs
Dependenz
Valenz
Parsing mit NLTK
Limitierungen für CFGs in NLTK (cf. [BKL09], Ch. 8.3):
auf der rechten Seite von Produktionen können
lexikalische Elemente (Strings) nicht mit Nonterminalen
gemischt werden
m.a.W. nicht zulässig sind Produktionen wie
PP -> ’of’ NP
auf der rechten Seite von Produktionen sind keine
Mehrwort-Strings zulässig
m.a.W. statt
NP -> ’New York’
muss z.B. geschrieben werden
NP -> ’New_York’
D. Rösner NSS I 2014/15 . . .
21
Grammatiken und Parsing mit NLTK
Probleme
Syntax
CFGs
Dependenz
Valenz
Parsing mit NLTK
Rekursion in CFGs (cf. [BKL09], Ch. 8.3):
>>> grammar2 = nltk.parse_cfg("""
S -> NP VP
NP -> Det Nom | PropN
Nom -> Adj Nom | N
VP -> V Adj | V NP | V S | V NP PP
PP -> P NP
PropN -> ’Buster’ | ’Chatterer’ | ’Joe’
Det -> ’the’ | ’a’
N -> ’bear’ | ’squirrel’ | ’tree’ | ’fish’ | ’log’
Adj -> ’angry’ | ’frightened’ | ’little’ | ’tall’
V -> ’chased’ | ’saw’ | ’said’ | ’thought’ | ’was’ | ’put’
P -> ’on’
""")
D. Rösner NSS I 2014/15 . . .
22
Grammatiken und Parsing mit NLTK
Probleme
Syntax
CFGs
Dependenz
Valenz
Parsing mit NLTK
Rekursion in CFGs (cf. [BKL09], Ch. 8.3):
direkte Rekursion:
Nom -> Adj Nom
indirekte Rekursion:
S -> NP VP
VP -> V S
Problem: RecursiveDescentParser können keine
linksrekursiven Produktionen verarbeiten von der Form
X -> X Y1 ... Yn
D. Rösner NSS I 2014/15 . . .
23
Grammatiken und Parsing mit NLTK
Probleme
Syntax
CFGs
Dependenz
Valenz
Parsing mit NLTK
Interaktive Parser-Demos in NLTK (cf. [BKL09], Ch. 8.3, 8.4):
nltk.app.rdparser()
nltk.app.srparser()
nltk.app.chartparser()
D. Rösner NSS I 2014/15 . . .
24
Grammatiken und Parsing mit NLTK
Probleme
Syntax
CFGs
Dependenz
Valenz
Parsing mit NLTK
Alternativer Ansatz: Dependenzgrammatik (cf. [BKL09], Ch.
8.5):
Phrasenstrukturgrammatik: Wie lassen sich Wörter und
Sequenzen von Wörtern kombinieren, um Konstituenten
zu bilden?
Dependenzgrammatik: In welchen Beziehungen stehen
Wörter zueinander?
Dependenz: binäre, asymmetrische Relation zwischen
einem Kopfelement (engl. head) und seinen abhängigen
Elementen (engl. dependents)
Beispiel: in Sätzen ist der Head meist das flektierte Verb
D. Rösner NSS I 2014/15 . . .
26
Grammatiken und Parsing mit NLTK
Probleme
Syntax
CFGs
Dependenz
Valenz
Parsing mit NLTK
Dependenzgrammatik (cf. [BKL09], Ch. 8.5):
Repräsentation von Dependenz-Beziehungen:
gerichteter Graph, bei dem
die Knoten die lexikalischen Elemente und
die Kanten markiert mit den Dependenz-Relationen
zwischen Köpfen und ihren abhängigen Elementen
Beispiel: cf. [BKL09], Ch. 8.5, Fig. 8.11
D. Rösner NSS I 2014/15 . . .
27
Grammatiken und Parsing mit NLTK
Probleme
Syntax
CFGs
Dependenz
Valenz
Parsing mit NLTK
Dependenzgrammatik (cf. [BKL09], Ch. 8.5):
ein Dependenzgraph heisst projektiv, wenn sich, wenn alle
Wörter in linearer Ordnung geschrieben sind, die Kanten
ohne Überschneidungen zeichnen lassen
gleichwertig: ein Wort und alle seine (direkt oder indirekt)
abhängigen Elemente lassen sich als Sequenz
benachbarter Wörter im Satz schreiben
D. Rösner NSS I 2014/15 . . .
28
Grammatiken und Parsing mit NLTK
Probleme
Syntax
CFGs
Dependenz
Valenz
Parsing mit NLTK
Dependenzgrammatik (cf. [BKL09], Ch. 8.5):
>>> groucho_dep_grammar = nltk.parse_dependency_grammar("""
’shot’ -> ’I’ | ’elephant’ | ’in’
’elephant’ -> ’an’ | ’in’
’in’ -> ’pajamas’
’pajamas’ -> ’my’
""")
D. Rösner NSS I 2014/15 . . .
29
Grammatiken und Parsing mit NLTK
Probleme
Syntax
CFGs
Dependenz
Valenz
Parsing mit NLTK
Dependenzgrammatik (cf. [BKL09], Ch. 8.5):
>>> print groucho_dep_grammar
Dependency grammar with 7 productions
’shot’ -> ’I’
’shot’ -> ’elephant’
’shot’ -> ’in’
’elephant’ -> ’an’
’elephant’ -> ’in’
’in’ -> ’pajamas’
’pajamas’ -> ’my’
D. Rösner NSS I 2014/15 . . .
30
Grammatiken und Parsing mit NLTK
Probleme
Syntax
CFGs
Dependenz
Valenz
Parsing mit NLTK
Dependenzgrammatik (cf. [BKL09], Ch. 8.5):
>>> pdp = nltk.ProjectiveDependencyParser(groucho_dep_grammar)
>>> sent = ’I shot an elephant in my pajamas’.split()
>>> trees = pdp.parse(sent)
>>> for tree in trees:
print tree
(shot I (elephant an (in (pajamas my))))
(shot I (elephant an) (in (pajamas my)))
D. Rösner NSS I 2014/15 . . .
31
Grammatiken und Parsing mit NLTK
Probleme
Syntax
CFGs
Dependenz
Valenz
Parsing mit NLTK
Dependenzgrammatik (cf. [BKL09], Ch. 8.5): Kriterien für die
Bestimmung von Head (H) und Dependents (D) in Konstruktion
C
H bestimmt die Distributionsklasse von C
H bestimmt den semantischen Typ von C
H ist obligatorisch, D aber optional
H wählt D aus und bestimmt, ob D obligatorisch oder
optional
die morphologische Form von D ist durch H bestimmt (z.B.
Kongruenz oder Kasusbestimmung)
D. Rösner NSS I 2014/15 . . .
32
Grammatiken und Parsing mit NLTK
Probleme
Syntax
CFGs
Dependenz
Valenz
Parsing mit NLTK
Valenz (cf. [BKL09], Ch. 8.5):
Verben unterscheiden sich danach, welche Komplemente
sie verlangen (subkategorisieren)
Beispiele:
The squirrel was frightened.
Chatterer saw the bear.
Chatterer thought Buster was angry.
Joe put the fish on the log.
D. Rösner NSS I 2014/15 . . .
34
Grammatiken und Parsing mit NLTK
Probleme
Syntax
CFGs
Dependenz
Valenz
Parsing mit NLTK
Valenz (cf. [BKL09], Ch. 8.5):
unzulässige oder fehlende Komplemente führen zu
ungrammatischen Sätzen
Beispiele:
*The squirrel was Buster was angry.
*Chatterer saw frightened.
*Chatterer thought the bear.
*Joe put on the log.
D. Rösner NSS I 2014/15 . . .
35
Grammatiken und Parsing mit NLTK
Probleme
Syntax
CFGs
Dependenz
Valenz
Parsing mit NLTK
Valenz (cf. [BKL09], Ch. 8.5):
in CFGs lassen sich Valenzrestriktionen für Verben
dadurch einbringen, dass bei Regeln, die VPs
expandieren, unterschiedliche Subkategorien von Verben
verwendet werden
Beispiel: transitive Verben erfordern eine NP als direktes
Objekt
mögliche Regeln:
VP -> TV NP
TV -> ’chased’ | ’saw’
D. Rösner NSS I 2014/15 . . .
36
Grammatiken und Parsing mit NLTK
Probleme
Syntax
CFGs
Dependenz
Valenz
Parsing mit NLTK
Valenz: Subkategorien von Verb (cf. [BKL09], Ch. 8.5, Table
8.4):
Symbol
Bedeutung
Beispiel
IV
intransitives Verb
barked
TV
transitives Verb
saw a man
DatV
ditransitives Verb
gave a dog to a man
SV
Verb mit Satzkomplement said that a dog barked
D. Rösner NSS I 2014/15 . . .
37
Grammatiken und Parsing mit NLTK
Probleme
Syntax
CFGs
Dependenz
Valenz
Parsing mit NLTK
Modifikatoren oder Adjunkte (cf. [BKL09], Ch. 8.5):
im Unterschied zu Komplementen sind Modifikatoren
optional und können iteriert verwendet werden
auch werden sie nicht wie die Komplemente durch den
Kopf selektiert
Beispiel: ’really’ kann bei allen folgenden Sätzen
verwendet werden
The squirrel really was frightened.
Chatterer really saw the bear.
Chatterer really thought Buster was angry.
Joe really put the fish on the log.
D. Rösner NSS I 2014/15 . . .
38
Grammatiken und Parsing mit NLTK
Probleme
Syntax
CFGs
Dependenz
Valenz
Literatur: I
Steven Bird, Ewan Klein, and Edward Loper.
Natural Language Processing with Python – Analyzing Text
with the Natural Language Toolkit.
O’Reilly Media, 2009.
Daniel Jurafsky and James H. Martin.
Speech and Language Processing: An introduction to
natural language processing, computational linguistics, and
speech recognition.
Prentice Hall, 2nd (May 26, 2008) edition, 2008.
D. Rösner NSS I 2014/15 . . .
39
Grammatiken und Parsing mit NLTK
Probleme
Syntax
CFGs
Dependenz
Valenz
Literatur: II
Christopher D. Manning and Hinrich Schütze.
Foundations of Statistical Natural Language Processing.
MIT Press, fifth printing edition, 2002.
D. Rösner NSS I 2014/15 . . .
40

Documents pareils