1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
|
//
// Program Name: chatterbot3
// Description: this is an improved version of the previous chatterbot program "chatterbot1"
// this one will try a littlebit more to understand what the user is trying to say and will also
// try to avoid repeating himself too much.
//
// Author: Gonzales Cenelia
//
#pragma warning(disable: 4786)
#include <iostream>
#include <string>
#include <vector>
#include <ctime>
const int MAX_RESP = 3;
const std::string delim = "?!.;,";
typedef std::vector vstring;
vstring find_match(std::string input);
void copy(char *array[], vstring &v);
void preprocess_input(std::string &str);
void UpperCase( std::string &str );
void cleanString(std::string &str);
bool isPunc(char c);
typedef struct {
char *input;
char *responses[MAX_RESP];
}record;
record KnowledgeBase[] = {
{"WHAT IS YOUR NAME",
{"MY NAME IS CHATTERBOT2.",
"YOU CAN CALL ME CHATTERBOT2.",
"WHY DO YOU WANT TO KNOW MY NAME?"}
},
{"HI",
{"HI THERE!",
"HOW ARE YOU?",
"HI!"}
},
{"HOW ARE YOU",
{"I'M DOING FINE!",
"I'M DOING WELL AND YOU?",
"WHY DO YOU WANT TO KNOW HOW AM I DOING?"}
},
{"WHO ARE YOU",
{"I'M AN A.I PROGRAM.",
"I THINK THAT YOU KNOW WHO I'M.",
"WHY ARE YOU ASKING?"}
},
{"ARE YOU INTELLIGENT",
{"YES,OFCORSE.",
"WHAT DO YOU THINK?",
"ACTUALY,I'M VERY INTELLIENT!"}
},
{"ARE YOU REAL",
{"DOES THAT QUESTION REALLY MATERS TO YOU?",
"WHAT DO YOU MEAN BY THAT?",
"I'M AS REAL AS I CAN BE."}
}
};
size_t nKnowledgeBaseSize = sizeof(KnowledgeBase)/sizeof(KnowledgeBase[0]);
int main()
{
srand((unsigned) time(NULL));
std::string sInput = "";
std::string sResponse = "";
std::string sPreviousResponse = "";
while(1) {
std::cout << ">";
sPreviousResponse = sResponse;
std::getline(std::cin, sInput);
preprocess_input(sInput);
vstring responses = find_match(sInput);
if(sInput == "BYE") {
std::cout << "IT WAS NICE TALKING TO YOU USER, SEE YOU NEXT TIME!" << std::endl;
break;
} else if(responses.size() == 0) {
std::cout << "I'M NOT SURE IF I UNDERSTAND WHAT YOU ARE TALKING ABOUT." << std::endl;
} else {
int nSelection = rand() % MAX_RESP;
sResponse = responses[nSelection];
// avoids repeating the same response
if(sResponse == sPreviousResponse) {
responses.erase(responses.begin() + nSelection);
nSelection = rand() % responses.size();
sResponse = responses[nSelection];
}
std::cout << sResponse << std::endl;
}
}
return 0;
}
void preprocess_input(std::string &str) {
cleanString(str);
UpperCase(str);
}
// make a search for the user's input
// inside the database of the program
vstring find_match(std::string input) {
vstring result;
for(int i = 0; i < nKnowledgeBaseSize; ++i) {
if(std::string(KnowledgeBase[i].input) == input) {
copy(KnowledgeBase[i].responses, result);
return result;
}
}
return result;
}
void copy(char *array[], vstring &v) {
for(int i = 0; i < MAX_RESP; ++i) {
v.push_back(array[i]);
}
}
void UpperCase( std::string &str ) {
int len = str.length();
for( int i = 0; i < len; i++ ) {
if ( str[i] >= 'a' && str[i] <= 'z' ) {
str[i] -= 'a' - 'A';
}
}
}
bool isPunc(char c) {
return delim.find(c) != std::string::npos;
}
// removes punctuation and redundant
// spaces from the user's input
void cleanString( std::string &str ) {
int len = str.length();
std::string temp = "";
char prevChar = 0;
for(int i = 0; i < len; ++i) {
if( (str[i] == ' ' && prevChar != ' ') || !isPunc(str[i]) ) {
temp += str[i];
prevChar = str[i];
}
else if(prevChar != ' ' && isPunc(str[i])) {
temp += ' ';
}
}
str = temp;
}
|