180 likes | 260 Views
LING 388: Language and Computers. Sandiway Fong 9/22 Lecture 9. Administrivia. Homework 3 Review Homework 4 out today Due next Wednesday Usual rules. Homework Question 1.
E N D
LING 388: Language and Computers Sandiway Fong 9/22 Lecture 9
Administrivia • Homework 3 Review • Homework 4 out today • Due next Wednesday • Usual rules
Homework Question 1 (6pts) Give the complete (i.e. all answers) step-by-step computation tree explaining how Prolog obtains the solutions for ?- mem([1,2,3],X). given the database • mem([X|_],X). • mem([_|L],X):- mem(L,X).
Database mem([X|_],X). mem([_|L],X):- mem(L,X). ?- mem([1,2,3],X). matches base casemem([X’|_],X’). when X’=1 and X=X’ Answer: X=1 matches recursive casemem([_|L],X’) when [2,3]=L and X=X’ subgoal?- mem([2,3],X’). matches base casemem([X”|_],X”). when X”=2 and X’=X” Answer: X=2 Homework Question 1
Database mem([X|_],X). mem([_|L],X):- mem(L,X). ?- mem([1,2,3],X). matches base casemem([X’|_],X’). when X’=1 and X=X’ Answer: X=1 matches recursive casemem([_|L],X’) when [2,3]=L and X=X’ subgoal?- mem([2,3],X’). matches base casemem([X”|_],X”). when X”=2 and X’=X” Answer: X=2 Homework Question 1 matches recursive casemem([_|L’],X”) when [3]=L’ and X’=X” subgoal?- mem([3],X”). matches base case mem([X”’|_],X”’). when X”’=3 and X”=X”’ Answer: X=3
Homework Question 1 ?- mem([1,2,3],X). matches base casemem([X’|_],X’). when X’=1 and X=X’ Answer: X=1 matches recursive casemem([_|L],X’) when [2,3]=L and X=X’ subgoal?- mem([2,3],X’). matches base casemem([X”|_],X”). when X”=2 and X’=X” Answer: X=2 matches recursive casemem([_|L’],X”) when [3]=L’ and X’=X” subgoal?- mem([3],X”). matches base case mem([X”’|_],X”’). when X”’=3 and X”=X”’ Answer: X=3 • subgoal?- mem([3],X”). • matches recursive case • mem([_|L”],X”’) • when []=L” and X”=X”’ • subgoal?-mem([],X”’). • fails • No match
Homework Question 1 Tree-like… • ?- mem([1,2,3],X). • Base case • Recursive case • ?- mem([2,3],X’). • Base case • Recursive case • ?- mem([3],X”). • Base case • Recursive case • ?-mem([],X”’). • case matching fails
Database addNT(W,Wnt) :- atom_chars(W,L), append(L,[n,’\’’,t],Lnt), atom_chars(Wnt,Lnt). modal(should). “should is a modal” modal(would). “would is a modal” modal(could). “could is a modal” modal(may). “may is a modal” (4pts) Modify the definition of addNT/2 to accept only modals Demonstrate your program works correctly for: ?- addNT(should,X). ?- addNT(would,X). ?- addNT(john,X). Homework Question 2
Database addNT(W,Wnt) :- atom_chars(W,L), append(L,[n,’\’’,t],Lnt), atom_chars(Wnt,Lnt). modal(should). “should is a modal” modal(would). “would is a modal” modal(could). “could is a modal” modal(may). “may is a modal” Idea: make addNT/2 be true only if word W is a modal i.e. call modal(W) as a sub-query of addNT/2 Revised definition of addNT/2 addNT(W,Wnt) :- modal(W), atom_chars(W,L), append(L,[n,’\’’,t],Lnt), atom_chars(Wnt,Lnt). Homework Question 2
(4pts) Further modify your definition of addNT/2 to exclude the ungrammatical case: should shouldn’t would wouldn’t could couldn’t may *mayn’t i.e. ?- addNT(may,X). No Idea: make sure W cannot be may i.e. call \+ W = may as a sub-query of addNT/2 Revised definition of addNT/2 addNT(W,Wnt) :- \+ W = may, modal(W), atom_chars(W,L), append(L,[n,’\’’,t],Lnt), atom_chars(Wnt,Lnt). Homework Question 2 isn’t quite correct ?- addNT(X,Y).
(6pts) Extra Credit Question Notice that the following query doesn’t work: ?- addNT(X,'shouldn\'t'). ERROR: atom_chars/2: Arguments are not sufficiently instantiated Write the corresponding “subtract n’t” rule, call it subNT/2, for removing the n’t suffix: ?- addNT(X,'shouldn\'t'). X = should Homework Question 2 This error message occurs with the original definition of addNT/2. The revised definition of addNT/2 due to call to modal/1 doesn’t trigger it.
Original definition addNT(W,Wnt) :- atom_chars(W,L), append(L,[n,’\’’,t],Lnt), atom_chars(Wnt,Lnt). Query ?- addNT(should,X). instantiates W = should Definition becomes addNT(should,Wnt) :- atom_chars(should,L), append(L,[n,’\’’,t],Lnt), atom_chars(Wnt,Lnt). Query ?- ?- addNT(X,'shouldn\'t'). instantiates Wnt = 'shouldn\'t' Definition becomes addNT(W,’shouldn\’t’) :- atom_chars(W,L), append(L,[n,’\’’,t],Lnt), atom_chars(Wnt,Lnt). Homework Question 2 Problem! atom_chars cannot operate without either an atom or list supplied
Original definition addNT(W,Wnt) :- atom_chars(W,L), append(L,[n,’\’’,t],Lnt), atom_chars(Wnt,Lnt). Reverse the order of the sub-goals in the original definition subNT(W,Wnt) :- atom_chars(Wnt,Lnt), append(L,[n,’\’’,t],Lnt), atom_chars(W,L). Homework Question 2
(6pts) Define a predicate pallindrome/1 that is true when a word can be spelt the same forwards or backwards Examples: radar redivider abba Definition reverse([],[]). reverse([X|L],R) :- reverse(L,LR), append(LR,[X],R). Homework Question 3
(6pts) Define a predicate pallindrome/1 that is true when a word can be spelt the same forwards or backwards Definition reverse([],[]). reverse([X|L],R) :- reverse(L,LR), append(LR,[X],R). Examples: radar redivider abba Idea: [r,a,d,a,r] reversed is [r,a,d,a,r] i.e. the same list! so convert the word into a list and compute that the reverse is the same Homework Question 3 pallindrome(W) :- atom_chars(W,L),reverse(L,L).
New Homework • Use Microsoft Word
Homework 4 • Use file Article247_500.txt from the American National Corpus (ANC) • File is available from course homepage
Homework 4 • Question 1 (5pts): • Give a regular expression that highlights only the acronyms/initialisms, e.g. ESPN, PR, in the article • Give a list of those acronyms/initialisms • Question 2 (5pts): • Typically, a period is used to end a sentence. Can you give a regular expression that highlights only the end of sentence characters in this article? • How many sentences are there?