/* Eliza in Picat. ' Analogy program. This Picat version is based on the Prolog code from Leon S. Sterling: "The Art of Prolog", 2nd edition, page 273ff This Picat model was created by Hakan Kjellerstrand, hakank@gmail.com See also my Picat page: http://www.hakank.org/picat/ */ % import util. % import cp. main => go. /* Here is a sample dialog, from The Art of Prolog, figure 14.6, page 273. (Don't forget the trailing ".") Picat> cl(eliza), eliza [i, am, unhappy]. How long have you been unhappy ? [six, months, can, you, help, me]. What makes you think I help you ? [you, make, me, think, of, my, father, and, my, brother]. Please you tell me more about father [I, like, teasing, father]. Does anyone else in your family like teasing father ? [no, only, me]. Please go on . [bye]. [Goodbye. I hope I have helped you] */ go => eliza, nl. go2 => eliza([i, am, unhappy]), nl. eliza ?=> Input = read_term(), eliza(Input). eliza([bye]) ?=> println(['Goodbye. I hope I have helped you']). eliza(Input) => pattern(Stimulus,Response), match(Stimulus,Table,Input), match(Response,Table,Output), reply(Output), Input1 = read_term(), eliza(Input1). reply(HT) ?=> HT=[Head|Tail], print(Head), print(' '), reply(Tail). reply([]) => nl. pattern(Stimulus,Response) ?=> Stimulus = [i,am,1], Response = ['How',long,have,you,been,1,?]. pattern(Stimulus, Response) ?=> Stimulus = [1,you,2,me], Response = ['What',makes,you,think,'I',2,you,?]. pattern(Stimulus, Response) ?=> Stimulus = [i,like,1], Response = ['Does',anyone,else,in,your,family,like,1,?]. pattern(Stimulus, Response) ?=> Stimulus = [i,feel,1], Response = ['Do',you,often,feel,that,way,?]. pattern(Stimulus, Response) ?=> Stimulus = [1,X,2], Response = ['Please',you,tell,me,more,about,X], important(X). pattern(Stimulus, Response) => Stimulus = [1], Response = ['Please',go,on,'.']. index(-) important(father). important(mother). important(sister). important(brother). important(son). important(daughter). match(NPattern,Table,Target) ?=> NPattern = [N|Pattern], integer(N), lookup1(N,Table,LeftTarget), append(LeftTarget,RightTarget,Target), match(Pattern,Table,RightTarget). match(WordPattern,Table,WordTarget) ?=> WordPattern = [Word|Pattern], WordTarget = [Word|Target], atom(Word), match(Pattern,Table,Target). match([],_Table,Target) => Target = []. lookup1(X,XV,V) ?=> XV = [(X,V)|_XVs]. lookup1(X,XV,V) => XV = [(X1,_V1)|XVs], X != X1, lookup1(X,XVs,V).