Prolog programming: a do-it-yourself course for beginners Day 4 - - PowerPoint PPT Presentation

prolog programming a do it yourself course for beginners
SMART_READER_LITE
LIVE PREVIEW

Prolog programming: a do-it-yourself course for beginners Day 4 - - PowerPoint PPT Presentation

Prolog programming: a do-it-yourself course for beginners Day 4 Kristina Striegnitz Department of Computational Linguistics Saarland University, Saarbr ucken, Germany kris@coli.uni-sb.de http://www.coli.uni-sb.de/kris Day 4: Definite


slide-1
SLIDE 1

Prolog programming: a do-it-yourself course for beginners

Day 4

Kristina Striegnitz Department of Computational Linguistics Saarland University, Saarbr¨ ucken, Germany

kris@coli.uni-sb.de http://www.coli.uni-sb.de/˜kris

Day 4: Definite Clause Grammars (1) – p.1

slide-2
SLIDE 2

Day 4: Definite Clause Grammars (1)

Today: Grammars as Prolog programms for recognizing natural language sentences. Reader: Lecture 7 of Learn Prolog Now!

Day 4: Definite Clause Grammars (1) – p.2

slide-3
SLIDE 3

Today’s goal: a NL recognizer

A Prolog programm for the following task: Given

  • a grammar specifi cation,
  • a list of words, and
  • a syntactic category C,

is the list of words a grammatical expression of category C?

Day 4: Definite Clause Grammars (1) – p.3

slide-4
SLIDE 4

Some examples

S → NP VP NP → Det N NP → PN VP → Vi VP → Vt NP Det → the Det → a PN → gogo N → nurse N → whiskey Vi → whistles Vt → drinks catgory: S words: [the,nurse,whistles] ➜ yes category: NP words: [the,whiskey] ➜ yes category: NP words: [drinks] ➜ no category: VP words: [fights] ➜ no

Day 4: Definite Clause Grammars (1) – p.4

slide-5
SLIDE 5

Strategy

For each syntactic category C, defi ne a predicate c(InList,OutList) which takes a list of words (InList) as input, “bites off” a sequence of words corresponding to an expression of category C and returns the rest (OutList).

Day 4: Definite Clause Grammars (1) – p.5

slide-6
SLIDE 6

Examples

?- np([the,nurse,whistles],Out). Out = [whistles] ?- np([the,whiskey],Out). Out = [] ?- vt([drinks,the,whiskey],Out). Out = [the,whiskey] ?- vp([nurse,whistles],Out). no

Day 4: Definite Clause Grammars (1) – p.6

slide-7
SLIDE 7

Definite Clause Grammars (DCGs) — words

Let’s start with single words: n([bride|Out],Out). n([nurse|Out],Out). n([sword|Out],Out). det([the|Out],Out). pn([bill|Out],Out). vt([kills|Out],Out). . . . If the head of the input list is the word bride, then we have found a noun. Return the tail of the list.

Day 4: Definite Clause Grammars (1) – p.7

slide-8
SLIDE 8

DCGs — complex categories

Now, we can build more complex categories: np(In,Out) :- det(In,DetOut), n(DetOut,Out). If we can bite a determiner off the list and then bite a noun of the list, then we have found an NP . Return what’s left when biting off the determiner and the noun. np(In,Out) :- pn(In,Out). If we can bite a proper name off the list, then we have found an NP . Return what’s left when biting off the proper name. vp(In,Out) :- vi(In,Out). vp(In,Out) :- vt(In,VtOut), np(VtOut,Out). s(In,Out) :- np(In,NPOut), vp(NPOut,Out).

Day 4: Definite Clause Grammars (1) – p.8

slide-9
SLIDE 9

How does this work?

s([the,bride,kills,bill],[]) np([the,bride,kills,bill], I1) vp(I1,[]) det([the,bride,kills,bill],I2) n(I2,I1) vp(I1,[]) vp(I1,[]) n([bride,kills,bill],I1) vp([kills,bill],[]) vi([kills,bill],[]) vt([kills,bill],I3) np(I3,[]) det([bill],I4) n(I4,[]) np([bill],[]) pn([bill],[]) I2=[bride,kills,bill] I1=[kills,bill] I3=[bill]

s(In,Out) :- np(In,NPOut), vp(NPOut,Out). np(In,Out) :- det(In,DetOut), n(DetOut,Out). np(In,Out) :- pn(In,Out). vp(In,Out) :- vi(In,Out). vp(In,Out) :- vt(In,VtOut), np(VtOut,Out). det([the|Out],Out). . . .

Day 4: Definite Clause Grammars (1) – p.9

slide-10
SLIDE 10

DCGs in Prolog

Prolog provides a simpler notation for specifying DCGs.

s --> np, vp.

s(In,Out) :- np(In,NPOut), vp(NPOut,Out). np --> det, n.

np(In,Out) :- det(In,DetOut), n(DetOut,Out). np --> pn.

np(In,Out) :- pn(In,Out). vp --> vi.

vp(In,Out) :- vi(In,Out). vp --> vt, np.

vp(In,Out) :- vt(In,VtOut), np(VtOut,Out). n --> [bride].

n([bride|Out],Out). det --> [the].

det([the|Out],Out). vi --> [whistles].

vi([whistles|Out],Out). vt --> [kills].

vt([kills|Out],Out).

Internally, Prolog uses this notation. Therefore: ?- s([the,nurse,whistles],[]). to ask whether [the,nurse,whistles] is a sentence.

Day 4: Definite Clause Grammars (1) – p.10

slide-11
SLIDE 11

Adding Pronouns

Here is our DCG:

s(In,Out) :- np(In,NPOut), vp(NPOut,Out). np(In,Out) :- pn(In,Out). np(In,Out) :- det(In,DetOut), n(DetOut,Out). vp(In,Out) :- vi(In,Out). vp(In,Out) :- vt(In,VtOut), np(VtOut,Out). n([bride|Out],Out). det([the|Out],Out). pn([bill|Out],Out). vt([kills|Out],Out). vi([whistles|Out],Out).

Imagine we want to add the pronouns he, she, him, her...

Day 4: Definite Clause Grammars (1) – p.11

slide-12
SLIDE 12

Adding Pronouns — first try

s(In,Out) :- np(In,NPOut), vp(NPOut,Out). np(In,Out) :- pn(In,Out). np(In,Out) :- det(In,DetOut), n(DetOut,Out). vp(In,Out) :- vi(In,Out). vp(In,Out) :- vt(In,VtOut), np(VtOut,Out). n([bride|Out],Out). det([the|Out],Out). pn([bill|Out],Out). vt([kills|Out],Out). vi([whistles|Out],Out). np(In,Out) :- pro(In,Out). pro([he|Out],Out). pro([she|Out],Out). pro([him|Out],Out). pro([her|Out],Out). ?- s([she,kills,him],[]). yes ?- s([him,kills,she],[]). no

Need to distinguish between subject and object pronouns.

Day 4: Definite Clause Grammars (1) – p.12

slide-13
SLIDE 13

Adding pronouns — second try

We use an extra argument to mark whether an NP or a pronoun is in subject or in object position. s(In,Out) :- np(subj,In,NPOut),vp(NPOut,Out). vp(In,Out) :- vt(In,VtOut),np(obj,VtOut,Out). np(CASE,In,Out) :- pro(CASE,In,Out). pro(subj,[he|Out],Out). pro(subj,[she|Out],Out). pro(obj,[him|Out],Out). pro(obj,[her|Out],Out). np( ,In,Out) :- pn(In,Out). np( ,In,Out) :- det(In,DetOut), n(DetOut,Out).

Day 4: Definite Clause Grammars (1) – p.13

slide-14
SLIDE 14

Example

s([her,kills,bill],[]) vp(I1,[]) np(subj,[her,kills,bill], I1) vp(I1,[]) pn([her,kills,bill],I1) n(I2,I1) vp(I1,[]) det([her,kills,bill],I2) vp(I1,[]) pro(subj,[her,kills,bill],I1)

s(In,Out) :- np(subj,In,NPOut),vp(NPOut,Out). np( ,In,Out) :- det(In,DetOut),n(DetOut,Out). np( ,In,Out) :- pn(In,Out). np(CASE,In,Out) :- pro(CASE,In,Out). vp(In,Out) :- vi(In,Out). vp(In,Out) :- vt(In,VtOut),np(obj,VtOut,Out). pro(subj,[he|Out],Out). pro(obj,[her|Out],Out). . . .

Day 4: Definite Clause Grammars (1) – p.14

slide-15
SLIDE 15

Extra arguments in the --> notation

s --> np( subj), vp.

s(In,Out) :- np(subj,In,NPOut), vp(NPOut,Out). np( ) --> det, n.

np( ,In,Out) :- det(In,DetOut), n(DetOut,Out). np( ) --> pn.

np( ,In,Out) :- pn(In,Out). np(CASE) --> pro(CASE).

np(CASE,In,Out) :- pro(CASE,In,Out). vp --> vi.

vp(In,Out) :- vi(In,Out). vp --> vt, np(obj).

vp(In,Out) :- vt(In,VtOut), np(obj,VtOut,Out).

To query: ?- s([the,nurse,whistles],[]). ?- np( ,[the,bride],[]).

Day 4: Definite Clause Grammars (1) – p.15

slide-16
SLIDE 16

Practical Session

Write your own DCG.

http://www.coli.uni-sb.de/˜kris/esslli04prolog

Day 4: Definite Clause Grammars (1) – p.16