From 1b78c44442cd578bad394bf6cbfabd4af637ca4b Mon Sep 17 00:00:00 2001 From: Kelly Rauchenberger Date: Thu, 17 Mar 2016 14:37:47 -0400 Subject: Updated to use verbly --- .gitmodules | 3 ++ CMakeLists.txt | 14 +++---- cmake/modules/FindMysql.cmake | 78 -------------------------------------- nancy.cpp | 88 ++++++++++++++++++------------------------- vendor/verbly | 1 + 5 files changed, 46 insertions(+), 138 deletions(-) delete mode 100644 cmake/modules/FindMysql.cmake create mode 160000 vendor/verbly diff --git a/.gitmodules b/.gitmodules index 581c545..253a6d8 100644 --- a/.gitmodules +++ b/.gitmodules @@ -4,3 +4,6 @@ [submodule "vendor/yaml-cpp"] path = vendor/yaml-cpp url = https://github.com/jbeder/yaml-cpp +[submodule "vendor/verbly"] + path = vendor/verbly + url = https://github.com/hatkirby/verbly diff --git a/CMakeLists.txt b/CMakeLists.txt index 554735d..d57c96a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -4,16 +4,14 @@ project (nancy) set(CMAKE_BUILD_TYPE Debug) add_subdirectory(vendor/twitcurl/libtwitcurl) - -set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake/modules/") - -find_package(Mysql REQUIRED) - +add_subdirectory(vendor/verbly) add_subdirectory(vendor/yaml-cpp EXCLUDE_FROM_ALL) -include_directories(vendor/yaml-cpp/include) -include_directories(vendor/twitcurl/libtwitcurl ${MYSQL_INCLUDE_DIR}) +find_package(PkgConfig) +pkg_check_modules(sqlite3 sqlite3 REQUIRED) + +include_directories(vendor/yaml-cpp/include vendor/twitcurl/libtwitcurl vendor/verbly/lib) add_executable(nancy nancy.cpp) set_property(TARGET nancy PROPERTY CXX_STANDARD 11) set_property(TARGET nancy PROPERTY CXX_STANDARD_REQUIRED ON) -target_link_libraries(nancy yaml-cpp ${MYSQL_LIBRARIES} twitcurl curl) +target_link_libraries(nancy yaml-cpp twitcurl curl verbly ${sqlite3_LIBRARIES}) diff --git a/cmake/modules/FindMysql.cmake b/cmake/modules/FindMysql.cmake deleted file mode 100644 index e060e49..0000000 --- a/cmake/modules/FindMysql.cmake +++ /dev/null @@ -1,78 +0,0 @@ -# This line added for distribution with Vermont: -MESSAGE(STATUS "Using bundled FindMySQL.cmake...") - -# - Find MySQL -# Find the MySQL includes and client library -# This module defines -# MYSQL_INCLUDE_DIR, where to find mysql.h -# MYSQL_LIBRARIES, the libraries needed to use MySQL. -# MYSQL_FOUND, If false, do not try to use MySQL. -# -# Copyright (c) 2006, Jaroslaw Staniek, -# -# Redistribution and use is allowed according to the terms of the BSD license. -# For details see the accompanying COPYING-CMAKE-SCRIPTS file. - - -IF(UNIX) - SET(MYSQL_CONFIG_PREFER_PATH "$ENV{MYSQL_HOME}/bin" CACHE FILEPATH - "preferred path to MySQL (mysql_config)") - FIND_PROGRAM(MYSQL_CONFIG mysql_config - ${MYSQL_CONFIG_PREFER_PATH} - /usr/local/mysql/bin/ - /usr/local/bin/ - /usr/bin/ - ) - - IF (MYSQL_CONFIG) - # set INCLUDE_DIR - EXEC_PROGRAM(${MYSQL_CONFIG} - ARGS --include - OUTPUT_VARIABLE MY_TMP) - string (REGEX REPLACE "-I([^ ]*)( .*)?" "\\1" MY_TMP "${MY_TMP}") - SET(MYSQL_ADD_INCLUDE_DIR ${MY_TMP} CACHE FILEPATH INTERNAL) - - # set LIBRARY_DIR - EXEC_PROGRAM(${MYSQL_CONFIG} - ARGS --libs - OUTPUT_VARIABLE MY_TMP) - string (REGEX REPLACE "(.* )?-L([^ ]*)( .*)?" "\\2" MY_TMP "${MY_TMP}") - SET(MYSQL_ADD_LIBRARY ${MY_TMP} CACHE FILEPATH INTERNAL) - ENDIF(MYSQL_CONFIG) - -ELSE(UNIX) - set(MYSQL_ADD_INCLUDE_DIR "c:/msys/local/include" CACHE FILEPATH INTERNAL) - set(MYSQL_ADD_LIBRARY "c:/msys/local/lib" CACHE FILEPATH INTERNAL) -ENDIF(UNIX) - -if (NOT MYSQL_FOUND) - - find_path(MYSQL_INCLUDE_DIR mysql.h - /usr/local/include - /usr/local/include/mysql - /usr/local/mysql/include - /usr/include - ${MYSQL_ADD_INCLUDE_DIR} - ) - - find_library(MYSQL_LIBRARIES NAMES mysqlclient - PATHS - /usr/lib - /usr/lib/mysql - /usr/local/lib - /usr/local/lib/mysql - /usr/local/mysql/lib - ${MYSQL_ADD_LIBRARY} - ) - - if(MYSQL_INCLUDE_DIR AND MYSQL_LIBRARIES) - set(MYSQL_FOUND TRUE CACHE INTERNAL "MySQL found") - message(STATUS "Found MySQL: ${MYSQL_INCLUDE_DIR}, ${MYSQL_LIBRARIES}") - else(MYSQL_INCLUDE_DIR AND MYSQL_LIBRARIES) - set(MYSQL_FOUND FALSE CACHE INTERNAL "MySQL found") - message(STATUS "MySQL not found.") - endif(MYSQL_INCLUDE_DIR AND MYSQL_LIBRARIES) - - mark_as_advanced(MYSQL_INCLUDE_DIR MYSQL_LIBRARIES) - -endif (NOT MYSQL_FOUND) \ No newline at end of file diff --git a/nancy.cpp b/nancy.cpp index 3605f71..00c68c5 100644 --- a/nancy.cpp +++ b/nancy.cpp @@ -1,16 +1,34 @@ #include #include -#include #include #include #include #include #include +#include -int db_error(MYSQL* driver, const char* error) +std::string capitalize(std::string input) { - std::cout << error << ": " << mysql_error(driver) << std::endl; - return 1; + std::string result; + bool capnext = true; + + for (auto ch : input) + { + if (capnext) + { + result += toupper(ch); + capnext = false; + } else { + result += ch; + } + + if ((ch == ' ') || (ch == '-')) + { + capnext = true; + } + } + + return result; } int main(int argc, char** argv) @@ -18,10 +36,6 @@ int main(int argc, char** argv) srand(time(NULL)); YAML::Node config = YAML::LoadFile("config.yml"); - const char* host = config["host"].as().c_str(); - const char* user = config["user"].as().c_str(); - const char* pass = config["pass"].as().c_str(); - const char* db = config["db"].as().c_str(); // Forms std::vector forms; @@ -50,13 +64,10 @@ int main(int argc, char** argv) return 2; } - // WordNet data - 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"); - } + // verbly + verbly::data database("data.sqlite3"); + // Twitter twitCurl twitter; twitter.getOAuth().setConsumerKey(config["consumer_key"].as()); twitter.getOAuth().setConsumerSecret(config["consumer_secret"].as()); @@ -73,50 +84,25 @@ int main(int argc, char** argv) int i; while ((i = form.find("{adj}")) != std::string::npos) { - 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::string adj {getword3[0]}; - mysql_free_result(getword2); - - adj[0] = toupper(adj[0]); - - int j; - while ((j = adj.find("_")) != std::string::npos) - { - adj[j] = ' '; - adj[j+1] = toupper(adj[j+1]); - } - - if (adj[adj.size()-1] == ')') - { - adj.resize(adj.size()-3); - } - - form.replace(i, 5, adj); + verbly::adjective adj = database.adjectives().random(true).limit(1).run().front(); + form.replace(i, 5, capitalize(adj.base_form())); } // Nouns while ((i = form.find("{noun}")) != std::string::npos) { - const char* getword = "SELECT word FROM wn_synset WHERE ss_type = 'n' 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::string noun {getword3[0]}; - mysql_free_result(getword2); - - noun[0] = toupper(noun[0]); - - int j; - while ((j = noun.find("_")) != std::string::npos) + std::string nf; + for (;;) { - noun[j] = ' '; - noun[j+1] = toupper(noun[j+1]); + verbly::noun n = database.nouns().is_not_proper(true).random(true).limit(1).run().front(); + if (n.singular_form().find("genus") == std::string::npos) + { + nf = n.singular_form(); + break; + } } - form.replace(i, 6, noun); + form.replace(i, 6, capitalize(nf)); } if (form.size() > 140) @@ -137,6 +123,4 @@ int main(int argc, char** argv) std::cout << "Waiting" << std::endl; sleep(60 * 60 * 3); } - - mysql_close(driver); } diff --git a/vendor/verbly b/vendor/verbly new file mode 160000 index 0000000..365dda7 --- /dev/null +++ b/vendor/verbly @@ -0,0 +1 @@ +Subproject commit 365dda7937dae0b6edef1f498473c57c715eec1d -- cgit 1.4.1