From 41decb9a671e4d0fbbe12533372435ec6ede2246 Mon Sep 17 00:00:00 2001 From: Kelly Rauchenberger Date: Wed, 9 Mar 2016 23:30:14 -0500 Subject: Started verbly rewrite verbly is intended to be a general use natural language generation library. Here, I'm using it to simply generate random verbs or adjectives. A schema for the sqlite database is provided, and for testing I manually added data. A generator program is being written that will generate a database from WordNet, VerbNet, PropBank, and AGID data. --- furries.cpp | 243 ++++++++++++++++++++++++------------------------------------ 1 file changed, 96 insertions(+), 147 deletions(-) (limited to 'furries.cpp') diff --git a/furries.cpp b/furries.cpp index 15c4657..2d0fb23 100644 --- a/furries.cpp +++ b/furries.cpp @@ -1,16 +1,85 @@ #include #include -#include #include #include #include #include +#include "verbly/verbly.h" -int db_error(MYSQL* driver, const char* error) -{ - std::cout << error << ": " << mysql_error(driver) << std::endl; - return 1; -} +class fill_blanks { + private: + verbly::data& database; + + public: + fill_blanks(verbly::data& database) : database(database) + { + + } + + void visit(std::unique_ptr& it) + { + switch (it->token_type()) + { + case verbly::type::utterance: + { + auto& action = *dynamic_cast(it.get()); + for (auto& tkn : action) + { + if (!tkn->complete()) + { + visit(tkn); + + break; + } + } + + break; + } + + case verbly::type::fillin: + { + auto& tkn = *dynamic_cast(it.get()); + switch (tkn.fillin_type()) + { + case verbly::fillin_type::participle_phrase: + { + const verbly::verb& v = database.verbs().random(true).limit(1).run().front(); + /*verbly::utterance_token phrase = verbly::random(v.frames).make_utterance(); + while (std::begin(phrase)->token_type() != verbly::type::verb) + { + phrase.erase(std::begin(phrase)); + } + + *std::begin(phrase) = verbly::verb_token(v).conjugate(verbly::conjugation::present_participle); + *it = phrase;*/ + auto avt = std::make_unique(v); + avt->conjugate(verbly::conjugation::present_participle); + it = std::move(avt); + + break; + } + + case verbly::fillin_type::adjective: + { + const verbly::adjective& adj = database.adjectives().random(true).limit(1).run().front(); + it = std::make_unique(adj.value); + + break; + } + + default: + { + it = std::make_unique("*the reality of the situation*"); + + break; + } + } + + break; + } + } + } +}; int main(int argc, char** argv) { @@ -21,13 +90,7 @@ int main(int argc, char** argv) const char* user = config["user"].as().c_str(); const char* pass = config["pass"].as().c_str(); const char* db = config["db"].as().c_str(); - - MYSQL* driver = mysql_init(NULL); - if (!mysql_real_connect(driver, host, user, pass, db, 0, NULL, 0)) - { - return db_error(driver, "Error connecting to database"); - } - + twitCurl twitter; twitter.getOAuth().setConsumerKey(config["consumer_key"].as()); twitter.getOAuth().setConsumerSecret(config["consumer_secret"].as()); @@ -38,141 +101,29 @@ int main(int argc, char** argv) { std::cout << "Generating tweet" << std::endl; - std::stringstream output; - output << "the furries are "; - - //if (rand() % 2 == 0) - { - // Adverb(s) + Adjective - while (rand() % 4 == 0) - { - const char* getadverb = "SELECT ss1.word FROM wn_pertainym INNER JOIN wn_synset AS ss1 ON wn_pertainym.synset_id_1 = ss1.synset_id AND wn_pertainym.wnum_1 = ss1.w_num INNER JOIN wn_synset AS ss2 ON wn_pertainym.synset_id_2 = ss2.synset_id AND wn_pertainym.wnum_2 = ss2.w_num WHERE ss1.ss_type = 'r' ORDER BY RAND() LIMIT 1"; - if (mysql_query(driver, getadverb)) return db_error(driver, "Query failed"); - MYSQL_RES* getadverb2 = mysql_use_result(driver); if (getadverb2 == NULL) return db_error(driver, "Query failed"); - MYSQL_ROW getadverb3 = mysql_fetch_row(getadverb2); if (getadverb3 == NULL) return db_error(driver, "Query failed"); - output << getadverb3[0] << " "; - mysql_free_result(getadverb2); - } - - const char* getword = "SELECT word FROM wn_synset WHERE ss_type = 'a' OR ss_type = 's' ORDER BY RAND() LIMIT 1"; - if (mysql_query(driver, getword)) return db_error(driver, "Query failed"); - MYSQL_RES* getword2 = mysql_use_result(driver); if (getword2 == NULL) return db_error(driver, "Query failed"); - MYSQL_ROW getword3 = mysql_fetch_row(getword2); if (getword3 == NULL) return db_error(driver, "Query failed"); - output << getword3[0]; - mysql_free_result(getword2); - } /*else { - // Verb phrase - const char* getword = "SELECT word FROM wn_synset WHERE ss_type = 'a ' OR ss_type = 's' ORDER BY RAND() LIMIT 1"; - if (mysql_query(driver, getword)) return db_error(driver, "Query failed"); - MYSQL_RES* getword2 = mysql_use_result(driver); if (getword2 == NULL) return db_error(driver, "Query failed"); - MYSQL_ROW getword3 = mysql_fetch_row(getword2); if (getword3 == NULL) return db_error(driver, "Query failed"); - } + std::vector forms; + forms.push_back({ + new verbly::string_token("the furries are"), + new verbly::fillin_token(verbly::fillin_type::participle_phrase) + }); + forms.push_back({ + new verbly::string_token("the furries are"), + new verbly::fillin_token(verbly::fillin_type::adjective) + }); - - - - if (rand() % 2 == 0) + verbly::data database {"data.sqlite3"}; + fill_blanks yeah {database}; + std::unique_ptr action = std::make_unique(forms[rand() % forms.size()]); + while (!action->complete()) { - std::stringstream ispart; - ispart << "SELECT wn_verb_frame.f_num FROM wn_participle INNER JOIN wn_verb_frame ON wn_verb_frame.synset_id_1 = synset_id_2 AND (wn_verb_frame.w_num = wnum_2 OR wn_verb_frame.w_num = 0) WHERE wn_participle.synset_id_1 = " << wordssid << " AND wn_participle.wnum_1 = " << wordnum; - if (mysql_query(driver, ispart.str().c_str())) return db_error(driver, "Query failed"); - MYSQL_RES* ispart2 = mysql_use_result(driver); if (ispart2 == NULL) return db_error(driver, "Query failed"); - MYSQL_ROW ispart3 = mysql_fetch_row(ispart2); - mysql_free_result(ispart2); - - if (ispart3 != NULL) - { - int frame = atoi(ispart3[0]); - std::cout << "frame " << frame << std::endl; - - if (frame == 4 || frame == 22) - { - // the furries are ----ing *prepositional phrase* - // ex: the furries are laughing over there - } else if (frame == 5) - { - // the furries are ----ing something *adj/n* - // ex: the furries are regarding life inconsequential - } else if (frame == 6 || frame == 7) - { - // the furries are ----ing *adj/n* - // ex: the furries are turning gay - } else if (frame == 8 || frame == 9 || frame == 10 || frame == 11) - { - // the furries are ----ing something/somebody - // ex: the furries are holding hostages - } else if (frame == 12 || frame == 27) - { - // the furries are ----ing to somebody - // ex: the furries are appealing to God - } else if (frame == 13) - { - // the furries are ----ing on something - // ex: the furries are lecturing on heterosexuality - } else if (frame == 14) - { - // the furries are ----ing somebody something - // ex: the furries are reading your mom the menu - } else if (frame == 15) - { - // the furries are ----ing something to somebody - // ex: the furries are pitching a product to Apple - } else if (frame == 16) - { - // the furries are ----ing something from somebody - // ex: the furries are separating your money from you - } else if (frame == 17) - { - // the furries are ----ing somebody with something - // ex: the furries are injecting me with solemnity - } else if (frame == 18) - { - // the furries are ----ing somebody of something - // ex: the furries are depriving me of hope - } else if (frame == 19) - { - // the furries are ----ing something on somebody - // ex: the furries are forcing pervision on us - } else if (frame == 20 || frame == 21) - { - // the furries are ----ing somebody/something *prepositional phrase* - // ex: the furries are leaving us behind - } else if (frame == 24) - { - // the furries are ----ing somebody to INF - // ex: the furries are getting us to eat - } else if (frame == 25) - { - // the furries are ----ing somebody INF - // ex: the furries are making us procreate - } else if (frame == 26) - { - // the furries are ----ing that CLAUSE - // ex: the furries are understaing that life is precious - } else if (frame == 28) - { - // the furries are ----ing to INF - // ex: the furries are beginning to understand - } else if (frame == 29) - { - // the furries are ----ing whether to INF - // ex: the furries are deciding whether to hallucinate - } else if (frame == 30) - { - // the furries are ----ing somebody into V-ing something - // ex: the furries are tempting me into consecrating a magnet - } else if (frame == 31) - { - // the furries are ----ing something with something - // ex: the furries are replacing existence with solidarity - } - } + yeah.visit(action); } - */ - std::string result = output.str(); + std::string result = action->compile(); result.resize(140); - + + std::cout << result << std::endl; + /* std::string replyMsg; if (twitter.statusUpdate(result)) { @@ -181,11 +132,9 @@ int main(int argc, char** argv) } else { twitter.getLastCurlError(replyMsg); std::cout << "Curl error: " << replyMsg << std::endl; - } + }*/ std::cout << "Waiting" << std::endl; - sleep(60 * 60 * 3); + sleep(/*60 * 60 * */ 3); } - - mysql_close(driver); } -- cgit 1.4.1