240 likes | 376 Vues
LING 388 Language and Computers. Lecture 10 10/2 /03 Sandiway FONG. Administrivia. Reminder Homework 2 due today Last minute help? This afternoon, 309 Douglass. Review. Last time… Introduced two new DCG features Prolog code: { … } Structures as non-terminals
E N D
LING 388Language and Computers Lecture 10 10/2/03 Sandiway FONG
Administrivia • Reminder • Homework 2 due today • Last minute help? • This afternoon, 309 Douglass
Review • Last time… • Introduced two new DCG features • Prolog code: { … } • Structures as non-terminals • As an illustration of the additional power provided by these features, we used them to convert: • a+b+ into • anbn
Today’s Lecture • We’re going to look at the use of these two features in more detail … • And, in the next set of computer laboratory exercises and homework, you’ll be using them to encode linguistic constraints
Non-terminals as structures • Can be used to convert an acceptor into a parser • Example grammar: • s --> np, vp. • np --> pronoun. • np --> [the,ball]. • pronoun --> [i]. pronoun --> [we]. • vp --> unergative. • vp --> transitive, np. • unergative --> [ran]. • transitive --> [hit]. • Accepts sentence: • I hit the ball
Non-terminals as structures • However, the DCG version of the grammar is only an acceptor (Yes/No) • i.e. • ?- s([i,hit,the,ball],[]). • Yes
Non-terminals as structures • We’d like to have the query return some representation of a parse tree: s np vp v np i det n hit the ball
Non-terminals as structures • Basic Idea: • Add a variable for each (relevant) non-terminal that will be used to return the fragment of the parse tree for that non-terminal
Non-terminals as structures • Adding a variable: • s(X) --> np(Y), vp(Z). • np(X) --> pronoun(X). • np(X) --> [the,ball]. • pronoun(X) --> [i]. • pronoun(X) --> [we]. • vp(X) --> unergative(X). • vp(X) --> transitive(Y), np(Z). • unergative(X) --> [ran]. • transitive(X) --> [hit].
Non-terminals as structures • Variable bindings: • s(X) --> np(Y), vp(Z). X = s(Y,Z) • np(X) --> pronoun(Y). X = np(Y) • np(X) --> [the,ball]. X = np(det(the),n(ball)) • pronoun(X) --> [i]. X = i • pronoun(X) --> [we]. X = we • vp(X) --> unergative(Y). X = vp(Y) • vp(X) --> transitive(Y), np(Z). X = vp(Y,Z) • unergative(X) --> [ran]. X = v(ran) • transitive(X) --> [hit]. X = v(hit)
Non-terminals as structures • Substituting: • s(s(Y,Z)) --> np(Y), vp(Z). • np(np(Y)) --> pronoun(Y). • np(np(det(the),n(ball))) --> [the,ball]. • pronoun(i) --> [i]. • pronoun(we) --> [we]. • vp(vp(Y)) --> unergative(Y). • vp(vp(Y,Z)) --> transitive(Y), np(Z). • unergative(v(ran)) --> [ran]. • transitive(v(hit)) --> [hit].
Non-terminals as structures • Translation into Prolog clausal form: • s(s(Y,Z)) --> np(Y), vp(Z). • s(s(Y, Z), L1, L3) :-np(Y,L1,L2),vp(Z, L2,L3). • np(np(Y)) --> pronoun(Y). • 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|L], L). • pronoun(i) --> [i]. • pronoun(i, [i|L], L). • Note: • In s/3, the 1st argument is the extra parameter we added to the gtrammar. • 2nd and 3rd arguments represent the difference list
Non-terminals as structures • Translation into Prolog clausal form contd.: • pronoun(we) --> [we]. • pronoun(we, [we|L], L). • vp(vp(Y)) --> unergative(Y). • vp(vp(Y), L1, L2) :-unergative(Y,L1,L2). • vp(vp(Y,Z)) --> transitive(Y), np(Z). • vp(vp(Y,Z), L1, L3) :- transitive(Y,L1,L2),np(Z,L2,L3). • unergative(v(ran)) --> [ran]. • unergative(v(ran), [ran|L], L). • transitive(v(hit)) --> [hit]. • transitive(v(hit), [hit|L], L).
Retrieving Parses • Prolog query • ?- s(X,[i,hit,the,ball],[]). • Note: • X precedes difference list • Response: • 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… • We can write a DCG for the context-sensitive language Labc = { anbncn | n >= 1 } (which cannot be encoded by any context-free grammar)
DCG more powerful than Context-Free • Basic Idea: • Same trick for converting a+b+ into anbn • Modify non-terminals to pass around a counter • Increment and decrement counter to keep track of the number of as and bs
DCG more powerful than Context-Free • We already know we can write a context-free grammar that keeps track of 2 things at a time • Let’s start by writing a grammar for anb+cn (which happens to be a context-free language)
DCG more powerful than Context-Free • L(Gn+n) = { anb+cn | n >= 1 } • Rules: • S -> aTc • T -> aTc • This will generate an equal number of as and cs • Note: • Need S and T to avoid generating bs only, see later…
DCG more powerful than Context-Free • Add rules for U: • S -> aTc • T -> aTc • T -> U • U -> b U • U -> b • Rules for non-terminal U will guarantee the generation of one or more bs
DCG more powerful than Context-Free • Convert grammar into DCG rules: • s --> [a],t,[c]. • t --> [a],t,[c]. • t --> u. • u --> [b],u. • u --> [b].
DCG more powerful than Context-Free • Add a variable for the counter we need: • s --> [a],t(N),[c]. • t(N) --> [a],t(M),[c]. • t(N) --> u(M). • u(N) --> [b],u(M). • u(N) --> [b].
DCG more powerful than Context-Free • Determine the correct counter values: • s --> [a],t(N),[c]. N=1 • t(N) --> [a],t(M),[c]. M is N+1 • t(N) --> u(M). M=N • u(N) --> [b],u(M). N>1, M is N-1 • u(N) --> [b]. N=1 • Note: • N>1 condition required to prevent grammar from decrementing counter below 1
DCG more powerful than Context-Free • Substitute values and insert Prolog code: • s --> [a],t(1),[c]. • t(N) --> [a],{M is N+1},t(M),[c]. • t(N) --> u(N). • u(N) --> {N>1}, [b],{M is N-1},u(M). • u(1) --> [b].
DCG more powerful than Context-Free • Sample Prolog queries: • ?- s([a,a,b,b,c,c],[]). • Yes • ?- s([a,a,b,b,c],[]). • No • ?- s([a,a,b,b,b,c,c],[]). • No • ?- s([a,a,b,c,c],[]). • No