
eliza :- read_word_list(Input), eliza(Input), !.
eliza([bye]) :- write_ln(['Goodbye. I hope I have helped you']).
eliza(Input) :- pattern(Stimulus, Response),
                match(Stimulus,Dictionary,Input),
                match(Response,Dictionary,Output),
                reply(Output).

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([],_,[]).

append([],Ys,Ys).
append([X|Xs],Ys,[X|Zs]) :- append(Xs,Ys,Zs).

lookup(Key,[(Key,Value)|_],Value).
lookup(Key,[(Key1,_)|Dictionary],Value) :- 
                  Key\=Key1, lookup(Key,Dictionary,Value).

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([i,hate,X],[did,X,tease,you,that,you,hate,him,?]) :-
                  important(X).
pattern([he,is,always,so,X,to,me],[you,should,try,to,ignore,him,!]) :-
                  important(X).
pattern([i,hate,1],[you,should,stop,1,immediately,!]).
pattern([i,do,not,like,1],[tell,me,about,something,you,like]).
pattern([i,can,not,X,it,anymore],[you,should,travel,!]) :-
                  trouble(X).

trouble(stand).
trouble(do).
trouble(support).
trouble(see).
trouble(believe).
trouble(get).

important(father).
important(mother).
important(son).
important(daughter).
important(sister).
important(brother).

pattern([1],[please,go,on]).

reply([Head|Tail]) :- write(Head), write(' '),reply(Tail).
reply([]) :- nl.
 
 


