INF2820 Datalingvistikk V2015 6. Gang - 23.2 Jan Tore Lønning
PARSING DEL 1 2
I dag Høyre- og venstreavledninger Recursive-descent parser (top-down) Begynne Shift-reduce parser (bottom-up) 25. februar 2015 3
Eksempel: Avledning S NP VP Det N VP the N VP the dog VP the dog V NP PP the dog saw NP PP the dog saw Det N PP the dog saw a N PP the dog saw a man PP the dog saw a man P NP the dog saw a man in NP the dog saw a man in Det N the dog saw a man in the N the dog saw a man in the park Flere avledninger av samme streng, for eksempel: S NP VP NP V NP PP NP V NP P NP NP V NP P Det N NP V NP P Det park 25. februar 2015 4
Høyre- og venstreavledninger Til hvert tre svarer det mange avledninger. For kontekstfrie grammatikker er forskjellene mellom avledninger som svarer til samme tre uinteressante. En avledning er en venstreavledning ( leftmost derivation ) hvis vi alltid ekspanderer ikke-terminalen lengst til venstre. Høyreavledning defineres tilsvarende. Til ethvert tre generert av grammatikken svarer det nøyaktig en venstreavledning og nøyaktig en høyreavledning. 25. februar 2015 5
Eksempel: VF og HF Venstreavledning: S NP VP Det N VP the N VP the dog VP the dog V NP PP the dog saw NP PP the dog saw Det N PP the dog saw a N PP the dog saw a man PP the dog saw a man P NP the dog saw a man in NP the dog saw a man in Det N the dog saw a man in the N the dog saw a man in the park Høyreavledning: S NP VP NP V NP PP NP V NP P NP NP V NP P Det N NP V NP P Det park NP V NP P the park NP V NP in the park NP V Det N in the park NP V Det man in the park NP V a man in the park NP saw a man in the park Det N saw a man in the park Det man saw a man in the park the dog saw a man in the park 25. februar 2015 6
I dag Høyre- og venstreavledninger Recursive-descent parser (top-down) Begynne Shift-reduce parser (bottom-up) 25. februar 2015 7
I dag Høyre- og venstreavledninger Recursive-descent parser (top-down) Hva er parsing? Algoritme for RD-anerkjenning Eksempelimplementasjon Trær i NLTK Algoritme for RD-parsing m/implementasjon Problemer for RD-parsing Begynne Shift-reduce parser (bottom-up) 25. februar 2015 8
Parsing Gitt en grammatikk G og streng s Spm1: Er s L(G) Spørsmål om anerkjennelse ( recognition ) Spm2: Hvorfor er s L(G)? Hva er frasestrukturen til s i G? Finn alle trær i T(G) som har s som utkomme ( yield ) Parsing 25. februar 2015 9
Recursive descent parser Lager en venstreavledning Bygger et tre: Fra toppen ( top-down ) Fra venstre mot høyre Merk Tilstrekkelig å bare se på venstreavledninger fordi de svarer til trær Streber mot tidligst mulig å sjekke mot input-data Et trinn er ikke-deterministisk: Velg en regel! Dette gir et søkerom å holde orden på 25. februar 2015 10
Venstreavledning S NP VP Det N VP the N VP the dog VP the dog V NP PP the dog saw NP PP the dog saw Det N PP the dog saw a N PP the dog saw a man PP the dog saw a man P NP the dog saw a man in NP the dog saw a man in Det N the dog saw a man in the N the dog saw a man in the park S the dog saw a man in the park Datastruktur NP VP the dog saw a man in the park Det N VP the dog saw a man in the park the N VP the dog saw a man in the park N VP dog saw a man in the park dog VP dog saw a man in the park VP saw a man in the park V NP PP saw a man in the park saw NP PP saw a man in the park NP PP a man in the park Det N PP a man in the park a N PP a man in the park N PP man in the park man PP man in the park PP in the park P NP in the park in NP in the park NP the park Det N the park the N the park N park park park # # 25. februar 2015 11
Algoritme ikke-deterministisk Struktur: Frontier: De symbolene det gjenstår å finne Words: Resten av input som så langt ikke er prosessert Intialisering Words:= Inputordene Frontier:= list( S ) Løkke: Hvis Frontier=Words=ε: stopp med suksess! Hvis first(words)=first(frontier): Words:=rest(Words), Frontier:=rest(Frontier) Hvis first(frontier) er en ikke-terminal B, velg en regel B β, og la Frontier:= β + rest(frontier) 25. februar 2015 12
Søkerom Recursive Descent: Deterministisk Dybde-først: Følg det alternativet du har valgt til bunns før du prøver det neste 25. februar 2015 13
def rd_recognize(grammar, words): top = grammar.start() return match(grammar, [top], words) def match(grammar, frontier, rwords): if len(frontier) == 0: return len(rwords) == 0 elif: len(rwords)>0 and rwords[0] == frontier[0]: if match(grammar, frontier[1:], rwords[1:]): return True else: for p in grammar.productions(): if p.lhs() == frontier[0]: newfrontier = list(p.rhs()) + frontier[1:] if match(grammar, newfrontier, rwords): return True return False February 25, 2015 14
I dag Høyre- og venstreavledninger Recursive-descent parser (top-down) Hva er parsing? Algoritme for RD-anerkjenning Eksempelimplementasjon Trær i NLTK Algoritme for RD-parsing m/implementasjon Problemer for RD-parsing Begynne Shift-reduce parser (bottom-up) 25. februar 2015 15
Representasjon av trær i Python 1. En enkel repr. (S, [(NP, [(Det, ['the']), (N, ['dog'])]), (VP, [(V, ['saw']), (NP, [(Det, ['a']), (N, ['man'])]), (PP, [(P, ['in']), (NP, [(Det, ['the']), (N, ['park'])] )])])]) Et tre = Et par av En streng Morens kategori En liste av trær døtrene Eller en streng et blad, en terminal February 25, 2015 16
2. NLTKs noe mer raffinerte repr. 1. Enkel repr. 2. NLTK s klasse: tree >>> atree ('S', [('NP', [('PN', ['Mary'])]), ('VP', [('V', ['saw']), ('NP', [('PN', ['John'])])])]) >>> print atree ('S', [('NP', [('PN', ['Mary'])]), ('VP', [('V', ['saw']), ('NP', [('PN', ['John'])])])]) Mother s category, S atree[0] atree.node First daughter atree[1][0] atree[0] Second daughter atree[1][1] atree[1] Tree('S', [Tree('NP', [Tree('PN', ['Mary'])]), Tree('VP', [Tree('V', ['saw']), Tree('NP', [Tree('PN', ['John'])])])]) (S (NP (PN Mary)) (VP (V saw) (NP (PN John)))) Adding daughter atree[1].append( ) atree.append( ) February 25, 2015 17
Et snapshot underveis i parsing (S, [(NP, [(Det, ['the']), (N, ['dog'])]), (VP, [(V, ['saw']), (NP, [(Det, [ ]), (N, [ ])]), (PP, [ ])])]) Delvis konstruert tre [,, ] Frontier= en liste av (inkomplette) noder i treet Gjenstående ord, liste 18
Eksempel: grammar1 25. februar 2015 19
def rd_parse(grammar, words): init_tree = Tree(grammar.start(), []) return match(grammar, [init_tree], words, init_tree) def match(grammar, frontier, rwords, tree): trees = [] if len(frontier) == 0: if len(rwords) == 0: trees = [Tree(tree.pprint())] elif type(frontier[0]) is str: Returner liste med kopi av arbeidstreet! if len(rwords)>0 and rwords[0] == frontier[0]: trees=match(grammar,frontier[1:], rwords[1:],tree) else: 25. februar 2015 20
def match(grammar, frontier, rwords, tree):... else: current = frontier[0] for p in grammar.productions(): if p.lhs() == current.node: daughters = [] for symb in p.rhs(): if type(symb) is str: t = symb else: t = Tree(symb, []) daughters.append(t) current.append(t) newfrontier = daughters+frontier[1:] return trees nts = match(grammar, newfrontier, words, tree) trees = trees + nts for i in range(len(current)): current.pop() Litt arbeid for å sikre at nodes er I treet! Plukk fra hverandre treet igjen! 25. februar 2015 21
Problemer for RD-parsing 1. Venstrerekursjon: Hvordan takler parseren N AP N N N PP? 2. Dobbeltarbeid: Som en del av en overordnet gal analyse kan den finne riktige deler, men disse blir glemt 3. Prøving og feiling som er litt blind 25. februar 2015 22
I dag Høyre- og venstreavledninger Recursive-descent parser (top-down) Begynne Shift-reduce parser (bottom-up) 25. februar 2015 23
Datastruktur Høyreavledning S NP VP NP V NP PP NP V NP P NP NP V NP P Det N NP V NP P Det park NP V NP P the park NP V NP in the park NP V Det N in the park NP V Det man in the park NP V a man in the park NP saw a man in the park Det N saw a man in the park Det man saw a man in the park the dog saw a man in the park Datastruktur reduce shift S # NP VP # N V NP PP # N V NP P NP # NP V NP P Det N # NP V NP P Det park # NP V NP P Det park NP V NP P the park NP V NP P the park NP V NP in the park NP V NP in the park NP V Det N in the park NP V Det man in the park NP V Det man in the park NP V a man in the park NP V a man in the park NP saw a man in the park NP saw a man in the park Det N saw a man in the park Det dog saw a man in the park Det dog saw a man in the park the dog saw a man in the park # the dog saw a man in the park 25. februar 2015 24
Bottom-up: Shift reduce parser Struktur: Words: en liste av ord (terminaler) Stack: en stack av symboler (terminaler og ikketerminaler) Vanlig notasjon: Stack Words (toppen av stacken til høyre i Stack) Start: Words:= ordene i setningen som skal analyseres Stack:= tom Løkke: Hvis Words=[] og Stack=[S]: stopp med suksess! Hvis mulig, gjør en av følgende: (Shift:) Hvis Words=/=[], La Stack:=Stack+ first(words) og Words:=rest(Words) (Reduce:) Hvis det fins α, β, B, en regel B β og Stack= α + β: la Stack= α + B 25. februar 2015 25
Bottom-up: Shift reduce parser ε Kim saw the girl with the telescope NP V Det girl with the telescope NP V Det girl with the telescope (SHIFT) NP V Det N with the telescope (REDUCE) NP V NP with the telescope (REDUCE) 25. februar 2015 26