5

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++.

4

1 回答 1

0

前段时间我用 IRC 机器人做了类似的事情,为此我使用了 AIML。计算关键字在句子中出现的次数似乎不是一个非常准确的方法。使用 AIML 可能是要走的路。如果您使用 AIML,则不必费心编写 C++ 代码,因为实际的“大脑”(可以称为)是用 AIML 编写的。我(我认为大部分来自示例)为加载 libaiml.xml(包含您的机器人的 AIML 代码的文件)编写的代码的一小部分摘录:

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

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

try {
    if(!interpreter->initialize("libaiml.xml"))
        throw 1;
} catch(int _ret) {
    cout << "ERROR: " << interpreter->getErrorStr(interpreter->getError())
         << " (" << interpreter->getError() << ")" << endl;
    if(!interpreter->getRuntimeErrorStr().empty())
        cout << "Runtime Error: " << interpreter->getRuntimeErrorStr() << endl;
}

然后我习惯interpreter->respond(message, "name", result);了得到回应。我链接了 libaiml(需要 libxml2),可以在http://www.alicebot.org/downloads/programs.html找到。(向下滚动到 C++,你应该在那里找到 libaiml;注意你还必须安装 std_utils) . 我不知道,由于库中的更新,我的代码可能不再工作。幸运的是,libaiml 还包含一个很好的示例,当时对我来说非常有用。

您应该能够找到很多关于 XML 格式本身的文档,例如。http://www.alicebot.org/TR/2011/。我建议您也看一下我上面提到的示例(包含在 libaiml 中)。

祝你好运!

于 2012-08-21T13:33:44.940 回答