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. --- CMakeLists.txt | 2 +- fefisms.cpp | 52 +++++++++++++++++++++++++--------------------------- fefisms.h | 28 ++++++++++++++++++++++++++++ main.cpp | 31 +++++++++++++++++++++++++++++++ 4 files changed, 85 insertions(+), 28 deletions(-) create mode 100644 fefisms.h create mode 100644 main.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index a2a8f0f..f7c21c5 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -6,7 +6,7 @@ add_subdirectory(vendor/verbly) add_subdirectory(vendor/yaml-cpp EXCLUDE_FROM_ALL) include_directories(vendor/libtwittercpp/src vendor/verbly/lib vendor/yaml-cpp/include) -add_executable(fefisms fefisms.cpp) +add_executable(fefisms fefisms.cpp main.cpp) set_property(TARGET fefisms PROPERTY CXX_STANDARD 11) set_property(TARGET fefisms PROPERTY CXX_STANDARD_REQUIRED ON) target_link_libraries(fefisms verbly twitter++ yaml-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; } } - diff --git a/fefisms.h b/fefisms.h new file mode 100644 index 0000000..98ff8d0 --- /dev/null +++ b/fefisms.h @@ -0,0 +1,28 @@ +#ifndef FEFISMS_H_09912EAE +#define FEFISMS_H_09912EAE + +#include +#include +#include +#include +#include +#include + +class fefisms { +public: + + fefisms( + std::string configFile, + std::mt19937& rng); + + void run() const; + +private: + + std::mt19937& rng_; + std::unique_ptr database_; + std::unique_ptr client_; + +}; + +#endif /* end of include guard: FEFISMS_H_09912EAE */ diff --git a/main.cpp b/main.cpp new file mode 100644 index 0000000..8aa9677 --- /dev/null +++ b/main.cpp @@ -0,0 +1,31 @@ +#include "fefisms.h" + +int main(int argc, char** argv) +{ + std::random_device randomDevice; + std::mt19937 rng(randomDevice()); + + if (argc != 2) + { + std::cout << "usage: fefisms [configfile]" << std::endl; + return -1; + } + + std::string configfile(argv[1]); + + try + { + fefisms bot(configfile, rng); + + try + { + bot.run(); + } catch (const std::exception& ex) + { + std::cout << "Error running bot: " << ex.what() << std::endl; + } + } catch (const std::exception& ex) + { + std::cout << "Error initializing bot: " << ex.what() << std::endl; + } +} -- cgit 1.4.1