ling 388 language and computers lecture 18 10/30/03 sandiway fong
Post on 21-Dec-2015
218 views
TRANSCRIPT
Administrivia
Computer Laboratory next time as well …Computer Laboratory next time as well … November 4th in SBS 224November 4th in SBS 224
Homework 4Homework 4 3 exercises today3 exercises today 1 extra-credit question1 extra-credit question Homework 4 continues next timeHomework 4 continues next time
Lecture 17 slides updated…Lecture 17 slides updated… Added an appendix illustrating the use of negation to Added an appendix illustrating the use of negation to
implement counting up to 2 instead of the extra argument implement counting up to 2 instead of the extra argument method [method [from suggestion in classfrom suggestion in class]]
Exercise 1: Relative Clauses
Examples:Examples: The cat that John sawThe cat that John saw (object)(object) The cat that saw JohnThe cat that saw John (subject)(subject)
Exercise 1: Relative Clauses
Sample DCG:Sample DCG: s(s(X,Y)) --> np(X), vp(Y).s(s(X,Y)) --> np(X), vp(Y). vp(vp(X,Y)) --> transitive_verb(X), np(Y).vp(vp(X,Y)) --> transitive_verb(X), np(Y). np(np(X)) --> proper_noun (X).np(np(X)) --> proper_noun (X).% % Special rules for relative clausesSpecial rules for relative clauses np(np(np(Y,Z),U)) --> det(Y), common_noun(Z), sbar(U).np(np(np(Y,Z),U)) --> det(Y), common_noun(Z), sbar(U). np(np(x)) --> [].np(np(x)) --> []. sbar(lambda(x,Y)) --> complementizer, s(Y).sbar(lambda(x,Y)) --> complementizer, s(Y). complementizer --> [that].complementizer --> [that].% % LexiconLexicon transitive_verb(v(hit)) --> [hit].transitive_verb(v(hit)) --> [hit]. transitive_verb(v(saw)) --> transitive_verb(v(saw)) -->
[saw].[saw]. proper_noun(john) --> [john]. proper_noun(john) --> [john]. proper_noun(mary) --> [mary].proper_noun(mary) --> [mary]. common_noun(n(man)) --> [man].common_noun(n(man)) --> [man]. common_noun(n(cat)) --> [cat]. common_noun(n(cat)) --> [cat]. det(det(the)) --> [the].det(det(the)) --> [the].
Exercise 1: Relative Clauses
Consult DCGConsult DCG Parsing noun phrases (NPs):Parsing noun phrases (NPs):
Run queriesRun queries?- np(X,[the,cat,that,john,saw],[]).?- np(X,[the,cat,that,john,saw],[]).?- np(X,[the,cat,that,saw,john],[]).?- np(X,[the,cat,that,saw,john],[]).
Verify DCG also (incorrectly) accepts as NPs:Verify DCG also (incorrectly) accepts as NPs: *The cat that John saw Mary*The cat that John saw Mary *The cat that saw*The cat that saw
Exercise 1: Relative Clauses
Pseudo-Logical Form for Pseudo-Logical Form for the cat that John sawthe cat that John saw::np
np
det
the
n
cat
lambda
x s
np
np vp
vjohn
saw x
Exercise 1: Relative Clauses
Homework Question (A):Homework Question (A): Modify the DCG to accept the omission of the Modify the DCG to accept the omission of the
complementizer complementizer that that for object relative clausesfor object relative clauses Your DCG should accept both of the following:Your DCG should accept both of the following:
the cat John sawthe cat John saw the cat that John sawthe cat that John saw
as NPsas NPs Hint:Hint:
Write an empty category rule in place of the complementizerWrite an empty category rule in place of the complementizer
Exercise 1: Relative Clauses
Parsing complete sentences:Parsing complete sentences: Run queries:Run queries:
?- s(X,[mary,hit,the,cat,that,john,saw],[]).?- s(X,[mary,hit,the,cat,that,john,saw],[]).?- s(X,[mary,hit,the,cat,that,saw,john],[]).?- s(X,[mary,hit,the,cat,that,saw,john],[]).
Verify the DCG also (incorrectly) accepts as Verify the DCG also (incorrectly) accepts as sentences:sentences: *Mary hit*Mary hit *hit John*hit John *hit*hit
Exercise 1: Relative Clauses
Multiple EmbeddingsMultiple Embeddings Add ruleAdd rule
transitive_verb(v(hissed_at)) --> [hissed,at].transitive_verb(v(hissed_at)) --> [hissed,at]. Turn on full printing (Turn on full printing (taken from Lecture 4taken from Lecture 4))
?- set_prolog_flag(toplevel_print_options,[max_depth(0)]).?- set_prolog_flag(toplevel_print_options,[max_depth(0)]).
Run query forRun query for Mary hit the man that the cat that John saw hissed atMary hit the man that the cat that John saw hissed at
Exercise 1: Relative Clauses
Homework Question (B):Homework Question (B): The previous sentenceThe previous sentence
Mary hit the man that the cat that John saw hissed atMary hit the man that the cat that John saw hissed at
has a doubly-embedded object relative clause has a doubly-embedded object relative clause structure:structure:
Mary hit [Mary hit [NPNP[[NPNP the man the manii] that [] that [NPNP[[NPNP the cat the catjj] that ] that
John saw [John saw [NPNP eejj]] hissed at []] hissed at [NPNP eeii]]]]
Exercise 1: Relative Clauses
Homework Question (B) contd.:Homework Question (B) contd.: WithoutWithout any further modification to the DCG, construct any further modification to the DCG, construct
and show parses for:and show parses for: A sentence with two levels of embedding using subject relative A sentence with two levels of embedding using subject relative
clausesclauses A sentence with two levels of embedding using one subject A sentence with two levels of embedding using one subject
and one object relative clauseand one object relative clause Any sentence with a triply-embedded relative clause structureAny sentence with a triply-embedded relative clause structure
Notes: Notes: i.e. you cannot add any rules or words to the DCG so fari.e. you cannot add any rules or words to the DCG so far The DCG only allows Det N sequences in relative clause The DCG only allows Det N sequences in relative clause
constructions, i.e. you have to make do without NP -> Det Nconstructions, i.e. you have to make do without NP -> Det N
Extra Credit Homework Question
Answer for Exercise 1 Part (A) will also allow the Answer for Exercise 1 Part (A) will also allow the complementizer to be dropped (incorrectly) for complementizer to be dropped (incorrectly) for subject relative clauses:subject relative clauses: the cat that saw Johnthe cat that saw John *the cat saw John*the cat saw John ((as a complex NPas a complex NP))
Extra Credit Homework Question
Modify the DCG to allow Modify the DCG to allow thatthat to be omitted to be omitted onlyonly in the case of object relative clauses, i.e. grammar in the case of object relative clauses, i.e. grammar should still accept:should still accept: the cat John sawthe cat John sawbut reject its subject relative clause counterpart:but reject its subject relative clause counterpart: *the cat saw John*the cat saw John
Hint (Hint (for one possible implementationfor one possible implementation):): Look at how subject/verb agreement was enforcedLook at how subject/verb agreement was enforced
Exercise 2: Disjunctive Tree-Walker
Tree-walker template (disjunctive form):Tree-walker template (disjunctive form): visit(X) :- visit(X) :-
X =.. [_,A1,_],X =.. [_,A1,_], visit(A1).visit(A1).
visit(X) :- visit(X) :- X =.. [_,_,A2],X =.. [_,_,A2], visit(A2).visit(A2).
visit(X) :-visit(X) :- X =.. [_,A],X =.. [_,A], visit(A).visit(A).
visit(X) :- atom(X).visit(X) :- atom(X). Note: Note:
used underscores to substitute for variables that are only used underscores to substitute for variables that are only mentioned oncementioned once
Exercise 2: Disjunctive Tree-Walker
Let’s modify visit/1 to look for Let’s modify visit/1 to look for JohnJohn only: only: visit(X) :- visit(X) :-
X =.. [_,A1,_],X =.. [_,A1,_], visit(A1).visit(A1).
visit(X) :- visit(X) :- X =.. [_,_,A2],X =.. [_,_,A2], visit(A2).visit(A2).
visit(X) :-visit(X) :- X =.. [_,A],X =.. [_,A], visit(A).visit(A).
visit(X) :- atom(X).visit(X) :- atom(X). <- <- to be substituted byto be substituted by - - visit(john).visit(john).
Exercise 2: Disjunctive Tree-Walker
Consult visit/1Consult visit/1 Make sure DCG is still loaded Make sure DCG is still loaded ?- listing. ?- listing. to checkto check
Turn on tracing for visit/1 onlyTurn on tracing for visit/1 only ?- spy(visit/1).?- spy(visit/1).
Run the query:Run the query: ?- s(X,[mary,hit,the,cat,that,hissed,at,john],[]), visit(X).?- s(X,[mary,hit,the,cat,that,hissed,at,john],[]), visit(X). Step through debugger using the command Step through debugger using the command ll (leap) (leap) Prolog should succeed and report X.Prolog should succeed and report X.
Exercise 2: Disjunctive Tree-Walker
Test the structures for sentences: Test the structures for sentences: Mary hit the cat that hissed at MaryMary hit the cat that hissed at Mary
Query should failQuery should fail John saw the cat that saw JohnJohn saw the cat that saw John
Query should succeed twiceQuery should succeed twice
Exercise 2: Disjunctive Tree-Walker
Homework Question:Homework Question: Modify visit/1 to search for all NP nodesModify visit/1 to search for all NP nodes How many times should visit/1 succeed for the How many times should visit/1 succeed for the
following sentence?following sentence? Mary hit the cat that hissed at JohnMary hit the cat that hissed at John
Hint:Hint: You’ll need to add more than one clause to visitYou’ll need to add more than one clause to visit//11
Exercise 3: Collecting Answers
Instead of entering Instead of entering ;; (possibly repeated) to the Prolog (possibly repeated) to the Prolog interpreter …interpreter … we can used a higher-order predicate findall/3 to collect we can used a higher-order predicate findall/3 to collect
together all possible answers to a querytogether all possible answers to a query findall/3findall/3
Prolog build-in Prolog build-in Usage:Usage:
findall(X,Query,List)findall(X,Query,List)• XX may be a variable or constant or structure bound once may be a variable or constant or structure bound once
per successful call to per successful call to QueryQuery• QueryQuery is the Prolog query we wish to test (minus is the Prolog query we wish to test (minus ?-?- and and ..))• ListList is the collected list of values of is the collected list of values of XX per successful call per successful call
Exercise 3: Collecting Answers
Example:Example: male(john).male(john). male(pete).male(pete). female(mary).female(mary). female(jill).female(jill). human(X) :- female(X) ; male(X).human(X) :- female(X) ; male(X). ( ; ( ; indicates disjunctionindicates disjunction))
Prolog query:Prolog query: ?- human(X).?- human(X).
X = mary ;X = mary ; X = jill ;X = jill ; X = john ;X = john ; X = pete ;X = pete ; NoNo
Exercise 3: Collecting Answers
Using findall/3:Using findall/3: ?- findall(X,human(X),List).?- findall(X,human(X),List).
X = _G283X = _G283 ((an internally generated variablean internally generated variable)) List = [mary,jill,john,pete] ;List = [mary,jill,john,pete] ; NoNo
length/2length/2 Prolog built-inProlog built-in Usage:Usage:
length(List,N)length(List,N)• ListList is a list is a list• NN is bound to the number of elements in is bound to the number of elements in ListList
ExampleExample:: ?- findall(X,human(X),List), length(List,N).?- findall(X,human(X),List), length(List,N).
N = 4N = 4
Exercise 3: Collecting Answers
Basic (unmodified) disjunctive tree-walker:Basic (unmodified) disjunctive tree-walker: visit(X) :- visit(X) :-
X =.. [_,A1,_],X =.. [_,A1,_], visit(A1).visit(A1).
visit(X) :- visit(X) :- X =.. [_,_,A2],X =.. [_,_,A2], visit(A2).visit(A2).
visit(X) :-visit(X) :- X =.. [_,A],X =.. [_,A], visit(A).visit(A).
visit(X) :- atom(X).visit(X) :- atom(X).
Exercise 3: Collecting Answers
Homework Question:Homework Question: Assuming the unmodified version of visit/1Assuming the unmodified version of visit/1 What does the queryWhat does the query
?- s(X,[mary,hit,the,cat,that,hissed,at,john],[]),?- s(X,[mary,hit,the,cat,that,hissed,at,john],[]), findall(once,visit(X),L), findall(once,visit(X),L),
length(L,N).length(L,N).do?do?