From bd806c551d3819eda88d046f067b5ecd6f53a464 Mon Sep 17 00:00:00 2001 From: Kelly Rauchenberger Date: Wed, 8 Nov 2017 15:11:12 -0500 Subject: Modernized bot structure This improves readability, and also will display a nicer error message for things such as the verbly datafile being invalid. --- fefisms.cpp | 52 +++++++++++++++++++++++++--------------------------- 1 file changed, 25 insertions(+), 27 deletions(-) (limited to 'fefisms.cpp') diff --git a/fefisms.cpp b/fefisms.cpp index 44e69b4..701b330 100644 --- a/fefisms.cpp +++ b/fefisms.cpp @@ -1,41 +1,40 @@ -#include -#include +#include "fefisms.h" #include #include #include -#include #include -int main(int argc, char** argv) +fefisms::fefisms( + std::string configFile, + std::mt19937& rng) : + rng_(rng) { - std::random_device random_device; - std::mt19937 random_engine{random_device()}; - - if (argc != 2) - { - std::cout << "usage: fefisms [configfile]" << std::endl; - return -1; - } + // Load the config file. + YAML::Node config = YAML::LoadFile(configFile); - std::string configfile(argv[1]); - YAML::Node config = YAML::LoadFile(configfile); + // Set up the verbly database. + database_ = std::unique_ptr( + new verbly::database(config["verbly_datafile"].as())); + // Set up the Twitter client. twitter::auth auth; auth.setConsumerKey(config["consumer_key"].as()); auth.setConsumerSecret(config["consumer_secret"].as()); auth.setAccessKey(config["access_key"].as()); auth.setAccessSecret(config["access_secret"].as()); - twitter::client client(auth); - - verbly::database database(config["verbly_datafile"].as()); + client_ = std::unique_ptr(new twitter::client(auth)); +} +void fefisms::run() const +{ verbly::filter formFilter = (verbly::form::complexity == 1) && (verbly::form::proper == false); - + + // Blacklist ethnic slurs verbly::filter cleanFilter = - !(verbly::word::usageDomains %= (verbly::notion::wnid == 106718862)); // Blacklist ethnic slurs + !(verbly::word::usageDomains %= (verbly::notion::wnid == 106718862)); for (;;) { @@ -45,18 +44,18 @@ int main(int argc, char** argv) verbly::inflection nounInfl = verbly::inflection::base; verbly::inflection hypoInfl = verbly::inflection::base; - - if (std::bernoulli_distribution(1.0/2.0)(random_engine)) + + if (std::bernoulli_distribution(1.0/2.0)(rng_)) { hypoInfl = verbly::inflection::plural; } - if (std::bernoulli_distribution(1.0/2.0)(random_engine)) + if (std::bernoulli_distribution(1.0/2.0)(rng_)) { nounInfl = verbly::inflection::plural; } - verbly::word noun = database.words( + verbly::word noun = database_->words( (verbly::notion::partOfSpeech == verbly::part_of_speech::noun) && (verbly::word::forms(nounInfl) %= formFilter) && cleanFilter @@ -64,13 +63,13 @@ int main(int argc, char** argv) (verbly::word::forms(hypoInfl) %= formFilter) && cleanFilter)).first(); - verbly::word hyponym = database.words( + verbly::word hyponym = database_->words( (verbly::notion::partOfSpeech == verbly::part_of_speech::noun) && (verbly::notion::hypernyms %= noun) && cleanFilter && (verbly::word::forms(hypoInfl) %= formFilter)).first(); - if (std::bernoulli_distribution(1.0/2.0)(random_engine)) + if (std::bernoulli_distribution(1.0/2.0)(rng_)) { utterance << verbly::token(noun, nounInfl); utterance << verbly::token(hyponym, hypoInfl); @@ -86,7 +85,7 @@ int main(int argc, char** argv) { std::cout << "Tweeting..." << std::endl; - client.updateStatus(action); + client_->updateStatus(action); std::cout << "Success!" << std::endl; std::cout << "Waiting..." << std::endl; @@ -100,4 +99,3 @@ int main(int argc, char** argv) std::cout << std::endl; } } - -- cgit 1.4.1