From 8de3134bf2cd26ff81359df703e5fbc6280448d7 Mon Sep 17 00:00:00 2001 From: Feffernoose Date: Tue, 1 Oct 2013 18:15:22 -0400 Subject: Wrote program --- Makefile.am | 4 +- config-example.yml | 6 ++ configure.ac | 28 ++++++++++ kgramstats.cpp | 110 ++++++++++++++++++++++++++++++++++++ kgramstats.h | 28 ++++++++++ main.cpp | 161 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 336 insertions(+), 1 deletion(-) create mode 100644 config-example.yml create mode 100644 kgramstats.cpp create mode 100644 kgramstats.h diff --git a/Makefile.am b/Makefile.am index 127042a..c5b52ce 100644 --- a/Makefile.am +++ b/Makefile.am @@ -2,4 +2,6 @@ AUTOMAKE_OPTIONS = subdir-objects ACLOCAL_AMFLAGS = ${ACLOCAL_FLAGS} bin_PROGRAMS = rawr-ebooks -rawr_ebooks_SOURCES = main.cpp \ No newline at end of file +rawr_ebooks_SOURCES = main.cpp kgramstats.cpp +AM_CPPFLAGS = $(LIBTWITCURL_CFLAGS) $(YAML_CFLAGS) +rawr_ebooks_LDADD = $(LIBTWITCURL_LIBS) $(YAML_LIBS) \ No newline at end of file diff --git a/config-example.yml b/config-example.yml new file mode 100644 index 0000000..a280e4c --- /dev/null +++ b/config-example.yml @@ -0,0 +1,6 @@ +--- + corpus: "corpus.txt" + username: "" + password: "" + consumer_key: "" + consumer_secret: "" \ No newline at end of file diff --git a/configure.ac b/configure.ac index c1c50bf..31cf8b9 100644 --- a/configure.ac +++ b/configure.ac @@ -4,4 +4,32 @@ AM_INIT_AUTOMAKE([1.10 no-define foreign]) AC_PROG_CXX AC_CONFIG_FILES([Makefile]) +# Get libtwitcurl library and include locations +AC_ARG_WITH([libtwitcurl-include-path], + [AS_HELP_STRING([--with-libtwitcurl-include-path], + [location of the libtwitcurl headers, defaults to /usr/include])], + [LIBTWITCURL_CFLAGS="-I$withval"], + [LIBTWITCURL_CFLAGS='']) +AC_SUBST([LIBTWITCURL_CFLAGS]) + +AC_ARG_WITH([libtwitcurl-lib-path], + [AS_HELP_STRING([--with-libtwitcurl-lib-path], [location of the libtwitcurl libraries])], + [LIBTWITCURL_LIBS="-L$withval -llibtwitcurl"], + [LIBTWITCURL_LIBS='-ltwitcurl']) +AC_SUBST([LIBTWITCURL_LIBS]) + +# Get yaml-cpp library and include locations +AC_ARG_WITH([yamlcpp-include-path], + [AS_HELP_STRING([--with-yamlcpp-include-path], + [location of the yamlcpp headers, defaults to /usr/include])], + [YAML_CFLAGS="-I$withval"], + [YAML_CFLAGS='']) +AC_SUBST([YAML_CFLAGS]) + +AC_ARG_WITH([yamlcpp-lib-path], + [AS_HELP_STRING([--with-yamlcpp-lib-path], [location of the yamlcpp libraries])], + [YAML_LIBS="-L$withval -lyaml-cpp"], + [YAML_LIBS='-lyaml-cpp']) +AC_SUBST([YAML_LIBS]) + AC_OUTPUT diff --git a/kgramstats.cpp b/kgramstats.cpp new file mode 100644 index 0000000..142b5aa --- /dev/null +++ b/kgramstats.cpp @@ -0,0 +1,110 @@ +#include "kgramstats.h" +#include +#include +#include + +kgramstats::kgramstats(string corpus, int maxK) +{ + this->maxK = maxK; + + vector tokens; + int start = 0; + int end = 0; + + while (end != string::npos) + { + end = corpus.find(" ", start); + + tokens.push_back(corpus.substr(start, (end == string::npos) ? string::npos : end - start)); + + start = ((end > (string::npos - 1) ) ? string::npos : end + 1); + } + + stats = new map* >(); + for (int k=0; k<=maxK; k++) + { + for (int i=0; i<(tokens.size() - k); i++) + { + kgram seq(tokens.begin()+i, tokens.begin()+i+k); + string f = tokens[i+k]; + + if ((*stats)[seq] == NULL) + { + (*stats)[seq] = new map(); + } + + (*((*stats)[seq]))[f]++; + } + } +} + +map* kgramstats::lookupExts(kgram tk) +{ + return (*stats)[tk]; +} + +int kgramstats::getMaxK() +{ + return maxK; +} + +void printKgram(kgram k) +{ + for (kgram::iterator it = k.begin(); it != k.end(); it++) + { + cout << *it << " "; + } + cout << endl; +} + +vector kgramstats::randomSentence(int n) +{ + vector result; + list cur; + + for (int i=0; i* probtable = lookupExts(cur); + int max = 0; + for (map::iterator it = probtable->begin(); it != probtable->end(); ++it) + { + max += it->second; + } + + int r = rand() % (max+1); + string next = probtable->begin()->first; + for (map::iterator it = probtable->begin(); it != probtable->end(); ++it) + { + if (it->second > r) + { + break; + } else { + next = it->first; + r -= it->second; + } + } + + if (cur.size() == maxK) + { + cur.pop_front(); + } + + cur.push_back(next); + result.push_back(next); + } + + return result; +} \ No newline at end of file diff --git a/kgramstats.h b/kgramstats.h new file mode 100644 index 0000000..069bb90 --- /dev/null +++ b/kgramstats.h @@ -0,0 +1,28 @@ +#include +#include +#include +#include + +using namespace::std; + +#ifndef KGRAMSTATS_H +#define KGRAMSTATS_H + +typedef list kgram; + +class kgramstats +{ +public: + kgramstats(string corpus, int maxK); + map* lookupExts(kgram tk); + int getMaxK(); + vector randomSentence(int n); + +private: + int maxK; + map* >* stats; +}; + +void printKgram(kgram k); + +#endif \ No newline at end of file diff --git a/main.cpp b/main.cpp index 8185573..8310bf4 100644 --- a/main.cpp +++ b/main.cpp @@ -1,6 +1,167 @@ #include +#include +#include +#include "kgramstats.h" +#include +#include +#include +#include +#include +#include +#include +#include + +using namespace::std; int main(int argc, char** args) { + srand(time(NULL)); + + YAML::Node config = YAML::LoadFile("config.yml"); + + for (;;) + { + ifstream infile(config["corpus"].as().c_str()); + string corpus; + string line; + while (getline(infile, line)) + { + corpus += " " + line; + } + + kgramstats* stats = new kgramstats(corpus, 5); + vector doc = stats->randomSentence(rand() % 25 + 5); + string hi; + for (vector::iterator it = doc.begin(); it != doc.end(); ++it) + { + hi += *it + " "; + } + + hi = hi.substr(0,140); + + twitCurl twitterObj; + std::string tmpStr, tmpStr2; + std::string replyMsg; + char tmpBuf[1024]; + std::string username(config["username"].as()); + std::string password(config["password"].as()); + + /* Set twitter username and password */ + twitterObj.setTwitterUsername(username); + twitterObj.setTwitterPassword(password); + + /* OAuth flow begins */ + /* Step 0: Set OAuth related params. These are got by registering your app at twitter.com */ + twitterObj.getOAuth().setConsumerKey( config["consumer_key"].as() ); + twitterObj.getOAuth().setConsumerSecret( config["consumer_secret"].as() ); + + /* Step 1: Check if we alredy have OAuth access token from a previous run */ + std::string myOAuthAccessTokenKey(""); + std::string myOAuthAccessTokenSecret(""); + std::ifstream oAuthTokenKeyIn; + std::ifstream oAuthTokenSecretIn; + + oAuthTokenKeyIn.open( "twitterClient_token_key.txt" ); + oAuthTokenSecretIn.open( "twitterClient_token_secret.txt" ); + + memset( tmpBuf, 0, 1024 ); + oAuthTokenKeyIn >> tmpBuf; + myOAuthAccessTokenKey = tmpBuf; + + memset( tmpBuf, 0, 1024 ); + oAuthTokenSecretIn >> tmpBuf; + myOAuthAccessTokenSecret = tmpBuf; + + oAuthTokenKeyIn.close(); + oAuthTokenSecretIn.close(); + + if( myOAuthAccessTokenKey.size() && myOAuthAccessTokenSecret.size() ) + { + /* If we already have these keys, then no need to go through auth again */ + printf( "\nUsing:\nKey: %s\nSecret: %s\n\n", myOAuthAccessTokenKey.c_str(), myOAuthAccessTokenSecret.c_str() ); + + twitterObj.getOAuth().setOAuthTokenKey( myOAuthAccessTokenKey ); + twitterObj.getOAuth().setOAuthTokenSecret( myOAuthAccessTokenSecret ); + } + else + { + /* Step 2: Get request token key and secret */ + std::string authUrl; + twitterObj.oAuthRequestToken( authUrl ); + + /* Step 3: Get PIN */ + memset( tmpBuf, 0, 1024 ); + printf( "\nDo you want to visit twitter.com for PIN (0 for no; 1 for yes): " ); + gets( tmpBuf ); + tmpStr = tmpBuf; + if( std::string::npos != tmpStr.find( "1" ) ) + { + /* Ask user to visit twitter.com auth page and get PIN */ + memset( tmpBuf, 0, 1024 ); + printf( "\nPlease visit this link in web browser and authorize this application:\n%s", authUrl.c_str() ); + printf( "\nEnter the PIN provided by twitter: " ); + gets( tmpBuf ); + tmpStr = tmpBuf; + twitterObj.getOAuth().setOAuthPin( tmpStr ); + } + else + { + /* Else, pass auth url to twitCurl and get it via twitCurl PIN handling */ + twitterObj.oAuthHandlePIN( authUrl ); + } + + /* Step 4: Exchange request token with access token */ + twitterObj.oAuthAccessToken(); + + /* Step 5: Now, save this access token key and secret for future use without PIN */ + twitterObj.getOAuth().getOAuthTokenKey( myOAuthAccessTokenKey ); + twitterObj.getOAuth().getOAuthTokenSecret( myOAuthAccessTokenSecret ); + + /* Step 6: Save these keys in a file or wherever */ + std::ofstream oAuthTokenKeyOut; + std::ofstream oAuthTokenSecretOut; + + oAuthTokenKeyOut.open( "twitterClient_token_key.txt" ); + oAuthTokenSecretOut.open( "twitterClient_token_secret.txt" ); + + oAuthTokenKeyOut.clear(); + oAuthTokenSecretOut.clear(); + + oAuthTokenKeyOut << myOAuthAccessTokenKey.c_str(); + oAuthTokenSecretOut << myOAuthAccessTokenSecret.c_str(); + + oAuthTokenKeyOut.close(); + oAuthTokenSecretOut.close(); + } + /* OAuth flow ends */ + + /* Account credentials verification */ + if( twitterObj.accountVerifyCredGet() ) + { + twitterObj.getLastWebResponse( replyMsg ); + printf( "\ntwitterClient:: twitCurl::accountVerifyCredGet web response:\n%s\n", replyMsg.c_str() ); + } + else + { + twitterObj.getLastCurlError( replyMsg ); + printf( "\ntwitterClient:: twitCurl::accountVerifyCredGet error:\n%s\n", replyMsg.c_str() ); + } + + /* Post a new status message */ + replyMsg = ""; + if( twitterObj.statusUpdate( hi ) ) + { + twitterObj.getLastWebResponse( replyMsg ); + printf( "\ntwitterClient:: twitCurl::statusUpdate web response:\n%s\n", replyMsg.c_str() ); + } + else + { + twitterObj.getLastCurlError( replyMsg ); + printf( "\ntwitterClient:: twitCurl::statusUpdate error:\n%s\n", replyMsg.c_str() ); + } + + sleep(900); + } + return 0; } \ No newline at end of file -- cgit 1.4.1