/*eliza*/ /*natural language conversational program Alan Bundy 15.11.79*/ /*variable marker*/ :- op(500,fx,[?]). /*top level*/ eliza :- read_in(Input), eliza(Input). eliza([bye,.]) :- !. eliza(Input) :- sr_pair(S,R), match(Input,S,off,Subst), replace(R,Subst,Output), print_list(Output), eliza. /*associative one way matcher*/ match([],[],off,[]). match([Word|RestSent],[Word|RestPatt],off,Subst) :- match(RestSent,RestPatt,off,Subst). match([Word|RestSent],[?Var|RestPatt],off,[[Word|Phrase]/Var|Subst]) :- match(RestSent,RestPatt,?Var,[Phrase/Var|Subst]). match(Sent,Patt,?Var,[[]/Var|Subst]) :- match(Sent,Patt,off,Subst). match([Word|RestSent],RestPatt,?Var,[[Word|Phrase]/Var|Subst]) :- match(RestSent,RestPatt,?Var,[Phrase/Var|Subst]). /*substitute text for variables*/ replace([],Subst,[]). replace([?Var|Rest],Subst,New) :- !, member(Phrase/Var,Subst), replace(Rest,Subst,Old), append(Phrase,Old,New). replace([Word|RestPatt],Subst,[Word|RestSent]) :- replace(RestPatt,Subst,RestSent). /*print list of words without brackets or commas*/ print_list([]) :- writef('\n \n',[]). print_list([Hd|Tl]) :- writef('%t ',[Hd]), print_list(Tl). /*stimulus response table*/ sr_pair([i,am,?x,.],[how,long,have,you,been,?x,?]). sr_pair([?x,you,?y,me,.],[what,makes,you,think,i,?y,you,?]). sr_pair([?x],[please,go,on,.]).