summary refs log tree commit diff stats
path: root/chemist.cpp
diff options
context:
space:
mode:
authorKelly Rauchenberger <fefferburbia@gmail.com>2016-03-26 09:42:45 -0400
committerKelly Rauchenberger <fefferburbia@gmail.com>2016-03-26 09:42:45 -0400
commitce7f42f1f3a45b9167a9cc4e85e1bc4488013c70 (patch)
treecf5d20927bc65a3a70303251ded0fb78072c642b /chemist.cpp
downloadinsult-ce7f42f1f3a45b9167a9cc4e85e1bc4488013c70.tar.gz
insult-ce7f42f1f3a45b9167a9cc4e85e1bc4488013c70.tar.bz2
insult-ce7f42f1f3a45b9167a9cc4e85e1bc4488013c70.zip
Initial commit
Diffstat (limited to 'chemist.cpp')
-rw-r--r--chemist.cpp156
1 files changed, 156 insertions, 0 deletions
diff --git a/chemist.cpp b/chemist.cpp new file mode 100644 index 0000000..a111931 --- /dev/null +++ b/chemist.cpp
@@ -0,0 +1,156 @@
1#include <yaml-cpp/yaml.h>
2#include <iostream>
3#include <cstdlib>
4#include <ctime>
5#include <sstream>
6#include <twitcurl.h>
7#include <verbly.h>
8
9int main(int argc, char** argv)
10{
11 srand(time(NULL));
12
13 YAML::Node config = YAML::LoadFile("config.yml");
14
15 twitCurl twitter;
16 twitter.getOAuth().setConsumerKey(config["consumer_key"].as<std::string>());
17 twitter.getOAuth().setConsumerSecret(config["consumer_secret"].as<std::string>());
18 twitter.getOAuth().setOAuthTokenKey(config["access_key"].as<std::string>());
19 twitter.getOAuth().setOAuthTokenSecret(config["access_secret"].as<std::string>());
20
21 std::map<std::string, std::vector<std::string>> groups;
22 std::ifstream datafile("data.txt");
23 if (!datafile.is_open())
24 {
25 std::cout << "Could not find data.txt" << std::endl;
26 return 1;
27 }
28
29 bool newgroup = true;
30 std::string line;
31 std::string curgroup;
32 while (getline(datafile, line))
33 {
34 if (line.back() == '\r')
35 {
36 line.pop_back();
37 }
38
39 if (newgroup)
40 {
41 curgroup = line;
42 newgroup = false;
43 } else {
44 if (line.empty())
45 {
46 newgroup = true;
47 } else {
48 groups[curgroup].push_back(line);
49 }
50 }
51 }
52
53 verbly::data database {"data.sqlite3"};
54 for (;;)
55 {
56 std::cout << "Generating tweet" << std::endl;
57 std::string action = "{Main}";
58 int tknloc;
59 while ((tknloc = action.find("{")) != std::string::npos)
60 {
61 std::string token = action.substr(tknloc+1, action.find("}")-tknloc-1);
62 std::string modifier;
63 int modloc;
64 if ((modloc = token.find(":")) != std::string::npos)
65 {
66 modifier = token.substr(modloc+1);
67 token = token.substr(0, modloc);
68 }
69
70 std::string canontkn;
71 std::transform(std::begin(token), std::end(token), std::back_inserter(canontkn), [] (char ch) {
72 return std::toupper(ch);
73 });
74
75 std::string result;
76 if (canontkn == "NOUN")
77 {
78 result = database.nouns().is_not_proper().random().limit(1).run().front().singular_form();
79 } else if (canontkn == "ADJECTIVE")
80 {
81 result = database.adjectives().random().limit(1).run().front().base_form();
82 } else if (canontkn == "VERBING")
83 {
84 result = database.verbs().random().limit(1).run().front().ing_form();
85 } else if (canontkn == "YEAR")
86 {
87 result = std::to_string(rand() % 100 + 1916);
88 } else if (canontkn == "REGION")
89 {
90 auto hem1 = database.nouns().with_singular_form("eastern hemisphere").limit(1).run().front();
91 auto hem2 = database.nouns().with_singular_form("western hemisphere").limit(1).run().front();
92 verbly::filter<verbly::noun> region{hem1, hem2};
93 region.set_orlogic(true);
94
95 result = database.nouns().full_part_holonym_of(region).random().limit(1).run().front().singular_form();
96 } else if (canontkn == "FAMOUSNAME")
97 {
98 auto person = database.nouns().with_singular_form("person").limit(1).run().front();
99 auto ptypes = database.nouns().full_hyponym_of({person}).is_class().random().limit(1).run().front();
100 result = database.nouns().instance_of({ptypes}).random().limit(1).run().front().singular_form();
101 } else {
102 auto group = groups[canontkn];
103 result = group[rand() % group.size()];
104 }
105
106 if (modifier == "indefinite")
107 {
108 if ((result.length() > 1) && (isupper(result[0])) && (isupper(result[1])))
109 {
110 result = "an " + result;
111 } else if ((result[0] == 'a') || (result[0] == 'e') || (result[0] == 'i') || (result[0] == 'o') || (result[0] == 'u'))
112 {
113 result = "an " + result;
114 } else {
115 result = "a " + result;
116 }
117 }
118
119 std::string finalresult;
120 if (islower(token[0]))
121 {
122 std::transform(std::begin(result), std::end(result), std::back_inserter(finalresult), [] (char ch) {
123 return std::tolower(ch);
124 });
125 } else if (isupper(token[0]) && !isupper(token[1]))
126 {
127 auto words = verbly::split<std::list<std::string>>(result, " ");
128 for (auto& word : words)
129 {
130 word[0] = std::toupper(word[0]);
131 }
132
133 finalresult = verbly::implode(std::begin(words), std::end(words), " ");
134 } else {
135 finalresult = result;
136 }
137
138 action.replace(tknloc, action.find("}")-tknloc+1, finalresult);
139 }
140
141 action.resize(140);
142
143 std::string replyMsg;
144 if (twitter.statusUpdate(action))
145 {
146 twitter.getLastWebResponse(replyMsg);
147 std::cout << "Twitter message: " << replyMsg << std::endl;
148 } else {
149 twitter.getLastCurlError(replyMsg);
150 std::cout << "Curl error: " << replyMsg << std::endl;
151 }
152
153 std::cout << "Waiting" << std::endl;
154 sleep(60 * 60);
155 }
156}