Logic Programming Lecture 6: Parsing, Difference Lists and Definite Clause Grammars.

Slides:



Advertisements
Similar presentations
Artificial Intelligence: Natural Language and Prolog
Advertisements

CSA2050: DCG I1 CSA2050 Introduction to Computational Linguistics Lecture 8 Definite Clause Grammars.
© Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 6: More Lists Theory –Define append/3, a predicate for concatenating two lists, and illustrate.
© Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 6: More Lists Theory –Define append/3, a predicate for concatenating two lists, and illustrate.
Cs7120 (Prasad)L21-DCG1 Definite Clause Grammars
Translator Architecture Code Generator ParserTokenizer string of characters (source code) string of tokens abstract program string of integers (object.
Logic Programming Lecture 3: Recursion, lists, and data structures.
1 Grammars and Parsing Allen ’ s Chapters 3, Jurafski & Martin ’ s Chapters 8-9.
DEFINITE CLAUSE GRAMMARS Ivan Bratko University of Ljubljana Faculty of Computer and Information Sc.
For Monday Read Chapter 23, sections 3-4 Homework –Chapter 23, exercises 1, 6, 14, 19 –Do them in order. Do NOT read ahead.
LING 388: Language and Computers Sandiway Fong Lecture 9: 9/22.
LING 438/538 Computational Linguistics Sandiway Fong Lecture 7: 9/12.
C. Varela; Adapted w/permission from S. Haridi and P. Van Roy1 Declarative Computation Model Defining practical programming languages Carlos Varela RPI.
Context-Free Parsing. 2/37 Basic issues Top-down vs. bottom-up Handling ambiguity –Lexical ambiguity –Structural ambiguity Breadth first vs. depth first.
LING 438/538 Computational Linguistics Sandiway Fong Lecture 22: 11/15.
LING/C SC/PSYC 438/538 Computational Linguistics Sandiway Fong Lecture 6: 9/6.
Amirkabir University of Technology Computer Engineering Faculty AILAB Parsing Ahmad Abdollahzadeh Barfouroush Aban 1381 Natural Language Processing Course,
© Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 7: Definite Clause Grammars Theory –Introduce context free grammars and some related concepts.
© Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 7: Definite Clause Grammars Theory –Introduce context free grammars and some related concepts.
LING 438/538 Computational Linguistics Sandiway Fong Lecture 6: 9/7.
LING 388 Language and Computers Lecture 10 10/2/03 Sandiway FONG.
January 14, 2015CS21 Lecture 51 CS21 Decidability and Tractability Lecture 5 January 14, 2015.
Parsing — Part II (Ambiguity, Top-down parsing, Left-recursion Removal)
Chapter 3: Formal Translation Models
LING 364: Introduction to Formal Semantics Lecture 5 January 26th.
LING 388: Language and Computers Sandiway Fong Lecture 13: 10/10.
LING 388 Language and Computers Lecture 6 9/18/03 Sandiway FONG.
LING 388 Language and Computers Lecture 15 10/21/03 Sandiway FONG.
Context-Free Grammar CSCI-GA.2590 – Lecture 3 Ralph Grishman NYU.
LING 388: Language and Computers Sandiway Fong 10/4 Lecture 12.
PPL Syntax & Formal Semantics Lecture Notes: Chapter 2.
Compiler Construction 1. Objectives Given a context-free grammar, G, and the grammar- independent functions for a recursive-descent parser, complete the.
Prolog for Linguists Symbolic Systems 139P/239P John Dowding Week 7, November 12, 2001
Lecture 21: Languages and Grammars. Natural Language vs. Formal Language.
ITEC 380 Organization of programming languages Lecture 2 – Grammar / Language capabilities.
October 2008csa3180: Setence Parsing Algorithms 1 1 CSA350: NLP Algorithms Sentence Parsing I The Parsing Problem Parsing as Search Top Down/Bottom Up.
1 CPE 480 Natural Language Processing Lecture 5: Parser Asst. Prof. Nuttanart Facundes, Ph.D.
For Friday Finish chapter 23 Homework: –Chapter 22, exercise 9.
LING 388: Language and Computers Sandiway Fong Lecture 7.
October 2004CSA4050: Semantics III1 CSA4050: Advanced Topics in NLP Semantics III Quantified Sentences.
Parsing Lecture 5 Fri, Jan 28, Syntax Analysis The syntax of a language is described by a context-free grammar. Each grammar rule has the form A.
Notes on Pinker ch.7 Grammar, parsing, meaning. What is a grammar? A grammar is a code or function that is a database specifying what kind of sounds correspond.
Syntax Why is the structure of language (syntax) important? How do we represent syntax? What does an example grammar for English look like? What strategies.
Parsing Introduction Syntactic Analysis I. Parsing Introduction 2 The Role of the Parser The Syntactic Analyzer, or Parser, is the heart of the front.
CSE 425: Syntax II Context Free Grammars and BNF In context free grammars (CFGs), structures are independent of the other structures surrounding them Backus-Naur.
For Wednesday Read chapter 23 Homework: –Chapter 22, exercises 1,4, 7, and 14.
1 Definite Clause Grammars for Language Analysis – A Survey of the Formalism and a Comparison with Augmented Transition Networks 인공지능 연구실 Hee keun Heo.
CSA2050 Introduction to Computational Linguistics Parsing I.
Context Free Grammars CFGs –Add recursion to regular expressions Nested constructions –Notation expression  identifier | number | - expression | ( expression.
CS412/413 Introduction to Compilers and Translators Spring ’99 Lecture 3: Introduction to Syntactic Analysis.
A Programming Languages Syntax Analysis (1)
November 2004csa3050: Sentence Parsing II1 CSA350: NLP Algorithms Sentence Parsing 2 Top Down Bottom-Up Left Corner BUP Implementation in Prolog.
CS 3240 – Chapter 5. LanguageMachineGrammar RegularFinite AutomatonRegular Expression, Regular Grammar Context-FreePushdown AutomatonContext-Free Grammar.
CPSC 422, Lecture 27Slide 1 Intelligent Systems (AI-2) Computer Science cpsc422, Lecture 27 Nov, 16, 2015.
LING/C SC/PSYC 438/538 Lecture 15 Sandiway Fong. Did you install SWI Prolog?
Syntax Analysis – Part I EECS 483 – Lecture 4 University of Michigan Monday, September 17, 2006.
LING/C SC/PSYC 438/538 Lecture 16 Sandiway Fong. SWI Prolog Grammar rules are translated when the program is loaded into Prolog rules. Solves the mystery.
Logic Programming Lecture 6: Parsing and Definite Clause Grammars.
Comp 311 Principles of Programming Languages Lecture 2 Syntax Corky Cartwright August 26, 2009.
Logic Programming Lecture 8: Term manipulation & Meta-programming.
Formal grammars A formal grammar is a system for defining the syntax of a language by specifying sequences of symbols or sentences that are considered.
Logic Programming Lecture 2: Unification and proof search.
LING/C SC/PSYC 438/538 Lecture 17 Sandiway Fong. Last Time Talked about: – 1. Declarative (logical) reading of grammar rules – 2. Prolog query: s(String,[]).
Programming Languages Translator
Compiler Construction
Syntax 2: Parsing Strategy and Active Chart Parsing
CSA2050 Introduction to Computational Linguistics
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
CSA4050: Advanced Topics in NLP
Lecture 7: Definite Clause Grammars
Presentation transcript:

Logic Programming Lecture 6: Parsing, Difference Lists and Definite Clause Grammars

James CheneyLogic ProgrammingOctober 20, 2014 Outline for today Context Free Grammars (review) Parsing in Prolog Definite Clause Grammars

James CheneyLogic ProgrammingOctober 20, 2014 Context Free Grammars A simple context free grammar S ⟶ NP VP NP ⟶ DET N VP ⟶ VI | VT NP DET ⟶ the N ⟶ cat | dog | food VI ⟶ meows | barks VT ⟶ bites | eats

James CheneyLogic ProgrammingOctober 20, 2014 Recognizing grammatical sentences Yes: "the cat meows" "the cat bites the dog" "the dog eats the food" No: "cat the cat cat" "dog bites meows"

James CheneyLogic ProgrammingOctober 20, 2014 Generation example thecatmeows S ⟶ NP VP NP ⟶ DET N VP ⟶ VI | VT NP DET ⟶ the N ⟶ cat | dog | food VI ⟶ meows | barks VT ⟶ bites | eats DETN NP S VP VI

James CheneyLogic ProgrammingOctober 20, 2014 An even simpler context-free grammar T ⟶ c T ⟶ a T b In Prolog: t([c]). t(S) :- t(S1), append([a],S1,S2), append(S2,[b],S).

James CheneyLogic ProgrammingOctober 20, 2014 Using append to parse s(L) :- np(L1), vp(L2), append(L1,L2,L). np(L) :- det(L1), n(L2), append(L1,L2,L). vp(L) :- vi(L) ; vt(L1), np(L2), append(L1,L2,L). det([the]). det([a]). n([cat]). n([dog]). n([food]). vi([meows]). vi([barks]). vt([bites]). vt([eats]).

James CheneyLogic ProgrammingOctober 20, 2014 A better way? Obviously, need to guess when we're generating But we also guess when we're parsing known sequence This is inefficient (lots of backtracking!) Reordering goals doesn't help much.

James CheneyLogic ProgrammingOctober 20, 2014 An idea: Accumulators Want to use input data to guide search Parse using two parameters L,M such that M is a suffix of L L = list "before" parsing item M = "remainder" of list after parsing item

James CheneyLogic ProgrammingOctober 20, 2014 An even simpler context-free grammar (mark II) T ⟶ c T ⟶ a T b In Prolog, using accumulators: t([c|L],L). t([a|L1],M) :- t(L1,[b|M]).

James CheneyLogic ProgrammingOctober 20, 2014 Using accumulator version ?- t(L,[]). L = [c]. L = [a,c,b]. L = [a,a,c,b,b]....

James CheneyLogic ProgrammingOctober 20, 2014 Difference lists A difference list is a pair (t,X) where t is a list term [t 1,...,t n |X] X is a variable Empty difference list: (X,X) Constant difference list: ([a 1,...,a n |X],X) Appending difference lists (t,X) and (u,Y): Simply unify X and u! Yields (t[u/X],Y)

James CheneyLogic ProgrammingOctober 20, 2014 An even simpler context-free grammar (mark III) T ⟶ c T ⟶ a T b In Prolog, using difference lists: t(L,M) :- L = [c|M]. t(L,M) :- L = [a|L1], t(L1,M1), M1 = [b|M].

James CheneyLogic ProgrammingOctober 20, 2014 Definite clause grammars Parsing using difference lists is so useful that Prolog has built-in syntax for it: s --> [c]. s --> [a], S, [b]. translates to: s(L,M) :- L = [c|M]. s(L,M) :- L = [a|L1], s(L1,M1), M1 = [b|M].

James CheneyLogic ProgrammingOctober 20, 2014 DCG syntax Rules of form nonterm --> body Body terms are: terminal lists [t 1,...,t n ] (may be []) nonterminals s,t,u,.... sequential composition body 1, body 2 alternative choice body 1 ; body 2

James CheneyLogic ProgrammingOctober 20, 2014 Using DCG version DCGs translated to difference list implementation So they are used the same way: ?- t(L,[]). L = [c]. L = [a,c,b]. L = [a,a,c,b,b]....

James CheneyLogic ProgrammingOctober 20, 2014 Using DCG version (II) Can also use built-ins phrase/2, phrase/3 ?- phrase(t,L,M). L = [c|M]. L = [a,c,b|M]. ?- phrase(t,L). L = [c]. L = [a,c,b].

James CheneyLogic ProgrammingOctober 20, 2014 Large example revisited s --> np, vp. np --> det, n. vp --> vi ; vt, np. det --> [the] ; [a]. n --> [cat] ; [dog] ; [food]. vi --> [meows] ; [barks]. vt --> [bites] ; [eats].

James CheneyLogic ProgrammingOctober 20, 2014 DCGs with tests DCG clause bodies can also contain tests: {pred} where pred is an arbitrary Prolog goal Example: n --> [Word], {noun(Word)}. noun(dog). noun(cat). noun(food).

James CheneyLogic ProgrammingOctober 20, 2014 DCGs and recursion Left recursion (as usual) leads to nontermination: exp --> exp,[+],exp. Avoid by using right recursion & fallthrough: exp --> simple_exp,[+],exp. exp --> simple_exp.

James CheneyLogic ProgrammingOctober 20, 2014 DCGs with parameters Nonterminals in DCGs can have parameters. t(0) --> [c]. t(succ(N)) --> [a], t(N), [b]. Keeps track of depth of nesting.

James CheneyLogic ProgrammingOctober 20, 2014 DCGs + parameters > CFGs With parameters we can parse non- CFGs: u(N) --> n(N,a), n(N,b), n(N,c). n(0,X) --> []. n(succ(N),X) --> [X], n(N,X).

James CheneyLogic ProgrammingOctober 20, 2014 Parsing with parse trees "the cat meows" S (NP (DET (the), N(cat)), VP (VI (meows)) "the cat bites the dog" S(NP (DET (the), N(cat), VP (VT (bites), NP(DET(the), N(dog))) Can build parse trees using parameters Look for this in a tutorial exercise...

James CheneyLogic ProgrammingOctober 20, 2014 Further reading: LPN, ch more difference list examples and translation to Prolog Next time: Search techniques depth-first, iterative deepening, breadth- first, best-first