summary refs log tree commit diff stats
path: root/fruity.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'fruity.cpp')
-rw-r--r--fruity.cpp132
1 files changed, 132 insertions, 0 deletions
diff --git a/fruity.cpp b/fruity.cpp new file mode 100644 index 0000000..b88bde7 --- /dev/null +++ b/fruity.cpp
@@ -0,0 +1,132 @@
1#include <yaml-cpp/yaml.h>
2#include <iostream>
3#include <sstream>
4#include <twitter.h>
5#include <verbly.h>
6#include <chrono>
7#include <thread>
8#include <random>
9
10int main(int argc, char** argv)
11{
12 if (argc != 2)
13 {
14 std::cout << "usage: fruity [configfile]" << std::endl;
15 return -1;
16 }
17
18 std::random_device random_device;
19 std::mt19937 random_engine{random_device()};
20
21 std::string configfile(argv[1]);
22 YAML::Node config = YAML::LoadFile(configfile);
23
24 twitter::auth auth;
25 auth.setConsumerKey(config["consumer_key"].as<std::string>());
26 auth.setConsumerSecret(config["consumer_secret"].as<std::string>());
27 auth.setAccessKey(config["access_key"].as<std::string>());
28 auth.setAccessSecret(config["access_secret"].as<std::string>());
29
30 twitter::client client(auth);
31
32 verbly::data database {config["verbly_datafile"].as<std::string>()};
33
34 verbly::noun fruit = database.nouns().with_wnid(113134947).run().front(); // fruit
35 verbly::noun plants = database.nouns().with_wnid(100017222).run().front(); // plant
36 verbly::noun drugs = database.nouns().with_wnid(103247620).run().front(); // drug
37 verbly::noun animals = database.nouns().with_wnid(100015388).run().front(); // animal
38
39 for (;;)
40 {
41 std::cout << "Generating tweet" << std::endl;
42
43 auto n1 = database.nouns().full_hyponym_of(fruit).random().limit(1).run();
44 auto n1w = n1.front();
45 auto n1hq = database.nouns().hypernym_of(n1w).limit(1).run();
46 auto n1h = n1hq.front();
47
48 std::list<std::string> tokens;
49
50 int choice = std::uniform_int_distribution<int>(0,2)(random_engine);
51 if (choice == 0)
52 {
53 auto descriptor = database.adjectives().is_pertainymic().random().limit(1).run();
54 tokens.push_back(descriptor.front().base_form());
55 } else if (choice == 1)
56 {
57 auto descriptor = database.adjectives().is_mannernymic().random().limit(1).run();
58 tokens.push_back(descriptor.front().base_form());
59 }
60
61 auto plantThing = database.nouns();
62 choice = std::uniform_int_distribution<int>(0,4)(random_engine);
63 if (choice < 3)
64 {
65 if (choice == 0)
66 {
67 plantThing.full_hyponym_of(plants);
68 } else if (choice == 1)
69 {
70 plantThing.full_hyponym_of(drugs);
71 } else if (choice == 2)
72 {
73 plantThing.full_hyponym_of(animals);
74 }
75
76 plantThing.is_not_proper().with_complexity(1).random().limit(1);
77 tokens.push_back(plantThing.run().front().base_form());
78 }
79
80 auto similar = database.nouns().full_hyponym_of(n1h).except(n1w).is_not_proper().with_complexity(1).random().limit(1).run();
81 if (!similar.empty())
82 {
83 tokens.push_back(similar.front().base_form());
84 } else {
85 auto different = database.nouns().full_hyponym_of(fruit).except(n1w).is_not_proper().with_complexity(1).random().limit(1).run();
86 tokens.push_back(different.front().base_form());
87 }
88
89 std::string fruitName = verbly::implode(std::begin(tokens), std::end(tokens), " ");
90
91 std::ostringstream result;
92 result << n1.front().base_form();
93 result << "? ";
94
95 choice = std::uniform_int_distribution<int>(0,3)(random_engine);
96 if (choice == 0)
97 {
98 result << "Oh, you mean ";
99 result << fruitName;
100 } else if (choice == 1)
101 {
102 result << "Don't you mean ";
103 result << fruitName;
104 result << "?";
105 } else if (choice == 2)
106 {
107 result << "You mean ";
108 result << fruitName;
109 result << "!";
110 } else if (choice == 3)
111 {
112 result << "Pfft, everyone just calls it ";
113 result << fruitName;
114 result << " now";
115 }
116
117 std::string tweet = result.str();
118 tweet.resize(140);
119
120 try
121 {
122 client.updateStatus(tweet);
123
124 std::cout << tweet << std::endl;
125 } catch (const twitter::twitter_error& e)
126 {
127 std::cout << "Twitter error: " << e.what() << std::endl;
128 }
129
130 std::this_thread::sleep_for(std::chrono::hours(1));
131 }
132}