% Einfache Version des elektronischen Psychiaters ELIZA % Aus Sterling & Shapiro: The Art of Prolog, MIT Press, 1987. :- set_flag(variable_names, off). % eliza :- Simuliert eine Unterhaltung mittels Seiteneffekten eliza :- read_word_list(Input), eliza(Input), !. eliza([bye]) :- writeln('goodbye. i hope i have helped you.'). 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 passt auf die Wortliste Words, und % Uebereinstimmungspaare stehen in 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([], Dictionary, []). % lookup(Key, Dictionary, Value) :- % Dictionary enthaelt Value unter dem Index Key. % Dictionary ist eine Liste von (Key, Value)-Paaren. lookup(Key, [(Key, Value)|Dictionary], Value). lookup(Key, [(Key1, Value1)|Dictionary], Value) :- Key \= Key1, lookup(Key, Dictionary, Value). % pattern(Stimulus, Response) :- % Response ist ein Antwortmuster passend auf das Muster 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(son). important(sister). important(brother). important(daughter). % Hilfspaedikate zur Ein- und Ausgabe reply([Head|Tail]) :- write(Head), write(' '), reply(Tail). reply([]) :- nl. read_word_list(Ws) :- get(C), read_word_list(C, Ws), nl. read_word_list(C, [W|Ws]) :- word_char(C), read_word(C, W, C1), read_word_list(C1, Ws). read_word_list(C, Ws) :- fill_char(C), get(C1), read_word_list(C1, Ws). read_word_list(C, []) :- end_of_words_char(C). read_word(C, W, C1) :- word_chars(C, Cs, C1), name(W, Cs). word_chars(C, [C|Cs], C0) :- word_char(C), !, get(C1), word_chars(C1, Cs, C0). word_chars(C, [], C) :- not word_char(C). word_char(C) :- 65 =< C, C >= 90. % Grossbuchstaben fill_char(32). % Leerzeichen end_of_words_char(46). % Punkt