Take the 2-minute tour ×

I have this personal C++ project of mine and I am currently stuck. It seems that a lot of people have asked questions about this topic, and I would like to apologize now if it is a repeat question.

Anyways, this project is a chat bot that will answer based on the user input. Currently as it stands, it takes the whole query and looks for a match using long, inefficient lines of if statements. If it finds a match, it will respond with a certain answer.

The reason why I came here is because I got sick and tired of writing very time consuming and inefficient if statements that don't even catch all the variations of the same question. I'm not looking for code or solutions that takes data from Wikipedia or something like that. What I would like is for the chat bot to just answer some simple questions.

During my time slogging away with the if statements, I came up with an idea. Why don't I use keywords just like a search engine ranks web pages?

I have written code so far as to count how many times a keyword (or several different ones) exist in the query. The problem is how to go about ranking them to find the best answer? I would presume that the answers and keywords would need to be stored in a special way.

My list of answers to my main question so far are:

1. When a query is received, rank it via an ini file where it has the keyword(s) in the key, and the answer as the value of it. If there are multiple answers, take another keyword and search for that as well as the original (decreasing the scope). Doing this enough times should yield the correct answer.

Pros: Suits what I need to do, and it is something I can grasp in C++.

Cons: Seems lengthy and inefficient, almost like diving into the if statements again...

2. Using a SQL database, tell it to find the applicable answer. Not sure how that would be done though.

Pros: It would be light weight, as the database computer would handle the search and could be quite detailed.

Cons: Might cause quite a bit of pain for me as I'm already treading quite high waters with C++. However, I'm starting to think it will end up just like the if statements.

3. The best answer that someone would probably suggest here is AIML (which was discussed here).

Pros: Used to develop smart chat bots, and is quite powerful.

Cons: Seems too "heavy" for my simple project and I can not nail down a search that finds me an easy to understand code for a bot that takes AIML.

I hope someone could suggest a smart route to take as I'm not really a fan of C++ and I feel like I'm already treading deep water with this project. However for this summer I felt like biting my tongue, going out of my comfort zone and for once making something useful in C++. I could have done this quickly in PHP, but in order to send the messages, I have to use C++.

share|improve this question
If you could have done it in php, what's stopping you from doing it in c++? Also, you should probably focus more on context than keyword ranking if you want it to appear intelligent. –  Greg Jun 29 '12 at 19:05
When it comes to making artificial intelligence at least somewhat "intelligent", there's going to be a lot of work involved. The first stage of grieving is acceptance. –  Alex W Jun 29 '12 at 19:06
Context would be something simple, I have already thought it through. It's just how to find the correct or best answer. As Alex brought up, I'm fearing that any sort of artificial intelligence isn't going to easy to do. I'm not the best at C++, but at the same time I'm not crawling. I'm not used to C++ and I'm still wrapping my head around some concepts. –  user1491774 Jun 29 '12 at 19:24
Can you give us a sense of the type of questions your bot will be answering, and what type of responses it will give? –  paddy Aug 14 '12 at 2:58
I think using SQL will be the correct way to go. That application is made for things like this and will probably give you the best performance under correct techniques and optimizations. (Which I believe will be easier to learn, compared to design the whole optimized system in C++) –  Etherealone Aug 15 '12 at 12:39
show 2 more comments

1 Answer

I have done something similar with an IRC bot some time ago, and I used AIML for this. Counting the amount of times a keyword occurs in a sentence doesn't seem like a very accurate way. Using AIML is probably the way to go. If you used AIML, you would not have to bother a lot with writing C++ code, since the actual "brain"(as it could be called) is written in AIML. A small extract from the code that I (well I think it came from the example for a large part) wrote to load libaiml.xml (the file containing the AIML code for your bot):

#include <aiml.h>
// ...
using namespace aiml;

// setup Ai bot:
cInterpreter* interpreter = cInterpreter::newInterpreter();

try {
        throw 1;
} catch(int _ret) {
    cout << "ERROR: " << interpreter->getErrorStr(interpreter->getError())
         << " (" << interpreter->getError() << ")" << endl;
        cout << "Runtime Error: " << interpreter->getRuntimeErrorStr() << endl;

I then used interpreter->respond(message, "name", result); to get a response. I linked with libaiml (needs libxml2), which can be found at http://www.alicebot.org/downloads/programs.html.(scroll down to C++, you should find libaiml there; notice that you must also install std_utils). It's possible that my code does no longer work because of updates in the library, I don't know. Luckily, libaiml also contains a nice example that was very useful at the time for me.

You should be able to find quite a lot of documentation about the XML format itself, eg. http://www.alicebot.org/TR/2011/. I recommend you to take a look at the example I mentioned above (included with libaiml) too.

I wish you good luck!

share|improve this answer
add comment

Your Answer


