Presentation is loading. Please wait.

Presentation is loading. Please wait.

LING 438/538 Computational Linguistics Sandiway Fong Lecture 9: 9/21.

Similar presentations


Presentation on theme: "LING 438/538 Computational Linguistics Sandiway Fong Lecture 9: 9/21."— Presentation transcript:

1 LING 438/538 Computational Linguistics Sandiway Fong Lecture 9: 9/21

2 2 Administrivia Homework 2 Review

3 3 Homework Exercise 1 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 s --> [a],b.s(s(a,B)) --> [a],b(B). b --> [a],b. b(b(a,B)) --> [a],b(B). b --> [b],c. b(b(b,C)) --> [b],c(C). b --> [b].b(b(b)) --> [b]. c --> [b],c. c(c(b,C)) --> [b],c(C). c --> [b]. c(c(b)) --> [b].

4 4 Homework Exercise 2 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 –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

5 5 Homework Exercise 2 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))))))

6 6 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. 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

7 7 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. Larger question the grammar is regular but is it linguistically justified?

8 8 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. 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

9 9 Homework Exercise 2 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

10 10 Grammars as Generators 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,!] ? ; 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

11 11 Homework Exercise 3 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

12 12 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.

13 13 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] ?

14 14 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

15 15 Homework Exercise 4 438/538 Optional (5pts) –write a grammar using left and right recursive regular rules for the non-regular language –ww R where w  {a,b} +, i.e. any non-empty sequence of a’s and b’s, and w R is w in reverse order examples –aabbaa w = aab w R = baa –aaaa w = aa w R = aa –*aaaaa –*abab submit both your grammar and the output

16 16 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

17 17 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?

18 18 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)


Download ppt "LING 438/538 Computational Linguistics Sandiway Fong Lecture 9: 9/21."

Similar presentations


Ads by Google