summary refs log tree commit diff stats
path: root/chemist.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'chemist.cpp')
-rw-r--r--chemist.cpp183
1 files changed, 0 insertions, 183 deletions
diff --git a/chemist.cpp b/chemist.cpp deleted file mode 100644 index 06e1992..0000000 --- a/chemist.cpp +++ /dev/null
@@ -1,183 +0,0 @@
1#include <yaml-cpp/yaml.h>
2#include <iostream>
3#include <sstream>
4#include <verbly.h>
5#include <fstream>
6#include <twitter.h>
7#include <random>
8#include <chrono>
9#include <thread>
10
11int main(int argc, char** argv)
12{
13 if (argc != 2)
14 {
15 std::cout << "usage: chemist [configfile]" << std::endl;
16 return -1;
17 }
18
19 std::string configfile(argv[1]);
20 YAML::Node config = YAML::LoadFile(configfile);
21
22 twitter::auth auth;
23 auth.setConsumerKey(config["consumer_key"].as<std::string>());
24 auth.setConsumerSecret(config["consumer_secret"].as<std::string>());
25 auth.setAccessKey(config["access_key"].as<std::string>());
26 auth.setAccessSecret(config["access_secret"].as<std::string>());
27
28 twitter::client client(auth);
29
30 std::map<std::string, std::vector<std::string>> groups;
31 std::ifstream datafile(config["forms_file"].as<std::string>());
32 if (!datafile.is_open())
33 {
34 std::cout << "Could not find datafile" << std::endl;
35 return 1;
36 }
37
38 bool newgroup = true;
39 std::string line;
40 std::string curgroup;
41 while (getline(datafile, line))
42 {
43 if (line.back() == '\r')
44 {
45 line.pop_back();
46 }
47
48 if (newgroup)
49 {
50 curgroup = line;
51 newgroup = false;
52 } else {
53 if (line.empty())
54 {
55 newgroup = true;
56 } else {
57 groups[curgroup].push_back(line);
58 }
59 }
60 }
61
62 std::random_device random_device;
63 std::mt19937 random_engine{random_device()};
64
65 verbly::data database {config["verbly_datafile"].as<std::string>()};
66 for (;;)
67 {
68 std::cout << "Generating tweet" << std::endl;
69 std::string action = "{Main}";
70 int tknloc;
71 while ((tknloc = action.find("{")) != std::string::npos)
72 {
73 std::string token = action.substr(tknloc+1, action.find("}")-tknloc-1);
74 std::string modifier;
75 int modloc;
76 if ((modloc = token.find(":")) != std::string::npos)
77 {
78 modifier = token.substr(modloc+1);
79 token = token.substr(0, modloc);
80 }
81
82 std::string canontkn;
83 std::transform(std::begin(token), std::end(token), std::back_inserter(canontkn), [] (char ch) {
84 return std::toupper(ch);
85 });
86
87 std::string result;
88 if (canontkn == "NOUN")
89 {
90 result = database.nouns().is_not_proper().random().limit(1).with_complexity(1).run().front().singular_form();
91 } else if (canontkn == "ATTRIBUTE")
92 {
93 result = database.nouns().random().limit(1).full_hyponym_of(database.nouns().with_wnid(100024264).limit(1).run().front()).run().front().singular_form();
94 } else if (canontkn == "ADJECTIVE")
95 {
96 result = database.adjectives().with_complexity(1).random().limit(1).run().front().base_form();
97 } else if (canontkn == "VERBING")
98 {
99 result = database.verbs().random().limit(1).run().front().ing_form();
100 } else if (canontkn == "YEAR")
101 {
102 std::uniform_int_distribution<int> yeardist(1916,2015);
103 int year = yeardist(random_engine);
104 result = std::to_string(year);
105 } else if (canontkn == "REGION")
106 {
107 auto hem1 = database.nouns().with_singular_form("eastern hemisphere").limit(1).run().front();
108 auto hem2 = database.nouns().with_singular_form("western hemisphere").limit(1).run().front();
109 verbly::filter<verbly::noun> region{hem1, hem2};
110 region.set_orlogic(true);
111
112 result = database.nouns().full_part_holonym_of(region).random().limit(1).run().front().singular_form();
113 } else if (canontkn == "FAMOUSNAME")
114 {
115 auto person = database.nouns().with_singular_form("person").limit(1).run().front();
116 auto ptypes = database.nouns().full_hyponym_of({person}).is_class().random().limit(1).run().front();
117 result = database.nouns().instance_of({ptypes}).random().limit(1).run().front().singular_form();
118 } else if (canontkn == "BODYPART")
119 {
120 auto bp = database.nouns().with_singular_form("body part").limit(1).run().front();
121 result = database.nouns().full_hyponym_of({bp}).with_complexity(1).random().limit(1).run().front().singular_form();
122 } else if (canontkn == "\\N")
123 {
124 result = "\n";
125 } else {
126 auto group = groups[canontkn];
127 std::uniform_int_distribution<int> groupdist(0, group.size()-1);
128 int groupind = groupdist(random_engine);
129 result = group[groupind];
130 }
131
132 if (modifier == "indefinite")
133 {
134 if ((result.length() > 1) && (isupper(result[0])) && (isupper(result[1])))
135 {
136 result = "an " + result;
137 } else if ((result[0] == 'a') || (result[0] == 'e') || (result[0] == 'i') || (result[0] == 'o') || (result[0] == 'u'))
138 {
139 result = "an " + result;
140 } else {
141 result = "a " + result;
142 }
143 }
144
145 std::string finalresult;
146 if (islower(token[0]))
147 {
148 std::transform(std::begin(result), std::end(result), std::back_inserter(finalresult), [] (char ch) {
149 return std::tolower(ch);
150 });
151 } else if (isupper(token[0]) && !isupper(token[1]))
152 {
153 auto words = verbly::split<std::list<std::string>>(result, " ");
154 for (auto& word : words)
155 {
156 word[0] = std::toupper(word[0]);
157 }
158
159 finalresult = verbly::implode(std::begin(words), std::end(words), " ");
160 } else {
161 finalresult = result;
162 }
163
164 action.replace(tknloc, action.find("}")-tknloc+1, finalresult);
165 }
166
167 action.resize(140);
168
169 try
170 {
171 client.updateStatus(action);
172
173 std::cout << "Tweeted!" << std::endl;
174 } catch (const twitter::twitter_error& e)
175 {
176 std::cout << "Twitter error: " << e.what() << std::endl;
177 }
178
179 std::cout << "Waiting..." << std::endl;
180
181 std::this_thread::sleep_for(std::chrono::hours(1));
182 }
183}