/* Eliza.c ******************************************************************
*
*	Eliza -	The AI program by Josef Weizenbaum.
*
*		Converted from the PET 2001 BASIC version by
*		Olaf Barthel of ED Electronic Design Hannover.
*
*		Programmed 05-May-89
*
*	I reprogrammed Eliza for a simple demonstration program
*	I am still developing. Currently the structure of this
*	program reflects the structure of the original BASIC
*	version. I could have made it a bit more structured but,
*	as you may know, BASIC has its tricks and traps and I
*	didn't want to lose track of the original concept.
*
*	I would like to thank Michael Nickles for his documented
*	source code. I would not have been able to convert the
*	program without his helpful comments.
*
****************************************************************************/

#include <stdio.h>

	/* Some useful global definitions. */

typedef unsigned char UBYTE;

#define TRUE	1
#define FALSE	!TRUE

#define FOREVER	for(;;)

#define NKEYS	38	/* Number of keywords. */
#define NVERBS	14	/* Number of words. */
#define NANSWS	118	/* Number of answers. */

	/* This array contains the offsets for the the answers
	 * corresponding to the keywords Eliza "understands".
	 */

short CurrentAnswers[NKEYS] =
{
	  0,  3,  5,  5,  9, 13, 16, 19, 21, 24,
	 27, 27, 31, 34, 39, 39, 39, 39, 39, 39,
	 48, 50, 54, 58, 62, 62, 63, 68, 73, 75,
	 79, 82, 89, 92, 98,115,112,105
};

	/* Similar to CurrentAnswers[] these are offsets for the
	 * answers corresponding to the keywords, but Eliza uses
	 * the CurrentAnswers[] to give a new answer each time
	 * one and the same keyword is encountered. The corresponding
	 * entry in CurrentAnswers[] is incremented each time the
	 * keyword is found.
	 */

short StartAnswers[NKEYS] =
{
	  0,  3,  5,  5,  9, 13, 16, 19, 21, 24,
	 27, 27, 31, 34, 39, 39, 39, 39, 39, 39,
	 48, 50, 54, 58, 62, 62, 63, 68, 73, 75,
	 79, 82, 89, 92, 98,115,112,105
};

	/* This array contains information Eliza uses to determine,
	 * how many answers there are for each keyword.
	 */

short MaxAnswers[NKEYS] =
{
	3,2,4,4,4,3,3,2,
	3,3,4,4,3,5,9,9,
	9,9,9,9,2,4,4,4,
	1,1,5,5,2,4,3,7,
	3,6,7,3,3,7
};

	/* This array reflects the DATA part of the original BASIC
	 * program. I don't think that this is the ideal solution
	 * to the problem, but it works and that's the point, isn't
	 * it?
	 */

struct
{
	char *	a_Text;
	UBYTE	a_Type;
	UBYTE	a_Question;
} Answers[] =
{
		/* These are the keyword Eliza knows. */

	" can you ",	FALSE,FALSE,
	" can i ",	FALSE,FALSE,
	" you are ",	FALSE,FALSE,
	" youre ",	FALSE,FALSE,
	" i dont ",	FALSE,FALSE,
	" i feel ",	FALSE,FALSE,
	" why dont you ",FALSE,FALSE,
	" why cant i ",	FALSE,FALSE,
	" are you ",	FALSE,FALSE,
	" i cant ",	FALSE,FALSE,
	" i am ",	FALSE,FALSE,
	" im ",		FALSE,FALSE,
	" you ",	FALSE,FALSE,
	" i want ",	FALSE,FALSE,
	" what ",	FALSE,FALSE,
	" how ",	FALSE,FALSE,
	" who ",	FALSE,FALSE,
	" where ",	FALSE,FALSE,
	" when ",	FALSE,FALSE,
	" why ",	FALSE,FALSE,
	" name ",	FALSE,FALSE,
	" cause ",	FALSE,FALSE,
	" sorry ",	FALSE,FALSE,
	" dream ",	FALSE,FALSE,
	" hello ",	FALSE,FALSE,
	" hi ",		FALSE,FALSE,
	" maybe ",	FALSE,FALSE,
	" no ",		FALSE,FALSE,
	" your ",	FALSE,FALSE,
	" always ",	FALSE,FALSE,
	" think ",	FALSE,FALSE,
	" alike ",	FALSE,FALSE,
	" yes ",	FALSE,FALSE,
	" friend ",	FALSE,FALSE,
	" computer ",	FALSE,FALSE,
	" eliza ",	FALSE,FALSE,
	" money ",	FALSE,FALSE,
	"NoKeyFound",	FALSE,FALSE,

		/* These are verbs Eliza converts from the input
		 * sentence, 3rd person is converted to 1st person.
		 */

	" are ",	FALSE,FALSE,
	" am ",		FALSE,FALSE,
	" were ",	FALSE,FALSE,
	" was ",	FALSE,FALSE,
	" you ",	FALSE,FALSE,
	" i ",		FALSE,FALSE,
	" me ",		FALSE,FALSE,
	" you ",	FALSE,FALSE,
	" your ",	FALSE,FALSE,
	" my ",		FALSE,FALSE,
	" ive ",	FALSE,FALSE,
	" youve ",	FALSE,FALSE,
	" im ",		FALSE,FALSE,
	" youre ",	FALSE,FALSE,

		/* These are the answers Eliza gives us. */

	"Don't you believe that I can",			TRUE,TRUE,
	"Perhaps you would like to be able to",		TRUE,FALSE,
	"You want me to be able to",			TRUE,TRUE,
	"Perhaps you don't want to",			TRUE,FALSE,
	"Do you want to be able to",			TRUE,TRUE,
	"What makes you think I am",			TRUE,TRUE,
	"Does it please you to believe I am",		TRUE,TRUE,
	"Perhaps you would like to be",			TRUE,FALSE,
	"Do you sometimes wish you were",		TRUE,TRUE,
	"Don't you really",				TRUE,TRUE,
	"Why don't you",				TRUE,TRUE,
	"Do you wish to be able to",			TRUE,TRUE,
	"Does that trouble you",			FALSE,TRUE,
	"Tell me more about such feelings",		FALSE,TRUE,
	"Do you often feel",				TRUE,TRUE,
	"Do you enjoy feeling",				TRUE,TRUE,
	"Do you really believe I don't",		TRUE,TRUE,
	"Perhaps in good time I will",			TRUE,FALSE,
	"Do you want me to",				TRUE,TRUE,
	"Do you think you should be able to",		TRUE,TRUE,
	"Why can't you",				TRUE,TRUE,
	"Why are you interested in whether or not I am",TRUE,TRUE,
	"Would you prefer if I were not",		TRUE,TRUE,
	"Perhaps in your fantasies I am",		TRUE,FALSE,
	"How do you know that you can't",		TRUE,TRUE,
	"Have you tried",				FALSE,TRUE,
	"Perhaps you can now",				TRUE,FALSE,
	"Did you come to me because you are",		TRUE,TRUE,
	"How long have you been",			TRUE,TRUE,
	"Do you believe it is normal to be",		TRUE,TRUE,
	"Do you enjoy being",				TRUE,TRUE,
	"We were discussing you - not me",		FALSE,FALSE,
	"Oh, I",					TRUE,FALSE,
	"You're not really talking about me, are you",	FALSE,TRUE,
	"What would it mean to you if you got",		TRUE,TRUE,
	"Why do you want",				TRUE,TRUE,
	"Suppose you soon got",				TRUE,FALSE,
	"What if you never got",			TRUE,TRUE,
	"I sometimes also want",			TRUE,FALSE,
	"Why do you ask",				FALSE,TRUE,
	"Does that question interest you",		FALSE,TRUE,
	"What answer would please you the most",	FALSE,TRUE,
	"What do you think",				FALSE,TRUE,
	"Are such questions in your mind often",	FALSE,TRUE,
	"What is that you really want to know",		FALSE,TRUE,
	"Have you asked anyone else",			FALSE,TRUE,
	"Have you asked such questions before",		FALSE,TRUE,
	"What else comes to mind when you ask that",	FALSE,TRUE,
	"Names don't interest me",			FALSE,FALSE,
	"I don't care about names - please go on",	FALSE,FALSE,
	"Is that the real reason",			FALSE,TRUE,
	"Don't any other reasons come to mind",		FALSE,TRUE,
	"Does that reason explain anything else",	FALSE,TRUE,
	"What other reasons might there be",		FALSE,TRUE,
	"Please don't apologize",			FALSE,FALSE,
	"Apologies are not necessary",			FALSE,FALSE,
	"What feelings do you have when you apologize",	FALSE,TRUE,
	"Don't be so defensive",			FALSE,FALSE,
	"What does that dream suggest you",		FALSE,TRUE,
	"Do you dream often",				FALSE,TRUE,
	"What persons appear in you dreams",		FALSE,TRUE,
	"Are you disturbed by your dreams",		FALSE,TRUE,
	"How do you do ... please state your problem",	FALSE,FALSE,
	"You don't seem quite certain",			FALSE,FALSE,
	"Why the uncertain tone",			FALSE,TRUE,
	"Can't you be more positive",			FALSE,TRUE,
	"You aren't sure",				FALSE,TRUE,
	"Don't you know",				FALSE,TRUE,
	"Why no",					TRUE,TRUE,
	"Don't say know, it's always so negative",	FALSE,FALSE,
	"Why not",					FALSE,TRUE,
	"Are you sure",					FALSE,TRUE,
	"Why no",					FALSE,TRUE,
	"Why are you concerned about my",		TRUE,TRUE,
	"What about your own",				TRUE,TRUE,
	"Can't you think about a specific example",	FALSE,TRUE,
	"When",						FALSE,TRUE,
	"What are you thinking of",			FALSE,TRUE,
	"Really, always",				FALSE,TRUE,
	"Do you really think so",			FALSE,TRUE,
	"But you are not sure you",			TRUE,TRUE,
	"Do you doubt you",				TRUE,TRUE,
	"In what way",					FALSE,TRUE,
	"What resemblance do you see",			FALSE,TRUE,
	"What does the similarity suggest to you",	FALSE,TRUE,
	"What other connections do you see",		FALSE,TRUE,
	"Could there really be some connections",	FALSE,TRUE,
	"How",						FALSE,TRUE,
	"You seem quite positive",			FALSE,FALSE,
	"Are you sure",					FALSE,TRUE,
	"I see",					FALSE,FALSE,
	"I understand",					FALSE,FALSE,
	"Why do you bring up the topic of friends",	FALSE,TRUE,
	"Do your friends worry you",			FALSE,TRUE,
	"Do your friends pick on you",			FALSE,TRUE,
	"Are you sure you have any friends",		FALSE,TRUE,
	"Do you impose on your friends",		FALSE,TRUE,
	"Perhaps your love for friends worries you",	FALSE,FALSE,
	"Do computers worry you",			FALSE,TRUE,
	"Are you talking about me in particular",	FALSE,TRUE,
	"Are you frightened by machines",		FALSE,TRUE,
	"Why do you mention computers",			FALSE,TRUE,
	"What do you think machines have to do with your problem",FALSE,TRUE,
	"Don't you think computers can help people",	FALSE,TRUE,
	"What is it about machines that worries you",	FALSE,TRUE,
	"Say, do you have any psychological problems",	FALSE,TRUE,
	"What does that suggest to you",		FALSE,TRUE,
	"I see",					FALSE,FALSE,
	"I'm not sure I understand you",		FALSE,FALSE,
	"Come come, elucidate your thougts",		FALSE,FALSE,
	"Can you elaborate on that",			FALSE,TRUE,
	"That is quite interesting",			FALSE,FALSE,
	"Why do you have problems with money",		FALSE,TRUE,
	"Do you think money is everything",		FALSE,TRUE,
	"Are you sure that money is the problem",	FALSE,TRUE,
	"I think we want to talk about you, not about me",FALSE,FALSE,
	"What's about me",				FALSE,TRUE,
	"Why do you always bring up my name",		FALSE,TRUE
};

	/* Erase(s,len) :
	 *
	 *	Clears the contents of a string.
	 */

void
Erase(s,len)
char *s;
short len;
{
	register short i;

	for(i = 0 ; i < len ; i++)
		s[i] = 0;
}

	/* StrCmp(s,t) :
	 *
	 *	Similar to strcmp(), but not case-sensitive.
	 */

short
StrCmp(s,t)
char *s,*t;
{
	register short i;

	for(i = 0 ; i < strlen(t) ; i++)
		if(toupper(s[i]) != toupper(t[i]))
			return(s[i] - t[i]);

	return(0);
}

	/* main() :
	 *
	 *	This is what Eliza is.
	 */

void
main()
{
		/* Various string buffers. */

	char Input[256],LastInput[256],TmpBuf[256],Action[256],Mini[2];

		/* Loop counters. */

	short i,j,k;

		/* Temporary values to reflect the current answer. */

	short KeyPos,TxtPos;

		/* Clear the input buffer. */

	Erase(Input,256);
	Erase(LastInput,256);

		/* Hi there... */

	printf("Hello, I'm Eliza, tell me your problem.\n");

		/* Ad infinitum... */

	FOREVER
	{
			/* Prompt and read... */

		putchar('>');
		gets(Input);

			/* Mini is a string buffer that is to
			 * to receive a single character.
			 */

		Mini[1] = 0;

			/* Add a blank at the beginning of the
			 * sentence and remove all single quotes.
			 */

		strcpy(TmpBuf," ");

		for(i = 0 ; i < strlen(Input) ; i++)
			if((Mini[0] = Input[i]) != '\'')
				strcat(TmpBuf,Mini);

		strcat(TmpBuf," ");

			/* Copy the string back. */

		strcpy(Input,TmpBuf);

			/* Do you wish to stop? */

		if(!StrCmp(Input," quit "))
			break;

			/* User repeated his last input. */

		if(LastInput[0] && !StrCmp(LastInput,Input))
		{
			printf("Please don't repeat yourself.\n");
			continue;
		}

			/* Update last input buffer. */

		strcpy(LastInput,Input);

			/* No key found. */

		KeyPos = -1;

			/* Try to find a keyword. This is a tricky loop
			 * since there isn't any MID$() function available
			 * in C.
			 */

		for(i = 0 ; i < NKEYS ; i++)
		{
			for(j = 0 ; j < strlen(Input) - strlen(Answers[i] . a_Text) ; j++)
			{
				if(!StrCmp(Input + j,Answers[i] . a_Text))
				{
					TxtPos = j;
					KeyPos = i;
				}

				if(KeyPos >= 0)
					break;
			}

			if(KeyPos >= 0)
				break;
		}

			/* No key found? */

		if(KeyPos < 0)
			KeyPos = 37;
		else
		{
				/* Now try to extract the part of the
				 * sentence following the keyword.
				 * Eliza might need it to set up its
				 * answer.
				 */

			strcpy(Action," ");
			strcat(Action,Input + TxtPos + strlen(Answers[KeyPos] . a_Text));

			Erase(TmpBuf,256);

			for(i = NKEYS ; i < NKEYS + NVERBS ; i += 2)
			{
				for(j = 0 ; j < strlen(Action) - strlen(Answers[i] . a_Text) ; j++)
				{
					if(!StrCmp(Action + j,Answers[i] . a_Text))
					{
						for(k = 0 ; k < j ; k++)
							TmpBuf[k] = Action[k];

						TmpBuf[k] = 0;

						strcat(TmpBuf,Answers[i + 1] . a_Text);
						strcat(TmpBuf,Action + j + strlen(Answers[i] . a_Text));

						j += strlen(Answers[i + i] . a_Text);

						strcpy(Action,TmpBuf);
					}
				}
			}
		}

			/* Let Eliza say what she wants to say. */

		if(Answers[NKEYS + NVERBS + CurrentAnswers[KeyPos]] . a_Type)
			printf("%s%s",Answers[NKEYS + NVERBS + CurrentAnswers[KeyPos]] . a_Text,Action);
		else
			printf("%s",Answers[NKEYS + NVERBS + CurrentAnswers[KeyPos]] . a_Text);

		if(Answers[NKEYS + NVERBS + CurrentAnswers[KeyPos]] . a_Question)
			printf("?\n");
		else
			printf(".\n");

			/* To make Eliza look "intelligent" Josef Weizenbaum
			 * told Eliza to change the answer she gives if she
			 * finds a keyword each time she prints it.
			 * So there is always a new answer even if the
			 * the user repeats his argument three or four
			 * times.
			 */

		if((CurrentAnswers[KeyPos] + 1) > StartAnswers[KeyPos] + MaxAnswers[KeyPos])
			CurrentAnswers[KeyPos] = StartAnswers[KeyPos];
		else
			CurrentAnswers[KeyPos]++;
	}
}
