INF2820 Datalingvistikk V Gang Jan Tore Lønning

Like dokumenter
INF2820 Datalingvistikk V Gang Jan Tore Lønning

INF2820 Datalingvistikk V2012. Jan Tore Lønning

INF2820 Datalingvistikk V2012

2/24/2012. Context-Free Grammars. I dag. Avledning. Eksempel: grammar1 PARSING. Jan Tore Lønning

INF2820 Datalingvistikk V Gang 2.3 Jan Tore Lønning

INF2820 Datalingvistikk V Gang 26.2 Jan Tore Lønning

INF2820 Datalingvistikk V Gang Jan Tore Lønning

INF2820 Datalingvistikk V2011. Jan Tore Lønning & Stephan Oepen

2/22/2011. Høyre- og venstreavledninger. I dag. Chomsky-normalform (CNF) Chomsky-normalform (CNF) PARSING. Jan Tore Lønning & Stephan Oepen

INF2820 Datalingvistikk V Gang 27.2 Jan Tore Lønning

INF2820 Datalingvistikk V Gang Jan Tore Lønning

INF2820 Datalingvistikk V2012. Jan Tore Lønning

INF2820 Datalingvistikk V Gang Jan Tore Lønning

2/20/2012. I dag. Parsing. Recursive descent parser SÆRLIG TABELLPARSING. Venstre- og høyreavledning. Jan Tore Lønning

INF2820 Datalingvistikk V Gang 4.5 Jan Tore Lønning

INF2820 Datalingvistikk V gang, Jan Tore Lønning

INF2820 Datalingvistikk V2012. Jan Tore Lønning

INF2820 Datalingvistikk V Gang 9.3 Jan Tore Lønning

INF2820 Datalingvistikk V2011. Jan Tore Lønning & Stephan Oepen

INF2820 Datalingvistikk V Gang 9.3 Jan Tore Lønning

INF2820 Datalingvistikk V Gang 13.3 Jan Tore Lønning

INF 2820 V2015: Obligatorisk innleveringsoppgave 3

INF2820 Datalingvistikk V Gang 19.3 del 1 Jan Tore Lønning

3/1/2011. I dag. Recursive descent parser. Problem for RD-parser: Top Down Space. Jan Tore Lønning & Stephan Oepen

3/5/2012. Chart alternativ datastruktur. Fundamentalregelen. Chart-parsing. Bottom-up FORMELL SPRÅKTEORI. Jan Tore Lønning

INF2820 Datalingvistikk V2012. Jan Tore Lønning

INF2820 Datalingvistikk V Gang Jan Tore Lønning

Oppgave 2. Eksamen INF2820, 2015, oppgave 2. La gramatikk G være:

INF2820 Datalingvistikk V Gang 6.3 Jan Tore Lønning

2/24/2012. Dynamic Programming. I dag. Example. Example PARSING. Jan Tore Lønning

INF2820 V2017 Oppgavesett 5 arbeidsoppgaver

INF2820 V2017 Oppgavesett 5 Gruppe 21.2

INF2820 Datalingvistikk V gang, 27.2 Jan Tore Lønning

INF5830, H2009, Obigatorisk innlevering 2. 1 Oppgave: Unære produksjoner i CKY

INF2820 Datalingvistikk V Gang 2.3 Jan Tore Lønning

INF2820 Datalingvistikk V Gang 2.3 Jan Tore Lønning

Spørsmål 1.1 (10%) Lag en ikke-deterministisk endelig tilstandsautomat (NFA) som beskriver dette språket.

Kap.4, del 2: Top Down Parsering Kap. 5, del 1: Bottom Up Parsing INF5110, 7/ Legger ut en oppgave til kap. 4 (se beskjed).

UNIVERSITETET I OSLO

INF2820 Datalingvistikk V2011. Jan Tore Lønning & Stephan Oepen

INF 2820 V2016: Obligatorisk innleveringsoppgave 3

INF5110 V2013 Stoff som i boka står i kap 4, men som er generelt stoff om grammatikker

INF2820 Datalingvistikk V2011. Jan Tore Lønning & Stephan Oepen

Oppgave 1. La G1 være grammatikken med hovedsymbol S og følgende regler:

3/8/2011. I dag. Dynamic Programming. Example. Example FORMELLE EGENSKAPER VED SPRÅK (KAP. 16) Jan Tore Lønning & Stephan Oepen

INF5110 V2012 Kapittel 4: Parsering ovenfra-ned

Kap. 5, del 1: Parsering nedenfra-opp (Bottom up parsing) INF5110. Stein Krogdahl Ifi, UiO

Kap. 4 del I Top Down Parsering INF5110 v2006. Stein Krogdahl Ifi, UiO

INF2820 Datalingvistikk V Gang 13.3 Jan Tore Lønning

Kap.4 del I Top Down Parsering INF5110 v2005. Arne Maus Ifi, UiO

Oppgave 1. Spørsmål 1.1 (10%) Gitt det regulære uttrykket: a((bcd)+(cd))*cd

INF3110 Programmeringsspråk

INF 2820 V2016: Innleveringsoppgave 3 hele

Syntaksanalyse. Skanner (repetisjon) Parsering top-down bottom-up LL(1)-parsering Recursive descent Forutsetninger. IN 211 Programmeringsspråk

INF2820-V2018 Oppgavesett 10 Gruppe 18.4

Kap. 5, del 1: Parsering nedenfra-opp (Bottom-up parsering) INF / Stein Krogdahl Ifi, UiO

INF2820 Datalingvistikk V2016. Jan Tore Lønning

Bottom up parsering (nedenfra-og-opp) Kap. 5 del 1 Intro til parsering nedenfra-og-opp samt LR(0) og SLR(1) grammatikker INF5110 v2006

Eksamen INF2820 Datalingvistikk, H2018, Løsningsforslag

Stoff som i boka står i kap 4, men som er. 10. Februar Ifi, UiO

INF2820 Datalingvistikk V2012. Jan Tore Lønning

Kap.4 del 2 Top Down Parsering INF5110 v2005. Arne Maus Ifi, UiO

INF2820 Datalingvistikk V2015. Jan Tore Lønning

Repetisjon. 1 binærtall. INF3110 Programmeringsspråk. Sist så vi ulike notasjoner for syntaks: Jernbanediagrammer. BNF-grammatikker.

INF2820 Datalingvistikk V gang, Jan Tore Lønning

1/26/2012 LITT PYTHON. INF2820 Datalingvistikk V2012. Hvorfor Pyhton. Python syntaks. Python er objektorientert. Python datatyper.

Syntax/semantics - I INF 3110/ /29/2005 1

Dagens Tema: Grammatikker Kap. 3 i K. C. Louden

Oppgave 1 Vi har gitt følgende grammatikk for noe vi kan kalle speilengelsk :

Oppgave 1 (samlet 40%)

INF2820 Datalingvistikk V gang, Jan Tore Lønning

INF2820 Datalingvistikk V2012. Jan Tore Lønning

Stoff som i boka står i kap 4, men som er

INF2820 Datalingvistikk V Gang 13.4 Jan Tore Lønning

INF2820 Datalingvistikk V Gang 6.4 Jan Tore Lønning

INF2820 Datalingvistikk V2014. Forelesning 4, 6.2 Jan Tore Lønning

INF2820 Datalingvistikk V forelesning, 30.1 Jan Tore Lønning

UNIVERSITETET I OSLO

Oppgave 2. INF5110 oppgave 2 på eksamen v04 med teori. FirstMengder. Arne Maus Ifi. Eks. 4.9 Beregning av First-mengde. terminal

Kap. 4: Ovenfra-ned (top-down) parsering

INF2820 Datalingvistikk V Gang 23.3 Jan Tore Lønning

INF2820 Datalingvistikk V Gang 6.4 Jan Tore Lønning

INF /2, 2015 Kap. 5, Del 2 Stein Krogdahl, Ifi, UiO

INF5110 Kap. 5: Parsering nedenfra-og-opp (Bottom-up parsing) 21/ Stein Krogdahl Ifi, UiO. Angående Oblig 1:

2/6/2012. Begrensninger ved regulære språk. INF2820 Datalingvistikk V2012. Formelle språk som ikke er regulære KONTEKSTFRIE GRAMMATIKKER.

INF 5110, 3. februar Dette foilheftet: Kapittel 3

UNIVERSITETET I OSLO

INF2820 Datalingvistikk V Gang 16.3 Jan Tore Lønning

INF januar Forelesninger fremover:

INF / Kap. 5, Del 2 Stein Krogdahl, Ifi, UiO

Kontekstfrie grammatikker og syntaksanalyse (parsering)

INF5110, 15/ Dagens temaer: Avslutning kap. 4 Begynne på kap. 5 Se på oppgave. Stein Krogdahl, Ifi UiO

INF2820 Datalingvistikk V Gang 9.3 Jan Tore Lønning

Kap. 5, Del 2: SLR(1), LR(1)- og LALR(1)-grammatikker INF5110 V2009

INF2820 Datalingvistikk V2016. Jan Tore Lønning

INF2820 Datalingvistikk V2016. Jan Tore Lønning

INF5110, onsdag 19. februar, Dagens tema: Parsering ovenfra-ned (top-down)

Kap. 4, del I: Top Down Parsering Pluss oppgave til kapittel 3 INF5110 V2008

Dagens Tema: Grammatikker Kap. 3 i K. C. Louden

Kap. 5, del 1: Parsering nedenfra-opp (Bottom-up parsering) 16/ Stein Krogdahl Ifi, UiO

Transkript:

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