Я начинающий, и мне нужно создать простого чат-бота. Вместо того, чтобы компилировать запросы / ответы в самом коде, мы должны создать текстовый файл с запросами / ответами, и код должен искать введенную фразу и отвечать соответствующим образом. Я искал в сети пример кода и увидел это. Он в основном содержит класс Learner, который содержит всю реализацию. Код прилагается ниже
#include "learner.h"
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
void Learner::respond(string phrase){
fstream memory;
memory.open("memory/memory.txt", ios::in); // Open the memory file for input
while( !memory.eof() ){ // While not at end of file
string identifier;
getline(memory,identifier); // Get next phrase
if(identifier == phrase){ // Is it the phrase we are looking for
string response;
getline(memory,response); // If so, get the response
voice.say(response); // Textually and audibly output the response!
return; // Leave the function
}
}
memory.close();
memory.open("memory/memory.txt", ios::out | ios::app); // Now open for output, and append at end of file
memory << phrase << endl; // Record initial phrase in memory
voice.say(phrase); // Repeat the phrase the user entered
string userResponse;
cout << "YOU: ";
getline(cin, userResponse); // Get the ideal response
memory << userResponse << endl; // Write the ideal response to memory
memory.close(); // Close the file!
}
void Learner::say(string phrase){
this->voice.say(phrase);
}