/* Simple ELIZA -- a natural language misunderstander -- Alan Bundy 15-Nov-79, Richard O'Keefe 17-Feb-81 */ :- [readin]. % load read_in(-) adn its support. :- public eliza/0. :- mode eliza. eliza :- read_in(Input), mandatory_substitutions(Input, Clean), Clean \== [bye,.], rule(Given, Yield), match(Given, Clean), reply(Yield), nl, !, eliza. eliza :- write('I hope I have helped you.'), nl. :- mode mandatory_substitutions(+, -), idiom(+, -). mandatory_substitutions(Given, Yield) :- idiom(Phrase, Translation), append(Phrase, Rest_of_Given, Given), append(Translation, Rest_of_Yield, Yield), !, mandatory_substitutions(Rest_of_Given, Rest_of_Yield). mandatory_substitutions([Period], [.]) :- !. mandatory_substitutions([Word|Rest_of_Given], [Word|Rest_of_Yield]) :- !, mandatory_substitutions(Rest_of_Given, Rest_of_Yield). idiom([i], [you]). idiom([me], [you]). idiom([we], [you]). idiom([us], [you]). idiom([you], [i]). idiom([my], [your]). idiom([mine], [yours]). idiom([our], [your]). idiom([ours], [yours]). idiom([your], [my]). idiom([yours], [mine]). idiom([am], [are]). idiom([you,are],[i,am]). idiom([stop], [bye]). idiom([quit], [bye]). idiom([goodbye],[bye]). idiom([please], []). :- mode match(+, +), append(?, ?, ?). 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). :- mode reply(+). reply([Head|Tail]) :- reply(Head), !, reply(Tail). reply([]). reply(Proper_Word) :- write(' '), write(Proper_Word). rule([you,are,X,.], [how,long,have,you,been,X,?]). 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], [please,go,on,.]).