ling 388 language and computers lecture 10 10/2/03 sandiway fong

24
LING 388 Language and Computers Lecture Lecture 10 10 10/2 10/2 /03 /03 Sandiway FONG Sandiway FONG

Post on 21-Dec-2015

215 views

Category:

Documents


1 download

TRANSCRIPT

LING 388Language and Computers

Lecture Lecture 1010

10/210/2/03/03

Sandiway FONGSandiway FONG

Administrivia

ReminderReminder Homework 2 due todayHomework 2 due today Last minute help?Last minute help?

This afternoon, 309 DouglassThis afternoon, 309 Douglass

Review

Last time…Last time… Introduced two new DCG featuresIntroduced two new DCG features

Prolog code: { … }Prolog code: { … } Structures as non-terminals Structures as non-terminals

As an illustration of the additional power provided by As an illustration of the additional power provided by these features, we used them to convert:these features, we used them to convert:

• aa++bb++ intointo

• aannbbnn

Today’s Lecture

We’re going to look at the use of these two We’re going to look at the use of these two features in more detail …features in more detail …

And, in the next set of computer laboratory And, in the next set of computer laboratory exercises and homework, you’ll be using them exercises and homework, you’ll be using them to encode linguistic constraintsto encode linguistic constraints

Non-terminals as structures Can be used to convert an acceptor into a Can be used to convert an acceptor into a parserparser

Example grammar:Example grammar: s --> np, vp.s --> np, vp. np --> pronoun.np --> pronoun. np --> [the,ball].np --> [the,ball]. pronoun --> [i].pronoun --> [i]. pronoun --> [we].pronoun --> [we]. vp --> unergative.vp --> unergative. vp --> transitive, np.vp --> transitive, np. unergative --> [ran].unergative --> [ran]. transitive --> [hit].transitive --> [hit].

Accepts sentence:Accepts sentence: I hit the ballI hit the ball

Non-terminals as structures

However, the DCG version of the However, the DCG version of the grammar is only an acceptor grammar is only an acceptor (Yes/No)(Yes/No) i.e. i.e.

?- s([i,hit,the,ball],[]).?- s([i,hit,the,ball],[]).YesYes

Non-terminals as structures

We’d like to have the query return some We’d like to have the query return some representation of a parse tree:representation of a parse tree:

s

np vp

v np

det n

the ball

hit

i

Non-terminals as structures

Basic Idea:Basic Idea: Add a variable for each (relevant) non-Add a variable for each (relevant) non-

terminal that will be used to return the terminal that will be used to return the fragment of the parse tree for that non-fragment of the parse tree for that non-terminalterminal

Non-terminals as structures

Adding a variable:Adding a variable: s(X) --> np(Y), vp(Z).s(X) --> np(Y), vp(Z). np(X) --> pronoun(X).np(X) --> pronoun(X). np(X) --> [the,ball].np(X) --> [the,ball]. pronoun(X) --> [i].pronoun(X) --> [i]. pronoun(X) --> [we].pronoun(X) --> [we]. vp(X) --> unergative(X).vp(X) --> unergative(X). vp(X) --> transitive(Y), np(Z).vp(X) --> transitive(Y), np(Z). unergative(X) --> [ran].unergative(X) --> [ran]. transitive(X) --> [hit].transitive(X) --> [hit].

Non-terminals as structures

Variable bindings:Variable bindings: s(X) --> np(Y), vp(Z).s(X) --> np(Y), vp(Z).X = s(Y,Z)X = s(Y,Z) np(X) --> pronoun(Y).np(X) --> pronoun(Y). X = np(Y)X = np(Y) np(X) --> [the,ball].np(X) --> [the,ball]. X = np(det(the),n(ball))X = np(det(the),n(ball)) pronoun(X) --> [i].pronoun(X) --> [i]. X = iX = i pronoun(X) --> [we].pronoun(X) --> [we]. X = weX = we vp(X) --> unergative(Y).vp(X) --> unergative(Y). X = vp(Y)X = vp(Y) vp(X) --> transitive(Y), np(Z).vp(X) --> transitive(Y), np(Z). X = vp(Y,Z)X = vp(Y,Z) unergative(X) --> [ran].unergative(X) --> [ran]. X = v(ran)X = v(ran) transitive(X) --> [hit].transitive(X) --> [hit]. X = v(hit)X = v(hit)

Non-terminals as structures

Substituting:Substituting: s(s(Y,Z)) --> np(Y), vp(Z).s(s(Y,Z)) --> np(Y), vp(Z). np(np(Y)) --> pronoun(Y).np(np(Y)) --> pronoun(Y). np(np(det(the),n(ball))) --> [the,ball].np(np(det(the),n(ball))) --> [the,ball]. pronoun(i) --> [i].pronoun(i) --> [i]. pronoun(we) --> [we].pronoun(we) --> [we]. vp(vp(Y)) --> unergative(Y). vp(vp(Y)) --> unergative(Y). vp(vp(Y,Z)) --> transitive(Y), np(Z).vp(vp(Y,Z)) --> transitive(Y), np(Z). unergative(v(ran)) --> [ran].unergative(v(ran)) --> [ran]. transitive(v(hit)) --> [hit].transitive(v(hit)) --> [hit].

Non-terminals as structures Translation into Prolog clausal form:Translation into Prolog clausal form:

s(s(Y,Z)) --> np(Y), vp(Z).s(s(Y,Z)) --> np(Y), vp(Z).• s(s(Y, Z), L1, L3) :- np(Y,L1,L2), vp(Z, L2,L3).s(s(Y, Z), L1, L3) :- np(Y,L1,L2), vp(Z, L2,L3).

np(np(Y)) --> pronoun(Y).np(np(Y)) --> pronoun(Y).• np(np(Y), L1, L2) :- pronoun(Y,L1,L2).np(np(Y), L1, L2) :- pronoun(Y,L1,L2).

np(np(det(the),n(ball))) --> [the,ball].np(np(det(the),n(ball))) --> [the,ball].• np(np(det(the), n(ball)), [the, ball|L], L).np(np(det(the), n(ball)), [the, ball|L], L).

pronoun(i) --> [i].pronoun(i) --> [i].• pronoun(i, [i|L], L).pronoun(i, [i|L], L).

NoteNote:: In s/3, the 1In s/3, the 1stst argument is the extra parameter we added to the argument is the extra parameter we added to the

gtrammar.gtrammar. 22ndnd and 3 and 3rdrd arguments represent the difference list arguments represent the difference list

Non-terminals as structures Translation into Prolog clausal form contd.:Translation into Prolog clausal form contd.:

pronoun(we) --> [we].pronoun(we) --> [we].• pronoun(we, [we|L], L).pronoun(we, [we|L], L).

vp(vp(Y)) --> unergative(Y). vp(vp(Y)) --> unergative(Y). • vp(vp(Y), L1, L2) :- unergative(Y,L1,L2).vp(vp(Y), L1, L2) :- unergative(Y,L1,L2).

vp(vp(Y,Z)) --> transitive(Y), np(Z).vp(vp(Y,Z)) --> transitive(Y), np(Z).• vp(vp(Y,Z), L1, L3) :- transitive(Y,L1,L2), vp(vp(Y,Z), L1, L3) :- transitive(Y,L1,L2),

np(Z,L2,L3).np(Z,L2,L3). unergative(v(ran)) --> [ran].unergative(v(ran)) --> [ran].

• unergative(v(ran), [ran|L], L).unergative(v(ran), [ran|L], L). transitive(v(hit)) --> [hit].transitive(v(hit)) --> [hit].

• transitive(v(hit), [hit|L], L).transitive(v(hit), [hit|L], L).

Retrieving Parses

Prolog queryProlog query ?- s(X,[i,hit,the,ball],[]).?- s(X,[i,hit,the,ball],[]).

NoteNote::

• XX precedes difference list precedes difference list

Response:Response: X = s(np(i),vp(v(hit),np(det(the),n(ball))))X = s(np(i),vp(v(hit),np(det(the),n(ball))))

DCG more powerful than Context-Free

In Lecture 9, there was a promissory note…In Lecture 9, there was a promissory note… We can write a DCG for the context-We can write a DCG for the context-

sensitive languagesensitive language

LLabcabc = { a = { annbbnnccnn | n >= 1 } | n >= 1 }

((which cannot be encoded by any which cannot be encoded by any context-free grammarcontext-free grammar))

DCG more powerful than Context-Free

Basic Idea:Basic Idea: Same trick for converting aSame trick for converting a++bb++ into a into annbbnn

1.1. Modify non-terminals to pass around Modify non-terminals to pass around a countera counter

2.2. Increment and decrement counter to Increment and decrement counter to keep track of the number of keep track of the number of aas and s and bbss

DCG more powerful than Context-Free

We already know we can write a context-We already know we can write a context-free grammar that keeps track of 2 things at free grammar that keeps track of 2 things at a timea time Let’s start by writing a grammar for Let’s start by writing a grammar for

aannbb++ccnn

((which happens to be a context-free which happens to be a context-free languagelanguage))

DCG more powerful than Context-Free

L(GL(Gn+nn+n) = { a) = { annbb++ccnn | n >= 1 } | n >= 1 } Rules:Rules:

S -> aTcS -> aTcT -> aTcT -> aTc

This will generate an equal number of This will generate an equal number of aas and s and ccss NoteNote::

Need S and T to avoid generating Need S and T to avoid generating bbs only, s only, see later…see later…

DCG more powerful than Context-Free

Add rules for U:Add rules for U:S -> aTcS -> aTcT -> aTcT -> aTcT -> UT -> UU -> b UU -> b UU -> bU -> b

Rules for non-terminal U will guarantee the Rules for non-terminal U will guarantee the generation of one or more generation of one or more bbss

DCG more powerful than Context-Free

Convert grammar into DCG rules: Convert grammar into DCG rules: s --> [a],t,[c].s --> [a],t,[c].t --> [a],t,[c].t --> [a],t,[c].t --> u.t --> u.u --> [b],u.u --> [b],u.u --> [b].u --> [b].

DCG more powerful than Context-Free

Add a variable for the counter we need: Add a variable for the counter we need: s --> [a],t(N),[c].s --> [a],t(N),[c].t(N) --> [a],t(M),[c].t(N) --> [a],t(M),[c].t(N) --> u(M).t(N) --> u(M).u(N) --> [b],u(M).u(N) --> [b],u(M).u(N) --> [b].u(N) --> [b].

DCG more powerful than Context-Free

Determine the correct counter values: Determine the correct counter values: s --> [a],t(N),[c].s --> [a],t(N),[c]. N=1N=1 t(N) --> [a],t(M),[c].t(N) --> [a],t(M),[c]. M is N+1M is N+1 t(N) --> u(M).t(N) --> u(M). M=NM=Nu(N) --> [b],u(M).u(N) --> [b],u(M). N>1, M is N-1 N>1, M is N-1u(N) --> [b].u(N) --> [b]. N=1N=1

Note:Note: N>1 condition required to prevent grammar from N>1 condition required to prevent grammar from

decrementing counter below 1decrementing counter below 1

DCG more powerful than Context-Free

Substitute values and insert Prolog code: Substitute values and insert Prolog code: 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) --> {N>1}, [b],{M is N-1},u(M).u(N) --> {N>1}, [b],{M is N-1},u(M).u(1) --> [b].u(1) --> [b].

DCG more powerful than Context-Free

Sample Prolog queries:Sample Prolog queries: ?- s([a,a,b,b,c,c],[]).?- s([a,a,b,b,c,c],[]).

YesYes ?- s([a,a,b,b,c],[]).?- s([a,a,b,b,c],[]).

NoNo ?- s([a,a,b,b,b,c,c],[]).?- s([a,a,b,b,b,c,c],[]).

NoNo ?- s([a,a,b,c,c],[]).?- s([a,a,b,c,c],[]).

NoNo