View
217
Download
2
Embed Size (px)
Citation preview
LING 388Language and Computers
Lecture Lecture 2020
11/611/6/03/03
Sandiway FONGSandiway FONG
Administrivia
Next Tuesday (11th)Next Tuesday (11th) Veterans’ DayVeterans’ Day
Today’s LectureToday’s Lecture Homework 3 reviewHomework 3 review Help with Homework 4Help with Homework 4
Next time …Next time … We start a brand-new topic known as We start a brand-new topic known as Shallow Shallow
ParsingParsing We’ll begin with Part-of-speech (POS) tagging, We’ll begin with Part-of-speech (POS) tagging,
stemming …stemming …
Homework 3 Review
Exercise 1 Part (B): Encoding the idiom kick the bucket Add a DCG rule to encode the idiomatic meaning Add a DCG rule to encode the idiomatic meaning
of of John kicked the bucket, John kicked the bucket, i.e.i.e.s
np vp
v
died
john
?- s(X,[john,kicked,the,bucket],[])?- s(X,[john,kicked,the,bucket],[])X = s(np(john),vp(v(died)))X = s(np(john),vp(v(died)))
HintHint: see how the terminal string : see how the terminal string the ballthe ball was was encoded in Lecture 10encoded in Lecture 10
np --> [the,ball].np --> [the,ball].
Sample DCG s(s(X,Y)) --> np(X), vp(Y).s(s(X,Y)) --> np(X), vp(Y). np(np(X,Y)) --> det(X,Num), common_noun(Y,Num).np(np(X,Y)) --> det(X,Num), common_noun(Y,Num). np(np(X)) --> proper_noun(X).np(np(X)) --> proper_noun(X). vp(vp(X)) --> intransitive_verb(X).vp(vp(X)) --> intransitive_verb(X). vp(vp(X,Y)) --> transitive_verb(X), np(Y).vp(vp(X,Y)) --> transitive_verb(X), np(Y). common_noun(n(man),sg) --> [man].common_noun(n(man),sg) --> [man]. common_noun(n(men),sg) --> [men].common_noun(n(men),sg) --> [men]. proper_noun(john) --> [john].proper_noun(john) --> [john]. det(det(the),_) --> [the].det(det(the),_) --> [the]. det(det(a),sg) det(det(a),sg)
--> [a].--> [a]. intransitive_verb(v(ran)) --> [ran].intransitive_verb(v(ran)) --> [ran]. transitive_verb(v(saw)) --> [saw].transitive_verb(v(saw)) --> [saw].
Exercise 4 Part (A): Implementing every and some Want parses:Want parses:
s(s(forall(X,man(X)),forall(X,man(X)),vp(v(likes),np(john))))vp(v(likes),np(john)))) s(s(exists(X,men(X)),exists(X,men(X)),vp(v(like),np(n(beer))))vp(v(like),np(n(beer))))
forfor Every man likes JohnEvery man likes John Some men like beerSome men like beer
Exercise 4 Part (A): Implementing every and some NP rules:NP rules:
np(np(X,Y)) --> quantifier(X,Num), common_noun(Y,Num).np(np(X,Y)) --> quantifier(X,Num), common_noun(Y,Num). quantifier(q(every),sg) --> [every].quantifier(q(every),sg) --> [every]. quantifier(q(some),_) --> [some].quantifier(q(some),_) --> [some]. common_noun(n(man),sg) --> [man].common_noun(n(man),sg) --> [man]. common_noun(n(men),pl) --> [men].common_noun(n(men),pl) --> [men].
Solution:Solution: Write one rule specialized for Write one rule specialized for everyevery and another for and another for somesome
Exercise 4 Part (A): Implementing every and some Case 1: Case 1: everyevery
np(np(X,Y)) --> quantifier(X,Num), common_noun(Y,Num).np(np(X,Y)) --> quantifier(X,Num), common_noun(Y,Num). quantifier(q(every),sg) --> [every].quantifier(q(every),sg) --> [every]. common_noun(n(man),sg) --> [man].common_noun(n(man),sg) --> [man].
Parsing Parsing every manevery man will produce the following series of will produce the following series of variable bindings:variable bindings:
np(np(X,Y)) --> np(np(X,Y)) --> quantifier(X,Num)quantifier(X,Num), common_noun(Y,Num)., common_noun(Y,Num).
quantifier(q(every),sg)quantifier(q(every),sg) common_noun(n(man),sg)common_noun(n(man),sg)
everyevery manman
Exercise 4 Part (A): Implementing every and some Perform a partial substitution; instantiate Perform a partial substitution; instantiate everyevery::
np(np(X,Y)) -->np(np(X,Y)) --> quantifier(quantifier(q(every)q(every),Num),,Num), common_noun(common_noun(n(N)n(N),Num).,Num).
NoteNote: : NP rule is now particular to sequences NP rule is now particular to sequences every Nevery N I.e. NP rule won’t fire for other NP sequences like:I.e. NP rule won’t fire for other NP sequences like:
some mensome men the catthe cat JohnJohn
Exercise 4 Part (A): Implementing every and some Substitute forall for np:Substitute forall for np:
np(np(forall(X,Y)forall(X,Y)) --> ) --> quantifier(q(every),Num),quantifier(q(every),Num), common_noun(n(N),Num).common_noun(n(N),Num).
Finally, add code in {…} to generate Y from N:Finally, add code in {…} to generate Y from N: np(forall(X,np(forall(X,YY)) --> )) -->
quantifier(q(every),Num),quantifier(q(every),Num), common_noun(n(N),Num),common_noun(n(N),Num), {Y=..[N,X]}{Y=..[N,X]}..
The same implementation strategy works also for The same implementation strategy works also for somesome and and exists(X,N(X))exists(X,N(X)) … …
Key Concepts
1.1. Use partial variable substitutionUse partial variable substitution … … to customize a ruleto customize a rule
2.2. We can build f(X) where We can build f(X) where ff cannot be determined cannot be determined staticallystatically, ,
i.e. i.e. at the time we specify the grammar ruleat the time we specify the grammar rule by …by …
using Prolog built-in univ (=..), andusing Prolog built-in univ (=..), and the DCG rule out-call mechanism {…}the DCG rule out-call mechanism {…}
Homework 4 Concepts
Logic and Negation in Prolog
Filter 1: Filter 1: All variables must be bound by an operatorAll variables must be bound by an operator
Implementation:Implementation: \+ (variable(X,F), var(F))\+ (variable(X,F), var(F))
variable/2 is an np(x) findervariable/2 is an np(x) finderF bound when F bound when xx can be found can be found
Question:Question: Why are we implementing a positive condition Why are we implementing a positive condition
using negationusing negation (\+)? (\+)?
Logic and Negation in Prolog
np
np
det
the
n
cat
lambda
x s
np
np vp
vjohn
saw x
np
vp
v
hissed at x
s
*The cat that John saw *The cat that John saw hissed athissed at
Logic and Negation in Prolog ExampleExample::
All tomatoes are redAll tomatoes are red Define predicates:Define predicates:
tomato(X)tomato(X)red(X)red(X)
Condition:Condition: ?- tomato(X), red(X).?- tomato(X), red(X).
holds if there exists some X holds if there exists some X such that X is a tomato and such that X is a tomato and X is redX is red
Doesn’t check all the Doesn’t check all the tomatoes in the boxtomatoes in the box
Hence, query does not Hence, query does not guarantee that all the guarantee that all the tomatoes in the box are redtomatoes in the box are red
Logic and Negation in Prolog Example:Example:
All tomatoes are redAll tomatoes are red Equivalent condition:Equivalent condition:
It cannot be the case that there It cannot be the case that there exists some tomato and that exists some tomato and that tomato is not redtomato is not red
Implementation:Implementation: ?- \+ ( tomato(X), \+ red(X)).?- \+ ( tomato(X), \+ red(X)).
holds if it is not the case holds if it is not the case that there exists some X that there exists some X such that X is a tomato such that X is a tomato and X is not redand X is not red
Guarantees that all the Guarantees that all the tomatoes in the box are tomatoes in the box are redred
Logic and Negation in Prolog
Basic Quantifier Conversion Rules:Basic Quantifier Conversion Rules: x p(x)x p(x) <=> <=> x x p(x)p(x) x p(x)x p(x) <=> <=> x x p(x)p(x)
Examples:Examples: X tomato(X), red(X) X tomato(X), red(X) <=> <=> X tomato(X), \+ red(X)X tomato(X), \+ red(X)
Prolog variables?Prolog variables? By default, existentially quantified (By default, existentially quantified (
Logic and Negation in Prolog
Filter 1: Filter 1: All variables must be bound by an operatorAll variables must be bound by an operator
Conversion:Conversion: There must not exist a variable that is not bound by an There must not exist a variable that is not bound by an
operatoroperator Implementation:Implementation:
\+ (variable(X,F), var(F))\+ (variable(X,F), var(F)) variable/2 is an np(x) findervariable/2 is an np(x) finder F bound when F bound when xx can be found can be found
Implementing the Exactly One Condition
In Exercise 5 …In Exercise 5 … Filter 2: Filter 2:
All operators must bind All operators must bind exactlyexactly one variable one variable Implementation:Implementation:
filter2(X) :- operator(X).filter2(X) :- operator(X). Predicate: Predicate:
operator(X) operator(X) • holds if X contains a holds if X contains a xx.Y with less than two .Y with less than two
variables in Y variables in Y
Implementing the Exactly One Condition
Operator/1 calls predicate lt2vars/2: Operator/1 calls predicate lt2vars/2: lt2vars(X,F) lt2vars(X,F)
• holds if there are less than two variables in X holds if there are less than two variables in X • F is bound to F is bound to oneone if there is one variable in X if there is one variable in X
We’ll need to impose either that:We’ll need to impose either that:• F == one F == one oror • \+ var(F)\+ var(F)
to the output of lt2vars/2 to implement the to the output of lt2vars/2 to implement the condition for condition for exactly one variableexactly one variable
Implementing the Universally Quantified Condition Not done yet …Not done yet …
Filter 2: Filter 2: AllAll operators must bind exactly one variable operators must bind exactly one variable
Implementation:Implementation: filter2(X) :- operator(X).filter2(X) :- operator(X).
Predicate: Predicate: operator(X) operator(X)
• holds if X contains holds if X contains a a xx.Y.Y with less than two variables in with less than two variables in Y Y
• I.e. if in X I.e. if in X xx.Y with less than two variables in Y.Y with less than two variables in Y
Implementing the Universally Quantified Condition Filter 2:Filter 2:
AllAll operators must bind exactly one variable operators must bind exactly one variable So we must convert this to an existentially So we must convert this to an existentially
quantified conditionquantified condition Equivalently:Equivalently:
In X, it cannot be the case that operator(X) In X, it cannot be the case that operator(X) holds and the number of variables bound is not holds and the number of variables bound is not oneone
Overlapping/Non-Overlapping Clauses Question: Question:
For tree-walkers, why do we sometimes want For tree-walkers, why do we sometimes want overlapping clauses, i.e. have more than one overlapping clauses, i.e. have more than one applicable clause, and why sometimes we applicable clause, and why sometimes we
don’t?don’t? Example:Example:
operator/1 is overlappingoperator/1 is overlapping lt2vars/2 is non-overlappinglt2vars/2 is non-overlapping
Case 1: Non-Overlapping Clauses
lt2vars(lt2vars(np(x)np(x),F) :-,F) :-var(F), F = one.var(F), F = one.
lt2vars(X,F) :- lt2vars(X,F) :- X =.. [_,A1,A2],X =.. [_,A1,A2],lt2vars(A1,F),lt2vars(A1,F),lt2vars(A2,F).lt2vars(A2,F).
lt2vars(lt2vars(XX,F) :-,F) :- \+ X = np(x),\+ X = np(x),X =.. [_,A],X =.. [_,A],lt2vars(A,F).lt2vars(A,F).
lt2vars(X,_) :- atom(X).lt2vars(X,_) :- atom(X).
np(x)
• Unary branching Unary branching structure:structure:
matches two clausesmatches two clauses
blocks np(x)blocks np(x)
Want only Want only one matchone match!!
Case 1: Non-Overlapping Clauses
np(x)
• Need to block the lower Need to block the lower clause from applyingclause from applying
• it does not set F, and it does not set F, and otherwise otherwise ?- lt2vars(X,F), var(F). ?- lt2vars(X,F), var(F). will succeed when X has, will succeed when X has, say, three np(x)ssay, three np(x)s
[[Upper clause will reject 3 Upper clause will reject 3 np(x)s, but lower clause will pass np(x)s, but lower clause will pass itit.].]
lt2vars(lt2vars(np(x)np(x),F) :-,F) :-var(F), F = one.var(F), F = one.
lt2vars(X,F) :- lt2vars(X,F) :- X =.. [_,A1,A2],X =.. [_,A1,A2],lt2vars(A1,F),lt2vars(A1,F),lt2vars(A2,F).lt2vars(A2,F).
lt2vars(lt2vars(XX,F) :-,F) :- \+ X = np(x),\+ X = np(x),X =.. [_,A],X =.. [_,A],lt2vars(A,F).lt2vars(A,F).
lt2vars(X,_) :- atom(X).lt2vars(X,_) :- atom(X).
Case 2: Overlapping Clauses
operator(lambda(x,Y)):-operator(lambda(x,Y)):-lt2vars(Y,_).lt2vars(Y,_).
operator(X) :- operator(X) :- X =.. [_,A1,_],X =.. [_,A1,_],operator(A1).operator(A1).
operator(X) :- operator(X) :- X =.. [_,_,A2],X =.. [_,_,A2],operator(A2).operator(A2).
operator(X) :-operator(X) :-X =.. [F,A],X =.. [F,A],operator(A).operator(A).
lambda(x,_)
• Binary branching Binary branching structure:structure:
matches three clausesmatches three clauses
Want multiple Want multiple matchesmatches!!
Case 2: Overlapping Clauses
operator(lambda(x,Y)):-operator(lambda(x,Y)):-lt2vars(Y,_).lt2vars(Y,_).
operator(X) :- operator(X) :- X =.. [_,A1,_],X =.. [_,A1,_],operator(A1).operator(A1).
operator(X) :- operator(X) :- X =.. [_,_,A2],X =.. [_,_,A2],operator(A2).operator(A2).
operator(X) :-operator(X) :-X =.. [F,A],X =.. [F,A],operator(A).operator(A).
lambda(x,_)
operator/1 is a operator/1 is a xx finder finder• We want it to be able to We want it to be able to find and test all find and test all xx structures structures within a phrase structurewithin a phrase structure• ?- operator(X). will succeed ?- operator(X). will succeed once for each once for each xx in X in X
np
np
det
the
n
cat
lambda
x s
np
np vp
vjohn
saw mary
np
vp
v
hissed at x
lambda
x s
*Mary hit the man that the cat *Mary hit the man that the cat that John saw Mary hissed atthat John saw Mary hissed at
Case 2 np
np
det
the
n
man
vp
v
hit
s
np
mary
first x
np
np
det
the
n
cat
lambda
x s
np
np vp
vjohn
saw mary
np
vp
v
hissed at x
lambda
x s
*Mary hit the man that the cat *Mary hit the man that the cat that John saw Mary hissed atthat John saw Mary hissed at
Case 2 np
np
det
the
n
man
vp
v
hit
s
np
mary
first x
Key Concept
We need to restrict or use overlap as We need to restrict or use overlap as appropriateappropriate Because of Prolog’s search strategyBecause of Prolog’s search strategy
i.e. if a clause fails during a query, i.e. if a clause fails during a query, Prolog will look for another way to Prolog will look for another way to succeed succeed