? <include list>;
? <include generator>;


// split a sentence
<StrSplit #list #str>
	::sys <split #l #str " .?">
	<foreach #o (#i #l)
		<nop #o>
		::sys <rightstr #right1 #i 1>
		::sys <leftstr #left1 #i 1>
		(
		  (<is #left1 '('> | <is #left1 '"'> | <is #left1 "'">)
		  ::sys <strlen #len #i>
		  ::sys <rightstr #right #i <_=#len-1>>
		  <is #o (#left1 #right)>
		 |
		  (<is #right1 ','> | <is #right1 ';'>
		     | <is #right1 ')'> | <is #right1 '"'> | <is #right1 "'">
		     | <is #right1 '!'> 
		     | <is #right1 '?'> | <is #right1 '.'> 
		  )
		  ::sys <strlen #len #i>
		  ::sys <leftstr #left #i <_=#len-1>>
		  <is #o (#left #right1)>
		 |
		  <is #o #i>
		)
	>
	::list <flatten #list #o>
	;

// replace I <--> you
<Replace () ()>;
<Replace (YOU : #l2) (DO I : #l1)>	<Replace #l2 #l1>;
<Replace (I : #l2) (DO YOU : #l1)>	<Replace #l2 #l1>;
<Replace (YOU : #l2) (I : #l1)>		<Replace #l2 #l1>;
<Replace (YOU'RE : #l2) (I'M : #l1)> 	<Replace #l2 #l1>;
<Replace (YOU'LL : #l2) (I'LL : #l1)> 	<Replace #l2 #l1>;
<Replace (YOU'VE : #l2) (I'VE : #l1)> 	<Replace #l2 #l1>;
<Replace (YOU'D : #l2) (I'D : #l1)> 	<Replace #l2 #l1>;
<Replace (YOUR : #l2) (MY : #l1)> 	<Replace #l2 #l1>;
<Replace (YOU : #l2) (ME : #l1)> 	<Replace #l2 #l1>;
<Replace (ARE : #l2) (AM : #l1)> 	<Replace #l2 #l1>;
<Replace (WERE : #l2) (WAS : #l1)> 	<Replace #l2 #l1>;
<Replace (#f : #l2) (#f : #l1)>		<Replace #l2 #l1>;

// main routine	
<Eliza>
	<print "HI! I'M ELIZA.">
	<print "WHAT'S YOUR PROBLEM?">
	<print>
	{ 
		<printf "> ">
		::sys <getline #x 
			::sys <strdelcntl #x2 #x> 
			::sys <toupper #str #x2>
			<StrSplit #list #str>
			<c #list2 #list>
			::list <flatten #list3 #list2>
			<printlist #list3>
		>
	};


// select one at random
<comb #r #l>
	::generator <combination #r1 #l 1>
	::sys <car #r #r1>
	;

// data operation
<d #o #list>
	<comb #o #list>
	;

// Keyword and conversation data	
<c #o (#h :#l)>
	(<is #h HELLO> | <is #h HI>)
	<d #o (
		(HI)
		(HOW DO YOU "DO?")
		(HELLO)
		)>
	;
<c #o (CAN YOU :#l)>
	<d #o (
		(YOU WANT ME TO BE ABLE TO #l ".")
		("DON'T" YOU BELIEVE THAT I CAN #l "?")
		(PERHAPS YOU WOULD LIKE TO #l ".")
		)>
	;
<c #o (CAN I :#l)>
	<d #o #l "?" (
		(DO YOU WANT TO BE ABLE TO)
		(PERHAPS YOU "DON'T" WANT TO)
		)>
	;
<c #o (YOU ARE :#l)>
	<d #o (
		(WHAT MAKES YOU THINK I AM #l "?")
		(DOES IT PLEASE YOU TO BELIEVE I AM #l "?")
		(PERHAPS YOU WOULD LIKE TO BE #l ".")
		(DO YOU SOMETIMES WISH YOU WERE #l "?")
		)>
	;
<c #o ("YOU'RE" :#l)>
	<d #o (
		(WHAT MAKES YOU THINK I AM #l "?")
		(DOES IT PLEASE YOU TO BELIEVE I AM #l "?")
		(PERHAPS YOU WOULD LIKE TO BE #l ".")
		(DO YOU SOMETIMES WISH YOU WERE #l "?")
		)>
	;
<c #o (I "DON'T" :#l)>
	<d #o (
		("DON'T" YOU REALLY #l "?")
		(WHY "DON'T" YOU #l "?")
		(DO YOU WISH TO BE ABLE TO #l "?")
		(DOES THAT TROUBLE YOU #l "?")
		)>
	;
<c #o (I FEEL :#l)>
	<d #o (
		(DO YOU OFTEN FEEL #l "?")
		(DO YOU ENJOY FEELING #l "?")
		)>
	;
<c #o (WHY "DON'T" YOU :#l)>
	<d #o (
		(DO YOU REALLY BELIEVE I "DON'T" #l "?")
		(PERHAPS IN GOOD TIME I WILL #l ".")
		(DO YOU WANT ME TO #l "?")
		)>
	;
<c #o (WHY "CAN'T" I :#l)>
	<d #o (
		(WHY "CAN'T" YOU #l "?")
		(DO YOU THINK YOU SHOULD BE ABLE TO #l "?")
		)>
	;
<c #o (ARE YOU :#l)>
	<d #o (
		(WOULD YOU PREFER IF I WERE NOT #l "?")
		(WHY ARE YOU INTERESTED IN WHETHER OR NOT I AM #l "?")
		(PERHAPS IN YOUR FANTASIES I AM #l "?")
		)>
	;
<c #o (I "CAN'T" :#l)>
	<d #o (
		(PERHAPS YOU CAN NOW #l ".")
		(HOW DO YOU KNOW YOU "CAN'T" #l "?")
		)>
	;
<c #o (I AM :#l)>
	<d #o (
		(DID YOU COME TO ME BECAUSE YOU ARE #l "?")
		(HOW LONG HAVE YOU BEEN #l "?")
		(DO YOU BELIEVE IT IS NORMAL TO BE #l "?")
		(DO YOU ENJOY BEING #l "?")
		)>
	;
<c #o (YOUR :#l)>
	<d #o (
		(WHY ARE YOU CONCERNED ABOUT MY #l "?")
		(WHAT ABOUT YOUR OWN #l "?")
		)>
	;
<c #o (I WANT :#l)>
	<d #o (
		(WHAT WOULD IT MEAN TO YOU IF YOU GOT #l "?")
		(WHY DO YOU WANT #l "?")
		(SUPPOSE YOU SOON GOT #l ".")
		(WHAT IF YOU NEVER GOT)
		(I SOMETIMES ALSO WANT #l "?")
		)>
	;
<c #o (CAUSE :#l)>
	<d #o (
		(WHAT OTHER REASONS MIGHT THERE "BE?")
		(IS THAT THE REAL "REASON?")
		(DOES THAT REASON EXPLAIN ANYTHING "ELSE?")
		("DON'T" ANY OTHER REASONS COME TO "MIND?")
		)>
	;
<c #o (SORRY :#l)>
	<d #o (
		(PLEASE "DON'T" "APOLOGIZE!")
		(APOLOGIES ARE NOT "NECESSARY.")
		(WHAT FEELINGS DO YOU HAVE WHEN YOU "APOLOGIZE?")
		("DON'T" BE SO "DEFENSIVE.")
		)>
	;
<c #o (MAYBE :#l)>
	<d #o (
		(YOU "DON'T" SEEM QUITE "CERTAIN.")
		(WHY THE UNCERTAIN "TONE?")
		("CAN'T" YOU BE MORE "POSITIVE?")
		(YOU "AREN'T" "SURE?")
		("DON'T" YOU "KNOW?")
		)>
	;
<c #o (ALWAYS :#l)>
	<d #o (
		(CAN YOU THINK OF A SPECIFIC "EXAMPLE?")
		("WHEN?")
		(WHAT ARE YOU THINKING "OF?")
		("REALLY," "ALWAYS?")
		)>
	;
<c #o (ALIKE :#l)>
	<d #o (
		(IN WHAT "WAY?")
		(WHAT RESEMBLANCE DO YOU "SEE?")
		(WHAT DOES THE SIMILARITY SUGGEST TO "YOU?")
		(WHAT OTHER CONNECTIONS DO YOU "SEE?")
		(COULD THERE REALLY BE SOME "CONNECTION?")
		("HOW?")
		(YOU SEEM QUITE "POSITIVE.")
		)>
	;
<c #o (FRIEND :#l)>
	<d #o (
		(WHY DO YOU BRING UP THE TOPIC OF "FRIENDS?")
		(DO YOUR FRIENDS WORRY "YOU?")
		(DO YOUR FRIENDS PICK ON "YOU?")
		(ARE YOU SURE YOU HAVE ANY "FRIENDS?")
		(DO YOU IMPOSE ON YOUR "FRIENDS?")
		(PERHAPS YOUR LOVE FOR FRIENDS WORRIES "YOU.")
		)>
	;
<c #o (THINK :#l)>
	<d #o (
		(DO YOU REALLY THINK "SO?")
		(IT "DOESN'T" NECESSARILY FOR YOU TO "THINK.")
		(DO YOU DOUBT "YOU?")
		)>
	;
<c #o (YES :#l)>
	<d #o (
		(ARE YOU "SURE?")
		(I "SEE.")
		(I "UNDERSTAND.")
		)>
	;
<c #o (NO :#l)>
	<d #o (
		(YOU ARE BEING A BIT "NEGATIVE.")
		(WHY "NOT?")
		(ARE YOU SAYING NO JUST TO BE "NEGATIVE?")
		(ARE YOU "SURE?")
		)>
	;
<c #o (ELIZA :#l)>
	<d #o (
		(WHY DO YOU MENTION "ELIZA?")
		(WHAT DO YOU THINK ELIZA HAS TO DO WITH YOUR "PROBLEM?")
		(ARE YOU FRIGHTENED BY "ELIZA?")
		(ARE YOU TALKING ABOUT ME IN "PARTICULAR?")
		("DON'T" YOU THINK ELIZA CAN HELP "PEOPLE?")
		)>
	;
<c #o ()>
	<d #o (
		(WHAT DOES THAT SUGGEST TO "YOU?")
		(COME COME ELUCIDATE YOUR "THOUGHTS.")
		("SAY," DO YOU HAVE ANY PSYCHOLOGICAL "PROBLEMS?")
		(THAT IS QUITE "INTERESTING.")
		(I "SEE.")
		(CAN YOU ELABORATE ON "THAT?")
		)>
	;
<c #o (#w :#l)>
	(<is #w WHAT> |<is #w HOW> |<is #w WHERE> |<is #w WHEN> |<is #w WHEN>
	 | <is #w WHY>)
	<d #o (
		(DOES THAT QUESTION INTEREST "YOU?")
		(WHY DO YOU "ASK?")
		(WHAT DO YOU "THINK?")
		(ARE SUCH QUESTIONS ON YOUR MIND "OFTEN?")
		(WHAT ANSWER WOULD PLEASE YOU THE "MOST?")
		(WHAT ELSE COMES TO MIND WHEN YOU ASK "THAT?")
		(WHAT IS IT THAT YOU REALLY WANT TO "KNOW?")
		(HAVE YOU ASKED ANYONE "ELSE?")
		(HAVE YOU ASKED SUCH QUESTIONS "BEFORE?")
		)>
	;
<c #o (#l1 : #l2)>
	<c #o #l2>
	;
	
? <Eliza>;

