/*
*********************************************************************
*
*	The Essence of OLE
*
*	Source code sample
*
*	Copyright (c) 1996, Rolling Thunder Computing, Ipswich MA
*
*	e-mail: essence@rollthunder.com
*	CompuServe: 73540,3430
*	Web: http://www.rollthunder.com
*
*	File Name: ELIZA.CPP
*
*	Purpose: Phoney psychiatrist program. Found on Web and translated from PASCL.
*
*********************************************************************
*/

#include <stdafx.h>

LPSTR pResps [] = {
//"One-based adjustment",
"Don't you believe that I can*",
"Perhaps you would like to be able to*",
"You want me to be able to*",
"Perhaps you don't want to*",
"Do you want to be able to*",
"What makes you think I am*",
"Does it please you to believe I am*",
"Perhaps you would like to be*",
"Do you sometimes wish you were*",
"Don't you really*",
"Why don't you*",
"Do you wish to be able to*",
"Does that trouble you?",
"Tell me more about such feelings.",
"Do you often feel*",
"Do you enjoy feeling*",
"Do you really believe I don't*",
"Perhaps in good time I will@",
"Do you want me to*",
"Do you think you should be able to*",
"Why can't you*",
"Why are you interested in whether or not I am*",
"Would you prefer if I were not*",
"Perhaps in your fantasies I am*",
"How do you know you can't*",
"Have you tried?",
"Perhaps you can now*",
"Did you come to me because you are*",
"How long have you been*",
"Do you believe it is normal to be*",
"Do you enjoy being*",
"We were discussing you --not me.",
"Oh,*",
"You're not really talking about me, are you?",
"What would it mean to you if you got*",
"Why do you want*",
"Suppose you got*",
"What if you never got*",
"I sometimes also want@",
"Why do you ask?",
"Does that question interest you?",
"What answer would please you the most?",
"What do you think?",
"Are such questions on your mind often?",
"What is it that you really want to know?",
"Have you asked anyone else?",
"Have you asked such questions before?",
"What else comes to mind when you ask that?",
"Names don't interest me.",
"I don't care about names --please go on.",
"Is that the real reason?",
"Don't any other reasons come to mind?",
"Does that reason explain anything else?",
"What other reasons might there be?",
"Please don't apologise!",
"Apologies are not necessary.",
"What feelings do you have when you apologise?",
"Don't be so defensive!",
"What does that dream suggest to you?",
"Do you dream often?",
"What persons appear in your dreams?",
"Are you disturbed by your dreams?",
"How do you...please state your problem.",
"You don't seem quite certain.",
"Why the uncertain tone?",
"Can't you be more positive?",
"You aren't sure?",
"Don't you know?",
"Are you saying no just to be negative?",
"You are being a bit negative.",
"Why not?",
"Are you sure?",
"Why no?",
"Why are you concerned about my*",
"What about your own*",
"Can you think of a specific example?",
"When?",
"What are you thinking of?",
"Really, always?",
"Do you really think so?",
"But you are not sure you*",
"Do you doubt you*",
"In what way?",
"What resemblence do you see?",
"What does the similarity suggest to you?",
"What other connections do you see?",
"Cound there really be some connection?",
"How?",
"You seem quite positive.",
"Are you Sure?",
"I see.",
"I understand.",
"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.",
"Do computers worry you?",
"Are you talking about me in particular?",
"Are you frightened by machines?",
"Why do you mention computers?",
"What do you think machines have to do with your problems?",
"Don't you think computers can help people?",
"What is it about machines that worries you?",
"Say, do you have any psychological problems?",
"What does that suggest to you?",
"I see.",
"I'm not sure I understand you fully.",
"Come, come, elucidate your thoughts.",
"Can you elaborate on that?",
"That is quite interesting.",
"Why did you repeat yourself?",
"Do you expect a different answer by repeating yourself?",
"Come, come, elucidate your thoughts.",
"Please don't repeat yourself!",
"This session is costing $150 an hour. Repeat yourself all you want."  } ;




/*
{=========================================================}
{      Keywords                                           }
{=========================================================}
*/

#define MAXKEY	37 

char PrevStr [1024] ;

const char *KeyWords [] = {
               "CAN YOU","CAN I","YOU ARE","YOU'RE","I DON'T",
               "I FEEL","WHY DON'T YOU","WHY CAN'T I","ARE YOU",
               "I CAN'T","I AM","I'M","YOU","I WANT","WHAT",
               "HOW","WHO","WHERE","WHEN","WHY","NAME","CAUSE",
               "SORRY","DREAM","HELLO","HI","MAYBE","NO",
               "YOUR","ALWAYS","THINK","ALIKE","YES","FRIEND",
               "COMPUTER","NO KEY FOUND","REPEAT INPUT"};

/*
{=========================================================}
{     Data for finding the right responses                }
{=========================================================}
*/

const char *RespFile = "RESPONSE.DAT";  // {response data file}
int MaxRespNum = 116;


int RspIndex [] = 
		{1, 4, 6, 6,10,14,17,20,22,25,
        28,28,32,35,40,40,40,40,40,40,
        49,51,55,59,63,63,64,69,74,76,
        80,83,90,93,99,106,113};  //  {- working response pointer array -}

/*
{- this array contains the start index to the response strings -}
*/

const int KeyIndex [] =
        {1, 4, 6, 6,10,14,17,20,22,25,
        28,28,32,35,40,40,40,40,40,40,
        49,51,55,59,63,63,64,69,74,76,
        80,83,90,93,99,106,113};

/*
{- this array contains the end index to the response strings -}
*/
const int KeyEnd [] =
        {3, 5, 9, 9,13,16,19,21,24,27,
        32,32,34,39,48,48,48,48,48,48,
        50,54,58,62,68,63,68,73,75,79,
        82,89,92,98,105,112,117};



/*
{=========================================================}
{     Other misc information needed by the program        }
{=========================================================}

{- possible punctuation -}
*/

const char *PuncSet = " .!?,";
/*
{- misc error messages -}
*/

const char *NoFileMsg = "Sorry, I seem to have mis-placed the response files.";
const char *LogicErrMsg = "Hmmm, I seem to be having problems myself.";

void ReadResp(char *Rstr, int RespNum)
{
	
/*
invalid response number given
*/
	if (RespNum > MaxRespNum)
	{
		strcpy (Rstr, LogicErrMsg) ;
		return ;
	}

/*
find the desired response in the response file
*/
  	lstrcpy (Rstr, pResps[RespNum]) ;

}


/*
{=========================================================}
{        Find keyword in Wstr                             }
{=========================================================}
{- a keyword is a relational word that we can respond to }
{- see the keyword table to see the types of relational words}
{- that are used. Returns "Key" pointing to keyword in table,}
{- returns "Kpos" pointing to first char after keyword in Wstr}
{- Returns function true if keyword found, or false if not}
{- if no keyword found Key = pred(MaxKey), repeated string = MaxKey}
*/

BOOL FindKey(char *Wstr, int *Kpos, int *Key)
{
	int i ;
	char *pKey ;
	
	for (i = 0 ; i < MAXKEY ; i ++)
	{
		if ((pKey = strstr (Wstr, KeyWords [i])) != NULL)
		{
			*Kpos = (pKey - Wstr) + strlen (KeyWords [i]) ;
			*Key = i ;
			return TRUE ;	
		}
	}  
 
  	return FALSE ;
}



/*
{=========================================================}
{   Take the right part of the string and conjugate it    }
{   using the list of strings to be swapped               }
{=========================================================}
*/

void Ctrim(char *Xstr)
{
	char *cp ;

/*
remove spaces and punctuation from the head of the string
*/
    while (strchr (PuncSet, *Xstr))
    {
    	memmove (Xstr, Xstr + 1, strlen (Xstr)) ;
	}

/*
remove spaces and puncuation from end of string
*/
	cp = Xstr + strlen (Xstr) - 1 ;

    while (strchr (PuncSet, *cp)) 
	{
      	*cp = 0x00 ;
      	cp -- ;
	}
}

/*
{=========================================================}
{      String data for conjugations                       }
{=========================================================}
*/

const MaxCon = 7;
const char *Con1 []  =
    {"are","we're","you","your","I've","I'm","me"};
const char *Con2 []  =
    {"am","was","I","my","you've","you're","you"};

void Conjugate (char *Wstr, char *Cstr, int Kpos)
{
	int i;
	char TempStart [256], TempFinal [256], *cp ;

    strcpy(TempStart, Wstr+Kpos);    //{pull out the right part}
	TempFinal [0] = 0x00 ;

	cp = strtok	(TempStart, PuncSet) ;

	while (cp != NULL)
	{
/*
for each word, check for conjugation changes. 
*/
    	for (i = 0 ; i < MaxCon ; i ++)
    	{
			if (strcmpi (cp, Con1 [i]) == 0)
			{
				strcat (TempFinal, " ") ;
				strcat (TempFinal, Con2 [i]) ;
				break ;
			}
			else if	(strcmpi (cp, Con2 [i]) == 0)
			{
				strcat (TempFinal, " ") ;
				strcat (TempFinal, Con1 [i]) ;
				break ;
			}
    	}

/*
no conjugation change, copy original word into dest string
*/
		if (i == MaxCon)
		{
			strcat (TempFinal, " ") ;
			strcat (TempFinal, cp) ;
		}

		cp = strtok (NULL, PuncSet) ;
	}


/*
special case fixup for trailing 'I's 
*/
    /*if Cstr[length(Cstr)] = 'I' then
    {
      //dec(Cstr[0]);
      strcat (Cstr, "me";
    }
*/

	strcpy (Wstr, TempFinal) ;
}


/*
{============================================================}
{ Get a response based on the keyword number in variable Key }
{============================================================}
*/

void GetResponse(char *Rstr, int Key)
{
	char Fstr [120] ;
	char temp [256] ;
	
/*
{get the desired response from data file}
subtract 1 to correct for difference between 0-based arrays in C and 1-based
arrays in Pascal
*/

  	ReadResp(Fstr, RspIndex[Key] - 1); 

/*
  {-Point to the next response so that no two are the same}
*/
  
  	RspIndex[Key] ++ ;

  	if (RspIndex[Key] == KeyEnd[Key])
  	{
  		RspIndex[Key] = KeyIndex[Key] ;
	}

/*
  {-if no "*" or "@" at the end of the response, then just return the response}
  {-if there was an "*" at the end of the response string, then return}
  {-the response plus the conjugation word/phrase in Rstr plus a "?"}
  {-if "@" then add a period instead}
*/

  	if (*(Fstr + strlen (Fstr) - 1) == '*')
  	{
//{- replace the '*' with a space, append the conjugated string and add "?" -}
		*(Fstr + strlen (Fstr)- 1) = ' ';
		strcpy (temp, Fstr) ;
		strcat (temp, Rstr) ;
		strcat (temp, "?") ;
		strcpy (Rstr, temp) ;
	  	return ;  
  	}
  	else if (*(Fstr + strlen (Fstr) - 1) == '@')
  	{

//{- replace the '@' with a space, append the conjugated string and add "." -}

	  	*(Fstr + strlen(Fstr) - 1) = ' ';
		strcpy (temp, Fstr) ;
		strcat (temp, Rstr) ;
		strcat (temp, ".")  ;
		strcpy (Rstr, temp) ;
  		return ;  
  	}
  	else
  	{
  		strcpy (Rstr,Fstr) ;
		return ;
	}
}


/*
{============================================================}
{- program Eliza -}
*/




/*
{============================================================}
{        Reads a response from the response file             }
{============================================================}
*/


void Eliza (char *InStr, char *OutStr)
{
	char CapStr [1024] ;
	int Kpos, Key ;

  	Key = MAXKEY-1;                   // {- set max for repeat input -}
    
	strcpy (CapStr, InStr) ;
	_strupr (CapStr) ;
   	
   	if (strcmpi (InStr,PrevStr)	!= 0)
    {       	           //{- get new key if not repeat -}
    	strcpy (PrevStr, InStr) ;	// {- save original input string -}
    	if (FindKey(CapStr, &Kpos, &Key))
      	{
			 //{- If keyword found in Istr -}
        	Conjugate(InStr, CapStr, Kpos);  //{- then conjugate the string -}
		}
		else
		{
			Key = MAXKEY - 2 ;
		}
	}

    GetResponse(InStr, Key); 				//{- Get response based on Keyword found -}
	wsprintf (OutStr, "Eliza: %s%c%c",InStr,0x0D,0x0A) ;
}
