Devdutt Shenoi 28 October 2016

How to build a Chatbot in C++

Program file

Header files

#include <iostream>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <fstream>// for saving and accessing data file.
#include <windows.h>// for system('cls');
#include <stdio.h>
	#include <time.h>// for accessing system time.

Don't forget to define the namespace as standard.

using namespace std;

Class defenitions

entry class

Define a class 'entry' which is used as a framework for the entries into the binary file. Create an object of this class 'no1' also. This is for later use.

class entry {
	char ans[20], user[20];
	public:
	char que[30];
	void re(){
		cout.write(ans,20);
	}
	void ideal(){
		cin.getline(ans,20);
	}
	void getUser(char u[20]){
		strcpy(user,u);
	}
} no1;
bot class

Define a class 'bot' which holds details pertaining to the Artificial Intelligence program.

class bot {
	char iq[30], re[20], ideal[20], userName[10];
	public:
	//Constuctor to make object of bot.
	bot(char n[20]){
		cout<"\n\n\t\tHey there, this is Devdutt\n\n\nBefore you start to chat with my bot, can I get your name?\n" << endl;
		cin.getline(userName,20);
		strcpy(n,userName);
	}
	void reply(char input[30]);
	void rename(char n[]){
		strcpy(n,userName);
	}
};

Class method defenitions

Define the bot's 'reply' method to answer queries based on data stored in a binary file or otherwise ask the user to give a fitting reply to the question, so that the bot can learn.

void bot::reply(char input[30]){
	fstream M;
	// Open the file for use.
	M.open("memory.bin", ios::in | ios::binary);
	// Search through the file until end is reached.
	while( M ) {
		M.read((char*)&no1,sizeof(no1));
		strcpy(iq,no1.que); // Get next question for comparison.
		// Check whether the question from memory is what we are looking for.
		if(iq == input) {
			no1.re(); // If so, get related reply.
		}
	}
	// If reply couldn't find input in textfile close it!
	M.close();
	// Open memory.dat for output, and use append at end of file.
	M.open("memory.bin", ios::out | ios::app| ios::binary);
	M.seekp(sizeof(no1),ios::end);
	strcpy(no1.que,input);   // Record initial input in memory.
	cout << "I don't know what you mean when you say \'" << input << "\', what should I say?\n\tIDEAL REPLY (PLEASE INPUT) : ";
	no1.ideal();    // Get the ideal output.
	no1.getUser(userName); // Save name of person who modified database.
	M.write((char*)&no1,sizeof(no1));    // Write the ideal output to memory.
	M.close();    // Close the file!
}

Main Function

Given inside the main function is the actual program. Access functions defined in the global scope to run the utility programs.

// These are activated when the User wants.
void quiz();
void joke();
void showTime();
void dataRe();
int ask(char in[]);
int i,n;
char name[20];
bot AI(name);

Generate AI object of bot class. This will be accessed from hereforth.

Main Function

Define the main() function. It is the portion of the program which does the work of deciding the execution sequence of other functions.

int main() {
	system("cls");
	cin.clear();
	cin.ignore();
	char input[30];
	//Char variable to store answer for Exit.
	for(;; i++) {
		// Prompt User for input.
		cout<<name<<": ";
		cin.getline(input,30);
		cout<<"AI: ";
		n==ask(input);
		// Check whether the user wanted to exit i.e. n==-1.
		if(n==-1) return 0;
		cout<<endl;
	}
	return 0;
}

Function Defenitons

Ask

Define a function that will handle questions that can be used to trigger an applet program such as a game of quiz.

int ask(char in[30]){
        char y;
        //Give maximum number of fallbacks to greetings, to not miss.
        if(strcmp(in,"Hello")==0||strcmp(in,"hello")==0||strcmp(in,"Hi")==0||strcmp(in,"hi")==0) cout << "Hello there ";
        // Let the User decide whether to exit the program.
        else if(strcmp(in,"exit")==0||strcmp(in,"Exit")==0||strcmp(in,"X")==0) {
                cout << "\n\t\t\tAre you sure?(Y/N) \n\t\t\tYou have asked "<<i<<" questions.\n";
                if(y=='y'||y=='Y') return -1;
        }
        else if(strcmp(in,"name")==0||strcmp(in,"Name")==0) cout <<"\n\t\tMy name is V!\n\t\tcreated by Devdutt Shenoi ";
        else if(strcmp(in,"data")==0) dataRe();
        else if (strcmp(in,"Game")==0||strcmp(in,"game")==0) quiz();
        else if (strcmp(in,"joke")==0||strcmp(in,"Joke")==0) joke();
        else if(strcmp(in,"What is the time?")==0||strcmp(in,"time")==0||strcmp(in,"Time")==0) showTime();
        /*Only if the User doen't want to exit should his question go to the AI.
           Pass User's input to the AI and let it respond.*/
        else AI.reply(in);
        return 0;
}
Definitions pertaining to random functions.

Define the functions whose prototypes were given before. These will do certain jobs that the user wants.

1 .quiz(): A game which asks questions
void quiz() {
        int r = rand() % 4;
        char in[20];
        switch(r) {
        case 0: cout<<"\nWho is the PM of India?\n";
                cin.getline(in,20);
                if(strcmp(in,"Narendra Modi")==0||strcmp(in,"Modi")==0) {
                        cout<<"\nCorrect";
                        exit(0);
                }
        case 1: {cout<<"\nWho is the President of India?\n";
                 cin.getline(in,20);
                 if(strcmp(in,"Pranab Mukherjee")==0)
                         cout<<"\nCorrect";
                 exit(0);}
        case 2: cout<<"\nWho am I?\n";
                cin.getline(in,20);
                if(strcmp(in,"Artificial Intelligence")==0||strcmp(in,"AI")==0||strcmp(in,"V!")==0) {
                        cout<<"\nCorrect";
                        exit(0);
                }
        case 3: cout<<"\nWho is Steve Jobs?\n";
                cin.getline(in,20);
                if(strcmp(in,"Artificial Intelligence")==0||strcmp(in,"AI")==0||strcmp(in,"V!")==0) {
                        cout<<"\nCorrect";
                        exit(0);
                }
        default: exit(0);//Add more questions
        }
        cout<<"\nWrong answer";
}
2. joke(): A function to say jokes as per a random number algorithm
void joke() {
	int r = rand() % 4;
	switch(r) {
		case 0: cout<<"\nAsk SiRi 'What is 10^200?' only if you are a musician."; break; case 1: cout<<"\nYou:'Who is Indian?'\nSiRi:'You must be Trump'";
			break;
		case 2: cout<<"\nOnce a friend said 'I have 8GB RAM, 2TB HDD, 8core CPU, what do you have?'\nI answered I have a 'mac'";
		  break;
		case 3: cout<<"\nPhysics students want Newton to have been a Keralite, why?\n Becuase coconut has more 'm', giving a larger 'F'.";
		  break;
		default: exit(0); //Add more jokes
	}
}
3. showTime(): A function to say the time by printing it into the console
void showTime(){
	time_t rawtime;
	struct tm * timeinfo;
	time ( &rawtime );
	cout<<"The current date/time is: "<< ctime(&rawtime);
}
4. dataRe(): Displays previous questions and their respective answers, saved in memory file
void dataRe(){
	ifstream in;
	in.open("memory.bin",ios::binary);
	cout<<endl;
	while( in ) {
		in.read((char*)&no1, sizeof(no1));
		cout<<"Q: ";
		cout.write(no1.que,30);
		cout<<"\nA: ";
		no1.re();
		cout<<endl;
		

Conclusion

The above program is a very good representation of the abilities of C++ in Artificial intelligence. Even though the program was very simple, the ability to learn from past experiences is a very basic trait of programs built for machine learning.

Download source code from:

Download actual project report from: OneDrive