:-module(shrink,[]).

:-[library(bim)].

run:-
	write('Welcome to my sofa'),
	nl, nl,
	repeat,
	getsentence(Sent),
	pick_response(Sent, Response),
	write_list(Response), nl,
	Response = [goodbye].

%---------------------------------------------------------------------------
getsentence(Words) :-
	get0(Ch),
	getrest(Ch, Words).

getrest(10, []) :- !.
getrest(32, Words) :-
	!,
	getsentence(Words).
getrest(Ch, [W|Ws]) :-
	getword(Ch, Chs, Next),
	name(W, Chs),
	getrest(Next, Ws).

getword(10, [], 10) :- !.
getword(32, [], 32) :- !.
getword(Ch, [Ch|Chs],Next) :-
	get0(Ch1),
	getword(Ch1,Chs,Next).
%---------------------------------------------------------------------------
pick_response(Sent, R) :-
	candidate_responses(Sent, Cands),
	random_member(R, Cands).

candidate_responses(Sent, Rs) :-
	findall(R, response(Sent,R), Rs0),
	punt_if_stumped(Rs0, Rs).

random_member(X, List) :-
	length(List, N),
	random(Rand),
	I is Rand mod N + 1,
	nth(I, List, X).

nth(1,[X|_],X).
nth(I, [_|T], X) :-
	I > 1,
	I1 is I - 1,
	nth(I1, T, X).

punt_if_stumped([], List) :-
	findall(R, punt(R), List).
punt_if_stumped([H|T], [H|T]).

%---------------------------------------------------------------------------
spit([],[]).
spit([H|T], [H1|T1]) :-
	you_me(H, H1),
	spit(T,T1).

you_me(i,you) :- !.
you_me(me,you) :- !.
you_me(you,me) :- !.
you_me(my,your) :- !.
you_me(your,my) :- !.
you_me(yours,mine) :- !.
you_me(mine,yours) :- !.
you_me(am,are) :- !.
you_me(X,X).

%---------------------------------------------------------------------------
response([bye],[goodbye]).
response([hello], [how,can,i,help,you]).
response([i, am | X],[please, tell, me, Wword, you, are | X1]) :-
	getWword(Wword),
	spit(X,X1).
response([i,have|X],[how,long,have,you,had|X1]) :-
	spit(X,X1).
response([i,feel|_],[i,sometimes,feel,the,same,way]).
response([because|_],[is,that,really,the,reason]).
response([],[please,say,something]).
response([yes|X],[how,can,you,be,so,sure |X1]) :-
	spit(X,X1).
response([you, are|X],[oh,yeah,i,am|X1]) :-
	spit(X,X1).
response([Verb|X],[oy,yoi,yoi,you,want,that,i,should,go,and,Verb|X1]) :-
	verb(Verb),
	spit(X,X1).
response([Wword|_],[you,tell,me,Wword]) :-
	wWord(Wword).
response([Dword,you|X],[perhaps,i,Dword|X1]) :-
	dWord(Dword),
	spit(X,X1).
response([do,you,think|_],[i,think,you,should,answer,that,yourself]).
response(Sent,[for,dream,analysis,see,freud]) :-
	member(dream,Sent).
response(Sent,[all,is,fair,in,love,and,war]) :-
	member(love,Sent).
response(Sent,[dont,be,so,negative]) :-
	member(no,Sent).
response(Sent,[be,more,decisive]) :-
	member(maybe,Sent).
response([am,i|X], [do, you,think,you,are|X]).
response(Sent, [tell, me, more, about, your, W]) :-
	append(_,[my, W|Ws], Sent), 
	member(me, Ws).
response(Sent,Sent1) :-
	member(i,Sent),
	spit(Sent,Sent1).

verb(go). verb(have). verb(be). verb(try). verb(eat). verb(take).
verb(help). verb(make). verb(get). verb(jump). verb(write). verb(type).
verb(fill). verb(put). verb(turn). verb(compute). verb(calculate).
verb(think). verb(drink). verb(blink). verb(crash). verb(crunch). verb(add).

dWord(do). dWord(can). dWord(should). dWord(would).

wWord(why). wWord(where). wWord(when). wWord(what).

getWword(W) :-
	findall(X,wWord(X), Ws),
	random_member(W,Ws).

punt([please,go,on]).
punt([tell,me,more]).
punt([i,see]).
punt([what,does,that,indicate]).
punt([but,why,be,concerned,about,it]).
punt([just,tell,me,how,you,feel]).   

%---------------------------------------------------------------------------
write_list([]) :-
	nl.
write_list([H|T]) :-
	write(H),
	write(' '),
	write_list(T).














