130 likes | 224 Vues
Learn about the convenient representation of grammatical relationships through Prolog Definite Clause Grammar (DCG) and its application in natural language processing. Explore parsing examples and syntax rules for working with DCG in Prolog.
E N D
Definite Clause Grammar CS440 Sept 29, 2009
Solution to HW1 • Maximum maximum([N],N):-number(N). maximum([X|List], M):- number(X), maximum(List, N), M is max(X,N).
Solution to HW1 • Depth depth([],1):-!. depth([X|List], M):- depth(X,N1), depth(List, N2), !, M is max(N1+1,N2). depth(_,0).
Definite Clause Grammar • Convenient way to represent grammatical relationship for parsing. • Well used in natural language processing application. • Prolog is well suited for natural language processing. • Eg: [The, cat, chases, the, mouse] Sentence->nounphrase, verbphrase. verbphrase->verb, nounphrase.
GCD -Difference List diffList(X,L,R) :- append(X,R,L). • How to parse : Sentence->nounphrase, verbphrase. 1. generate and test Sentence(X):- append(N,V,X) nounphrase(N), verbphrase(V). 2. difference list – get the part each predicate wants and pass the remainder to next. sentences(S,R):- nounphrase(P, S, R1), R1 = S-P verbphrase(V, R1, R). R = R1 – V
DCG Syntax • --> operator indicates a DCG rule, replacing the normal neck ( :- ) used for Prolog clauses. For example, the sentence grammar rule can be written using a DCG rule: sentence --> subject, verb, object. • Each goal is assumed to refer to the head of a DCG rule, and the preprocessor adds two extra arguments for the difference list. • Curly braces {} are used to isolate normal Prolog goals from the DCG preprocessor. For example: subject --> modifier, noun, {write('found subject')}. • Square brackets [], list notation, are used to indicate terminal symbols of the grammar. For example: noun --> [cat]
GCD -Difference List • Thus, • each goal refers to one predicates head and two argument for different list. • Eg: term(Z) -> number(X), “*”, number(Y), {Z is X*Y} • Will be • term(Z, S, R):- • number(X,S,R1), number(Y,R1,R), Z is X*Y
Single Digit Calculus • Eg: 3*4+5*8+6*3 • expr(Z) --> term(X), "+", expr(Y), {Z is X + Y}. expr(Z) --> term(Z). term(Z) --> number(X), "*", term(Y), {Z is X * Y}. term(Z) --> number(Z). number(X) --> [C], {"0"=<C, C=<"9", X is C - "0"}.
Single Digit Calculus • expr(A, B, C) :- term(D, B, E), E=[43|F], expr(G, F, H), A is D+G, C=H. • expr(A, B, C) :- term(A, B, C).
Single Digit Calculus • term(A, B, C) :- number(D, B, E), E=[42|F], term(G, F, H), A is D*G, C=H. • term(A, B, C) :- number(A, B, C).
Single Digit Calculus • number(A, B, C) :- B=[D|E], [48]=<D, D=<[57], A is D-[48], C=E.
Predicates Call • expr(Z,”1*2*4+5”,[]) . Need one extra argument for remainder after parsing. • expr(Z,”1*3*4+50”,R). R?