C++ 聊天机器人:使用语料库实现问答功能
以下是一个简单的 C++ 聊天机器人的示例代码,该代码读取配置语料库文件 config.ini,通过匹配用户输入的问题,并输出相应的回答。代码假设 config.ini 文件位于当前工作目录中,并且每个问题和回答之间都使用等号 (=) 分隔。
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <algorithm>
std::vector<std::string> split(const std::string& text, const std::string& delimiters) {
std::vector<std::string> tokens;
std::size_t start = 0, end = 0;
while ((end = text.find_first_of(delimiters, start)) != std::string::npos) {
if (end != start) {
tokens.push_back(text.substr(start, end - start));
}
start = end + 1;
}
if (start < text.length()) {
tokens.push_back(text.substr(start));
}
return tokens;
}
std::string trim(const std::string& str) {
std::size_t first = str.find_first_not_of("
");
std::size_t last = str.find_last_not_of("
");
if (first == std::string::npos || last == std::string::npos) {
return "";
}
return str.substr(first, last - first + 1);
}
std::string findBestMatch(const std::string& question, const std::vector<std::string>& questions) {
std::string bestMatch = "";
int bestMatchScore = 0;
for (const std::string& q : questions) {
int score = 0;
std::vector<std::string> qWords = split(q, " ");
for (const std::string& word : qWords) {
if (question.find(word) != std::string::npos) {
score++;
}
}
if (score > bestMatchScore) {
bestMatch = q;
bestMatchScore = score;
}
}
return bestMatch;
}
std::string getAnswer(const std::string& question, const std::vector<std::string>& questions, const std::vector<std::string>& answers) {
std::string bestMatch = findBestMatch(question, questions);
if (bestMatch.empty()) {
return "对不起,我不明白您的问题";
}
int index = std::distance(questions.begin(), std::find(questions.begin(), questions.end(), bestMatch));
return answers[index];
}
int main() {
std::vector<std::string> questions;
std::vector<std::string> answers;
std::ifstream configFile("config.ini");
if (!configFile) {
std::cout << "无法打开config.ini文件" << std::endl;
return 1;
}
std::string line;
while (std::getline(configFile, line)) {
std::size_t delimiterPos = line.find('=');
if (delimiterPos != std::string::npos) {
std::string question = trim(line.substr(0, delimiterPos));
std::string answer = trim(line.substr(delimiterPos + 1));
if (!question.empty() && !answer.empty()) {
questions.push_back(question);
answers.push_back(answer);
}
}
}
configFile.close();
std::string userInput;
std::cout << "请输入您的问题:" << std::endl;
std::getline(std::cin, userInput);
std::vector<std::string> userSentences = split(userInput, ".,,");
for (const std::string& sentence : userSentences) {
std::string trimmedSentence = trim(sentence);
if (!trimmedSentence.empty()) {
std::string answer = getAnswer(trimmedSentence, questions, answers);
std::cout << "回答: " << answer << std::endl;
}
}
return 0;
}
此代码的基本思路是:
- 读取
config.ini文件并将问题和回答存储在两个分别的向量中。 - 接收用户的输入,并使用
split函数将输入拆分为多个句子。 - 对于每个句子,使用
trim函数去除前导和尾随空格,并通过getAnswer函数找到与问题最匹配的回答。 - 输出回答。
请注意,此示例代码是一个简单的聊天机器人,并且匹配问题的方法非常基础。在实际应用中,您可能需要更复杂的算法和技术来提高匹配准确性,如自然语言处理(NLP)和机器学习。
原文地址: https://www.cveoy.top/t/topic/jTC 著作权归作者所有。请勿转载和采集!