diff options
| author | Kelly Rauchenberger <fefferburbia@gmail.com> | 2017-11-08 15:11:12 -0500 |
|---|---|---|
| committer | Kelly Rauchenberger <fefferburbia@gmail.com> | 2017-11-08 15:11:12 -0500 |
| commit | bd806c551d3819eda88d046f067b5ecd6f53a464 (patch) | |
| tree | 4f2d6fecec2cd148bca2475e7907a59fd115b6f3 | |
| parent | 7802b1e8cce56989c2588f2c852aab7a3620203a (diff) | |
| download | fefisms-bd806c551d3819eda88d046f067b5ecd6f53a464.tar.gz fefisms-bd806c551d3819eda88d046f067b5ecd6f53a464.tar.bz2 fefisms-bd806c551d3819eda88d046f067b5ecd6f53a464.zip | |
Modernized bot structure
This improves readability, and also will display a nicer error message for things such as the verbly datafile being invalid.
| -rw-r--r-- | CMakeLists.txt | 2 | ||||
| -rw-r--r-- | fefisms.cpp | 52 | ||||
| -rw-r--r-- | fefisms.h | 28 | ||||
| -rw-r--r-- | main.cpp | 31 |
4 files changed, 85 insertions, 28 deletions
| 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) | |||
| 6 | add_subdirectory(vendor/yaml-cpp EXCLUDE_FROM_ALL) | 6 | add_subdirectory(vendor/yaml-cpp EXCLUDE_FROM_ALL) |
| 7 | 7 | ||
| 8 | include_directories(vendor/libtwittercpp/src vendor/verbly/lib vendor/yaml-cpp/include) | 8 | include_directories(vendor/libtwittercpp/src vendor/verbly/lib vendor/yaml-cpp/include) |
| 9 | add_executable(fefisms fefisms.cpp) | 9 | add_executable(fefisms fefisms.cpp main.cpp) |
| 10 | set_property(TARGET fefisms PROPERTY CXX_STANDARD 11) | 10 | set_property(TARGET fefisms PROPERTY CXX_STANDARD 11) |
| 11 | set_property(TARGET fefisms PROPERTY CXX_STANDARD_REQUIRED ON) | 11 | set_property(TARGET fefisms PROPERTY CXX_STANDARD_REQUIRED ON) |
| 12 | target_link_libraries(fefisms verbly twitter++ yaml-cpp) | 12 | 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 @@ | |||
| 1 | #include <verbly.h> | 1 | #include "fefisms.h" |
| 2 | #include <twitter.h> | ||
| 3 | #include <yaml-cpp/yaml.h> | 2 | #include <yaml-cpp/yaml.h> |
| 4 | #include <iostream> | 3 | #include <iostream> |
| 5 | #include <chrono> | 4 | #include <chrono> |
| 6 | #include <random> | ||
| 7 | #include <thread> | 5 | #include <thread> |
| 8 | 6 | ||
| 9 | int main(int argc, char** argv) | 7 | fefisms::fefisms( |
| 8 | std::string configFile, | ||
| 9 | std::mt19937& rng) : | ||
| 10 | rng_(rng) | ||
| 10 | { | 11 | { |
| 11 | std::random_device random_device; | 12 | // Load the config file. |
| 12 | std::mt19937 random_engine{random_device()}; | 13 | YAML::Node config = YAML::LoadFile(configFile); |
| 13 | |||
| 14 | if (argc != 2) | ||
| 15 | { | ||
| 16 | std::cout << "usage: fefisms [configfile]" << std::endl; | ||
| 17 | return -1; | ||
| 18 | } | ||
| 19 | 14 | ||
| 20 | std::string configfile(argv[1]); | 15 | // Set up the verbly database. |
| 21 | YAML::Node config = YAML::LoadFile(configfile); | 16 | database_ = std::unique_ptr<verbly::database>( |
| 17 | new verbly::database(config["verbly_datafile"].as<std::string>())); | ||
| 22 | 18 | ||
| 19 | // Set up the Twitter client. | ||
| 23 | twitter::auth auth; | 20 | twitter::auth auth; |
| 24 | auth.setConsumerKey(config["consumer_key"].as<std::string>()); | 21 | auth.setConsumerKey(config["consumer_key"].as<std::string>()); |
| 25 | auth.setConsumerSecret(config["consumer_secret"].as<std::string>()); | 22 | auth.setConsumerSecret(config["consumer_secret"].as<std::string>()); |
| 26 | auth.setAccessKey(config["access_key"].as<std::string>()); | 23 | auth.setAccessKey(config["access_key"].as<std::string>()); |
| 27 | auth.setAccessSecret(config["access_secret"].as<std::string>()); | 24 | auth.setAccessSecret(config["access_secret"].as<std::string>()); |
| 28 | 25 | ||
| 29 | twitter::client client(auth); | 26 | client_ = std::unique_ptr<twitter::client>(new twitter::client(auth)); |
| 30 | 27 | } | |
| 31 | verbly::database database(config["verbly_datafile"].as<std::string>()); | ||
| 32 | 28 | ||
| 29 | void fefisms::run() const | ||
| 30 | { | ||
| 33 | verbly::filter formFilter = | 31 | verbly::filter formFilter = |
| 34 | (verbly::form::complexity == 1) | 32 | (verbly::form::complexity == 1) |
| 35 | && (verbly::form::proper == false); | 33 | && (verbly::form::proper == false); |
| 36 | 34 | ||
| 35 | // Blacklist ethnic slurs | ||
| 37 | verbly::filter cleanFilter = | 36 | verbly::filter cleanFilter = |
| 38 | !(verbly::word::usageDomains %= (verbly::notion::wnid == 106718862)); // Blacklist ethnic slurs | 37 | !(verbly::word::usageDomains %= (verbly::notion::wnid == 106718862)); |
| 39 | 38 | ||
| 40 | for (;;) | 39 | for (;;) |
| 41 | { | 40 | { |
| @@ -45,18 +44,18 @@ int main(int argc, char** argv) | |||
| 45 | 44 | ||
| 46 | verbly::inflection nounInfl = verbly::inflection::base; | 45 | verbly::inflection nounInfl = verbly::inflection::base; |
| 47 | verbly::inflection hypoInfl = verbly::inflection::base; | 46 | verbly::inflection hypoInfl = verbly::inflection::base; |
| 48 | 47 | ||
| 49 | if (std::bernoulli_distribution(1.0/2.0)(random_engine)) | 48 | if (std::bernoulli_distribution(1.0/2.0)(rng_)) |
| 50 | { | 49 | { |
| 51 | hypoInfl = verbly::inflection::plural; | 50 | hypoInfl = verbly::inflection::plural; |
| 52 | } | 51 | } |
| 53 | 52 | ||
| 54 | if (std::bernoulli_distribution(1.0/2.0)(random_engine)) | 53 | if (std::bernoulli_distribution(1.0/2.0)(rng_)) |
| 55 | { | 54 | { |
| 56 | nounInfl = verbly::inflection::plural; | 55 | nounInfl = verbly::inflection::plural; |
| 57 | } | 56 | } |
| 58 | 57 | ||
| 59 | verbly::word noun = database.words( | 58 | verbly::word noun = database_->words( |
| 60 | (verbly::notion::partOfSpeech == verbly::part_of_speech::noun) | 59 | (verbly::notion::partOfSpeech == verbly::part_of_speech::noun) |
| 61 | && (verbly::word::forms(nounInfl) %= formFilter) | 60 | && (verbly::word::forms(nounInfl) %= formFilter) |
| 62 | && cleanFilter | 61 | && cleanFilter |
| @@ -64,13 +63,13 @@ int main(int argc, char** argv) | |||
| 64 | (verbly::word::forms(hypoInfl) %= formFilter) | 63 | (verbly::word::forms(hypoInfl) %= formFilter) |
| 65 | && cleanFilter)).first(); | 64 | && cleanFilter)).first(); |
| 66 | 65 | ||
| 67 | verbly::word hyponym = database.words( | 66 | verbly::word hyponym = database_->words( |
| 68 | (verbly::notion::partOfSpeech == verbly::part_of_speech::noun) | 67 | (verbly::notion::partOfSpeech == verbly::part_of_speech::noun) |
| 69 | && (verbly::notion::hypernyms %= noun) | 68 | && (verbly::notion::hypernyms %= noun) |
| 70 | && cleanFilter | 69 | && cleanFilter |
| 71 | && (verbly::word::forms(hypoInfl) %= formFilter)).first(); | 70 | && (verbly::word::forms(hypoInfl) %= formFilter)).first(); |
| 72 | 71 | ||
| 73 | if (std::bernoulli_distribution(1.0/2.0)(random_engine)) | 72 | if (std::bernoulli_distribution(1.0/2.0)(rng_)) |
| 74 | { | 73 | { |
| 75 | utterance << verbly::token(noun, nounInfl); | 74 | utterance << verbly::token(noun, nounInfl); |
| 76 | utterance << verbly::token(hyponym, hypoInfl); | 75 | utterance << verbly::token(hyponym, hypoInfl); |
| @@ -86,7 +85,7 @@ int main(int argc, char** argv) | |||
| 86 | { | 85 | { |
| 87 | std::cout << "Tweeting..." << std::endl; | 86 | std::cout << "Tweeting..." << std::endl; |
| 88 | 87 | ||
| 89 | client.updateStatus(action); | 88 | client_->updateStatus(action); |
| 90 | 89 | ||
| 91 | std::cout << "Success!" << std::endl; | 90 | std::cout << "Success!" << std::endl; |
| 92 | std::cout << "Waiting..." << std::endl; | 91 | std::cout << "Waiting..." << std::endl; |
| @@ -100,4 +99,3 @@ int main(int argc, char** argv) | |||
| 100 | std::cout << std::endl; | 99 | std::cout << std::endl; |
| 101 | } | 100 | } |
| 102 | } | 101 | } |
| 103 | |||
| diff --git a/fefisms.h b/fefisms.h new file mode 100644 index 0000000..98ff8d0 --- /dev/null +++ b/fefisms.h | |||
| @@ -0,0 +1,28 @@ | |||
| 1 | #ifndef FEFISMS_H_09912EAE | ||
| 2 | #define FEFISMS_H_09912EAE | ||
| 3 | |||
| 4 | #include <random> | ||
| 5 | #include <string> | ||
| 6 | #include <verbly.h> | ||
| 7 | #include <stdexcept> | ||
| 8 | #include <memory> | ||
| 9 | #include <twitter.h> | ||
| 10 | |||
| 11 | class fefisms { | ||
| 12 | public: | ||
| 13 | |||
| 14 | fefisms( | ||
| 15 | std::string configFile, | ||
| 16 | std::mt19937& rng); | ||
| 17 | |||
| 18 | void run() const; | ||
| 19 | |||
| 20 | private: | ||
| 21 | |||
| 22 | std::mt19937& rng_; | ||
| 23 | std::unique_ptr<verbly::database> database_; | ||
| 24 | std::unique_ptr<twitter::client> client_; | ||
| 25 | |||
| 26 | }; | ||
| 27 | |||
| 28 | #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 @@ | |||
| 1 | #include "fefisms.h" | ||
| 2 | |||
| 3 | int main(int argc, char** argv) | ||
| 4 | { | ||
| 5 | std::random_device randomDevice; | ||
| 6 | std::mt19937 rng(randomDevice()); | ||
| 7 | |||
| 8 | if (argc != 2) | ||
| 9 | { | ||
| 10 | std::cout << "usage: fefisms [configfile]" << std::endl; | ||
| 11 | return -1; | ||
| 12 | } | ||
| 13 | |||
| 14 | std::string configfile(argv[1]); | ||
| 15 | |||
| 16 | try | ||
| 17 | { | ||
| 18 | fefisms bot(configfile, rng); | ||
| 19 | |||
| 20 | try | ||
| 21 | { | ||
| 22 | bot.run(); | ||
| 23 | } catch (const std::exception& ex) | ||
| 24 | { | ||
| 25 | std::cout << "Error running bot: " << ex.what() << std::endl; | ||
| 26 | } | ||
| 27 | } catch (const std::exception& ex) | ||
| 28 | { | ||
| 29 | std::cout << "Error initializing bot: " << ex.what() << std::endl; | ||
| 30 | } | ||
| 31 | } | ||
