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 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231
|
#pragma warning(disable: 4786)
#include <iostream>
#include <string>
#include <vector>
#include <ctime>
const int MAX_RESP = 5;
const std::string delim = "?!.;,";
typedef std::vector<std::string> 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[] = {
{"HI",
{"\n HI THERE!",
"\n HOW ARE YOU?",
"\n HI!"}
},
{"WHAT IS YOUR NAME",
{"\n My name is Aritificial Intelligence.",
"\n You can call me Artificial Intelligence.",
"\n I already told you. My name is Aritificial Intelligence",
"\n How many times do I have to tell you?",}
},
{"HOW ARE YOU",
{"\n I' fine thanks!",
"\n I'm doing well, and you?",
"\n How many more times do I have to tell you?"}
},
{"WHO ARE YOU",
{"\n I'M AN A.I PROGRAM.",
"\n I think you perfectly know who I am",
"\n Why do you want know?"}
},
{"ARE YOU INTELLIGENT",
{"\n YES,OFCORSE.",
"\n WHAT DO YOU THINK?",
"\n ACTUALY,I'M VERY INTELLIENT!"}
},
{"ARE YOU REAL",
{"\n DOES THAT QUESTION REALLY MATERS TO YOU?",
"\n WHAT DO YOU MEAN BY THAT?",
"\n I'M AS REAL AS I CAN BE."}
},
{"I'M FINE",
{"\n Thats brilliant!",
"\n Good, Good",
"\n Oh, that's nice"}
},
{"SORRY",
{"\n Apology accepted!",
"\n It's fine",
"\n Oh, don't worry"}
},
{"DO I",
{"\n Yes you do!",
"\n Of course you do",
"\n YEP"}
},
{"REALLY",
{"\n Yes! Really!",
"\n Yes.",
"\n Yes! Why not?"}
},
{"how",
{"\n How am I supposed to know?",
"\n I don't know.",
"\n Why are you asking me?"}
},
};
size_t nKnowledgeBaseSize = sizeof(KnowledgeBase)/sizeof(KnowledgeBase[0]);
int main()
{
srand((unsigned) time(NULL));
std::string sInput = "";
std::string sResponse = "";
std::string sPreviousResponse = "";
std::string sPreviousInput = "";
while(1)
{
system("COLOR 0A");
std::cout << "\n ";
sPreviousResponse = sResponse;
sPreviousInput = sInput;
std::getline(std::cin, sInput);
preprocess_input(sInput);
vstring responses = find_match(sInput);
if ( sInput == sPreviousInput && sInput.length() > 0 )
{
std::cout << "\n Why are repeating yourself?" << std::endl;
}
else if ( sInput == "bye" )
{
std::cout << "\n It was nice talking to you, see you next time!" << std::endl;
break;
}
else if ( responses.size() == 0 )
{
std::cout << "\n I don't understand what you are saying." << std::endl;
}
else
{
int nSelection = rand() % responses.size();
sResponse = responses[nSelection];
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);
}
vstring find_match(std::string input)
{
vstring result;
for(int i = 0; i < nKnowledgeBaseSize; ++i)
{
std::string keyWord(KnowledgeBase[i].input);
if( input.find(keyWord) != std::string::npos )
{
copy( KnowledgeBase[i].responses, result );
return result;
}
}
return result;
}
void copy(char *array[], vstring &v)
{
for(int i = 0; i < MAX_RESP; ++i)
{
if(array[i] != NULL)
{
v.push_back(array[i]);
}
else
{
break;
}
}
}
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;
}
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 == ' ')
{
continue;
}
else if(!isPunc(str[i]))
{
temp += str[i];
}
prevChar = str[i];
}
str = temp;
}
|