LING 388: Language and Computers Sandiway Fong Lecture 13: 10/10.

Slides:



Advertisements
Similar presentations
Artificial Intelligence: Natural Language and Prolog
Advertisements

CSA2050: DCG I1 CSA2050 Introduction to Computational Linguistics Lecture 8 Definite Clause Grammars.
LING 388: Language and Computers Sandiway Fong Lecture 5: 9/5.
LING 388: Language and Computers Sandiway Fong Lecture 5: 9/8.
CSA4050: Advanced Topics in NLP Semantics IV Partial Execution Proper Noun Adjective.
Grammars, constituency and order A grammar describes the legal strings of a language in terms of constituency and order. For example, a grammar for a fragment.
Grammars, Languages and Parse Trees. Language Let V be an alphabet or vocabulary V* is set of all strings over V A language L is a subset of V*, i.e.,
LING 388: Language and Computers Sandiway Fong Lecture 2: 8/24.
LING 388: Language and Computers Sandiway Fong Lecture 2: 8/25.
LING 364: Introduction to Formal Semantics Lecture 8 February 7th.
LING 438/538 Computational Linguistics Sandiway Fong Lecture 7: 9/12.
LING 388 Language and Computers Lecture 2 9/04/03 Sandiway FONG.
LING 364: Introduction to Formal Semantics Lecture 9 February 9th.
LING 438/538 Computational Linguistics Sandiway Fong Lecture 9: 9/21.
LING 364: Introduction to Formal Semantics
LING/C SC/PSYC 438/538 Computational Linguistics Sandiway Fong Lecture 7: 9/11.
LING 388 Language and Computers Lecture 8 9/25/03 Sandiway FONG.
LING 364: Introduction to Formal Semantics Lecture 4 January 24th.
LING/C SC/PSYC 438/538 Computational Linguistics Sandiway Fong Lecture 6: 9/6.
LING 364: Introduction to Formal Semantics Lecture 11 February 16th.
LING 388: Language and Computers Sandiway Fong Lecture 6: 9/13.
LING 388: Language and Computers Sandiway Fong Lecture 4: 8/31.
LING 388: Language and Computers Sandiway Fong Lecture 3: 8/29.
© 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 Take-Home Final Examination 12/9/03 Sandiway FONG.
LING 364: Introduction to Formal Semantics Lecture 7 February 2nd.
LING 364: Introduction to Formal Semantics Lecture 3 January 19th.
LING 388 Language and Computers Lecture 18 10/30/03 Sandiway FONG.
LING 388: Language and Computers Sandiway Fong Lecture 17: 10/25.
LING 388 Language and Computers Lecture 11 10/7/03 Sandiway FONG.
1 Note As usual, these notes are based on the Sebesta text. The tree diagrams in these slides are from the lecture slides provided in the instructor resources.
LING 438/538 Computational Linguistics Sandiway Fong Lecture 5: 9/5.
Chapter 3: Formal Translation Models
LING 364: Introduction to Formal Semantics Lecture 5 January 26th.
LING 388 Language and Computers Lecture 12 10/9/03 Sandiway FONG.
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 Lecture 8.
LING 388: Language and Computers Sandiway Fong 10/4 Lecture 12.
LING 388: Language and Computers Sandiway Fong Lecture 11.
October 2004csa4050: Semantics II1 CSA4050: Advanced Topics in NLP Semantics II The Lambda Calculus Semantic Representation Encoding in Prolog.
Logic Programming Lecture 6: Parsing, Difference Lists and Definite Clause Grammars.
LING 388: Language and Computers Sandiway Fong Lecture 14 10/11.
LING 388: Language and Computers Sandiway Fong Lecture 17.
LING 388: Language and Computers Sandiway Fong Lecture 4.
LING 388: Language and Computers Sandiway Fong Lecture 7.
CSI 3120, Grammars, page 1 Language description methods Major topics in this part of the course: –Syntax and semantics –Grammars –Axiomatic semantics (next.
A sentence (S) is composed of a noun phrase (NP) and a verb phrase (VP). A noun phrase may be composed of a determiner (D/DET) and a noun (N). A noun phrase.
CS Describing Syntax CS 3360 Spring 2012 Sec Adapted from Addison Wesley’s lecture notes (Copyright © 2004 Pearson Addison Wesley)
LING 388: Language and Computers Sandiway Fong Lecture 10.
October 2005csa3180: Parsing Algorithms 11 CSA350: NLP Algorithms Sentence Parsing I The Parsing Problem Parsing as Search Top Down/Bottom Up Parsing Strategies.
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.
LING 388: Language and Computers Sandiway Fong Lecture 11: 10/4.
1 Chart Parsing Allen ’ s Chapter 3 J & M ’ s Chapter 10.
LING 388: Language and Computers Sandiway Fong Lecture 25.
LING/C SC/PSYC 438/538 Lecture 15 Sandiway Fong. Did you install SWI Prolog?
1 Introduction to Computational Linguistics Eleni Miltsakaki AUTH Fall 2005-Lecture 3.
Parsing and Code Generation Set 24. Parser Construction Most of the work involved in constructing a parser is carried out automatically by a program,
CSC312 Automata Theory Lecture # 26 Chapter # 12 by Cohen Context Free Grammars.
GRAMMARS & PARSING. Parser Construction Most of the work involved in constructing a parser is carried out automatically by a program, referred to as a.
Logic Programming Lecture 6: Parsing and Definite Clause Grammars.
NLP. Introduction to NLP #include int main() { int n, reverse = 0; printf("Enter a number to reverse\n"); scanf("%d",&n); while (n != 0) { reverse =
7.2 Programming Languages - An Introduction to Informatics WMN Lab. Hye-Jin Lee.
Chapter 3 – Describing Syntax CSCE 343. Syntax vs. Semantics Syntax: The form or structure of the expressions, statements, and program units. Semantics:
Natural Language Processing
LING/C SC/PSYC 438/538 Lecture 17 Sandiway Fong.
LING/C SC/PSYC 438/538 Lecture 20 Sandiway Fong.
Lecture 7: Definite Clause Grammars
Presentation transcript:

LING 388: Language and Computers Sandiway Fong Lecture 13: 10/10

2 Administrivia homework 4 –due Thursday This Thursday (12th October) –Computer Lab class –meet in SS 224 –exercises: on writing grammar rules –(no homework)

3 Today’s Topic Grammars –built-in Prolog support for writing grammars

4 What is a grammar? informally –set of rules to parse or “diagram” a sentence example NP = Noun Phrase VP = Verb Phrase

5 What is a grammar? example NP = Noun Phrase VP = Verb Phrase production (or grammar) rule format –LHS → RHS LHS = Left Hand Side → = “expands to” or “rewrites to” RHS = Right Hand Side example –Sentence → NP VP –VP → Verb NP –Verb → took –NP → the man –NP → the book

6 What is a grammar? example –Sentence → NP VP –VP → Verb NP –Verb → took –NP → the man –NP → the book derivation –top-down (one of many) 1.Sentence 2.NP VP 3.NP Verb NP 4.NP took NP 5.the man took NP 6.the man took the book derivation –top-down (alternative) 1.Sentence 2.NP VP 3.the man VP 4.the man Verb NP 5.the man took NP 6.the man took the book

7 What is a grammar? example –Sentence → NP VP –VP → Verb NP –Verb → took –NP → the man –NP → the book derivation –bottom-up (one of many) 1.the man took the book 2.NP took the book 3.NP Verb the book 4.NP Verb NP 5.NP VP 6.Sentence derivation –bottom-up (alternative) 1.the man took the book 2.the man took NP 3.the man Verb NP 4.the man VP 5.NP VP 6.Sentence

8 What is a grammar? example –Sentence → NP VP –VP → Verb NP –Verb → took –NP → the man –NP → the book this grammar can generate more than one sentence examples –the man took the book –#the book took the man# = semantically odd –other sentences? add new rule –Verb → bought examples –the man took the book –the man bought the book –#the book took the man# = semantically odd –#the book bought the man

9 What is a grammar? example –Sentence → NP VP –VP → Verb NP –Verb → took –NP → the man –NP → the book formally: a grammar contains the following 4 things – a set of non-terminal symbols (N) a set of terminal symbols (T) production rules (P) of the form a designated start symbol (S) example –Non-terminals: {Sentence,VP,NP,Verb} –Terminals: {the,man,book,took} –Start symbol: Sentence –Production rules:set of LHS → RHS rules

10 Prolog Grammar Rules Good news! –Prolog supports grammar rules –it knows how to interpret them (directly) –it can use the grammar rules to construct a derivation automatically Prolog’s computation rule implies a top-down, left-to-right derivation

11 Prolog Grammar Rules Prolog’s version of grammar rules = Definite Clause Grammar (DCG) Prolog ’ s format –terminals and non-terminal symbols both begin with lowercase letters e.g. sentence, vp, np, book, took Note: variables begin with an uppercase letter (or underscore) –--> is the rewrite symbol –terminals are enclosed in square brackets to distinguish them from non- terminals (list notation) e.g. [the], [book], [took] list of terminals are possible, e.g. [good,luck] –comma (, ) is the concatenation symbol –semicolon ( ; ) is the disjunction symbol –a period (. ) is required at the end of all DCG rules

12 Prolog Grammar Rules example –Sentence → NP VP –VP → Verb NP –Verb → took –NP → the man –NP → the book Prolog DCG version –sentence --> np, vp. –vp --> verb, np. –verb --> [took]. –np --> [the], [man]. –np --> [the], [book]. Important Note –don’t enter these rules into the database using assert/1. –Must use a file and consult it. reason: Prolog needs to translate DCG rules into underlying (regular) Prolog rules

13 Prolog Grammar Rules example –sentence --> np, vp. –vp --> verb, np. –verb --> [took]. –np --> [the], [man]. –np --> [the], [book]. query format ?- sentence(S,[]). S = sentence (as a list) [] = empty list i.e. call the start symbol as a predicate and supply two arguments, a list and an empty list

14 Prolog Grammar Rules example –sentence --> np, vp. –vp --> verb, np. –verb --> [took]. –np --> [the], [man]. –np --> [the], [book]. example queries ?- sentence([the,man,took,the,book],[]). Yes “the man took the book” is a member of the language generated by the grammar ?- sentence([man,took,the,book],[]). No “man took the book” is not in the grammar “man took the book” is not generated by the grammar

15 Prolog Grammar Rules example –sentence --> np, vp. –vp --> verb, np. –verb --> [took]. –np --> [the], [man]. –np --> [the], [book]. other queries ?- sentence([the,man,took,X,book],[]). X = the ?- sentence(S,[]). S = [the, man, took, the, man] ; S = [the, man, took, the, book] ; S = [the, book, took, the, man] ; S = [the, book, took, the, book] ; No

16 Prolog Grammar Rules example –sentence --> np, vp. –vp --> verb, np. –verb --> [took]. –np --> [the], [man]. –np --> [the], [book]. notes –np --> [the,man]. OK –np --> [the,book]. OK more grammar det = determiner –np --> det, [man]. –np --> det, [book]. –det --> [the]. –det --> [a].

17 Prolog Grammar Rules example –sentence --> np, vp. –vp --> verb, np. –verb --> [took]. –np --> det, [man]. –np --> det, [book]. –det --> [the]. –det --> [a]. query ?- sentence(S,[]). generates 16 different answers for S 2 choices for deta, the 2 choices for head nounman, book total of 4 different choices for NP (a|(the))((man)|(book)) 2 choices for NPas subject, as object total = 4 2 = 16

18 Prolog Grammar Rules example –sentence --> np, vp. –vp --> verb, np. –verb --> [took]. –np --> det, [man]. –np --> det, [book]. –det --> [the]. –det --> [a]. query ?- sentence([the,man,took|L],[]). L = [the, man] ; L = [a, man] ; L = [the, book] ; L = [a, book] ; No 4 choices

19 Prolog Grammar Rules example –sentence --> np, vp. –vp --> verb, np. –verb --> [took]. –np --> det, [man]. –np --> det, [book]. –det --> [the]. –det --> [a]. query ?- sentence([X,man,took,X,book],[]). X = the ; X = a ; No 2 choices

20 Prolog Grammar Rules Prolog uses the same computation rule for DCG rules –we pattern-match against the database from the 1st rule on down example –sentence --> np, vp. –vp --> verb, np. –verb --> [took]. –np --> det, [man]. –np --> det, [book]. –det --> [the]. –det --> [a]. listing –vp(A, B) :- – verb(A, C), – np(C, B). –np(A, B) :- – det(A, C), – 'C'(C, man, B). –np(A, B) :- – det(A, C), – 'C'(C, book, B). –verb([took|A], A). –det([the|A], A). –det([a|A], A). –sentence(A, B) :- – np(A, C), – vp(C, B).

21 Prolog Grammar Rules derivation ?- sentence([the,man,took,the,book],[]). Call: (7) sentence([the, man, took, the, book], []) ? creep Call: (8) np([the, man, took, the, book], _G353) ? creep Call: (9) det([the, man, took, the, book], _G353) ? creep Exit: (9) det([the, man, took, the, book], [man, took, the, book]) ? creep Call: (9) 'C'([man, took, the, book], man, _G357) ? creep Exit: (9) 'C'([man, took, the, book], man, [took, the, book]) ? creep Exit: (8) np([the, man, took, the, book], [took, the, book]) ? creep Call: (8) vp([took, the, book], []) ? creep Call: (9) verb([took, the, book], _G353) ? creep Exit: (9) verb([took, the, book], [the, book]) ? creep Call: (9) np([the, book], []) ? creep Call: (10) det([the, book], _G353) ? creep Exit: (10) det([the, book], [book]) ? creep Call: (10) 'C'([book], man, []) ? creep Fail: (10) 'C'([book], man, []) ? creep Redo: (10) det([the, book], _G353) ? creep Fail: (10) det([the, book], _G353) ? creep Redo: (9) np([the, book], []) ? creep Call: (10) det([the, book], _G353) ? creep Exit: (10) det([the, book], [book]) ? creep Call: (10) 'C'([book], book, []) ? creep Exit: (10) 'C'([book], book, []) ? creep Exit: (9) np([the, book], []) ? creep Exit: (8) vp([took, the, book], []) ? creep Exit: (7) sentence([the, man, took, the, book], []) ? creep Yes listing –vp(A, B) :- – verb(A, C), – np(C, B). –np(A, B) :- – det(A, C), – 'C'(C, man, B). –np(A, B) :- – det(A, C), – 'C'(C, book, B). –verb([took|A], A). –det([the|A], A). –det([a|A], A). –sentence(A, B) :- – np(A, C), – vp(C, B).