ling 388 language and computers lecture 15 10/21/03 sandiway fong

23
LING 388 Language and Computers Lecture Lecture 15 15 10/21 10/21 /03 /03 Sandiway FONG Sandiway FONG

Post on 22-Dec-2015

213 views

Category:

Documents


1 download

TRANSCRIPT

LING 388Language and Computers

Lecture Lecture 1515

10/2110/21/03/03

Sandiway FONGSandiway FONG

Administrivia

Homework Assignment 3Homework Assignment 3 Due todayDue today Need help? Need help?

Error in Lecture 10 slide for aError in Lecture 10 slide for annbbnnccnn fixed … fixed … s --> [a],t(1),[c].s --> [a],t(1),[c]. t(N) --> [a],{M is N+1},t(M),[c].t(N) --> [a],{M is N+1},t(M),[c]. t(N) --> u(N).t(N) --> u(N). u(N) --> u(N) --> {N>1},{N>1}, [b],{M is N-1},u(M). [b],{M is N-1},u(M). u(1) --> [b].u(1) --> [b].

-Calculus

Lambda (Lambda () calculus:) calculus: Concerned with the definition of anonymous functions Concerned with the definition of anonymous functions

and their applicationand their application Invented by Alonzo Church to explore computability of functionsInvented by Alonzo Church to explore computability of functions

Example definition:Example definition: x.x+3x.x+3 “is a function that adds 3 to x”“is a function that adds 3 to x”

Note :Note :

• we did not give this function a name - hence the term we did not give this function a name - hence the term “anonymous”“anonymous”

-Calculus

Example of application:Example of application: x.x+3) 2x.x+3) 2 “apply lambda function to 2” “apply lambda function to 2”

((-reduction)-reduction) x+3) [2/x]x+3) [2/x] “substitute 2 for x”“substitute 2 for x” 2+32+3 “evaluate function”“evaluate function” 55

There is much more to There is much more to -calculus-calculus But this is all we need …But this is all we need …

Last Time …

We looked at the parsing of empty NPs in relative We looked at the parsing of empty NPs in relative clause constructionsclause constructions Example:Example:

the cat that I sawthe cat that I saw the catthe catii that I saw [ that I saw [NPNP e eii]] (the cat)((the cat)(xx.I saw .I saw xx))

By analogy:By analogy: that I sawthat I saw [ [NPNP e eii] has the semantics of ] has the semantics of xx.I saw .I saw xx This function returns This function returns xx

Last Time …

The rule that generated the empty category The rule that generated the empty category [[NPNP e eii] ]

allowed for considerable overgenerationallowed for considerable overgeneration Examples:Examples:

*Hit the ball*Hit the ball [[NPNP e eii] hit the ball] hit the ball

*John hit*John hit John hit [John hit [NPNP e eii] ]

*Hit*Hit [ [NPNP e eii] hit [] hit [NPNP e eii]]

… … all of which can be accepted as complete sentences by the DCGall of which can be accepted as complete sentences by the DCG

Last Time …

To control the overgeneration, we needed To control the overgeneration, we needed some condition on representation …some condition on representation … Filter:Filter:

All variables must be bound by an operator (All variables must be bound by an operator (xx)) Example:Example:

*John hit*John hit s(np(john),vp(v(hit),np(s(np(john),vp(v(hit),np(xx))))))

Today’s Lecture:Today’s Lecture: How to implement this filterHow to implement this filter

Condition on Representation

Basic Idea:Basic Idea: Scan phrase structure looking for a variable and operatorScan phrase structure looking for a variable and operator Succeed only if matching operator foundSucceed only if matching operator found

Scan Operation:Scan Operation: Tree-walkerTree-walker

Walk through a tree node-by-node, testing each node to see if Walk through a tree node-by-node, testing each node to see if we have found what we’re looking forwe have found what we’re looking for

Condition on Representation

Also make use of two elements that we’ve already seen:Also make use of two elements that we’ve already seen: =.. (univ)=.. (univ) man(X) =.. [man,X]man(X) =.. [man,X]

to deconstruct structuresto deconstruct structures Feature propagationFeature propagation

to send information up the treeto send information up the trees

np vp

v np

det n

the man

likes[3,sg]

John [3,sg]

P,N

Tree-Walker

Question:Question: How to scan phrase structure tree?How to scan phrase structure tree?

Answer:Answer: Write a recursive predicate, a Write a recursive predicate, a tree-tree-

walkerwalker, to visit , to visit everyevery node of a tree node of a tree

Tree-Walker

Assuming phrase structure trees Assuming phrase structure trees are binary branching at the are binary branching at the most…most…

Tree-Walker must deal with Tree-Walker must deal with three cases:three cases:

A.A. Binary branchingBinary branchingvisit both subtreesvisit both subtrees

B.B. Unary branchingUnary branchingvisit child subtreevisit child subtree

C.C. Terminal nodeTerminal node

donedone

np

det n

the man

vp

ran

v

A

B

C

Tree-Walker

Example Prolog code: Example Prolog code: Define a predicate visit/1 taking as its Define a predicate visit/1 taking as its argument a phrase structure treeargument a phrase structure tree visit(X) :- visit(X) :- % Case A% Case A

X =.. [F,A1,A2],X =.. [F,A1,A2], visit(A1),visit(A1), visit(A2).visit(A2).

visit(X) :-visit(X) :- % Case B% Case B X =.. [F,A],X =.. [F,A], visit(A).visit(A).

visit(X) :-visit(X) :- % Case C% Case C atom(X).atom(X). atom/1 built-inatom/1 built-in

np

det n

the man

vp

ran

v

A

B

C

XF=npA1A2

XF=vpA

Tree-Walker visit/1 example:visit/1 example:

Inorder traversalInorder traversal

s

np vp

v np

det n

the man

saw

john

1

11

10

9

8

7

6

5

4

3

2

Tree-Walker

visit/1 doesn’t really do anything yetvisit/1 doesn’t really do anything yet Need to add a specific test for np(x)Need to add a specific test for np(x)

Example:Example: *John hit*John hit s(np(john),vp(v(hit),np(s(np(john),vp(v(hit),np(xx))))))

visit(X) :- visit(X) :- X =.. [F,A1,A2],X =.. [F,A1,A2],visit(A1),visit(A1),visit(A2).visit(A2).

visit(X) :-visit(X) :-X =.. [F,A],X =.. [F,A],visit(A).visit(A).

visit(X) :- atom(X).visit(X) :- atom(X).

visit(np(x)).visit(np(x)).

Tree-Walker We can write visit/1 to succeed only if it finds np(x)We can write visit/1 to succeed only if it finds np(x)

visit(np(x)).visit(np(x)).visit(X) :- visit(X) :-

X =.. X =.. [F,A1,A2],[F,A1,A2],visit(A1),visit(A1),visit(A2).visit(A2).

visit(X) :-visit(X) :-X =.. [F,A],X =.. [F,A],visit(A).visit(A).

visit(X) :- atom(X).visit(X) :- atom(X).

visit(np(x)).visit(np(x)).visit(X) :- visit(X) :-

X =.. X =.. [F,A1,A2],[F,A1,A2],visit(A1).visit(A1).

visit(X) :- visit(X) :- X =.. X =.. [F,A1,A2],[F,A1,A2],visit(A2).visit(A2).

visit(X) :-visit(X) :-X =.. [F,A],X =.. [F,A],visit(A).visit(A).• Succeeds always

• Succeeds only if np(x) exists

Tree-Walker

New visit/1 example for *New visit/1 example for *John hit John hit [[NPNP ee]:]:

s

np vp

v np

xhit

john

visit(X) succeedsvisit(X) fails

Tree-Walker Call the first form a Call the first form a conjunctive tree-walkerconjunctive tree-walker, the 2nd a , the 2nd a disjunctive disjunctive

tree-walkertree-walker

visit(np(x)).visit(np(x)).visit(X) :- visit(X) :-

X =.. X =.. [F,A1,A2],[F,A1,A2],visit(A1),visit(A1),visit(A2).visit(A2).

visit(X) :-visit(X) :-X =.. [F,A],X =.. [F,A],visit(A).visit(A).

visit(X) :- atom(X).visit(X) :- atom(X).

visit(np(x)).visit(np(x)).visit(X) :- visit(X) :-

X =.. X =.. [F,A1,A2],[F,A1,A2],visit(A1).visit(A1).

visit(X) :- visit(X) :- X =.. X =.. [F,A1,A2],[F,A1,A2],visit(A2).visit(A2).

visit(X) :-visit(X) :-X =.. [F,A],X =.. [F,A],visit(A).visit(A).

Either visit A1 or A2

Visit A1 and A2

Tree-Walker Let’s rename our modified version of visit/1 to reflect the new semanticsLet’s rename our modified version of visit/1 to reflect the new semantics

variable(np(x)).variable(np(x)).variable(X) :- variable(X) :-

X =.. X =.. [F,A1,A2],[F,A1,A2],variable(A1).variable(A1).

variable(X) :- variable(X) :- X =.. X =.. [F,A1,A2],[F,A1,A2],variable(A2).variable(A2).

variable(X) :-variable(X) :-X =.. [F,A],X =.. [F,A],variable(A).variable(A).

• variable/1 holds only if there exists variable/1 holds only if there exists np(x) in the phrase structurenp(x) in the phrase structure

ExamplesExamples::

•?- variable(s(np(john),vp(v(hit),np(x)))).?- variable(s(np(john),vp(v(hit),np(x)))).•YesYes

•?- variable(s(np(john),vp(v(hit),np(mary)))).?- variable(s(np(john),vp(v(hit),np(mary)))).•NoNo

•?-variable?-variable((np(np(det(the),n(cat)),np(np(det(the),n(cat)),lambda(x,s(np(i),vp(v(saw),np(x)))))).lambda(x,s(np(i),vp(v(saw),np(x)))))).

•YesYes

Tree-Walker Let’s build another modified version of visit/1 that succeeds only if there Let’s build another modified version of visit/1 that succeeds only if there

exists an operatorexists an operatorxx, i.e. lambda(x,_), in the phrase structure, i.e. lambda(x,_), in the phrase structure

operator(lambda(x,_)).operator(lambda(x,_)).operator(X) :- operator(X) :-

X =.. [F,A1,A2],X =.. [F,A1,A2],operator(A1).operator(A1).

operator(X) :- operator(X) :- X =.. [F,A1,A2],X =.. [F,A1,A2],operator(A2).operator(A2).

operator(X) :-operator(X) :-X =.. [F,A],X =.. [F,A],operator(A).operator(A).

ExamplesExamples::

•?- operator(s(np(john),vp(v(hit),np(x)))).?- operator(s(np(john),vp(v(hit),np(x)))).•NoNo

•?- operator(s(np(john),vp(v(hit),np(mary)))).?- operator(s(np(john),vp(v(hit),np(mary)))).•NoNo

•?-operator?-operator((np(np(det(the),n(cat)),np(np(det(the),n(cat)),lambda(x,s(np(i),vp(v(saw),np(x)))))).lambda(x,s(np(i),vp(v(saw),np(x)))))).

•YesYes

Filter Implementation

Filter:Filter: All variables must be bound by an operator (All variables must be bound by an operator (xx))

Implementation (Implementation (Initial tryInitial try)):: filter(X) :- variable(X), operator(X).filter(X) :- variable(X), operator(X). filter(X) :- \+ variable(X).filter(X) :- \+ variable(X).

succeeds either when there is no variable in succeeds either when there is no variable in XX or when a or when a variable and an operator co-occur in variable and an operator co-occur in XX

UseUse:: ?- s(X,Sentence,[]), filter(X).?- s(X,Sentence,[]), filter(X).

XX is the phrase structure returned by the DCG is the phrase structure returned by the DCG SentenceSentence is the input sentence encoded as a list is the input sentence encoded as a list filter/1 is a condition on representationfilter/1 is a condition on representation

Filter Implementation

Filter:Filter: All variables must be bound by an operator (All variables must be bound by an operator (xx))

ImplementationImplementation:: filter(X) :- variable(X), operator(X).filter(X) :- variable(X), operator(X). filter(X) :- \+ variable(X).filter(X) :- \+ variable(X).

Description:Description: filter(X) succeeds either when there is no variable in filter(X) succeeds either when there is no variable in XX or when a or when a

variable and an operator co-occur in variable and an operator co-occur in XX

Filter Implementation

A simpler rendering of filter/1A simpler rendering of filter/1:: filter(X) :- variable(X) -> operator(X) ; true.filter(X) :- variable(X) -> operator(X) ; true. Advantage: one clause implementation instead of two …Advantage: one clause implementation instead of two …

Note:Note: -> is the “then” operator in Prolog -> is the “then” operator in Prolog

compare with the DCG operator -->compare with the DCG operator --> Semantics:Semantics: X -> Y ; ZX -> Y ; Z if if XX is true then is true then YY must be true must be true

if X is not true, Z must be trueif X is not true, Z must be true

Filter Implementation Note:Note:

filter(X) :- variable(X) -> operator(X) ; true.filter(X) :- variable(X) -> operator(X) ; true.is only a is only a first approximationfirst approximation implementation of implementation of

All variables must be All variables must be boundbound by an operator ( by an operator (xx))

ExampleExample:: *the cat that I saw hit*the cat that I saw hit s(np(np(det(the),n(cat)),lambda(x,s(np(i),vp(v(saw),np(x))))),s(np(np(det(the),n(cat)),lambda(x,s(np(i),vp(v(saw),np(x))))),

vp(v(hit),np(x)))vp(v(hit),np(x)))

filter/1 (incorrectly) holds for the above example because:filter/1 (incorrectly) holds for the above example because: filter/1 looks only for one variable, andfilter/1 looks only for one variable, and filter/1 looks for an operator anywhere in the phrase structurefilter/1 looks for an operator anywhere in the phrase structure