From d7e934156858287cdb7597ad0dced97d4d279f2f Mon Sep 17 00:00:00 2001 From: Kelly Rauchenberger Date: Thu, 28 Feb 2019 20:28:20 -0500 Subject: Got it bot-ready! Using C++ randomization now, along with an update to rawr-ebooks that allows it to do the same. Cleaned up the code here and there. Added in the twitter client and the config file reader. We can use librawr's histogram class now, so we don't need our own. Also why did we not name this bot "aspartame". It's a Sugar replacer. Hahaha. I'm Pink Diamond. --- dialogue.cpp | 66 +++++++++++++++++++++++++++++++++++++++++++----------------- 1 file changed, 47 insertions(+), 19 deletions(-) (limited to 'dialogue.cpp') diff --git a/dialogue.cpp b/dialogue.cpp index 47a761d..1e95d2b 100644 --- a/dialogue.cpp +++ b/dialogue.cpp @@ -2,19 +2,17 @@ #include "identifier.h" #include "histogram.h" #include -#include -#include #include #include #include #include - - +#include +#include +#include using speakerstore = identifier; using speaker_id = speakerstore::key_type; - struct speaker_data { std::string name; @@ -24,21 +22,39 @@ struct speaker_data { }; +int main(int argc, char** argv) +{ + if (argc != 2) + { + std::cout << "usage: garnet [configfile]" << std::endl; + return -1; + } + + std::random_device randomDevice; + std::mt19937 rng(randomDevice()); + std::string configfile(argv[1]); + YAML::Node config = YAML::LoadFile(configfile); + twitter::auth auth( + config["consumer_key"].as(), + config["consumer_secret"].as(), + config["access_key"].as(), + config["access_secret"].as()); -int main(int, char**) -{ - srand(time(NULL)); - rand(); rand(); rand(); rand(); + twitter::client client(auth); speakerstore speakers; std::map speakerData; histogram allSpeakers; + using csv = + io::CSVReader< + 2, + io::trim_chars<' ', '\t'>, + io::double_quote_escape<',', '"'>>; - - io::CSVReader<2,io::trim_chars<' ', '\t'>,io::double_quote_escape<',', '"'>> in("../dialogue.csv"); + csv in(config["transcript"].as()); std::string speaker; std::string line; @@ -85,11 +101,10 @@ int main(int, char**) { std::set pastSpeakers; - - speaker_id curSpeaker = allSpeakers.next(); + speaker_id curSpeaker = allSpeakers.next(rng); std::ostringstream theEnd; - int maxLines = rand() % 4 + 3; + int maxLines = std::uniform_int_distribution(3, 6)(rng); for (int i = 0; i < maxLines; i++) { @@ -102,7 +117,8 @@ int main(int, char**) theEnd << curSd.name << ": "; } - std::string curLine = curSd.chain.randomSentence(rand() % 30 + 1); + int maxL = std::uniform_int_distribution(1, 30)(rng); + std::string curLine = curSd.chain.randomSentence(maxL, rng); if (curSd.name == "" && curLine[0] != '[' && @@ -116,13 +132,16 @@ int main(int, char**) theEnd << std::endl; - speaker_id repeatSpeaker = *std::next(std::begin(pastSpeakers), rand() % pastSpeakers.size()); + int psi = + std::uniform_int_distribution(0, pastSpeakers.size()-1)(rng); + + speaker_id repeatSpeaker = *std::next(std::begin(pastSpeakers), psi); if (repeatSpeaker != curSpeaker && - rand() % 3 == 0) + std::bernoulli_distribution(1.0 / 3.0)(rng)) { curSpeaker = repeatSpeaker; } else { - curSpeaker = curSd.nextSpeaker.next(); + curSpeaker = curSd.nextSpeaker.next(rng); } } @@ -131,9 +150,18 @@ int main(int, char**) output = output.substr(0, output.find_last_of('\n')); std::cout << output; + try + { + client.updateStatus(output); + } catch (const twitter::twitter_error& error) + { + std::cout << "Twitter error while tweeting: " << error.what() + << std::endl; + } + std::cout << std::endl; std::cout << std::endl; - getc(stdin); + std::this_thread::sleep_for(std::chrono::hours(4)); } } -- cgit 1.4.1