260 likes | 364 Views
LING 388: Language and Computers. Sandiway Fong Lecture 19: 10/31. Administrivia. Homework 4 graded and returned by email Computer Lab Class this Thursday usual place: Social Sciences 224 Homework 6. Today’s Topics. Homework 4 Review More on grammar writing. s. ize. ize. a-z.
E N D
LING 388: Language and Computers Sandiway Fong Lecture 19: 10/31
Administrivia • Homework 4 • graded and returned by email • Computer Lab Class • this Thursday • usual place: Social Sciences 224 • Homework 6
Today’s Topics • Homework 4 Review • More on grammar writing...
s ize ize a-z Homework 4: Question 1 • Given Prolog code: ends_ize(Word) :- atom_chars(Word,List), s(List). s([_|L]) :- s(L). s([i,z,e|L]) :- ize(L). ize([]). s([_|L]) :- s(L). s([i,z,e]). s i i z a-z iz e ize
Homework 4: Question 1 • Part 1: (8pts) • Modify your machine so that it accepts • formalize • modernize • summarize • concretize • sterilize • but rejects • *summaryize • *concreteize • *sterileize s([_|L]) :- s(L). s([X,i,z,e]) :- \+ vowel(X). vowel(e). vowel(y).
Homework 4: Question 2 • (10pts) • Part 1: (8pts) • Transform the NDFSA shown on the right into a deterministic FSA • Give the Prolog implementation of the machine • Part 2: (2pts) • what does this machine do? • i.e. what is the language it accepts? > q4
NDFSA Set-of-states construction a a a a b a b b b b Homework 4: Question 2 {q1 ,q2 ,q4 } {q1 ,q2 ,q4 } {q1 ,q2 } q4 { q1 } {q1 ,q3 ,q4 } {q1 ,q3 ,q4 } {q1 ,q3 } deterministic FSA
Part 2: (2pts) what does this machine do? i.e. what is the language it accepts? anything with “aa” or “bb” in it a a a b b b Homework 4: Question 2 seen two “a”s seen one “a” { q1 } seen two “b”s seen one “b”
Part 2: (2pts) what does this machine do? i.e. what is the language it accepts? anything with “aa” or “bb” in it a a a b a,b b b Homework 4: Question 2 seen two “a”s or two “b”s seen two “a”s or two “b”s seen one “a” { q1 } seen one “b”
Last time VP → VP PP Problem left recursion (infinite loop) Idea: control/restrict how many times we can loop around... Lookahead: spot a preposition (by) in the input list Add a mark indicating how many prepositions we’ve seen Controlling Left Recursion
Idea: have the lookahead check “mark” the preposition so 2nd time around the recursion, preposition is already marked and the recursion fails Implementation: modify the input list the ball was hit by me the ball was hit by mark me where mark is an artificial word introduced into the input list delete the extraneous mark when we parse the preposition Modified Prolog rule: L1 is the input list to VP vp(vp(A,B), L1, L2) :- append(Left,[by|Right],L1), \+ Right = [mark|_], append(Left,[by,mark|Right],L1p), vp(A, L1p, L3), pp(B, L3, L1). this succeeds if it finds by in L1 and mark is not already next to by and replaces it with by mark Modified preposition grammar rule: preposition(p(by)) --> [by,mark]. extra mark introduced into the input must be removed Controlling Left Recursion
So far, we’ve been using Prolog DCG rules to implement natural language constraints such as: determiner-noun agreement the/*a men subject-verb number agreement the men were/*was also constructions such as: passives and progressives be V+en, be V+ing optional by-phrase So Far...
Today’s Topic • with these properties in mind, • want to take a step back, • and look at the properties of certain constrained grammar rules called regular grammars
Regular Grammars • regular grammars are a class of formal grammars with a special restriction • akaChomsky hierarchy type-3 grammars • production rules are constrained to have normal forms L or R • L: x --> y, [t].x --> [t]. (left recursive) or • R: x --> [t], y.x --> [t]. (right recursive) • where x and y represent non-terminals and • t represents a terminal. • note: • can’t have both left and right recursive rules at the same time • can’t have ternary branching, e.g. x --> y, [t], z. • can’t have a single nonterminal only, e.g. x --> y.
FSA Regular Expressions Equivalence NDFSA FSA with -transitions Regular Grammars
t x t x y x t A B A A a A b A a a b a Regular Grammars ➡ FSA • each regular grammar rule has a FSA analog (and vice versa) • example: right recursive form FSA • x --> [t]. • x --> [t], y. • x --> [t], x. • note: • we can transform a left recursive regular grammar into a weakly-equivalent right recursive regular grammar, and vice-versa
a s x y b a > ! z FSA ➡ Regular Grammars • from lecture 8 • Prolog FSA implementation • regular expression: ba+! • query • ?- s([b,a,a,’!’]). • code • s([b|L]) :- x(L). • x([a|L]) :- y(L). • y([a|L]) :- y(L). • y([‘!’|L]) :- z(L). • z([]).
a s x y b a > ! z FSA ➡ Regular Grammars • equivalent (right-recursive) regular grammar • s --> [b], x. • x --> [a], y. • y --> [!]. • y --> [a], y. • by construction • query • ?- s(L,[]). • is it really equivalent to the FSA? • s([b|L]) :- x(L). • x([a|L]) :- y(L). • y([a|L]) :- y(L). • y([‘!’|L]) :- z(L). • z([]). • query • ?- s(L).
s b x y a a y ! Parse Tree • regular grammar • s --> [b], x. • x --> [a], y. • y --> [!]. • y --> [a], y. • parse tree version • add an extra argument • s(s(b,X)) --> [b], x(X). • x(x(a,Y)) --> [a], y(Y). • y(y(!)) --> [!]. • y(y(a,Y)) --> [a], y(Y). • query • ?- s(P,[b,a,a,!],[]). • X = s(b,x(a,y(a,y(!)))) ? ; • no
s s b x x ! y a y a a y y a ! b Right and Left Recursive Regular Grammars • right recursive regular grammar • s(s(b,X)) --> [b], x(X). • x(x(a,Y)) --> [a], y(Y). • y(y(!)) --> [!]. • y(y(a,Y)) --> [a], y(Y). • left recursive regular grammar • s(s(X,!)) --> x(X),[!]. • x(x(Y,a)) --> y(Y),[a]. • y(y(b)) --> [b]. • y(y(Y,a)) --> y(Y),[a]. • same query • ?- s(P,[b,a,a,!],[]). • X = s(x(y(y(b),a),a),!) ? ; • [what happens?] are they equivalent?
right recursive regular grammar s(s(b,X)) --> [b], x(X). x(x(a,Y)) --> [a], y(Y). y(y(!)) --> [!]. y(y(a,Y)) --> [a], y(Y). left recursive regular grammar s(s(X,!)) --> x(X),[!]. x(x(Y,a)) --> y(Y),[a]. y(y(b)) --> [b]. y(y(Y,a)) --> y(Y),[a]. same query ?- s(P,[b,a,a,!],[]). X = s(x(y(y(b),a),a),!) ? ; [what happens?] they are not equivalent wrt Prolog execution after hitting semicolon ; right recursive grammar terminates left recursive grammar goes into an infinite loop explanation recursive rule is the 4th rule shown in both grammar right recursive rule must match terminal a before recursing Right and Left Recursive Regular Grammars
Determiner-Noun Agreement • idea • give determiners a number feature as well • and make it agree with the noun • rules • thecan combine with singular or plural nouns • a can combine only with singular nouns • from lecture 12 • data • the man/men • a man/*a men • grammar (NP section) • np(np(Y)) --> pronoun(Y). • np(np(D,N)) --> det(D,Number), common_noun(N,Number). • det(det(the),sg) --> [the]. • det(det(the),pl) --> [the]. • det(det(a),sg) --> [a]. • common_noun(n(ball),sg) --> [ball]. • common_noun(n(man),sg) --> [man]. • common_noun(n(men),pl) --> [men]. • pronoun(i) --> [i]. • pronoun(we) --> [we].
questions is the determiner-noun system a regular grammar? can we write a regular grammar for it? determiner-noun grammar np(np(Y)) --> pronoun(Y). np(np(D,N)) --> det(D,Number), common_noun(N,Number). det(det(the),sg) --> [the]. det(det(the),pl) --> [the]. det(det(a),sg) --> [a]. common_noun(n(ball),sg) --> [ball]. common_noun(n(man),sg) --> [man]. common_noun(n(men),pl) --> [men]. pronoun(i) --> [i]. pronoun(we) --> [we]. Determiner-Noun Agreement
question 1 is the determiner-noun system a regular grammar? No for example: rules highlighted in red aren’t in regular grammar form determiner-noun grammar np(np(Y)) --> pronoun(Y). np(np(D,N)) --> det(D,Number), common_noun(N,Number). det(det(the),sg) --> [the]. det(det(the),pl) --> [the]. det(det(a),sg) --> [a]. common_noun(n(ball),sg) --> [ball]. common_noun(n(man),sg) --> [man]. common_noun(n(men),pl) --> [men]. pronoun(i) --> [i]. pronoun(we) --> [we]. Determiner-Noun Agreement
question 2 can we write a regular grammar for it? Yes regular grammar shown on right determiner-noun regular grammar np(np(i)) --> [i]. np(np(we)) --> [we]. np(np(the,N)) --> [the], common_nounsg(N). np(np(the,N)) --> [the], common_nounpl(N). np(np(a,N)) --> [a], common_nounsg(N). common_nounsg(n(ball)) --> [ball]. common_nounsg(n(man)) --> [man]. common_nounpl(n(men)) --> [men]. Determiner-Noun Agreement
by extension this determiner-noun system also has a FSA encoding determiner-noun regular grammar np(np(i)) --> [i]. np(np(we)) --> [we]. np(np(the,N)) --> [the], common_nounsg(N). np(np(the,N)) --> [the], common_nounpl(N). np(np(a,N)) --> [a], common_nounsg(N). common_nounsg(n(ball)) --> [ball]. common_nounsg(n(man)) --> [man]. common_nounpl(n(men)) --> [men]. the np > a common_nounpl the common_nounsg i men we ball man Determiner-Noun Agreement note: machine is a NDFSA. can be transformed into a deterministic machine