about summary refs log tree commit diff stats
path: root/ebooks.cpp
blob: e38ebabedf9a25b736cdde33ceab8eff0d03de3c (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
#include <cstdio>
#include <list>
#include <map>
#include "kgramstats.h"
#include <ctime>
#include <vector>
#include <cstdlib>
#include <fstream>
#include <iostream>
#include <twitcurl.h>
#include <unistd.h>
#include <yaml-cpp/yaml.h>
#include "freevars.h"

int main(int argc, char** args)
{
  srand(time(NULL));
	
  YAML::Node config = YAML::LoadFile("config.yml");
  int delay = config["delay"].as<int>();

  std::ifstream infile(config["corpus"].as<std::string>().c_str());
  std::string corpus;
  std::string line;
  while (getline(infile, line))
  {
    corpus += line + "\n ";
  }

  std::cout << "Preprocessing corpus..." << std::endl;
  kgramstats* stats = new kgramstats(corpus, 4);
    
  std::cout << "Preprocessing freevars..." << std::endl;
  freevars* vars = new freevars();
  vars->addVar("name", "names.txt");
  vars->addVar("noun", "nouns.txt");
  
  twitCurl twitter;
  twitter.getOAuth().setConsumerKey(config["consumer_key"].as<std::string>());
  twitter.getOAuth().setConsumerSecret(config["consumer_secret"].as<std::string>());
  twitter.getOAuth().setOAuthTokenKey(config["access_key"].as<std::string>());
  twitter.getOAuth().setOAuthTokenSecret(config["access_secret"].as<std::string>());

  std::cout << "Generating..." << std::endl;
  for (;;)
  {
    std::vector<std::string> doc = stats->randomSentence(rand() % 45 + 5);
    std::string hi;
    for (std::vector<std::string>::iterator it = doc.begin(); it != doc.end(); ++it)
    {
      hi += vars->parse(*it) + " ";
    }

    hi.resize(140);

    size_t lastperiod = hi.find_last_of(".!?,");
    if ((lastperiod != std::string::npos) && (rand() % 3 > 0))
    {
      hi = hi.substr(0, lastperiod+1);
    }
    
    std::string replyMsg;
    if (twitter.statusUpdate(hi))
    {
      twitter.getLastWebResponse(replyMsg);
      std::cout << "Twitter message: " << replyMsg << std::endl;
    } else {
      twitter.getLastCurlError(replyMsg);
      std::cout << "Curl error: " << replyMsg << std::endl;
    }

    sleep(rand() % delay);
  }
	
  return 0;
}