summary refs log tree commit diff stats
path: root/fruity.cpp
blob: 80a6f6d0afbcb28b3a33622b88ec859d5d25e536 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#include <yaml-cpp/yaml.h>
#include <iostream>
#include <sstream>
#include <twitter.h>
#include <verbly.h>
#include <chrono>
#include <thread>
#include <random>

int main(int argc, char** argv)
{
  if (argc != 2)
  {
    std::cout << "usage: fruity [configfile]" << std::endl;
    return -1;
  }

  std::random_device random_device;
  std::mt19937 random_engine{random_device()};

  std::string configfile(argv[1]);
  YAML::Node config = YAML::LoadFile(configfile);

  twitter::auth auth;
  auth.setConsumerKey(config["consumer_key"].as<std::string>());
  auth.setConsumerSecret(config["consumer_secret"].as<std::string>());
  auth.setAccessKey(config["access_key"].as<std::string>());
  auth.setAccessSecret(config["access_secret"].as<std::string>());

  twitter::client client(auth);

  verbly::database database(config["verbly_datafile"].as<std::string>());

  verbly::filter fruitFilter = (
    (verbly::notion::partOfSpeech == verbly::part_of_speech::noun)
    && (verbly::notion::fullHypernyms %= (
      (verbly::notion::wnid == 113134947) // fruit
      || (verbly::notion::wnid == 107705931)))); // edible fruit

  verbly::query<verbly::word> fruitQuery = database.words(fruitFilter);

  verbly::query<verbly::word> pertainymQuery = database.words(
    (verbly::notion::partOfSpeech == verbly::part_of_speech::adjective)
    && (verbly::word::antiPertainyms));

  verbly::query<verbly::word> antiMannernymQuery = database.words(
    (verbly::notion::partOfSpeech == verbly::part_of_speech::adjective)
    && (verbly::word::mannernyms));

  for (;;)
  {
    std::cout << "Generating tweet" << std::endl;

    verbly::word fruit = fruitQuery.first();
    verbly::word hyper = database.words(verbly::notion::hyponyms %= fruit).first();

    verbly::token utterance;

    int choice = std::uniform_int_distribution<int>(0,2)(random_engine);
    if (choice == 0)
    {
      utterance << pertainymQuery.first();
    } else if (choice == 1)
    {
      utterance << antiMannernymQuery.first();
    }

    verbly::filter thingFilter = (
      (verbly::notion::partOfSpeech == verbly::part_of_speech::noun)
      && (verbly::form::proper == false)
      && (verbly::form::complexity == 1));

    choice = std::uniform_int_distribution<int>(0,4)(random_engine);
    if (choice < 3)
    {
      if (choice == 0)
      {
        // plant
        thingFilter &= (verbly::notion::fullHypernyms %= (verbly::notion::wnid == 100017222));
      } else if (choice == 1)
      {
        // drug
        thingFilter &= (verbly::notion::fullHypernyms %= (verbly::notion::wnid == 103247620));
      } else if (choice == 2)
      {
        // animal
        thingFilter &= (verbly::notion::fullHypernyms %= (verbly::notion::wnid == 100015388));
      }

      utterance << database.words(thingFilter).first();
    }

    verbly::query<verbly::word> similarQuery = database.words(
      (verbly::notion::partOfSpeech == verbly::part_of_speech::noun)
      && (verbly::notion::fullHypernyms %= hyper)
      && !fruit.getBaseForm()
      && !hyper
      && (verbly::form::proper == false)
      && (verbly::form::complexity == 1));
    std::vector<verbly::word> similarResults = similarQuery.all();

    if (!similarResults.empty())
    {
      utterance << similarResults.front();
    } else {
      verbly::query<verbly::word> differentQuery = database.words(
        fruitFilter
        && !fruit.getBaseForm()
        && !hyper
        && (verbly::form::proper == false)
        && (verbly::form::complexity == 1));

      utterance << differentQuery.first();
    }

    std::string fruitName = utterance.compile();

    std::ostringstream result;
    result << fruit.getBaseForm().getText();
    result << "? ";

    choice = std::uniform_int_distribution<int>(0,3)(random_engine);
    if (choice == 0)
    {
      result << "Oh, you mean ";
      result << fruitName;
    } else if (choice == 1)
    {
      result << "Don't you mean ";
      result << fruitName;
      result << "?";
    } else if (choice == 2)
    {
      result << "You mean ";
      result << fruitName;
      result << "!";
    } else if (choice == 3)
    {
      result << "Pfft, everyone just calls it ";
      result << fruitName;
      result << " now";
    }

    std::string tweet = result.str();
    tweet.resize(140);

    try
    {
      client.updateStatus(tweet);
      
      std::cout << tweet << std::endl;
    } catch (const twitter::twitter_error& e)
    {
      std::cout << "Twitter error: " << e.what() << std::endl;
    }
    
    std::this_thread::sleep_for(std::chrono::hours(1));
  }
}