|
|
|
@ -27,11 +27,10 @@ bool MarkovChain::process_string(std::string& message) {
@@ -27,11 +27,10 @@ bool MarkovChain::process_string(std::string& message) {
|
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
message += " MESSAGE_END"; |
|
|
|
|
|
|
|
|
|
return false; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
std::vector<std::string> MarkovChain::split_string(std::string message) { |
|
|
|
|
std::istringstream ss(message); |
|
|
|
|
|
|
|
|
@ -43,11 +42,12 @@ std::vector<std::string> MarkovChain::split_string(std::string message) {
@@ -43,11 +42,12 @@ std::vector<std::string> MarkovChain::split_string(std::string message) {
|
|
|
|
|
return words; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
bool MarkovChain::add_ngrams(std::string message) { |
|
|
|
|
std::vector<std::vector<std::string>> MarkovChain::get_words(std::string message) { |
|
|
|
|
this->process_string(message); |
|
|
|
|
|
|
|
|
|
std::vector<std::string> words (split_string(message)); |
|
|
|
|
|
|
|
|
|
std::vector<std::vector<std::string>> import; |
|
|
|
|
|
|
|
|
|
for (int i = 0; i < words.size(); i++) { |
|
|
|
|
if (i >= order) { |
|
|
|
@ -58,11 +58,11 @@ bool MarkovChain::add_ngrams(std::string message) {
@@ -58,11 +58,11 @@ bool MarkovChain::add_ngrams(std::string message) {
|
|
|
|
|
add_words.push_back(words[j]); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
markov.add_ngrams(add_words); |
|
|
|
|
import.push_back(add_words); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return false; |
|
|
|
|
return import; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
@ -88,6 +88,7 @@ std::string MarkovChain::continue_message(std::string message, int &score) {
@@ -88,6 +88,7 @@ std::string MarkovChain::continue_message(std::string message, int &score) {
|
|
|
|
|
std::cout << "Using seed: "; |
|
|
|
|
for (auto x: newwords)
|
|
|
|
|
std::cout << " " << x; |
|
|
|
|
|
|
|
|
|
std::cout << "\n"; |
|
|
|
|
|
|
|
|
|
std::string ret = markov.get_continuation(newwords, score); |
|
|
|
@ -95,6 +96,7 @@ std::string MarkovChain::continue_message(std::string message, int &score) {
@@ -95,6 +96,7 @@ std::string MarkovChain::continue_message(std::string message, int &score) {
|
|
|
|
|
return ret; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
std::string TelegramBotM::get_token() { |
|
|
|
|
using namespace std; |
|
|
|
|
fstream ftoken; |
|
|
|
@ -117,8 +119,9 @@ void TelegramBotM::add_echo() {
@@ -117,8 +119,9 @@ void TelegramBotM::add_echo() {
|
|
|
|
|
|
|
|
|
|
try { |
|
|
|
|
markov.add_ngrams(message_text); |
|
|
|
|
} catch (std::exception &e) { |
|
|
|
|
std::cout << "Error adding ngram: " << e.what() << std::endl; |
|
|
|
|
} catch (char const * e) { |
|
|
|
|
std::cout << "Error adding ngram: " << e << std::endl; |
|
|
|
|
return; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
//if (replies && rand() % replies != 1) {
|
|
|
|
@ -129,7 +132,6 @@ void TelegramBotM::add_echo() {
@@ -129,7 +132,6 @@ void TelegramBotM::add_echo() {
|
|
|
|
|
std::string reply; |
|
|
|
|
|
|
|
|
|
reply = markov.continue_message(message_text, score); |
|
|
|
|
std::cout << "Reply generated: " << reply << std::endl; |
|
|
|
|
|
|
|
|
|
if (reply == "") { |
|
|
|
|
return; |
|
|
|
@ -148,24 +150,56 @@ void TelegramBotM::add_echo() {
@@ -148,24 +150,56 @@ void TelegramBotM::add_echo() {
|
|
|
|
|
}); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
int import_from_file(std::string filename, MarkovChain &m) { |
|
|
|
|
|
|
|
|
|
bool MarkovChain::add_ngrams(std::string message) { |
|
|
|
|
|
|
|
|
|
process_string(message); |
|
|
|
|
std::vector<std::vector<std::string>> add_words(get_words(message)); |
|
|
|
|
|
|
|
|
|
if (markov.batch_add_ngrams(add_words)) |
|
|
|
|
throw "Failed to add ngrams"; |
|
|
|
|
|
|
|
|
|
return 0; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int MarkovChain::import_from_file(std::string filename) { |
|
|
|
|
|
|
|
|
|
std::ifstream infile(filename); |
|
|
|
|
|
|
|
|
|
int i = 0; |
|
|
|
|
std::string line; |
|
|
|
|
std::vector<std::vector<std::string>> imports(0); |
|
|
|
|
|
|
|
|
|
while(std::getline(infile, line)) { |
|
|
|
|
m.add_ngrams(line); |
|
|
|
|
std::cout << "\radded " << ++i << " lines."; |
|
|
|
|
|
|
|
|
|
this->process_string(line); |
|
|
|
|
std::vector<std::vector<std::string>> add_words(get_words(line)); |
|
|
|
|
|
|
|
|
|
imports.insert(imports.end(),
|
|
|
|
|
std::make_move_iterator(add_words.begin()),
|
|
|
|
|
std::make_move_iterator(add_words.end())); |
|
|
|
|
|
|
|
|
|
i++; |
|
|
|
|
if (i % 21 == 0) { |
|
|
|
|
std::cout << "\rread " << i << " lines." << std::flush; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
std::cout << "\nImporting...\n" << std::flush; |
|
|
|
|
|
|
|
|
|
if (markov.batch_add_ngrams(imports)) |
|
|
|
|
return 1; |
|
|
|
|
|
|
|
|
|
std::cout << "imported " << imports.size() << " ngrams.\n" << std::flush; |
|
|
|
|
|
|
|
|
|
return 0; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
struct options { |
|
|
|
|
std::string filename = "markov.sqlite"; |
|
|
|
|
std::string tablename = "markov"; |
|
|
|
|
int order = 2; |
|
|
|
|
int order = 1; |
|
|
|
|
std::string importname = ""; |
|
|
|
|
bool run_bot = true; |
|
|
|
|
}; |
|
|
|
@ -203,7 +237,6 @@ struct options parse_args(int argc, char ** argv) {
@@ -203,7 +237,6 @@ struct options parse_args(int argc, char ** argv) {
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
int main(int argc, char **argv) { |
|
|
|
|
|
|
|
|
|
struct options opts = parse_args(argc, argv); |
|
|
|
|
|
|
|
|
|
SQLiteMarkov db = SQLiteMarkov(opts.order, opts.filename, opts.tablename);
|
|
|
|
@ -223,8 +256,7 @@ int main(int argc, char **argv) {
@@ -223,8 +256,7 @@ int main(int argc, char **argv) {
|
|
|
|
|
while(std::getline(std::cin, line)) { |
|
|
|
|
if (line.size() > 0) { |
|
|
|
|
if (std::tolower(line[0]) == 'y') { |
|
|
|
|
std::cout << "Importing...\n"; |
|
|
|
|
if (import_from_file(opts.importname, m)) { |
|
|
|
|
if (m.import_from_file(opts.importname)) { |
|
|
|
|
std::cerr << "Failed import." << std::endl; |
|
|
|
|
} |
|
|
|
|
break; |
|
|
|
|