summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorKelly Rauchenberger <fefferburbia@gmail.com>2019-02-28 20:28:20 -0500
committerKelly Rauchenberger <fefferburbia@gmail.com>2019-02-28 20:28:20 -0500
commitd7e934156858287cdb7597ad0dced97d4d279f2f (patch)
treee1822649bb449d972839bb5c8adfd6b1c51d652c
parentb85830e8a852392036a803b7f6bb417c28ff17c4 (diff)
downloadaspartame-d7e934156858287cdb7597ad0dced97d4d279f2f.tar.gz
aspartame-d7e934156858287cdb7597ad0dced97d4d279f2f.tar.bz2
aspartame-d7e934156858287cdb7597ad0dced97d4d279f2f.zip
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.
-rw-r--r--CMakeLists.txt12
-rw-r--r--dialogue.cpp66
-rw-r--r--histogram.cpp44
-rw-r--r--histogram.h20
m---------vendor/rawr-ebooks0
5 files changed, 56 insertions, 86 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt index f563ae5..096d08d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt
@@ -1,11 +1,17 @@
1cmake_minimum_required (VERSION 3.1) 1cmake_minimum_required (VERSION 3.1)
2project (rawr-ebooks) 2project (rawr-ebooks)
3 3
4find_package(PkgConfig)
5pkg_check_modules(yaml-cpp yaml-cpp REQUIRED)
6
4add_subdirectory(vendor/rawr-ebooks EXCLUDE_FROM_ALL) 7add_subdirectory(vendor/rawr-ebooks EXCLUDE_FROM_ALL)
5 8
6include_directories(vendor/rawr-ebooks) 9include_directories(
10 vendor/rawr-ebooks
11 vendor/rawr-ebooks/vendor/libtwittercpp/src
12 ${yaml-cpp_INCLUDE_DIRS})
7 13
8add_executable(garnet dialogue.cpp histogram.cpp) 14add_executable(garnet dialogue.cpp)
9set_property(TARGET garnet PROPERTY CXX_STANDARD 17) 15set_property(TARGET garnet PROPERTY CXX_STANDARD 17)
10set_property(TARGET garnet PROPERTY CXX_STANDARD_REQUIRED ON) 16set_property(TARGET garnet PROPERTY CXX_STANDARD_REQUIRED ON)
11target_link_libraries(garnet rawr) 17target_link_libraries(garnet rawr twitter++ ${yaml-cpp_LIBRARIES})
diff --git a/dialogue.cpp b/dialogue.cpp index 47a761d..1e95d2b 100644 --- a/dialogue.cpp +++ b/dialogue.cpp
@@ -2,19 +2,17 @@
2#include "identifier.h" 2#include "identifier.h"
3#include "histogram.h" 3#include "histogram.h"
4#include <rawr.h> 4#include <rawr.h>
5#include <cstdlib>
6#include <ctime>
7#include <map> 5#include <map>
8#include <string> 6#include <string>
9#include <iostream> 7#include <iostream>
10#include <sstream> 8#include <sstream>
11 9#include <random>
12 10#include <twitter.h>
11#include <yaml-cpp/yaml.h>
13 12
14using speakerstore = identifier<std::string>; 13using speakerstore = identifier<std::string>;
15using speaker_id = speakerstore::key_type; 14using speaker_id = speakerstore::key_type;
16 15
17
18struct speaker_data { 16struct speaker_data {
19 17
20 std::string name; 18 std::string name;
@@ -24,21 +22,39 @@ struct speaker_data {
24 22
25}; 23};
26 24
25int main(int argc, char** argv)
26{
27 if (argc != 2)
28 {
29 std::cout << "usage: garnet [configfile]" << std::endl;
30 return -1;
31 }
32
33 std::random_device randomDevice;
34 std::mt19937 rng(randomDevice());
27 35
36 std::string configfile(argv[1]);
37 YAML::Node config = YAML::LoadFile(configfile);
28 38
39 twitter::auth auth(
40 config["consumer_key"].as<std::string>(),
41 config["consumer_secret"].as<std::string>(),
42 config["access_key"].as<std::string>(),
43 config["access_secret"].as<std::string>());
29 44
30int main(int, char**) 45 twitter::client client(auth);
31{
32 srand(time(NULL));
33 rand(); rand(); rand(); rand();
34 46
35 speakerstore speakers; 47 speakerstore speakers;
36 std::map<speaker_id, speaker_data> speakerData; 48 std::map<speaker_id, speaker_data> speakerData;
37 histogram<speaker_id> allSpeakers; 49 histogram<speaker_id> allSpeakers;
38 50
51 using csv =
52 io::CSVReader<
53 2,
54 io::trim_chars<' ', '\t'>,
55 io::double_quote_escape<',', '"'>>;
39 56
40 57 csv in(config["transcript"].as<std::string>());
41 io::CSVReader<2,io::trim_chars<' ', '\t'>,io::double_quote_escape<',', '"'>> in("../dialogue.csv");
42 std::string speaker; 58 std::string speaker;
43 std::string line; 59 std::string line;
44 60
@@ -85,11 +101,10 @@ int main(int, char**)
85 { 101 {
86 std::set<speaker_id> pastSpeakers; 102 std::set<speaker_id> pastSpeakers;
87 103
88 104 speaker_id curSpeaker = allSpeakers.next(rng);
89 speaker_id curSpeaker = allSpeakers.next();
90 105
91 std::ostringstream theEnd; 106 std::ostringstream theEnd;
92 int maxLines = rand() % 4 + 3; 107 int maxLines = std::uniform_int_distribution<int>(3, 6)(rng);
93 108
94 for (int i = 0; i < maxLines; i++) 109 for (int i = 0; i < maxLines; i++)
95 { 110 {
@@ -102,7 +117,8 @@ int main(int, char**)
102 theEnd << curSd.name << ": "; 117 theEnd << curSd.name << ": ";
103 } 118 }
104 119
105 std::string curLine = curSd.chain.randomSentence(rand() % 30 + 1); 120 int maxL = std::uniform_int_distribution<int>(1, 30)(rng);
121 std::string curLine = curSd.chain.randomSentence(maxL, rng);
106 122
107 if (curSd.name == "" && 123 if (curSd.name == "" &&
108 curLine[0] != '[' && 124 curLine[0] != '[' &&
@@ -116,13 +132,16 @@ int main(int, char**)
116 132
117 theEnd << std::endl; 133 theEnd << std::endl;
118 134
119 speaker_id repeatSpeaker = *std::next(std::begin(pastSpeakers), rand() % pastSpeakers.size()); 135 int psi =
136 std::uniform_int_distribution<int>(0, pastSpeakers.size()-1)(rng);
137
138 speaker_id repeatSpeaker = *std::next(std::begin(pastSpeakers), psi);
120 if (repeatSpeaker != curSpeaker && 139 if (repeatSpeaker != curSpeaker &&
121 rand() % 3 == 0) 140 std::bernoulli_distribution(1.0 / 3.0)(rng))
122 { 141 {
123 curSpeaker = repeatSpeaker; 142 curSpeaker = repeatSpeaker;
124 } else { 143 } else {
125 curSpeaker = curSd.nextSpeaker.next(); 144 curSpeaker = curSd.nextSpeaker.next(rng);
126 } 145 }
127 } 146 }
128 147
@@ -131,9 +150,18 @@ int main(int, char**)
131 output = output.substr(0, output.find_last_of('\n')); 150 output = output.substr(0, output.find_last_of('\n'));
132 std::cout << output; 151 std::cout << output;
133 152
153 try
154 {
155 client.updateStatus(output);
156 } catch (const twitter::twitter_error& error)
157 {
158 std::cout << "Twitter error while tweeting: " << error.what()
159 << std::endl;
160 }
161
134 std::cout << std::endl; 162 std::cout << std::endl;
135 std::cout << std::endl; 163 std::cout << std::endl;
136 164
137 getc(stdin); 165 std::this_thread::sleep_for(std::chrono::hours(4));
138 } 166 }
139} 167}
diff --git a/histogram.cpp b/histogram.cpp deleted file mode 100644 index 38fca45..0000000 --- a/histogram.cpp +++ /dev/null
@@ -1,44 +0,0 @@
1#include "histogram.h"
2#include <cstdlib>
3#include <iostream>
4
5template <class T>
6void histogram<T>::add(const T& inst)
7{
8 freqtable[inst]++;
9}
10
11template <class T>
12void histogram<T>::compile()
13{
14 distribution.clear();
15
16 int max = 0;
17 for (auto& it : freqtable)
18 {
19 max += it.second;
20 distribution.emplace(max, it.first);
21 }
22
23 freqtable.clear();
24}
25
26template <class T>
27const T& histogram<T>::next() const
28{
29 int max = distribution.rbegin()->first;
30 int r = rand() % max;
31
32 return distribution.upper_bound(r)->second;
33}
34
35template <class T>
36void histogram<T>::print() const
37{
38 for (auto& freqpair : freqtable)
39 {
40 std::cout << freqpair.first << ": " << freqpair.second << std::endl;
41 }
42}
43
44template class histogram <unsigned long>;
diff --git a/histogram.h b/histogram.h deleted file mode 100644 index 76d8f1b..0000000 --- a/histogram.h +++ /dev/null
@@ -1,20 +0,0 @@
1#ifndef HISTOGRAM_H_24094D97
2#define HISTOGRAM_H_24094D97
3
4#include <map>
5#include <string>
6
7template <class T>
8class histogram {
9 public:
10 void add(const T& inst);
11 void compile();
12 const T& next() const;
13 void print() const;
14
15 private:
16 std::map<T, int> freqtable;
17 std::map<int, T> distribution;
18};
19
20#endif /* end of include guard: HISTOGRAM_H_24094D97 */
diff --git a/vendor/rawr-ebooks b/vendor/rawr-ebooks
Subproject 1890eb5d4a496aea5e9114550081ca63bd280f3 Subproject 01fcbeb60da0bff33d5d9f5b870d444cc418a01