1 / 52

DEFINITE CLAUSE GRAMMARS

DEFINITE CLAUSE GRAMMARS. Ivan Bratko University of Ljubljana Faculty of Computer and Information Sc. DCG = Definite Clause Grammar. DCG notation in Prolog Context-free grammars in DCG Introducing context-dependence through arguments Prolog goals in grammars rules

Download Presentation

DEFINITE CLAUSE GRAMMARS

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. DEFINITE CLAUSE GRAMMARS Ivan Bratko University of Ljubljana Faculty of Computer and Information Sc.

  2. DCG = Definite Clause Grammar • DCG notation in Prolog • Context-free grammars in DCG • Introducing context-dependence through arguments • Prolog goals in grammars rules • Defining semantics with DCG • Natural language syntax in DCG • Defining meaning of natural language

  3. A BNF grammar • <s> ::= a b | a <s> b • Grammar generates / recognises • s • a s b • Generation Recognition • a s b • a b • Sentence = a a a b b b

  4. COMMAND SEQUENCES FOR A ROBOT • up • up up down up down • CORRESPONDING BNF GRAMMAR • <move> ::= <step> | <step> <move> • <step> ::= up | down

  5. CORRESPONDING DCG • move --> step. • move --> step, move. • step --> [up]. • step --> [down].

  6. SENTENCES IN DCG • DCG expects a sentence to be represented as difference list • aabb ~ [a,a,b,b], [] • ~ [a,a,b,b,c], [c] • ~ [a,a,b,b,x,y,z], [x,y,z] • ~ ...

  7. QUESTIONS TO DCG • ?- move( [up,down,up], []). • yes • ?- move( [up, X, up], []). • X = up; • X = down; • no • ?- move( [ up, down, up, a, b, c], [ a, b, c]). • yes

  8. PROLOG CONVERTS DCG INTO A RECOGNITION PROGRAM • move( List, Rest) :- • step( List, Rest). • move( List1, Rest) :- • step( List1, List2), • move( List2, Rest). • step( [up | Rest], Rest). • step( [down | Rest], Rest).

  9. FROM DCG TO STANDARD PROLOG • DCG rule: • move --> step, move. • Corresponding Prolog rule: • move( List1, Rest) :- • step( List1, List2), • move( List2, Rest).

  10. FROM DCG TO STANDARD PROLOG • move( List1, Rest) :- • step( List1, List2), • move( List2, Rest). • move • step move • Rest • List2 • List1

  11. A SIMPLE NATURAL LANGUAGE DCG • The cat scares the mouse. • det noun verb det noun • noun_phrase noun_phrase • verb_phrase • sentence

  12. A SIMPLE NATURAL LANGUAGE DCG • sentence --> noun_phrase, verb_phrase. • verb_phrase --> verb, noun_phrase. • noun_phrase --> determiner, noun. • determiner --> [ the]. • noun --> [ cat]. • noun --> [ cats]. • noun --> [ mouse]. • verb --> [ scares]. • verb --> [ scare].

  13. THIS GRAMMAR GENERATES • [ the, cat, scares, the, mouse] • [ the, mouse, scares, the, mouse] • [ the, cats, scare, the, mouse] • [ the, cats, scares, the, mouse] • CONTEXT DEPENDENT!

  14. NUMBER AGREEMENT CAN BE FORCEDBY ARGUMENTS • sentence( Number) --> • noun_phrase( Number), verb_phrase( Number). • verb_phrase( Number) --> • verb( Number), noun_phrase( Number1). • noun_phrase( Number) --> • determiner( Number), noun( Number). • noun( singular) --> [ mouse]. • noun( plural) --> [ mice]. • verb( singular) --> [scares]. • verb( plural) --> [scare].

  15. NUMBER AGREEMENT CAN BE FORCEDBY ARGUMENTS • ?- sentence( Number, [the, cats, scares, the, mouse], []). • no • ?- sentence( Number, [the, cats, scare, the, mouse], []). • Number = plural

  16. CONSTRUCTING PARSE TREES • [ up, down, up] • move • step move • up step move • down step • up

  17. DCG TO CONSTRUCT PARSE TREES • Tree = move( step(up), move( step(down), ...)) • move( move( Step)) --> step( Step). • move( move( Step, Move)) --> • step( Step), move( Move). • step( step( up)) --> [up]. • step( step( down)) --> [down].

  18. DEFINING MEANING • Define: Meaning of move = Distance travelled • meaning ( ‘up up down up’) = 1+1-1+1 = 2 • meaning( ‘up up down up’) = • meaning( ‘up’) + meaning( ‘up down up’)

  19. FROM PARSE TREE TO MEANING • To find meaning of sentence: • (1) Construct parse tree • (2) Process parse tree

  20. MEANING OF MOVES TREE • meaning( move( Step, Move), Dist) :- • meaning( Step, D1), • meaning( Move, D2), • Dist is D1 + D2. • meaning( step( up), 1). • meaning( step( down), -1).

  21. INTERLEAVING SYNTAX AND MEANING • sentence parse tree meaning • Avoid parse tree, encode meaning directly in DCG

  22. PROLOG goals in DCG: {Goal} • move( Dist) --> step( Dist). • move( Dist) --> • step( D1), move( D2), {Dist is D1 + D2}. • step( 1) --> [ up]. • step( -1) --> [ down]. • ?- move( D, [ up, up, down, up], [ ] ). • D = 2

  23. MEANING OF NATURAL LANGUAGE • Representation of meaning = ? • Depends on use of meaning, • e.g. natural language querying • Logic is a good candidate for representing meaning

  24. SOME MEANINGS IN LOGIC • Sentence Formalised meaning • ``John paints’’ paints( john) • ``John likes Annie’’ likes( john, annie)

  25. SOME MEANINGS IN LOGIC • Sentence • ``A man paints’’ • Formalised meaning • exists( X, man(X) and paints(X)) • Note: ``paints’’ is intransitive verb, ``likes’’ is trans. verb

  26. A SYNTAX • sentence ---> noun_phrase, verb_phrase. • noun_phrase --> proper_noun. • verb_phrase --> intrans_verb. • verb_phrase --> trans_verb, noun_phrase. • intrans_verb --> [ paints]. • trans_verb --> [ likes]. • proper_noun --> [ john]. • ...

  27. INCORPORATING MEANING • % “john’’ means “john’’ • proper_noun( john) --> [ john]. • % “paints’’ means “paints( X)’’ • intrans_verb( paints( X)) --> [ paints].

  28. COMBINING MEANING OF NOUN PHRASE AND VERB PHRASE • “John paints” • sentence • noun_phrase verb_phrase • proper_noun intrans_verb • john paints

  29. COMBINING MEANING OF NOUN PHRASE AND VERB PHRASE • paints( john) • sentence • johnnoun_phrase verb_phrasepaints( X) • john proper_noun intrans_verbpaints( X) • john paints

  30. COMBINING MEANING OF NOUN PHRASE AND VERB PHRASE • john + paints( X) = paints( john) • Meaning of NP Meaning of sentence • Meaning of VP

  31. COMBINING OF MEANING OF VERB PHRASE AND NOUN PHRASE • noun_phrase( NP) --> proper_noun( NP). • verb_phrase( VP) --> intrans_verb( VP). • sentence( S) ---> • noun_phrase( NP), verb_phrase( VP), • { compose( NP, VP, S) } .

  32. COMPOSING NP AND VP MEANINGS • actor( paints( X), X). • compose( NP, VP, VP) :- % Meaning of sentence is VP • actor( VP, NP). % Where actor in VP is NP • ?- compose( john, paints( X), S). • S = paints( john)

  33. MORE ELEGANT • DEFINE MEANING OF TRANSITIVE VERB • SO THAT ACTOR BECOMES EXTRA ARGUMENT • intrans_verb( Actor, paints( Actor)) --> [ paints]. • verb_phrase( Actor, VP) --> intrans_verb( Actor, VP). • sentence( VP) ---> • noun_phrase( Actor), verb_phrase( Actor, VP).

  34. This makes “slots” in meaning “visible” • paints( Actor) • Slot to be filled by context

  35. MEANING OF TRANSITIVE VERBS • “likes” means likes( Somebody, Something) • trans_verb( Somebody, Something, likes(Somebody,Something)) --> • [ likes]. • verb_phrase( Somebody, VP) --> • trans_verb( Somebody, Something, VP), • noun_phrase( Something).

  36. MEANING OF DETERMINERS “A” AND “EVERY” • “A man paints” means: • exists( X, man( X) and paints( X) ) • Determiner “a” dictates the meaning of whole sentence! • “a man” means: • exists( X, man( X) and Assertion) • Statement about X

  37. MEANING OF “A” • “a” means: • There is some X such that: • X has some property and • some assertion holds about X. • exists( X, Property and Assertion) • Make X, Property and Assertion visible: • determiner( X, Property, Assn, exists( X, Property and Assn)) --> • [ a].

  38. INTEGRATING MEANING OF DETERMINERS AND OTHER PHRASES • sentence • noun_phrase verb_phrase • determiner noun intrans_verb • a man paints exists( X, man(X) and paints(X) ) paints(X) exists( X, man(X) and A) exists( X, P and A) man(X) paints(X)

  39. INTEGRATING MEANING OF DETERMINERS AND OTHER PHRASES • sentence( S) --> • noun_phrase( X, Assn, S), verb_phrase( X, Assn). • noun_phrase( X, Assn, S) --> • determiner( X, Prop, Assn, S), noun( X, Prop). • noun_phrase( X, Assn, Assn) --> • proper_noun( X).

  40. INTEGRATING MEANING OF DETERMINERS AND OTHER PHRASES • verb_phrase( X, Assn) --> • intrans_verb( X, Assn). • determiner( X, Prop, Assn, exists( X, Prop and Assn)) --> [a]. • noun( X, man( X)) --> [ man]. • intrans_verb( X, paints(X)) --> [paints].

  41. DETERMINER “EVERY” • “Every woman dances” means: • all( X, woman( X) ==> dances( X) ) • determiner( X, Prop, Assn, all( X, Prop ==> Assn ) ) • --> [ every].

  42. RELATIVE CLAUSES • “Every man that paints admires Monet” • SYNTAX: • noun_phrase --> determiner, noun, rel_clause. • rel_clause --> [ that], verb_phrase. • rel_clause --> [ ]. % Empty relative clause

  43. RELATIVE CLAUSES • “Every man that paints admires Monet” • MEANING: • all( X, man( X) and paints( X) ==> admires( X, monet) )

  44. RELATIVE CLAUSES • MEANING, GENERAL FORM • all( X, Prop1 and Prop2 ==> Assertion ) • noun verb phrase verb phrase • of rel. clause of sentence

  45. RELATIVE CLAUSES: SYNTAX AND MEANING • rel_clause( X, Prop1, Prop1 and Prop2) --> • [ that ], verb_phrase( Prop2). • noun_phrase( X, Assn, S) --> • determiner( X, Prop12, Assn, S), noun( X, Prop1), • rel_clause( X, Prop1, Prop12). • rel_clause( X, Prop1, Prop1) --> [ ].

  46. COMPLETE GRAMMAR • % Meaning of subset of natural language • :- op( 100, xfy, and). • :- op( 150, xfy, =>). • sentence( S) --> • noun_phrase( X, P, S), verb_phrase( X, P). • noun_phrase( X, P, S) --> • determiner( X, P12, P, S), noun( X, P1), rel_clause( X, P1, P12). • noun_phrase( X, P, P) --> • proper_noun( X). • verb_phrase( X, P) --> • trans_verb( X, Y, P1), noun_phrase( Y, P1, P). • verb_phrase( X, P) --> • intrans_verb( X, P).

  47. COMPLETE GRAMMAR, CTD. • rel_clause( X, P1, P1 and P2) --> • [that], verb_phrase( X, P2). • rel_clause( X, P1, P1) --> []. • determiner( X, P1, P, all( X, P1 => P)) --> [every]. • determiner( X, P1, P, exists( X, P1 and P)) --> [a]. • noun( X, man(X)) --> [man]. • noun( X, woman(X)) --> [woman]. • proper_noun( john) --> [john]. • proper_noun( annie) --> [annie]. • proper_noun( monet) --> [monet]. • trans_verb( X, Y, likes( X, Y)) --> [ likes]. • trans_verb( X, Y, admires( X, Y)) --> [admires]. • intrans_verb( X, paints(X)) --> [paints].

  48. SOME TEST SENTENCES • % Some tests • test1( M) :- • sentence( M, [john,paints],[]). • test2( M) :- • sentence( M, [a, man, paints], []). • test3( M) :- • sentence( M, [every,man,that,paints,admires,monet],[]). • test4( M) :- • sentence( M, [annie,admires,every,man,that,paints],[]). • test5( M) :- • sentence( M, [every,woman,that,admires,a,man,that,paints,likes,monet],[]).

  49. EXAMPLES OF SENTENCES THIS GRAMMAR HANDLES • “Every man that paints admires Monet” • “Annie admires every man that paints” • “Every woman that admires a man that paints likes Monet” • Meaning constructed for this sentence: • all( X, woman( X) and exists( Y, ( man( Y) and paints (Y)) • and admires( X, Y) ) ==> likes( X, monet ) )

  50. USE IN QUESTION ANSWERING • Meanings in logic can be translated into Prolog: • admires( X, monet) :- • man( X), paints( X). • admires( annie, X) :- • man( X), paints( X). • likes( X, monet) :- • woman( X), man( Y), paints( Y), admires( X, Y). • “Does Annie admire anybody who admires Monet?” • ?- admires( annie, X), admires( X, monet).

More Related