about summary refs log tree commit diff stats
diff options
context:
space:
mode:
-rw-r--r--.gitmodules3
-rw-r--r--CMakeLists.txt14
-rw-r--r--cmake/modules/FindMysql.cmake78
-rw-r--r--nancy.cpp88
m---------vendor/verbly0
5 files changed, 45 insertions, 138 deletions
diff --git a/.gitmodules b/.gitmodules index 581c545..253a6d8 100644 --- a/.gitmodules +++ b/.gitmodules
@@ -4,3 +4,6 @@
4[submodule "vendor/yaml-cpp"] 4[submodule "vendor/yaml-cpp"]
5 path = vendor/yaml-cpp 5 path = vendor/yaml-cpp
6 url = https://github.com/jbeder/yaml-cpp 6 url = https://github.com/jbeder/yaml-cpp
7[submodule "vendor/verbly"]
8 path = vendor/verbly
9 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)
4set(CMAKE_BUILD_TYPE Debug) 4set(CMAKE_BUILD_TYPE Debug)
5 5
6add_subdirectory(vendor/twitcurl/libtwitcurl) 6add_subdirectory(vendor/twitcurl/libtwitcurl)
7 7add_subdirectory(vendor/verbly)
8set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake/modules/")
9
10find_package(Mysql REQUIRED)
11
12add_subdirectory(vendor/yaml-cpp EXCLUDE_FROM_ALL) 8add_subdirectory(vendor/yaml-cpp EXCLUDE_FROM_ALL)
13include_directories(vendor/yaml-cpp/include)
14 9
15include_directories(vendor/twitcurl/libtwitcurl ${MYSQL_INCLUDE_DIR}) 10find_package(PkgConfig)
11pkg_check_modules(sqlite3 sqlite3 REQUIRED)
12
13include_directories(vendor/yaml-cpp/include vendor/twitcurl/libtwitcurl vendor/verbly/lib)
16add_executable(nancy nancy.cpp) 14add_executable(nancy nancy.cpp)
17set_property(TARGET nancy PROPERTY CXX_STANDARD 11) 15set_property(TARGET nancy PROPERTY CXX_STANDARD 11)
18set_property(TARGET nancy PROPERTY CXX_STANDARD_REQUIRED ON) 16set_property(TARGET nancy PROPERTY CXX_STANDARD_REQUIRED ON)
19target_link_libraries(nancy yaml-cpp ${MYSQL_LIBRARIES} twitcurl curl) 17target_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 @@
1# This line added for distribution with Vermont:
2MESSAGE(STATUS "Using bundled FindMySQL.cmake...")
3
4# - Find MySQL
5# Find the MySQL includes and client library
6# This module defines
7# MYSQL_INCLUDE_DIR, where to find mysql.h
8# MYSQL_LIBRARIES, the libraries needed to use MySQL.
9# MYSQL_FOUND, If false, do not try to use MySQL.
10#
11# Copyright (c) 2006, Jaroslaw Staniek, <js@iidea.pl>
12#
13# Redistribution and use is allowed according to the terms of the BSD license.
14# For details see the accompanying COPYING-CMAKE-SCRIPTS file.
15
16
17IF(UNIX)
18 SET(MYSQL_CONFIG_PREFER_PATH "$ENV{MYSQL_HOME}/bin" CACHE FILEPATH
19 "preferred path to MySQL (mysql_config)")
20 FIND_PROGRAM(MYSQL_CONFIG mysql_config
21 ${MYSQL_CONFIG_PREFER_PATH}
22 /usr/local/mysql/bin/
23 /usr/local/bin/
24 /usr/bin/
25 )
26
27 IF (MYSQL_CONFIG)
28 # set INCLUDE_DIR
29 EXEC_PROGRAM(${MYSQL_CONFIG}
30 ARGS --include
31 OUTPUT_VARIABLE MY_TMP)
32 string (REGEX REPLACE "-I([^ ]*)( .*)?" "\\1" MY_TMP "${MY_TMP}")
33 SET(MYSQL_ADD_INCLUDE_DIR ${MY_TMP} CACHE FILEPATH INTERNAL)
34
35 # set LIBRARY_DIR
36 EXEC_PROGRAM(${MYSQL_CONFIG}
37 ARGS --libs
38 OUTPUT_VARIABLE MY_TMP)
39 string (REGEX REPLACE "(.* )?-L([^ ]*)( .*)?" "\\2" MY_TMP "${MY_TMP}")
40 SET(MYSQL_ADD_LIBRARY ${MY_TMP} CACHE FILEPATH INTERNAL)
41 ENDIF(MYSQL_CONFIG)
42
43ELSE(UNIX)
44 set(MYSQL_ADD_INCLUDE_DIR "c:/msys/local/include" CACHE FILEPATH INTERNAL)
45 set(MYSQL_ADD_LIBRARY "c:/msys/local/lib" CACHE FILEPATH INTERNAL)
46ENDIF(UNIX)
47
48if (NOT MYSQL_FOUND)
49
50 find_path(MYSQL_INCLUDE_DIR mysql.h
51 /usr/local/include
52 /usr/local/include/mysql
53 /usr/local/mysql/include
54 /usr/include
55 ${MYSQL_ADD_INCLUDE_DIR}
56 )
57
58 find_library(MYSQL_LIBRARIES NAMES mysqlclient
59 PATHS
60 /usr/lib
61 /usr/lib/mysql
62 /usr/local/lib
63 /usr/local/lib/mysql
64 /usr/local/mysql/lib
65 ${MYSQL_ADD_LIBRARY}
66 )
67
68 if(MYSQL_INCLUDE_DIR AND MYSQL_LIBRARIES)
69 set(MYSQL_FOUND TRUE CACHE INTERNAL "MySQL found")
70 message(STATUS "Found MySQL: ${MYSQL_INCLUDE_DIR}, ${MYSQL_LIBRARIES}")
71 else(MYSQL_INCLUDE_DIR AND MYSQL_LIBRARIES)
72 set(MYSQL_FOUND FALSE CACHE INTERNAL "MySQL found")
73 message(STATUS "MySQL not found.")
74 endif(MYSQL_INCLUDE_DIR AND MYSQL_LIBRARIES)
75
76 mark_as_advanced(MYSQL_INCLUDE_DIR MYSQL_LIBRARIES)
77
78endif (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 @@
1#include <yaml-cpp/yaml.h> 1#include <yaml-cpp/yaml.h>
2#include <iostream> 2#include <iostream>
3#include <mysql/mysql.h>
4#include <cstdlib> 3#include <cstdlib>
5#include <ctime> 4#include <ctime>
6#include <sstream> 5#include <sstream>
7#include <twitcurl.h> 6#include <twitcurl.h>
8#include <unistd.h> 7#include <unistd.h>
8#include <verbly.h>
9 9
10int db_error(MYSQL* driver, const char* error) 10std::string capitalize(std::string input)
11{ 11{
12 std::cout << error << ": " << mysql_error(driver) << std::endl; 12 std::string result;
13 return 1; 13 bool capnext = true;
14
15 for (auto ch : input)
16 {
17 if (capnext)
18 {
19 result += toupper(ch);
20 capnext = false;
21 } else {
22 result += ch;
23 }
24
25 if ((ch == ' ') || (ch == '-'))
26 {
27 capnext = true;
28 }
29 }
30
31 return result;
14} 32}
15 33
16int main(int argc, char** argv) 34int main(int argc, char** argv)
@@ -18,10 +36,6 @@ int main(int argc, char** argv)
18 srand(time(NULL)); 36 srand(time(NULL));
19 37
20 YAML::Node config = YAML::LoadFile("config.yml"); 38 YAML::Node config = YAML::LoadFile("config.yml");
21 const char* host = config["host"].as<std::string>().c_str();
22 const char* user = config["user"].as<std::string>().c_str();
23 const char* pass = config["pass"].as<std::string>().c_str();
24 const char* db = config["db"].as<std::string>().c_str();
25 39
26 // Forms 40 // Forms
27 std::vector<std::string> forms; 41 std::vector<std::string> forms;
@@ -50,13 +64,10 @@ int main(int argc, char** argv)
50 return 2; 64 return 2;
51 } 65 }
52 66
53 // WordNet data 67 // verbly
54 MYSQL* driver = mysql_init(NULL); 68 verbly::data database("data.sqlite3");
55 if (!mysql_real_connect(driver, host, user, pass, db, 0, NULL, 0))
56 {
57 return db_error(driver, "Error connecting to database");
58 }
59 69
70 // Twitter
60 twitCurl twitter; 71 twitCurl twitter;
61 twitter.getOAuth().setConsumerKey(config["consumer_key"].as<std::string>()); 72 twitter.getOAuth().setConsumerKey(config["consumer_key"].as<std::string>());
62 twitter.getOAuth().setConsumerSecret(config["consumer_secret"].as<std::string>()); 73 twitter.getOAuth().setConsumerSecret(config["consumer_secret"].as<std::string>());
@@ -73,50 +84,25 @@ int main(int argc, char** argv)
73 int i; 84 int i;
74 while ((i = form.find("{adj}")) != std::string::npos) 85 while ((i = form.find("{adj}")) != std::string::npos)
75 { 86 {
76 const char* getword = "SELECT word FROM wn_synset WHERE ss_type = 'a' OR ss_type = 's' ORDER BY RAND() LIMIT 1"; 87 verbly::adjective adj = database.adjectives().random(true).limit(1).run().front();
77 if (mysql_query(driver, getword)) return db_error(driver, "Query failed"); 88 form.replace(i, 5, capitalize(adj.base_form()));
78 MYSQL_RES* getword2 = mysql_use_result(driver); if (getword2 == NULL) return db_error(driver, "Query failed");
79 MYSQL_ROW getword3 = mysql_fetch_row(getword2); if (getword3 == NULL) return db_error(driver, "Query failed");
80 std::string adj {getword3[0]};
81 mysql_free_result(getword2);
82
83 adj[0] = toupper(adj[0]);
84
85 int j;
86 while ((j = adj.find("_")) != std::string::npos)
87 {
88 adj[j] = ' ';
89 adj[j+1] = toupper(adj[j+1]);
90 }
91
92 if (adj[adj.size()-1] == ')')
93 {
94 adj.resize(adj.size()-3);
95 }
96
97 form.replace(i, 5, adj);
98 } 89 }
99 90
100 // Nouns 91 // Nouns
101 while ((i = form.find("{noun}")) != std::string::npos) 92 while ((i = form.find("{noun}")) != std::string::npos)
102 { 93 {
103 const char* getword = "SELECT word FROM wn_synset WHERE ss_type = 'n' ORDER BY RAND() LIMIT 1"; 94 std::string nf;
104 if (mysql_query(driver, getword)) return db_error(driver, "Query failed"); 95 for (;;)
105 MYSQL_RES* getword2 = mysql_use_result(driver); if (getword2 == NULL) return db_error(driver, "Query failed");
106 MYSQL_ROW getword3 = mysql_fetch_row(getword2); if (getword3 == NULL) return db_error(driver, "Query failed");
107 std::string noun {getword3[0]};
108 mysql_free_result(getword2);
109
110 noun[0] = toupper(noun[0]);
111
112 int j;
113 while ((j = noun.find("_")) != std::string::npos)
114 { 96 {
115 noun[j] = ' '; 97 verbly::noun n = database.nouns().is_not_proper(true).random(true).limit(1).run().front();
116 noun[j+1] = toupper(noun[j+1]); 98 if (n.singular_form().find("genus") == std::string::npos)
99 {
100 nf = n.singular_form();
101 break;
102 }
117 } 103 }
118 104
119 form.replace(i, 6, noun); 105 form.replace(i, 6, capitalize(nf));
120 } 106 }
121 107
122 if (form.size() > 140) 108 if (form.size() > 140)
@@ -137,6 +123,4 @@ int main(int argc, char** argv)
137 std::cout << "Waiting" << std::endl; 123 std::cout << "Waiting" << std::endl;
138 sleep(60 * 60 * 3); 124 sleep(60 * 60 * 3);
139 } 125 }
140
141 mysql_close(driver);
142} 126}
diff --git a/vendor/verbly b/vendor/verbly new file mode 160000
Subproject 365dda7937dae0b6edef1f498473c57c715eec1