Hello. I'm trying to make a simple chatbot, but I'm stuck on checking the input for keywords/phrases. My current method goes through the input and all the keywords and extracts a string the size of the keyword from the input string at a given spot. This is done with multiple nested for loops like so:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
//inp and temp are strings
//keywords is an array of 5 strings
for (int c = 0; c<inp.size(); c++){ //cycle through all posible "start"
// positions in input string
temp.clear(); //clear previous word
for (int t = 0; t<4; t++){ //cycle through all keywords
//cycle through all letters in current word
for (int i = 0; i<keywords[t].size(); i++){
//add # of letters in keyword from start position in inp string
temp.push_back(inp[c+i]);}
if (temp==keywords[t])
returntrue;}}
This method of detection worked for smaller words like "hi", but not the larger words. Any ideas on how I can fix this, or a better way? Thanks!
haha, actually, I'm trying to compare section of the string to a keyword string.
Say the user types in "whats your name?". I want to scan the input string to find keywords such as "your name". Then, based on the keywords found, give an appropriate response.
I'm now playing around with the find() member of std::string and it appears to be working.
Cool, do you plan on posting the complete code? Stuff like Smarter Child and CleverBot have always facinated me. Are you shooting for the Turing test? Or is this just some string parsing excersize?
I'll post a download link when its finished, I just need to add some bug fixes and give it some sort of memory so it doesn't ask the same question twice, etc.
If you want your bot to do different things depending on the keyword found, you should use a map, where the key is the keyword string and the value is some kind of function pointer. In effect, you create a jump-table.
Actually, I'm using a vector of a keyword class that contains a string and some other info on how it effects the response. Its pretty simple, but it works. I'm using find() from the string class.
A vector of keywords will of course work, but will require a linear search (which will get slow as the text gets larger or as the number of keywords increases).
You mentioned:
This method of detection worked for smaller words like "hi", but not the larger words. Any ideas on how I can fix this, or a better way?
Specifically, what do you mean? What larger words don't work and how do they not work?
I fixed that by using find() rather than my own, faulty algorithm. Speed isn't too much of an isssue here, this is more of a toy than an official project.
does your program work with irc or what chat network/client? I am interested in starting network programming and would like to see your finished product.
Right now its just a console app, but you would only need to change <20 lines of code to get rid of the console. I'm lost when it comes to network programming, so I won't be of any help there.
Its not finished yet, and my databases are nowhere near complete, but if your curious, here is the source and a binary: http://www.modshop.0sites.org/Chatbot.rar
If you made any additions to the database files (through the chatbot), feel free to send them to me at invince24@earthlink.net
Any help would be greatly appreciated!