You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

135 lines
3.4 KiB

#include <libtelegram/libtelegram.h>
#include <boost/algorithm/string.hpp>
#include <vector>
#include <sstream>
#include <algorithm>
#include <iterator>
#include <regex>
#include "bot.h"
#include "sqlite_markov.h"
std::string TelegramBotM::get_token() {
using namespace std;
fstream ftoken;
ftoken.open("apikey.txt", ios::in);
if (!ftoken) {
perror("No apikey.txt file provided");
exit(1);
}
string apikey;
ftoken >> apikey;
return apikey;
}
bool MarkovChain::test() {
add_ngrams("so long and thanks\nfor\tall the fishes bro man");
return false;
}
bool MarkovChain::process_string(std::string& message) {
if (this->MAKE_LOWER) {
for (int i = 0; i < message.length(); i++) {
if (std::isupper(message[i])) {
message[i] = std::tolower(message[i], std::locale());
}
}
}
message += " MESSAGE_END";
return false;
}
std::vector<std::string> MarkovChain::split_string(std::string message) {
std::istringstream ss(message);
std::vector<std::string> words{
std::istream_iterator<std::string>{ss},
std::istream_iterator<std::string>{}
};
return words;
}
bool MarkovChain::add_ngrams(std::string message) {
this->process_string(message);
std::vector<std::string> words (split_string(message));
std::cout << message << std::endl;
for (int i = 0; i < words.size(); i++) {
if (i >= order) {
std::vector<std::string> add_words;
int count = 0;
for (int j = i - order; j <= i; j++, count++) {
add_words.push_back(words[j]);
std::cout << j << ": " << add_words[count] << std::endl;
}
markov.add_ngrams(add_words);
}
}
return false;
}
std::string MarkovChain::continue_message(std::string message, int &score) {
std::vector<std::string> words {split_string(message)};
words.erase(words.begin(), words.begin() + words.size() - order);
std::string ret = markov.get_continuation(words, score);
return ret;
}
void TelegramBotM::add_echo() {
listener.set_callback_message([&](telegram::types::message const &message) {
// listener.set_callback_json([&](nlohmann::json const &input) {
std::string message_text = message.text.value();
try {
markov.add_ngrams(message_text);
} catch (std::exception &e) {
std::cout << "Error adding ngram: " << e.what() << std::endl;
}
//if (replies && rand() % replies != 1) {
//return;
//}
int score = 0;
std::string reply;
try {
// reply = markov.continue_message(message_text, score);
reply = message_text;
} catch (std::exception &e) {
std::cout << "Error getting message" << std::endl;
}
std::cerr << "SCORE: " << score << std::endl;
std::cerr << "Send" << std::endl;
try {
sender.send_message(message.chat.id, reply);
} catch (std::exception &e) {
std::cerr << "Send error: " << e.what() << std::endl;
}
});
}
int main() {
int order = 1;
SQLiteMarkov db = SQLiteMarkov(order, "markov.sqlite", "markov");
MarkovChain m = MarkovChain(order, true, db);
TelegramBotM b = TelegramBotM(order, m, "markov");
std::cout << "Running...\n";
b.run();
return 0;
}