%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %% %% %% Version: 1.00 Date: 19/02/92 File: eliza.pl %% %% Last Version: File: %% %% Changes: %% %% 19/02/92 Created %% %% %% %% Purpose: %% %% %% %% Author: Steffen H\"olldobler %% %% %% %% Usage: prolog eliza.pl %% %% %% %% %% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% ?-use_module(library(lists)). % eliza: simulates a conversation via side-effects. eliza :- read_word_list(Input), eliza(Input), !. eliza([bye]) :- nl, write(['Goodbye, I hope I have helped you']), nl. eliza(Input) :- pattern(Stimulus,Response), match(Stimulus,Dictionary,Input), match(Response,Dictionary,Output), reply(Output), read_word_list(Input1), !, eliza(Input1). % match(Pattern,Dictionary,Words): pattern matches the list of words Words, % and matchings are recorded in the dictionary. match([N|Pattern],Dictionary,Target) :- integer(N), lookup(N,Dictionary,LeftTarget), append(LeftTarget,RightTarget,Target), match(Pattern,Dictionary,RightTarget). match([Word|Pattern],Dictionary,[Word|Target]) :- atom(Word), match(Pattern,Dictionary,Target). match([],_,[]). % lookup(Key,Dictionary,Value): Dictionary contains Value indexed under Key. % Dictionary is represented as a list of pairs (Key,Value). lookup(Key,[(Key,Value)|_],Value). lookup(Key,[(Key1,_)|Dictionary],Value) :- Key \= Key1, lookup(Key,Dictionary,Value). % pattern(Stimulus,Response): Response is an applicable response pattern % to the pattern Stimulus. pattern([i,am,1],[how,long,have,you,been,1,?]). pattern([1,you,2,me],[what,makes,you,think,i,2,you,?]). pattern([i,like,1],[does,anyone,else,in,your,family,like,1,?]). pattern([i,feel,1],[do,you,often,feel,that,way,?]). pattern([1,X,2],[can,you,tell,me,more,about,X,?]) :- important(X). pattern([1],[please,go,on]). important(father). important(mother). important(sister). important(brother). important(son). important(daughter). reply([Head|Tail]) :- write(Head), write(' '), reply(Tail). reply([]) :- nl. % read_word_list(Ws): reads in a list of words Ws using get/1 and get0/1. % Words are formed of lower case letters only and are seperated by blanks. % The end of the list of words is indicated by a period. read_word_list(Ws) :- get(C), read_word_list(C,Ws). read_word_list(C,[W|Ws]) :- % next word is found word_char(C), read_word(C,W,C1), read_word_list(C1,Ws). read_word_list(C,Ws) :- % ignore filling characters fill_char(C), get0(C1), read_word_list(C1,Ws). read_word_list(C,[]) :- % end of word is reached end_of_words_char(C). % read_word(C,W,C1): reads a word W given first character C and returns the % first symbol C1 after the word. read_word(C,W,C1) :- word_chars(C,Cs,C1), name(W,Cs). word_chars(C,[C|Cs],C0) :- word_char(C), !, get0(C1), word_chars(C1,Cs,C0). word_chars(C,[],C) :- \+ word_char(C). word_char(C) :- 97 =< C, C =< 122. % lower-case letter word_char(95). % underscore fill_char(32). % blank end_of_words_char(46). % period