180 likes | 351 Views
LING 438/538 Computational Linguistics. Sandiway Fong Lecture 9: 9/21. Administrivia. Homework 2 Review. Question 1 (4pts) 438/538 convert the following grammar to generate a parse s --> [a],b. b --> [a],b. b --> [b],c. b --> [b]. c --> [b],c. c --> [b]. new query
E N D
LING 438/538Computational Linguistics Sandiway Fong Lecture 9: 9/21
Administrivia • Homework 2 Review
Question 1 (4pts) 438/538 convert the following grammar to generate a parse s --> [a],b. b --> [a],b. b --> [b],c. b --> [b]. c --> [b],c. c --> [b]. new query ?- s(Tree,List,[]). submit both your grammar and example parses Homework Exercise 1 s --> [a],b. s(s(a,B)) --> [a],b(B). b --> [a],b. b(b(a,B)) --> [a],b(B). b(b(b,C)) --> [b],c(C). b --> [b],c. b --> [b]. b(b(b)) --> [b]. c(c(b,C)) --> [b],c(C). c --> [b],c. c(c(b)) --> [b]. c --> [b].
Question 1: (2pts) grammar dp --> [the], np. np --> [man], relc. relc --> [that], s. s --> [i], vp. vp --> [saw]. add regular grammar rules to handle additional examples the man that you saw the man that you met Homework Exercise 2 • the man that you saw • the man that you met • relevant rules • s --> [i], vp. • vp --> [saw]. • new rules • s --> [you], vp. • vp --> [met]. Note: also accepts now: • the man that i met grouping clauses together * clauses for user:s/2 are not together
grammar dp --> [the], np. np --> [man], relc. relc --> [that], s. s --> [i], vp. s --> [you], vp. vp --> [saw]. vp --> [met]. Question 2 (2pts) modify your grammar to generate parses exercising some freedom here... new grammar dp(dp(d(the),NP)) --> [the], np(NP). np(np(n(man),RC)) --> [man], relc(RC). relc(cp(c(that),S)) --> [that], s(S). s(s(np(i),VP)) --> [i], vp(VP). s(s(np(you),VP)) --> [you], vp(VP). vp(vp(v(saw))) --> [saw]. vp(vp(v(met))) --> [met]. example query ?- dp(T,[the,man,that,i,saw],[]). T = dp(d(the),np(n(man),cp(c(that),s(np(i),vp(v(saw)))))) Homework Exercise 2
Grammar dp --> [the], np. np --> [man], relc. relc --> [that], s. s --> [i], vp. s --> [you], vp. vp --> [saw]. vp --> [met]. (Additional) rules vp --> [saw], relc. vp --> [met], relc. still regular One more set of VP rules vp --> [heard]. vp --> [heard], relc. Homework Exercise 2 • Question 3 (3pts) • modify your grammar to accept recursive embeddings of the form • the man that I saw that you heard • the man that you heard that I saw • submit both your grammar and the output • Note: make sure your grammar is still regular
Grammar dp --> [the], np. np --> [man], relc. relc --> [that], s. s --> [i], vp. s --> [you], vp. vp --> [saw]. vp --> [met]. vp --> [saw], relc. vp --> [met], relc. vp --> [heard]. vp --> [heard], relc. Larger question the grammar is regular but is it linguistically justified? Homework Exercise 2
Grammar dp --> [the], np. np --> [man], relc. relc --> [that], s. s --> [i], vp. s --> [you], vp. vp --> [saw]. vp --> [met]. vp --> [saw], relc. vp --> [met], relc. vp --> [heard]. vp --> [heard], relc. Not regular relc --> s. To preserve form np --> [man], s. Homework Exercise 2 • Question 4 (2pts) • modify your grammar to allow the option of dropping the relative pronoun that, • e.g. • the man that I saw • the man I saw • submit both your grammar and the output • Note: make sure your grammar is still regular
Grammar dp --> [the], np. np --> [man], relc. np --> [man], s. relc --> [that], s. s --> [i], vp. s --> [you], vp. vp --> [saw]. vp --> [met]. vp --> [saw], relc. vp --> [met], relc. vp --> [heard]. vp --> [heard], relc. Queries ?- dp([the,man,you,heard,that,i,saw],[]). yes ?- dp([the,man,that,you,heard,i,saw],[]). no Homework Exercise 2
regular sheeptalk s --> [b], x. x --> [a], y. y --> [‘!’]. y --> [a], y. query ?- s(X,[]). X = [b,a,!] ? ; X = [b,a,a,!] ? ; X = [b,a,a,a,!] ? ; Grammars as Generators a DCG can be used not only to do recognition (yes/no) and return parse structures but can also be used to generate strings of the language generated by the grammar
438/538 this grammar will go into an infinite loop when used as a generator s --> [a],b. b --> [a],b. b --> [b],c. b --> [b]. c --> [b],c. c --> [b]. query ?- s(L,[]). i.e. give me strings generated by the grammar (one at a time) {ab, aab, abb, aaab, aabb, abbb, aaaab, …} will give a memory error Homework Exercise 3
Question 1 (3pts) re-arrange example grammar so it doesn’t go into an infinite loop immediately but instead will generate strings from the language one at a time query ?- s(L,[]). L = string1 ; L = string2 ; L = string3 ; and so on… submit both the grammar and the run for the above query Grammar s --> [a],b. b --> [a],b. recursive case b --> [b],c. b --> [b]. base case c --> [b],c. c --> [b]. Grammar-writing Heuristic Avoid loops by having: base case first recursive case 2nd Revised Grammar s --> [a],b. b --> [b]. base case b --> [a],b. recursive case b --> [b],c. c --> [b]. c --> [b],c. Homework Exercise 3
Revised Grammar s --> [a],b. b --> [b]. base case b --> [a],b. recursive case b --> [b],c. c --> [b]. c --> [b],c. Query (Generator) ?- s(X,[]). X = [a,b] ? ; X = [a,a,b] ? ; X = [a,a,a,b] ? ; X = [a,a,a,a,b] ? ; X = [a,a,a,a,a,b] ? Homework Exercise 3
(438 optional/538 mandatory) Question 2 (4pts) is it possible to re-arrange the example grammar to enumerate all the strings of the language? explain your answer It’s a limitation of Prolog Prolog simply picks the first matching rule it tries other rules when it fails by keeping track of where it last had a choice and expanding on that (which may generate new (i.e. even later) choice points) so earlier choice points can get buried/lost As a result it can’t deal with the two (simultaneous) loops Homework Exercise 3
Homework Exercise 4 • 438/538 Optional (5pts) • write a grammar using left and right recursive regular rules for the non-regular language • wwR • where w {a,b}+, i.e. any non-empty sequence of a’s and b’s, and • wR is w in reverse order • examples • aabbaa w = aab wR = baa • aaaa w = aa wR = aa • *aaaaa • *abab • submit both your grammar and the output
Homework Exercise 4 • Let’s work on the logic together here • Crucial Idea: • It’s about memory • Where can memory be stored? • Answer: in the name of the nonterminal
Homework Exercise 4 • Start with seeing an “a” • This means we have to remember we have to have an “a” at the end • We define the nonterminal A to encode or indicate that we need an “a” • s --> [a], a. • a --> [a]. (simplest case) • what is the more general case?
Homework Exercise 4 • Grammar • s --> [a], a. • s --> [b], b. • a --> [a]. • a --> s, [a]. • b --> [b]. • b --> s, [b]. • Note: • this grammar is not regular (contains both left and right recursive rules)