Help with an artificial intelligence program

January 22, 2011 at 08:47:17
Specs: Windows XP
I have to do a c++ project for school and decided to aim a little higher by making an artificial intelligence program in the style of cleverbot. It works by processing the users input by converting the string array into a 2-D array with each word in a new row. It then compares the words with a set of keywords and sub-keywords and depending on the matches, prints a reply. The following is a very simple version of the program: it says 'Hi' if your input contains hello; it says it's name if your input contains 'name' and 'What' and it complements your name if your input contains 'name' and 'My'; it says goodbye if your input contains 'Bye' and quits the program; if there are no matches at all then it prints that it cannot understand. However I cannot get the program to work. It simply prints three sentences with any input, which seems impossible as there are conditions for it to print each sentence and the value of the condition cannot change spontaneously.

Here is the code:


//ai program

#include <iostream.h>

int x;

char keywords[3][10] = {"Hello", "name", "Bye"}; //primary set of keywords
char skeywords[2][10] = {"What", "My"}; //secondary set of keywords
char temp[10][10]; //the temporary 2-D array to which the user's sentence is converted during processing

void process(char you[]); //function to process what the user says

void main()
{
	x = 1; //to check whether the user still wants the program to run
	char you[30];
	cout<<"Ai: Hello! My name is Ai and I am a being possessing articial intelligence created by Akhil and Arvind!";
	while (x) //primary conversation loop begins
	{
		cout<<"\n\nYou:  ";
		cin.getline(you, 30); 
		process (you);
	}
}

void process(char you[30])
{
	int i=0, j=0, k=0, l=0, m=0, n=0, count=0; 
	
	while (you[i]!=NULL) //loop to convert the user's sentence to a 2-D string array with each word in a different row 
	{
		if (you[i]==' ') //to move to the next row when there is a space
		{
			i++; //i refers to the array holding the original sentence
			j++; //j refers to the row number which is incremented
			k=0; //k refers to the column number which is reset since the loop needs to continue from the beginning of the second row
		}
		else //to convert the sentence into a temporary 2-D array
		{
			you[i]=temp[j][k]; //data copied
			i++; //sentence array number incremented
			k++; //column number of 2-D array incremented
		}
	}

	i=0, j=0; //i and j are re-used to keep track of the keywords array

	do
	{
		for (l=0; l<3; l++)
		{
			for(m=0; temp[l][m]!=NULL; m++)
			{
				if(keywords[i][j]==temp[l][m]) //if there is a match
				{
					count++;
					j++; //increment the column of the keyword array
				}
				if(keywords[i][j]=NULL) //if there is a match on the final letter of the keyword
						break;
				else
				{
					if(count>0)
						count=0;
					if(keywords[i][j]=NULL) //if there is no match
					{
						i=3;
						break;
					}
				}
			}
		}
		i++;
	}while(count=0);

	count=0; //resetting the value of count in case it needs to be used in later loops to search for sub keywords
	k=0, n=0; //since i and j cannot be used, k and n are used instead to keep track of the sub keyword loops
	
	if(i=0)
		cout<<"\n\nAi: Hi!";
	
	if(i=1)
	{
		do
		{
			for (l=0; l<2; l++)
			{
				for(m=0; temp[l][m]!=NULL; m++)
				{
					if(keywords[k][n]==temp[l][m]) //if there is a match
					{
						if (keywords[k][n]=NULL) //if there is a match on the final letter of the keyword
							break;
						count++;
						n++; //increment the column of the keyword array
						
					}
					else
					{
						if(count>0) //reset count if there is no match with consecutive letter
							count=0;
					}
				}
			}
			k++;
		}while(count=0);

		if(k=0)
			cout<<"\n\nAi: My name is Ai! What's yours?";
		if(k=1)
			cout<<"\n\nAi: That's a great name!";
	}

	if(i=2)
	{
		cout<<"\n\nAi: Bye! Talking to you was fun!\n\n";
		x=0;
	}

	if(i=3)
	{
		cout<<"\n\nAi: Sorry I did not understand. Can you rephrase your question?\n\n";
	}
}

Help would be appreciated!


See More: Help with an artificial intelligence program

Report •

#1
January 22, 2011 at 15:12:42
Dunno about all of the loops - they give me a headache. :P If you're wanting to split text around a space, then you can simplify things a bit by using strtok.

... // other bits of code
char *p = strtok(user_input, " ");
int index = 0; // for temp array 

if(p != NULL) {   
   while(p != NULL) {
      strncpy(temp[index++], p, max_length_of_temps_elements - 1);
      p = strtok(NULL, " ");
   }
} else {
   strncpy(temp[0], input, max_length_of_temps_elements - 1);
} 

// assuming there is enough space to copy a given number of words

Unless they're just typos, you've got a lot of assignment where you mean to compare values; for example, if(k=0){ ... } for comparison of values, use ==


Report •
Related Solutions


Ask Question