mode none.
closure none.

shrink_me equals seq(greet, shrink).

	greet equals println('Enter a list of words without punctuation.  End the session with [bye].').

	shrink equals  seq( print_prompt('You: ' ), doc(read_term)).

	doc([bye])      equals println('Your bill is in the mail!').
	doc(Sentence)   equals seq(print('Me: '),
				   printlistln(parse(lex(Sentence))),
				   shrink).

	printlistln(L)       equals printlistln(0,L).
	printlistln(_,[])    equals println(' ').
	printlistln(_,[H|T]) equals printlistln(seq(print(H),print(' ')), T).

	seq(_,_) equals ' '.
	seq(_,_,_) equals ' '.

/**************************** lex *******************************/


lex(Sent) equals [scan(Sent, word_weights) | edit(Sent, changes)].


/**************************** edit *******************************/

edit([],    Changes) equals [].
edit([H|T], Changes) equals edit2(H,T,Changes,assoc2(Changes, H)).

	edit2(H,T,Changes, yyy)  equals  [H   | edit(T, Changes)].
	edit2(H,T,Changes, Ass)  equals  [Ass | edit(T, Changes)].

	assoc2({[A|B] \_}, A)  equals B.
	assoc2(_,          _)  equals yyy.


/****************************** scan ******************************/

scan(Stmt, WordWeights)  equals scan2(Stmt, [0], WordWeights).

	scan2([], [0|_], WW)     equals rest(searchkey(WW, xxxx)).
	scan2([], [_|B],  _)     equals B. 
	scan2([H|T], Answer, WW) equals scan3([H|T], Answer, 
						searchkey(WW, H), WW).

		searchkey({[Word|Weight]\_},Word) equals [Weight|script(Word)].
		searchkey(_		   ,_   ) equals [0].

	scan3(Stmt, Answer, Next, WW) equals
		if left(Next) > left(Answer) then scan2(rest(Stmt), Next, WW)
		else scan2(rest(Stmt), Answer, WW).

		left([H|T]) equals H.  
		rest([H|T]) equals T.


/************************  Parsing phase  ***************************/

parse([{Pat\Rest}| Stmt]) equals parse(Pat,Rest,Stmt,
				decompose(left(Pat), Stmt, [])).

parse(_,Rest, St, [_])  equals parse([Rest|St]).
parse(Pat,_, _, [H2|_]) equals reassemble(H2, rest(Pat)).


/************************  Decomposition  ***************************/


decompose([],    Stmt, Result) equals [true | Result].
decompose([0],   Stmt, Result) equals [app(Result, [Stmt]) | true].
decompose([0|X], Stmt, Result) equals decomp2(find(Stmt,left(X),[]),X,Result).
decompose([_|_],  [],     Result)  equals [[] | []].
decompose([W|P1], [W|S1], Result)  equals decompose(P1,S1,app(Result,[[W]])).
decompose([W1|P1],[W2|S1],Result)  equals [[] | []].

	find([],      W, R) equals [[]].
	find([W|S1],  W, R) equals [true, R, S1].
	find([W1|S1], W, R) equals find(S1,W,app(R,[W1])).

decomp2([[]|_], _, _) equals [[] | []].
decomp2([_,Phrase,Stmt|_], [Word|Pat], Result) equals
        decompose(Pat, Stmt, app(Result, [Phrase, [Word]])).

	app([],    X) equals X.
	app([H|T], Y) equals [H | app(T, Y)].


/************************  Reassembly  ***************************/


reassemble(Phrases, Rules) equals
        substitute(choose(rand(length(Phrases),length(Rules)), 
		   Rules), Phrases, []).

		choose(N, [H|Rest]) equals if N<2 then H else choose(N-1,Rest).

		length([]) equals 0.
		length([H|T]) equals 1+length(T).

		rand(P,N) equals rand2(P*37,N).

			rand2(R,N) equals 1 + R - ((R/N) * N).

	substitute([], Phrases, Result) equals Result.
	substitute([H|R1], Phrases, Result) equals
		if numberp(H)
		then substitute(R1, Phrases, app(Result, choose(H,Phrases)))
		else substitute(R1, Phrases, app(Result, [H])).


/*************************** Change list *************************/

changes equals
  { [ am | are ],
    [ i | you ],
    [ im | youre ],
    [ me | you ],
    [ mine | yours ],
    [ my | your ],
    [ myself | yourself ],
    [ you | i ],
    [ your | my ],
    [ yours | mine ],
    [ youre | im ],
    [ yourself | myself ],
    [ were | was ]
  }.


/*************************** Word Weights **************************/
/*			(abbreviated list)			   */

word_weights equals 
	{[xxxx  |0], [afraid|25], [alike |20], [always|30], [am    |8],
	 [are   |4], [i     |6],  [how   |2],  [were  |3],  [what  |3],
	 [why   |7], [when  |5],  [you   |2],  [yes   |1]
	}.


/************************* Abbreviated Script *****************************/

script(xxxx) contains 
             {[ [0],
               [do,you,feel,strongly,about,that],
               [please,go,on],
               [what,does,that,suggest,to,you],
               [what,i,hear,you,saying,is,1],
               [is,that,something,important,to,you],
               [i,would,like,to,hear,more,about,that]
             ]}.


script(afraid) contains
                {[[0,you,0,afraid,0,i],
                  [i,dont,quite,see,what,i,have,to,do,with,your,fear]
                ]}.
script(afraid) contains
                { [[0,you,0,afraid,of,0],
                   [what,is,the,worst,thing,about,6],
                   [what,about,6,is,especially,frightening]
                  ]}.
script(afraid) contains
                {[[0],
                  [well,you,know,what,churchill,said,about,fear],
                  [what,ideas,do,you,have,about,overcoming,this,fear]
                ]}.

script(alike) contains
		{[[0],
		 [what,resemblance,do,you,see],
		 [what,do,you,suppose,is,the,similarity]
                ]}.

script(always) contains
    		{[ [0],
                  [can,you,think,of,a,specific,example],
                  [what,incident,are,you,thinking,of],
                  [when,in,particular]
                ]}.

script(am) contains
	   {[ [0,are,you,0,i,0],
             [do,you,think,you,are]
           ]}.
script(am) contains
           {[ [0,are,you,0],
             [do,you,believe,you,are,4],
             [would,you,want,to,be,4],
             [what,would,it,mean,if,you,were,4]
           ]}.
script(am) contains
           {[ [0,you,are,sick,0],
             [do,you,think,that,coming,here,will,help,you,get,well]
           ]}.
script(am) contains
           {[ [0,you,are,0,sad],
             [is,this,sadness,a,big,thing,in,your,life]
           ]}.
script(am) contains
           {[ [0,you,are,0,unhappy,0],
             [sounds,like,sometimes,you,are,pretty,miserable]
           ]}.
script(am) contains
           {[ [0,you,are,0,not,happy,0],
             [tell,me,about,this,unhappiness]
           ]}.
script(am) contains
           {[ [0,you,are,0,happy,0 ],
             [it,is,good,to,see,you,so,happy]
           ]}.
script(am) contains
           {[ [0,you,are,0,i,0],
             [are,you,really],
             [it,sounds,like,you,really,are]
           ]}.
script(am) contains
           {[ [0,you,are,0],
             [are,you,concerned,that,you,are,4],
             [is,it,because,you,are,4,that,you,came,to,me],
             [do,you,think,it,is,normal,to,be,4]
           ]}.
script(am) contains
           {[ [0,are,0],
             [i,dont,quite,understand,that],
             [yes,you,do,seem,to,be],
             [do,you,wish,that,you,were,not]
           ]}.

script(are) contains
    	    {[ [are,i,0],
              [why,are,you,interested,in,whether,i,am,3,or,not],
              [would,you,prefer,if,i,werent,3],
              [perhaps,i,am,3,in,your,fantasies]
            ]}.
script(are) contains
            {[ [0,i,0,are,0],
              [perhaps,you,would,like,to,be,5],
              [what,makes,you,think,i,am,5],
              [does,it,please,you,to,believe,i,am,5],
              [perhaps,you,like,to,think,that,i,am,5]
            ]}.
script(are) contains
            {[ [0,we,are,0],
              [what,would,it,mean,if,we,were,4]
            ]}.
script(are) contains
            {[ [0,are,0],
              [did,you,think,they,might,not,be,3],
              [would,you,like,it,if,they,were,not,3],
              [perhaps,they,are,3]
            ]}.

script(i) contains
	{[[0,you,0,i],
	    [i,wonder,if,you,imagine,that,i,3,you],
	    [perhaps,in,your,fantasies,we,3,each,other]
	 ]}.
script(i) contains
	 {[ [0,you,0,i,0],
	    [horsefeathers],
	    [am,i,really,so,important,to,you],
	    [you,seem,to,have,strong,feelings,about,me]
	  ]}.
script(i) contains
	 {[ [0,you,think,0],
	    [do,you,really,think,so],
	    [but,you,are,not,sure,4]
	  ]}.
script(i) contains
	 {[[0,you,believe,0],
	    [you,seem,to,have,a,little,bit,of,doubt,about,that],
	    [how,sure,are,you,about,that]
	  ]}.
script(i) contains
	 {[ [0,you,wish,0],
	    [suppose,your,wish,never,came,true],
	    [what,are,you,doing,to,try,to,make,your,wish,come,true]
	  ]}.
script(i) contains
	 {[ [0,you,cant,0],
	    [have,you,tried,to,4],
	    [how,do,you,know,you,cant,4],
	    [do,you,really,want,to,be,able,to,4]
	  ]}.
script(i) contains
	  {[ [0,you,dont,0],
	    [dont,you,really,4],
	    [is,that,something,you,would,like,to,change]
	   ]}.
script(i) contains
	  {[[0,you,do,not,0],
	    [why,dont,you,5],
	    [does,it,trouble,you,not,to,5]
	   ]}.
script(i) contains
	  {[[0,you,feel,0],
	    [do,you,often,feel,4],
	    [tell,me,more,about,feeling,4]
	   ]}.
script(i) contains
	  {[[0],
	    [you,say,1],
	    [do,you,say,1,for,some,special,reason],
	    [can,you,elaborate,on,that]
           ]}.

script(how) contains
   	    {[ [how,0],
              [how,do,you,think],
              [why,do,you,ask]
            ]}.
script(how) contains
            {[ [0,how,0],
              [how,3],
              [tell,me,more,about,that]
            ]}.
script(how) contains
    	     {[ [0,was,i,0],
	       [would,you,like,to,believe,i,was,4],
	       [what,suggests,that,i,was,4]
	     ]}.
script(how) contains
	     {[ [0,i,was,0],
	       [perhaps,i,was,4],
	       [perhaps,you,just,want,to,believe,that,i,was,4]
	     ]}.
script(how) contains
	     {[[0,was,0,i,0],
	       [havent,i,met,you,somewhere,before]
	     ]}.
script(how) contains
	     {[ [0,was,0],
	       [what,does,3,suggest,to,you],
	       [tell,me,more,about,3],
	       [were,they,really]
	     ]}.

script(what) contains
	     {[ [what,0],
	       [what,answer,would,please,you,most],
	       [what,is,it,you,really,want,to,know],
	       [what,do,you,think],
	       [why,do,you,ask,about,that]
	     ]}.
script(what) contains
	     {[ [0],
	       [hogwash]
	     ]}.

script(when) contains
    	     {[ [when,0],
	       [when,do,you,think]
	     ]}.
script(when) contains
	     {[ [0],
	       [can,you,say,more,about,that]
	     ]}.
   
script(why) contains
    	    {[ [0,why,dont,i,0],
	      [do,you,believe,i,dont,5],
	      [you,want,me,to,5],
	      [perhaps,i,will,5,in,good,time]
	    ]}.
script(why) contains
	    {[ [0,why,cant,you,0],
	      [do,you,think,you,should,be,able,to,5]
	    ]}.
script(why) contains
	    {[
              [why,0],
	      [do,you,often,wonder,about,such,things],
	      [what,is,the,importance,of,that,question]
	    ]}.
script(why) contains
	    {[ [0],
	      [do,you,often,think,about,the,reasons,for,things]
	    ]}.
    
script(yes) contains
    	    {[ [0],
	      [you,seem,quite,positive],
	      [you,sound,pretty,sure,about,that],
	      [can,you,explain,that,a,little,more],
	      [i,understand],
	      [i,wonder,whether,you,might,like,to,be,able,to,say,no,to,that]
	    ]}.
    

script(you) contains
	    {[ [0,i,remind,you,of,0],
	      [why,does,that,resemblance,come,to,mind,now],
	      [does,that,resemblance,have,something,to,do,with,your,problem]
	    ]}.
script(you) contains
	    {[ [0,i,0],
	      [did,you,really,come,here,to,talk,about,me],
	      [you,seem,quite,interested,in,talking,about,me],
	      [it,might,be,more,productive,for,us,to,discuss,you,instead,of,me],
	      [are,you,really,talking,about,someone,other,than,me]
	    ]}.  

