Download presentation
Presentation is loading. Please wait.
1
LING 438/538 Computational Linguistics Sandiway Fong Lecture 7: 9/12
2
2 Administrivia no class (this) Thursday today –homework 2 we will go through the questions in class usual rules apply... –cite your sources (if you use them) –due next Tuesday –in my mailbox by midnight
3
3 Last Time the definite clause grammar (DCG) system –uses the same computation rule and query format as regular Prolog programs –grammar rules get translated automatically into an equivalent Prolog program s-->[a],b. s(L1,L3):-'C'(L1, a, L2),b(L2,L3). –difference list pairs: –L1 - L3 “s spans the list L1 minus L3” –L1 - L2, L2 - L3. ?- s(List,[]). (grammar rule query)
4
4 Last Time the definite clause grammar (DCG) system –uses the same computation rule and query format as regular Prolog programs –grammar rules get translated automatically into an equivalent Prolog program s-->[a],b. s(L1,L3):-'C'(L1, a, L2),b(L2,L3). other concepts –difference list pairs: L1 - L3, L1 - L2, L2 - L3. –adding an extra argument to compute a syntax tree
5
5 Last Time Prolog grammar –s --> [b], a, [’!’]. –a --> [a]. (base case) –a --> [a], a. (right recursive case) s b‘!’‘!’ a a a a s(b,a(a,a(a)),’!’) extra argument to hold a parse tree modified Prolog grammar –a(a(a)) --> [a]. –a(a(a,A)) --> [a], a(A). –s(s(b,A,’!’)) --> [b], a(A), [’!’]. query –?- s(Parse,List,[]).
6
6 Today’s Topic Regular Grammars (Type-3)
7
7 Chomsky Hierarchy Regular Grammars FSA Regular Expressions DCG = Type-0 Type-3 Type-2 Type-1 from lecture 5
8
8 Regular Grammars Regular or Chomsky hierarchy type-3 grammars –are a class of formal grammars with a restricted RHS LHS → RHS“LHS rewrites/expands to RHS” format –x --> y, [t].x --> [t]. (left recursive) or –x --> [t], y.x --> [t]. (right recursive) –where x and y are non-terminals and –t represents a terminal. i.e. all rules contain only a single non-terminal, and (possibly) a single terminal) on the right hand side Note: –can ’ t have both left and right recursive rules at the same time
9
9 Regular Grammars example (from previous lecture) –s --> [a],b. –b --> [a],b. –b --> [b],c. –b --> [b]. –c --> [b],c. –c --> [b]. query –?- s(L,[]). regular –grammar only contains right recursive rules –or a rule containing a single terminal symbol –right recursive regular grammar
10
10 Is sheeptalk regular? regular grammar format x --> y, [t]. x --> [t]. (left recursive) or x --> [t],y. x --> [t]. (right recursive) DCG Regular Grammar Rules ( ba! baa! baaa! baaaa! …) –s --> [b], x. –x --> [a], y. –y --> [‘!’]. –y --> [a], y. (note: not y --> y, [a]. ) Note: –can ’ t have both left and right recursive rules at the same time –otherwise, we can write a grammar for some non-regular languages like a n b n DCG Rules ( ba! baa! baaa! baaaa! …) –s --> [b], a, [‘!’]. –a --> [a]. –a --> a, [a].
11
11 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 rule restrictions –limit the generative capacity or power of regular grammars strictly speaking, –allowing an extra argument permits regular grammars to have more power –but using the extra argument to compute parses only doesn’t
12
12 Homework Exercise 2 –a regular grammar for object relative clauses example – the man that I saw grammar –dp --> [the], np. –np --> [man], relc. –relc --> [that], s. –s --> [i], vp. –% object relative sentence –vp --> [saw]. query ?- dp([the,man,that,i,saw],[]). Yes
13
13 Homework Exercise 2 438/538 Question 1: (2pts) –add regular grammar rules to handle additional examples –the man that you saw –the man that you met Question 2 (2pts) –modify your grammar to generate parses query ?- dp(Tree,[the,man,that,i,saw],[]).
14
14 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 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
15
15 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
16
16 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
17
17 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 (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
18
18 Homework Exercise 4 beyond regular grammars –a n b n = {ab, aabb, aaabbb,... } –is not regular however, a grammar with both left and right recursive rules can accept it –a --> [a], b. –b --> [b]. –b --> a, [b]. examples –this grammar accepts –aabb aaaabbbb –and rejects –aab aaaabbbbb B A b a A B A b a
19
19 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
20
20 Summary Homework 2 –Exercise 1: 4pts –Exercise 2: 9pts –Exercise 3: 3pts + 4pts (optional 438) –Exercise 4: 5pts (optional 438/538)
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.