/* Very Simple ELIZA - LIZ -- a natural language misunderstander -- Alan Bundy 15-Nov-79, Richard O'Keefe 17-Feb-81 Peter Ross 5-Nov-81, Helen Pain 26-2-85 */ /* The top level: call liz (so named 'cos it's a mini-version of ELIZA) to get going. ?- liz. The predicate "read" reads in sentences, input as a list. It will read in input: [are,you,a,computer]. NOTE: use lower-case letters at the front of words, this is a simple beast! */ liz :- write('input:'), nl, read(Input), nl, alter(Input,Clean), Clean \== [bye], rule(Given, Yield), match(Given, Clean), reply(Yield), nl, liz. liz :- write('I hope I have helped you.'), nl. /* How it works: imagine typing [do,you,like,me]. read sets Input = [do,you,like,me] then alter(Input,Clean) sets Clean = [do,i,like,you] Then rule(Given,Yield) will come up with Given = [you,are,X] Yield = [how,long,have,you,been,X] and match(Given,Clean) will fail (see below), backtracking to rule(Given,Yield) which comes up with Given = [X,i,Y,you] Yield = [what,makes,you,think,i,Y,you] whereupon match(Given,Clean) can now succeed, in the process instantiating X to 'do' and Y to 'like', so that Yield is now instantiated to [what,makes,you,think,i,like,you] . The predicate reply prints this out.... Exercise 1: follow through the input [i,am,fed,up]. Exercise 2: copy this file into one of your own, and extend the range of responses it can make. */ alter([],[]). alter([H|T],[X|Y]):- change(H,X), alter(T,Y). change(i,you). change(me,you). change(ours,yours). change(my,your). change(we,you). change(us,you). change(you,i). change(you,me). change(are,am). change(your,my). change(yours,mine). change(am,are). change(mine,yours). change(our,your). change(stop,bye). change(quit,bye). change(goodbye,bye). change(X,X). match([Head|Rest], Fragment) :- append(Head, Leftover, Fragment), match(Rest, Leftover). match([Head|Rest], [Head|Leftover]) :- match(Rest, Leftover). match([], []). append([], List, List). append([Head|Tail], List, [Head|Rest]) :- append(Tail, List, Rest). reply([Head|Tail]) :- reply(Head), reply(Tail). reply([]). reply(Proper_Word) :- tab(1), write(Proper_Word). rule([you,are,X], [how,long,have,you,been,X]). rule([X,me,Y,you], [what,makes,you,think,i,Y,you]). rule([X,i,Y,you], [what,makes,you,think,i,Y,you]). rule([you,like,Y], [does,anyone,else,in,your,family,like,Y]). rule([you,feel,X], [do,you,often,feel,that,way]). rule([X], [X]).