230 likes | 371 Vues
In this lecture, we explore the fundamental concepts of grammars in language and computing, particularly focusing on Definite Clause Grammar (DCG) and its implementation in Prolog. We discuss what constitutes a grammar, including rewrite rules for parsing sentences. Using examples, we delve into top-down and bottom-up parsing methods, and illustrate Prolog's built-in support for grammar creation. Additionally, we walk through the process of deriving sentences from non-terminals and the computational steps taken by Prolog during parsing.
E N D
LING 388: Language and Computers Sandiway Fong 10/6 Lecture 13
Last Time • Grammars • what is a grammar? • informally • set of “rewrite” rules to parse or “diagram” a sentence • derivation • top-down (from the sentence symbol), bottom-up (from the words) • Definite Clause Grammar (DCG) System • built-in Prolog support for writing grammars • --> is the rewrite symbol • [book], [took] terminals are enclosed in brackets • sentence, vp, npterminals and non-terminal symbols begin with lowercase letters Sentence → NP VP VP → Verb NP Verb → took NP → the man NP → the book sentence --> np, vp. vp --> verb, np. verb --> [took]. np --> [the], [man]. np --> [the], [book].
Exercise 1 • example grammar (from last time) • 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
Exercise 1 • Steps: • Put the grammar in a file • Consult it • ?- listing. • (see the underlying Prolog translation of the rules) • Queries ?- sentence([the,man,took,the,book],[]). ?- sentence([the,man|L],[]). ?- sentence(L,[]). ?- sentence([the,X,took,the,Y],[]), \+ X=Y.
?- listing. • Prolog translates DCG rules into ordinary Prolog rules • 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). • example • sentence --> np, vp. • vp --> verb, np. • verb --> [took]. • np --> det, [man]. • np --> det, [book]. • det --> [the]. • det --> [a]. variables A,B,C are all going to be lists 'C'(C, man, B) is true when man is the head of C and B is the tail of C i.e. C = [man|B]
Exercise 2 • Let’s follow the computation steps Prolog uses to prove it can derive a sentence from the starting non-terminal
?- trace. • 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). • 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
np([the,man,took,the,book],C). vp([took,the,book],[]). vp(C,[]). det([the,man,took,the,book],C’). verb([took,the,book],C”). ‘C’(C’,man,C). np(C”,[]). det([the,man,took,the,book],[man,took,the,book]). verb([took,the,book],[the,book]). ‘C’([man,took,the,book],man,C). ‘C’([man,took,the,book],man,[took,the,book]). np([the,book],[]). det([the,book],C”’). det([the,book],C”’). ‘C’(C”’,man,[]). ‘C’(C”’,book,[]). det([the,book],[book]). ‘C’([book],man,[]). ‘C’([book],book,[]). Grammar Computation ?- sentence([the,man,took,the,book],[]). Underlying Prolog 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). DCG rules vp --> verb, np. np --> det,[man]. np --> det, [book]. verb --> [took]. det --> [the]. det --> [a]. sentence --> np, vp. C=[took,the,book] Animation
np([the,man,took,the,book],C). vp(C,[]). det([the,book],[book]). ‘C’([book],book,[]). Grammar Computation Note: the computation tree resembles a parse tree ?- sentence([the,man,took,the,book],[]). verb([took,the,book],C”). np(C”,[]). det([the,man,took,the,book],C’). ‘C’(C’,man,C). verb([took,the,book],[the,book]). det([the,man,took,the,book],[man,took,the,book]). np([the,book],[]). ‘C’([man,took,the,book],man,C). det([the,book],C”’). ‘C’(C”’,book,[]). ‘C’([man,took,the,book],man,[took,the,book]).
Grammar Computation Note: the computation tree resembles a parse tree
Exercise 3 • Getting Prolog to build a representation of the parse tree [S [NP [Det the] man][VP [V took][NP [Det the] book]]] sentence(np(det(the),man),vp(verb(took),np(det(the),book)))
observation ?- trace. computation tree disappears after computation is finished would be nice to keep a parse tree around technique add one argument to each grammar rule to hold part of the parse tree compositionality: combine sub-trees to form larger trees and the complete parse tree Computing Parse Trees
Computing Parse Trees • use a recursive Prolog data structure to represent a parse • examples • data structure:verb(took) • functor = verb • argument = took • data structure:det(the) • functor = det • argument = the • data structure:np(det(the),man) • functor = np • 1st argument = det(the) • functor = det • argument = the • 2nd argument = man (1) verb | took (2) det | the (3) np / \ det man | the
np([the,man,took,the,book],C). vp(C,[]). det([the,book],[book]). ‘C’([book],book,[]). Computing Parse Trees let’s look at how to create a tree for “the book” ?- sentence([the,man,took,the,book],[]). verb([took,the,book],C”). np(C”,[]). det([the,man,took,the,book],C’). ‘C’(C’,man,C). verb([took,the,book],[the,book]). np --> det, [book]. det([the,man,took,the,book],[man,took,the,book]). np([the,book],[]). ‘C’([man,took,the,book],man,C). det([the,book],C”’). ‘C’(C”’,book,[]). ‘C’([man,took,the,book],man,[took,the,book]).
np([the,man,took,the,book],C). vp(C,[]). det(X,[the,book],[book]). ‘C’([book],book,[]). Computing Parse Trees computation tree = parse tree ?- sentence([the,man,took,the,book],[]). X is the extra parameter verb([took,the,book],C”). np(C”,[]). det([the,man,took,the,book],C’). ‘C’(C’,man,C). np --> det, [book]. np(X) --> det(D),[book]. np(np(D,book)) --> det(D),[book]. verb([took,the,book],[the,book]). np --> det, [book]. det([the,man,took,the,book],[man,took,the,book]). det --> [the]. det(X) --> [the]. det(det(the)) --> [the]. np([the,book],[]). ‘C’([man,took,the,book],man,C). np(D,book) det(the) det(X,[the,book],C”’). ‘C’(C”’,book,[]). ‘C’([man,took,the,book],man,[took,the,book]).
Computing Parse Trees • original DCG • sentence --> np, vp. • vp --> verb, np. • verb --> [took]. • np --> det, [man]. • np --> det, [book]. • det --> [the]. • revised DCG • sentence(s(NP,VP)) --> np(NP), vp(VP). • vp(vp(V,NP)) --> verb(V), np(NP). • verb(v(took)) --> [took]. • np(np(D,man)) --> det(D), [man]. • np(np(D,book)) --> det(D), [book]. • det(det(the)) --> [the].
Computing Parse Trees • revised DCG • sentence(s(NP,VP)) --> np(NP), vp(VP). • vp(vp(V,NP)) --> verb(V), np(NP). • verb(v(took)) --> [took]. • np(np(D,man)) --> det(D), [man]. • np(np(D,book)) --> det(D), [book]. • det(det(the)) --> [the]. • query (with an extra argument) • ?- sentence(P,List,[]). • P = parse tree • List = sentence s(np(det(the),man),vp(v(took),np(det(the),man)))
Examples • query • what parse P corresponds to the sentence “the man took the book”? • ?- sentence(P,[the,man,took,the,book],[]). • P = s(np(det(the),man),vp(v(took),np(det(the),book))) ? ; • no • query • what are the possible parses P and word X for the sentence “the man took the X”? • ?- sentence(P,[the,man,took,the,X],[]). • P = s(np(det(the),man),vp(v(took),np(det(the),man))), • X = man ? ; • P = s(np(det(the),man),vp(v(took),np(det(the),book))), • X = book ? ; • no
Examples • query • given a parse, what is the corresponding Sentence? • ?- sentence(s(np(det(the),man),vp(v(took),np(det(the),book))),Sentence,[]). • Sentence = [the,man,took,the,book] ? ; • no • query • supply no information, i.e. Parse and Sentence are both variables, • what are the possible sentences and parses? • ?- sentence(Parse,Sentence,[]). • Parse = s(np(det(the),man),vp(v(took),np(det(the),man))), • Sentence = [the,man,took,the,man] ? ; • Parse = s(np(det(the),man),vp(v(took),np(det(the),book))), • Sentence = [the,man,took,the,book] ? ; • Parse = s(np(det(the),book),vp(v(took),np(det(the),man))), • Sentence = [the,book,took,the,man] ? ; • Parse = s(np(det(the),book),vp(v(took),np(det(the),book))), • Sentence = [the,book,took,the,book] ? ; • no
Exercise 4 • Idiom chunks • kicked the bucket • has both a literal meaning • and is also a VP (verb phrase) idiom • meaning: • died
Exercise 4 • add new rule • “kicked the bucket” is a VP idiom meaning “died” • vp(vp(v(died))) --> [kicked,the,bucket]. • example illustrates the ability to return any parse we like for a given rule • query • what are the possible parses for “the man kicked the bucket”? • ?- sentence(Parse,[the,man,kicked,the,bucket],[]). • Parse = s(np(det(the),man),vp(v(died))) ? ; • no • idiomatic meaning only
Exercise 4 • add new rules • for “kicked” and “bucket” as a verb and noun, respectively • verb(v(kicked)) --> [kicked]. • np(np(D,bucket)) --> det(D), [bucket]. • provides the ability to return the literal parse for “kicked the bucket”
Exercise 4 • query • what are the possible parses for “the man kicked the bucket”? • ?- sentence(Parse,[the,man,kicked,the,bucket],[]). • Parse = s(np(det(the),man),vp(v(kicked),np(det(the),bucket))) ? ; • Parse = s(np(det(the),man),vp(v(died))) ? ; • no • both idiomatic and literal meanings are now possible • which one comes first? is preferred?