check string for "key phrases"?????

May 1, 2011 at 5:29pm
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])
        return true;}}

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!
May 1, 2011 at 5:36pm
You do know you can just compare strings with the "==" operator right? There's also "compare": http://www.cplusplus.com/reference/string/string/compare/

If you have a reason for complicating it I'll listen but right now the phrase "reinventing the wheel" comes to mind.
May 1, 2011 at 5:54pm
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.
May 1, 2011 at 6:14pm
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?
May 1, 2011 at 6:46pm
go through the string one letter at a time until you find a space. then you know you have a word. then compare it to your list of keywords.
Last edited on May 1, 2011 at 6:47pm
May 1, 2011 at 6:49pm
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.
May 1, 2011 at 9:14pm
A much simpler (and faster) approach is simply to stuff your five words in an stl set:

http://www.cplusplus.com/reference/stl/set/

and then call find() to see if it's in the set.

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.
May 1, 2011 at 10:32pm
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.
May 1, 2011 at 10:46pm
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?
May 1, 2011 at 10:50pm
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.
May 2, 2011 at 1:33am
closed account (zwA4jE8b)
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.
May 2, 2011 at 2:57pm
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!
Topic archived. No new replies allowed.