diff options
-rw-r--r-- | CMakeLists.txt | 7 | ||||
-rw-r--r-- | furries.cpp | 243 | ||||
-rw-r--r-- | schema.sql | 38 | ||||
-rw-r--r-- | verbly/adjective.h | 21 | ||||
-rw-r--r-- | verbly/c++14.h | 35 | ||||
-rw-r--r-- | verbly/data.h | 201 | ||||
-rw-r--r-- | verbly/token.h | 336 | ||||
-rw-r--r-- | verbly/verb.h | 67 | ||||
-rw-r--r-- | verbly/verbly.h | 10 |
9 files changed, 809 insertions, 149 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt index a754833..054c972 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt | |||
@@ -5,7 +5,10 @@ add_subdirectory(vendor/twitcurl/libtwitcurl) | |||
5 | 5 | ||
6 | find_package(PkgConfig) | 6 | find_package(PkgConfig) |
7 | pkg_check_modules(YamlCpp yaml-cpp REQUIRED) | 7 | pkg_check_modules(YamlCpp yaml-cpp REQUIRED) |
8 | pkg_check_modules(sqlite3 sqlite3 REQUIRED) | ||
8 | 9 | ||
9 | include_directories(vendor/twitcurl/libtwitcurl) | 10 | include_directories(vendor/twitcurl/libtwitcurl ${sqlite3_INCLUDE_DIR}) |
10 | add_executable(furries furries.cpp) | 11 | add_executable(furries furries.cpp) |
11 | target_link_libraries(furries ${YamlCpp_LIBRARIES} mysqlclient twitcurl curl) | 12 | set_property(TARGET furries PROPERTY CXX_STANDARD 11) |
13 | set_property(TARGET furries PROPERTY CXX_STANDARD_REQUIRED ON) | ||
14 | target_link_libraries(furries ${sqlite3_LIBRARIES} ${YamlCpp_LIBRARIES} twitcurl curl) | ||
diff --git a/furries.cpp b/furries.cpp index 15c4657..2d0fb23 100644 --- a/furries.cpp +++ b/furries.cpp | |||
@@ -1,16 +1,85 @@ | |||
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> |
7 | #include "verbly/verbly.h" | ||
8 | 8 | ||
9 | int db_error(MYSQL* driver, const char* error) | 9 | class fill_blanks { |
10 | { | 10 | private: |
11 | std::cout << error << ": " << mysql_error(driver) << std::endl; | 11 | verbly::data& database; |
12 | return 1; | 12 | |
13 | } | 13 | public: |
14 | fill_blanks(verbly::data& database) : database(database) | ||
15 | { | ||
16 | |||
17 | } | ||
18 | |||
19 | void visit(std::unique_ptr<verbly::token>& it) | ||
20 | { | ||
21 | switch (it->token_type()) | ||
22 | { | ||
23 | case verbly::type::utterance: | ||
24 | { | ||
25 | auto& action = *dynamic_cast<verbly::utterance_token*>(it.get()); | ||
26 | for (auto& tkn : action) | ||
27 | { | ||
28 | if (!tkn->complete()) | ||
29 | { | ||
30 | visit(tkn); | ||
31 | |||
32 | break; | ||
33 | } | ||
34 | } | ||
35 | |||
36 | break; | ||
37 | } | ||
38 | |||
39 | case verbly::type::fillin: | ||
40 | { | ||
41 | auto& tkn = *dynamic_cast<verbly::fillin_token*>(it.get()); | ||
42 | switch (tkn.fillin_type()) | ||
43 | { | ||
44 | case verbly::fillin_type::participle_phrase: | ||
45 | { | ||
46 | const verbly::verb& v = database.verbs().random(true).limit(1).run().front(); | ||
47 | /*verbly::utterance_token phrase = verbly::random(v.frames).make_utterance(); | ||
48 | while (std::begin(phrase)->token_type() != verbly::type::verb) | ||
49 | { | ||
50 | phrase.erase(std::begin(phrase)); | ||
51 | } | ||
52 | |||
53 | *std::begin(phrase) = verbly::verb_token(v).conjugate(verbly::conjugation::present_participle); | ||
54 | *it = phrase;*/ | ||
55 | auto avt = std::make_unique<verbly::verb_token>(v); | ||
56 | avt->conjugate(verbly::conjugation::present_participle); | ||
57 | it = std::move(avt); | ||
58 | |||
59 | break; | ||
60 | } | ||
61 | |||
62 | case verbly::fillin_type::adjective: | ||
63 | { | ||
64 | const verbly::adjective& adj = database.adjectives().random(true).limit(1).run().front(); | ||
65 | it = std::make_unique<verbly::string_token>(adj.value); | ||
66 | |||
67 | break; | ||
68 | } | ||
69 | |||
70 | default: | ||
71 | { | ||
72 | it = std::make_unique<verbly::string_token>("*the reality of the situation*"); | ||
73 | |||
74 | break; | ||
75 | } | ||
76 | } | ||
77 | |||
78 | break; | ||
79 | } | ||
80 | } | ||
81 | } | ||
82 | }; | ||
14 | 83 | ||
15 | int main(int argc, char** argv) | 84 | int main(int argc, char** argv) |
16 | { | 85 | { |
@@ -21,13 +90,7 @@ int main(int argc, char** argv) | |||
21 | const char* user = config["user"].as<std::string>().c_str(); | 90 | const char* user = config["user"].as<std::string>().c_str(); |
22 | const char* pass = config["pass"].as<std::string>().c_str(); | 91 | const char* pass = config["pass"].as<std::string>().c_str(); |
23 | const char* db = config["db"].as<std::string>().c_str(); | 92 | const char* db = config["db"].as<std::string>().c_str(); |
24 | 93 | ||
25 | MYSQL* driver = mysql_init(NULL); | ||
26 | if (!mysql_real_connect(driver, host, user, pass, db, 0, NULL, 0)) | ||
27 | { | ||
28 | return db_error(driver, "Error connecting to database"); | ||
29 | } | ||
30 | |||
31 | twitCurl twitter; | 94 | twitCurl twitter; |
32 | twitter.getOAuth().setConsumerKey(config["consumer_key"].as<std::string>()); | 95 | twitter.getOAuth().setConsumerKey(config["consumer_key"].as<std::string>()); |
33 | twitter.getOAuth().setConsumerSecret(config["consumer_secret"].as<std::string>()); | 96 | twitter.getOAuth().setConsumerSecret(config["consumer_secret"].as<std::string>()); |
@@ -38,141 +101,29 @@ int main(int argc, char** argv) | |||
38 | { | 101 | { |
39 | std::cout << "Generating tweet" << std::endl; | 102 | std::cout << "Generating tweet" << std::endl; |
40 | 103 | ||
41 | std::stringstream output; | 104 | std::vector<verbly::utterance_token> forms; |
42 | output << "the furries are "; | 105 | forms.push_back({ |
43 | 106 | new verbly::string_token("the furries are"), | |
44 | //if (rand() % 2 == 0) | 107 | new verbly::fillin_token(verbly::fillin_type::participle_phrase) |
45 | { | 108 | }); |
46 | // Adverb(s) + Adjective | 109 | forms.push_back({ |
47 | while (rand() % 4 == 0) | 110 | new verbly::string_token("the furries are"), |
48 | { | 111 | new verbly::fillin_token(verbly::fillin_type::adjective) |
49 | 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"; | 112 | }); |
50 | if (mysql_query(driver, getadverb)) return db_error(driver, "Query failed"); | ||
51 | MYSQL_RES* getadverb2 = mysql_use_result(driver); if (getadverb2 == NULL) return db_error(driver, "Query failed"); | ||
52 | MYSQL_ROW getadverb3 = mysql_fetch_row(getadverb2); if (getadverb3 == NULL) return db_error(driver, "Query failed"); | ||
53 | output << getadverb3[0] << " "; | ||
54 | mysql_free_result(getadverb2); | ||
55 | } | ||
56 | |||
57 | const char* getword = "SELECT word FROM wn_synset WHERE ss_type = 'a' OR ss_type = 's' ORDER BY RAND() LIMIT 1"; | ||
58 | if (mysql_query(driver, getword)) return db_error(driver, "Query failed"); | ||
59 | MYSQL_RES* getword2 = mysql_use_result(driver); if (getword2 == NULL) return db_error(driver, "Query failed"); | ||
60 | MYSQL_ROW getword3 = mysql_fetch_row(getword2); if (getword3 == NULL) return db_error(driver, "Query failed"); | ||
61 | output << getword3[0]; | ||
62 | mysql_free_result(getword2); | ||
63 | } /*else { | ||
64 | // Verb phrase | ||
65 | const char* getword = "SELECT word FROM wn_synset WHERE ss_type = 'a ' OR ss_type = 's' ORDER BY RAND() LIMIT 1"; | ||
66 | if (mysql_query(driver, getword)) return db_error(driver, "Query failed"); | ||
67 | MYSQL_RES* getword2 = mysql_use_result(driver); if (getword2 == NULL) return db_error(driver, "Query failed"); | ||
68 | MYSQL_ROW getword3 = mysql_fetch_row(getword2); if (getword3 == NULL) return db_error(driver, "Query failed"); | ||
69 | } | ||
70 | 113 | ||
71 | 114 | verbly::data database {"data.sqlite3"}; | |
72 | 115 | fill_blanks yeah {database}; | |
73 | 116 | std::unique_ptr<verbly::token> action = std::make_unique<verbly::utterance_token>(forms[rand() % forms.size()]); | |
74 | if (rand() % 2 == 0) | 117 | while (!action->complete()) |
75 | { | 118 | { |
76 | std::stringstream ispart; | 119 | yeah.visit(action); |
77 | 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; | ||
78 | if (mysql_query(driver, ispart.str().c_str())) return db_error(driver, "Query failed"); | ||
79 | MYSQL_RES* ispart2 = mysql_use_result(driver); if (ispart2 == NULL) return db_error(driver, "Query failed"); | ||
80 | MYSQL_ROW ispart3 = mysql_fetch_row(ispart2); | ||
81 | mysql_free_result(ispart2); | ||
82 | |||
83 | if (ispart3 != NULL) | ||
84 | { | ||
85 | int frame = atoi(ispart3[0]); | ||
86 | std::cout << "frame " << frame << std::endl; | ||
87 | |||
88 | if (frame == 4 || frame == 22) | ||
89 | { | ||
90 | // the furries are ----ing *prepositional phrase* | ||
91 | // ex: the furries are laughing over there | ||
92 | } else if (frame == 5) | ||
93 | { | ||
94 | // the furries are ----ing something *adj/n* | ||
95 | // ex: the furries are regarding life inconsequential | ||
96 | } else if (frame == 6 || frame == 7) | ||
97 | { | ||
98 | // the furries are ----ing *adj/n* | ||
99 | // ex: the furries are turning gay | ||
100 | } else if (frame == 8 || frame == 9 || frame == 10 || frame == 11) | ||
101 | { | ||
102 | // the furries are ----ing something/somebody | ||
103 | // ex: the furries are holding hostages | ||
104 | } else if (frame == 12 || frame == 27) | ||
105 | { | ||
106 | // the furries are ----ing to somebody | ||
107 | // ex: the furries are appealing to God | ||
108 | } else if (frame == 13) | ||
109 | { | ||
110 | // the furries are ----ing on something | ||
111 | // ex: the furries are lecturing on heterosexuality | ||
112 | } else if (frame == 14) | ||
113 | { | ||
114 | // the furries are ----ing somebody something | ||
115 | // ex: the furries are reading your mom the menu | ||
116 | } else if (frame == 15) | ||
117 | { | ||
118 | // the furries are ----ing something to somebody | ||
119 | // ex: the furries are pitching a product to Apple | ||
120 | } else if (frame == 16) | ||
121 | { | ||
122 | // the furries are ----ing something from somebody | ||
123 | // ex: the furries are separating your money from you | ||
124 | } else if (frame == 17) | ||
125 | { | ||
126 | // the furries are ----ing somebody with something | ||
127 | // ex: the furries are injecting me with solemnity | ||
128 | } else if (frame == 18) | ||
129 | { | ||
130 | // the furries are ----ing somebody of something | ||
131 | // ex: the furries are depriving me of hope | ||
132 | } else if (frame == 19) | ||
133 | { | ||
134 | // the furries are ----ing something on somebody | ||
135 | // ex: the furries are forcing pervision on us | ||
136 | } else if (frame == 20 || frame == 21) | ||
137 | { | ||
138 | // the furries are ----ing somebody/something *prepositional phrase* | ||
139 | // ex: the furries are leaving us behind | ||
140 | } else if (frame == 24) | ||
141 | { | ||
142 | // the furries are ----ing somebody to INF | ||
143 | // ex: the furries are getting us to eat | ||
144 | } else if (frame == 25) | ||
145 | { | ||
146 | // the furries are ----ing somebody INF | ||
147 | // ex: the furries are making us procreate | ||
148 | } else if (frame == 26) | ||
149 | { | ||
150 | // the furries are ----ing that CLAUSE | ||
151 | // ex: the furries are understaing that life is precious | ||
152 | } else if (frame == 28) | ||
153 | { | ||
154 | // the furries are ----ing to INF | ||
155 | // ex: the furries are beginning to understand | ||
156 | } else if (frame == 29) | ||
157 | { | ||
158 | // the furries are ----ing whether to INF | ||
159 | // ex: the furries are deciding whether to hallucinate | ||
160 | } else if (frame == 30) | ||
161 | { | ||
162 | // the furries are ----ing somebody into V-ing something | ||
163 | // ex: the furries are tempting me into consecrating a magnet | ||
164 | } else if (frame == 31) | ||
165 | { | ||
166 | // the furries are ----ing something with something | ||
167 | // ex: the furries are replacing existence with solidarity | ||
168 | } | ||
169 | } | ||
170 | } | 120 | } |
171 | */ | ||
172 | 121 | ||
173 | std::string result = output.str(); | 122 | std::string result = action->compile(); |
174 | result.resize(140); | 123 | result.resize(140); |
175 | 124 | ||
125 | std::cout << result << std::endl; | ||
126 | /* | ||
176 | std::string replyMsg; | 127 | std::string replyMsg; |
177 | if (twitter.statusUpdate(result)) | 128 | if (twitter.statusUpdate(result)) |
178 | { | 129 | { |
@@ -181,11 +132,9 @@ int main(int argc, char** argv) | |||
181 | } else { | 132 | } else { |
182 | twitter.getLastCurlError(replyMsg); | 133 | twitter.getLastCurlError(replyMsg); |
183 | std::cout << "Curl error: " << replyMsg << std::endl; | 134 | std::cout << "Curl error: " << replyMsg << std::endl; |
184 | } | 135 | }*/ |
185 | 136 | ||
186 | std::cout << "Waiting" << std::endl; | 137 | std::cout << "Waiting" << std::endl; |
187 | sleep(60 * 60 * 3); | 138 | sleep(/*60 * 60 * */ 3); |
188 | } | 139 | } |
189 | |||
190 | mysql_close(driver); | ||
191 | } | 140 | } |
diff --git a/schema.sql b/schema.sql new file mode 100644 index 0000000..7c1b52c --- /dev/null +++ b/schema.sql | |||
@@ -0,0 +1,38 @@ | |||
1 | DROP TABLE IF EXISTS `verbs`; | ||
2 | CREATE TABLE `verbs` ( | ||
3 | `verb_id` INTEGER PRIMARY KEY, | ||
4 | `infinitive` VARCHAR(32) NOT NULL, | ||
5 | `past_tense` VARCHAR(32) NOT NULL, | ||
6 | `past_participle` VARCHAR(32) NOT NULL, | ||
7 | `ing_form` VARCHAR(32) NOT NULL, | ||
8 | `s_form` VARCHAR(32) NOT NULL | ||
9 | ); | ||
10 | |||
11 | DROP TABLE IF EXISTS `groups`; | ||
12 | CREATE TABLE `groups` ( | ||
13 | `group_id` INTEGER PRIMARY KEY, | ||
14 | `parent_id` INTEGER, | ||
15 | FOREIGN KEY (`parent_id`) REFERENCES `groups`(`group_id`) | ||
16 | ); | ||
17 | |||
18 | DROP TABLE IF EXISTS `frames`; | ||
19 | CREATE TABLE `frames` ( | ||
20 | `frame_id` INTEGER PRIMARY KEY, | ||
21 | `group_id` INTEGER NOT NULL, | ||
22 | `data` BLOB NOT NULL, | ||
23 | FOREIGN KEY (`group_id`) REFERENCES `groups`(`group_id`) | ||
24 | ); | ||
25 | |||
26 | DROP TABLE IF EXISTS `verb_groups`; | ||
27 | CREATE TABLE `verb_groups` ( | ||
28 | `verb_id` INTEGER NOT NULL, | ||
29 | `group_id` INTEGER NOT NULL, | ||
30 | FOREIGN KEY (`verb_id`) REFERENCES `verbs`(`verb_id`), | ||
31 | FOREIGN KEY (`group_id`) REFERENCES `groups`(`group_id`) | ||
32 | ); | ||
33 | |||
34 | DROP TABLE IF EXISTS `adjectives`; | ||
35 | CREATE TABLE `adjectives` ( | ||
36 | `adjective_id` INTEGER PRIMARY KEY, | ||
37 | `adjective` VARCHAR(32) NOT NULL | ||
38 | ); \ No newline at end of file | ||
diff --git a/verbly/adjective.h b/verbly/adjective.h new file mode 100644 index 0000000..58c490e --- /dev/null +++ b/verbly/adjective.h | |||
@@ -0,0 +1,21 @@ | |||
1 | #ifndef ADJECTIVE_H_87B3FB75 | ||
2 | #define ADJECTIVE_H_87B3FB75 | ||
3 | |||
4 | namespace verbly { | ||
5 | |||
6 | class adjective { | ||
7 | private: | ||
8 | int id; | ||
9 | |||
10 | public: | ||
11 | std::string value; | ||
12 | |||
13 | adjective(int id) : id(id) | ||
14 | { | ||
15 | |||
16 | } | ||
17 | }; | ||
18 | |||
19 | }; | ||
20 | |||
21 | #endif /* end of include guard: ADJECTIVE_H_87B3FB75 */ | ||
diff --git a/verbly/c++14.h b/verbly/c++14.h new file mode 100644 index 0000000..b3efbe2 --- /dev/null +++ b/verbly/c++14.h | |||
@@ -0,0 +1,35 @@ | |||
1 | #include <cstddef> | ||
2 | #include <memory> | ||
3 | #include <type_traits> | ||
4 | #include <utility> | ||
5 | |||
6 | namespace std { | ||
7 | template<class T> struct _Unique_if { | ||
8 | typedef unique_ptr<T> _Single_object; | ||
9 | }; | ||
10 | |||
11 | template<class T> struct _Unique_if<T[]> { | ||
12 | typedef unique_ptr<T[]> _Unknown_bound; | ||
13 | }; | ||
14 | |||
15 | template<class T, size_t N> struct _Unique_if<T[N]> { | ||
16 | typedef void _Known_bound; | ||
17 | }; | ||
18 | |||
19 | template<class T, class... Args> | ||
20 | typename _Unique_if<T>::_Single_object | ||
21 | make_unique(Args&&... args) { | ||
22 | return unique_ptr<T>(new T(std::forward<Args>(args)...)); | ||
23 | } | ||
24 | |||
25 | template<class T> | ||
26 | typename _Unique_if<T>::_Unknown_bound | ||
27 | make_unique(size_t n) { | ||
28 | typedef typename remove_extent<T>::type U; | ||
29 | return unique_ptr<T>(new U[n]()); | ||
30 | } | ||
31 | |||
32 | template<class T, class... Args> | ||
33 | typename _Unique_if<T>::_Known_bound | ||
34 | make_unique(Args&&...) = delete; | ||
35 | } | ||
diff --git a/verbly/data.h b/verbly/data.h new file mode 100644 index 0000000..2c23c15 --- /dev/null +++ b/verbly/data.h | |||
@@ -0,0 +1,201 @@ | |||
1 | #ifndef DATA_H_C4AEC3DD | ||
2 | #define DATA_H_C4AEC3DD | ||
3 | |||
4 | #include "verb.h" | ||
5 | #include <sqlite3.h> | ||
6 | #include <stdexcept> | ||
7 | |||
8 | namespace verbly { | ||
9 | |||
10 | class data { | ||
11 | private: | ||
12 | sqlite3* ppdb; | ||
13 | |||
14 | public: | ||
15 | class verb_query { | ||
16 | public: | ||
17 | const static int unlimited = -1; | ||
18 | |||
19 | private: | ||
20 | const data& database; | ||
21 | int m_limit = unlimited; | ||
22 | bool m_random = false; | ||
23 | |||
24 | public: | ||
25 | verb_query(const data& database) : database(database) | ||
26 | { | ||
27 | |||
28 | } | ||
29 | |||
30 | verb_query& limit(int m_limit) | ||
31 | { | ||
32 | if ((m_limit > 0) || (m_limit == unlimited)) | ||
33 | { | ||
34 | this->m_limit = m_limit; | ||
35 | } | ||
36 | |||
37 | return *this; | ||
38 | } | ||
39 | |||
40 | verb_query& random(bool m_random) | ||
41 | { | ||
42 | this->m_random = m_random; | ||
43 | |||
44 | return *this; | ||
45 | } | ||
46 | |||
47 | std::list<verb> run() const | ||
48 | { | ||
49 | std::stringstream construct; | ||
50 | construct << "SELECT verb_id, infinitive, past_tense, past_participle, ing_form, s_form FROM verbs"; | ||
51 | |||
52 | if (m_random) | ||
53 | { | ||
54 | construct << " ORDER BY RANDOM()"; | ||
55 | } | ||
56 | |||
57 | if (m_limit != unlimited) | ||
58 | { | ||
59 | construct << " LIMIT " << m_limit; | ||
60 | } | ||
61 | |||
62 | sqlite3_stmt* ppstmt; | ||
63 | std::string query = construct.str(); | ||
64 | if (sqlite3_prepare_v2(database.ppdb, query.c_str(), query.length(), &ppstmt, NULL) != SQLITE_OK) | ||
65 | { | ||
66 | throw std::runtime_error(sqlite3_errmsg(database.ppdb)); | ||
67 | } | ||
68 | |||
69 | std::list<verb> output; | ||
70 | while (sqlite3_step(ppstmt) == SQLITE_ROW) | ||
71 | { | ||
72 | verb tnc {sqlite3_column_int(ppstmt, 0)}; | ||
73 | tnc.infinitive = std::string(reinterpret_cast<const char*>(sqlite3_column_text(ppstmt, 1))); | ||
74 | tnc.past_tense = std::string(reinterpret_cast<const char*>(sqlite3_column_text(ppstmt, 2))); | ||
75 | tnc.past_participle = std::string(reinterpret_cast<const char*>(sqlite3_column_text(ppstmt, 3))); | ||
76 | tnc.ing_form = std::string(reinterpret_cast<const char*>(sqlite3_column_text(ppstmt, 4))); | ||
77 | tnc.s_form = std::string(reinterpret_cast<const char*>(sqlite3_column_text(ppstmt, 5))); | ||
78 | |||
79 | output.push_back(tnc); | ||
80 | } | ||
81 | |||
82 | sqlite3_finalize(ppstmt); | ||
83 | |||
84 | return output; | ||
85 | } | ||
86 | |||
87 | }; | ||
88 | |||
89 | class adjective_query { | ||
90 | public: | ||
91 | const static int unlimited = -1; | ||
92 | |||
93 | private: | ||
94 | const data& database; | ||
95 | int m_limit = unlimited; | ||
96 | bool m_random = false; | ||
97 | |||
98 | public: | ||
99 | adjective_query(const data& database) : database(database) | ||
100 | { | ||
101 | |||
102 | } | ||
103 | |||
104 | adjective_query& limit(int m_limit) | ||
105 | { | ||
106 | if ((m_limit > 0) || (m_limit == unlimited)) | ||
107 | { | ||
108 | this->m_limit = m_limit; | ||
109 | } | ||
110 | |||
111 | return *this; | ||
112 | } | ||
113 | |||
114 | adjective_query& random(bool m_random) | ||
115 | { | ||
116 | this->m_random = m_random; | ||
117 | |||
118 | return *this; | ||
119 | } | ||
120 | |||
121 | std::list<adjective> run() const | ||
122 | { | ||
123 | std::stringstream construct; | ||
124 | construct << "SELECT adjective_id, adjective FROM adjectives"; | ||
125 | |||
126 | if (m_random) | ||
127 | { | ||
128 | construct << " ORDER BY RANDOM()"; | ||
129 | } | ||
130 | |||
131 | if (m_limit != unlimited) | ||
132 | { | ||
133 | construct << " LIMIT " << m_limit; | ||
134 | } | ||
135 | |||
136 | sqlite3_stmt* ppstmt; | ||
137 | std::string query = construct.str(); | ||
138 | if (sqlite3_prepare_v2(database.ppdb, query.c_str(), query.length(), &ppstmt, NULL) != SQLITE_OK) | ||
139 | { | ||
140 | throw std::runtime_error(sqlite3_errmsg(database.ppdb)); | ||
141 | } | ||
142 | |||
143 | std::list<adjective> output; | ||
144 | while (sqlite3_step(ppstmt) == SQLITE_ROW) | ||
145 | { | ||
146 | adjective tnc {sqlite3_column_int(ppstmt, 0)}; | ||
147 | tnc.value = std::string(reinterpret_cast<const char*>(sqlite3_column_text(ppstmt, 1))); | ||
148 | |||
149 | output.push_back(tnc); | ||
150 | } | ||
151 | |||
152 | sqlite3_finalize(ppstmt); | ||
153 | |||
154 | return output; | ||
155 | } | ||
156 | |||
157 | }; | ||
158 | |||
159 | data(std::string datafile) | ||
160 | { | ||
161 | if (sqlite3_open_v2(datafile.c_str(), &ppdb, SQLITE_OPEN_READONLY, NULL) != SQLITE_OK) | ||
162 | { | ||
163 | throw std::invalid_argument(sqlite3_errmsg(ppdb)); | ||
164 | } | ||
165 | } | ||
166 | |||
167 | data(const data& other) = delete; | ||
168 | data& operator=(const data& other) = delete; | ||
169 | |||
170 | data(data&& other) | ||
171 | { | ||
172 | ppdb = other.ppdb; | ||
173 | } | ||
174 | |||
175 | data& operator=(data&& other) | ||
176 | { | ||
177 | ppdb = other.ppdb; | ||
178 | |||
179 | return *this; | ||
180 | } | ||
181 | |||
182 | ~data() | ||
183 | { | ||
184 | sqlite3_close_v2(ppdb); | ||
185 | } | ||
186 | |||
187 | verb_query verbs() const | ||
188 | { | ||
189 | return verb_query(*this); | ||
190 | } | ||
191 | |||
192 | adjective_query adjectives() const | ||
193 | { | ||
194 | return adjective_query(*this); | ||
195 | } | ||
196 | |||
197 | }; | ||
198 | |||
199 | }; | ||
200 | |||
201 | #endif /* end of include guard: DATA_H_C4AEC3DD */ | ||
diff --git a/verbly/token.h b/verbly/token.h new file mode 100644 index 0000000..bbe7c2d --- /dev/null +++ b/verbly/token.h | |||
@@ -0,0 +1,336 @@ | |||
1 | #ifndef TOKEN_H_AD62C505 | ||
2 | #define TOKEN_H_AD62C505 | ||
3 | |||
4 | #include <string> | ||
5 | #include <list> | ||
6 | #include <sstream> | ||
7 | #include "verb.h" | ||
8 | |||
9 | namespace verbly { | ||
10 | |||
11 | enum class type { | ||
12 | verb, | ||
13 | fillin, | ||
14 | string, | ||
15 | utterance | ||
16 | }; | ||
17 | |||
18 | class selrestr { | ||
19 | }; | ||
20 | |||
21 | class synrestr { | ||
22 | }; | ||
23 | |||
24 | enum class fillin_type { | ||
25 | noun_phrase, | ||
26 | participle_phrase, | ||
27 | adjective | ||
28 | }; | ||
29 | |||
30 | class token { | ||
31 | protected: | ||
32 | // General | ||
33 | type type; | ||
34 | |||
35 | token(enum type type) : type(type) | ||
36 | { | ||
37 | |||
38 | } | ||
39 | |||
40 | public: | ||
41 | enum type token_type() const | ||
42 | { | ||
43 | return type; | ||
44 | } | ||
45 | |||
46 | virtual bool complete() const = 0; | ||
47 | virtual std::string compile() const = 0; | ||
48 | virtual token* copy() const = 0; | ||
49 | }; | ||
50 | |||
51 | class verb_token : public token { | ||
52 | private: | ||
53 | // Verb | ||
54 | const verb* m_verb; | ||
55 | conjugation verb_infl = conjugation::infinitive; | ||
56 | |||
57 | public: | ||
58 | verb_token(const class verb& verb) : token(type::verb), m_verb(&verb) | ||
59 | { | ||
60 | |||
61 | } | ||
62 | |||
63 | const class verb& verb() const | ||
64 | { | ||
65 | return *m_verb; | ||
66 | } | ||
67 | |||
68 | verb_token& conjugate(conjugation infl) | ||
69 | { | ||
70 | verb_infl = infl; | ||
71 | return *this; | ||
72 | } | ||
73 | |||
74 | bool complete() const | ||
75 | { | ||
76 | return true; | ||
77 | } | ||
78 | |||
79 | std::string compile() const | ||
80 | { | ||
81 | return m_verb->conjugate(verb_infl); | ||
82 | } | ||
83 | |||
84 | token* copy() const | ||
85 | { | ||
86 | return new verb_token(*this); | ||
87 | } | ||
88 | }; | ||
89 | |||
90 | class utterance_token : public token { | ||
91 | private: | ||
92 | // Utterance | ||
93 | std::list<std::unique_ptr<token>> utterance; | ||
94 | |||
95 | public: | ||
96 | typedef std::list<std::unique_ptr<token>>::iterator iterator; | ||
97 | /*class iterator { | ||
98 | private: | ||
99 | friend class utterance_token; | ||
100 | |||
101 | std::list<std::unique_ptr<token>>::iterator it; | ||
102 | |||
103 | public: | ||
104 | iterator(std::list<std::unique_ptr<token>>::iterator it) : it(it) | ||
105 | { | ||
106 | |||
107 | } | ||
108 | |||
109 | iterator& operator++() | ||
110 | { | ||
111 | ++it; | ||
112 | return *this; | ||
113 | } | ||
114 | |||
115 | iterator& operator--() | ||
116 | { | ||
117 | --it; | ||
118 | return *this; | ||
119 | } | ||
120 | |||
121 | bool operator==(const iterator& other) const | ||
122 | { | ||
123 | return it == other.it; | ||
124 | } | ||
125 | |||
126 | bool operator!=(const iterator& other) const | ||
127 | { | ||
128 | return it != other.it; | ||
129 | } | ||
130 | |||
131 | token* operator*() | ||
132 | { | ||
133 | return *it->get(); | ||
134 | } | ||
135 | |||
136 | token* operator->() | ||
137 | { | ||
138 | return *it->get(); | ||
139 | } | ||
140 | };*/ | ||
141 | |||
142 | utterance_token(std::initializer_list<token*> tkns) : token(type::utterance) | ||
143 | { | ||
144 | for (auto tkn : tkns) | ||
145 | { | ||
146 | utterance.push_back(std::unique_ptr<token>(tkn)); | ||
147 | } | ||
148 | } | ||
149 | |||
150 | utterance_token(const utterance_token& other) : token(type::utterance) | ||
151 | { | ||
152 | for (auto& tkn : other.utterance) | ||
153 | { | ||
154 | utterance.push_back(std::unique_ptr<token>(tkn->copy())); | ||
155 | } | ||
156 | } | ||
157 | |||
158 | utterance_token(utterance_token&& other) : token(type::utterance), utterance(std::move(other.utterance)) | ||
159 | { | ||
160 | |||
161 | } | ||
162 | |||
163 | utterance_token& operator=(const utterance_token& other) | ||
164 | { | ||
165 | utterance.clear(); | ||
166 | |||
167 | for (auto& tkn : other.utterance) | ||
168 | { | ||
169 | utterance.push_back(std::unique_ptr<token>(tkn->copy())); | ||
170 | } | ||
171 | |||
172 | return *this; | ||
173 | } | ||
174 | |||
175 | utterance_token& operator=(utterance_token&& other) | ||
176 | { | ||
177 | utterance = std::move(other.utterance); | ||
178 | |||
179 | return *this; | ||
180 | } | ||
181 | |||
182 | iterator begin() | ||
183 | { | ||
184 | return std::begin(utterance); | ||
185 | } | ||
186 | |||
187 | iterator end() | ||
188 | { | ||
189 | return std::end(utterance); | ||
190 | } | ||
191 | |||
192 | const iterator begin() const | ||
193 | { | ||
194 | return std::begin(utterance); | ||
195 | } | ||
196 | |||
197 | const iterator end() const | ||
198 | { | ||
199 | return std::end(utterance); | ||
200 | } | ||
201 | |||
202 | void erase(iterator it) | ||
203 | { | ||
204 | utterance.erase(it); | ||
205 | } | ||
206 | |||
207 | bool complete() const | ||
208 | { | ||
209 | return std::all_of(std::begin(utterance), std::end(utterance), [] (const std::unique_ptr<token>& tkn) { | ||
210 | return tkn->complete(); | ||
211 | }); | ||
212 | } | ||
213 | |||
214 | std::string compile() const | ||
215 | { | ||
216 | std::stringstream result; | ||
217 | for (auto& t : utterance) | ||
218 | { | ||
219 | if (t->complete()) | ||
220 | { | ||
221 | result << t->compile() << " "; | ||
222 | } else { | ||
223 | return "Could not compile!"; | ||
224 | } | ||
225 | } | ||
226 | |||
227 | std::string output = result.str(); | ||
228 | if (output != "") | ||
229 | { | ||
230 | output.pop_back(); | ||
231 | } | ||
232 | |||
233 | return output; | ||
234 | } | ||
235 | |||
236 | token* copy() const | ||
237 | { | ||
238 | return new utterance_token(*this); | ||
239 | } | ||
240 | }; | ||
241 | |||
242 | class fillin_token : public token { | ||
243 | private: | ||
244 | // Fillin | ||
245 | std::string m_theme; | ||
246 | fillin_type m_fillin_type; | ||
247 | |||
248 | public: | ||
249 | fillin_token(fillin_type ft) : token(type::fillin), m_fillin_type(ft) | ||
250 | { | ||
251 | |||
252 | } | ||
253 | |||
254 | /* void synrestrs(std::initializer_list<synrestr> ins) | ||
255 | { | ||
256 | m_synrestrs = std::set<synrestr>(ins); | ||
257 | } | ||
258 | |||
259 | std::set<synrestr>& synrestrs() | ||
260 | { | ||
261 | return m_synrestrs; | ||
262 | } | ||
263 | |||
264 | void selrestrs(std::initializer_list<selrestr> ins) | ||
265 | { | ||
266 | m_selrestrs = std::set<selrestr>(ins); | ||
267 | } | ||
268 | |||
269 | std::set<selrestr>& selrestrs() | ||
270 | { | ||
271 | return m_selrestrs; | ||
272 | }*/ | ||
273 | |||
274 | fillin_token theme(std::string theme) | ||
275 | { | ||
276 | m_theme = theme; | ||
277 | |||
278 | return *this; | ||
279 | } | ||
280 | |||
281 | std::string& theme() | ||
282 | { | ||
283 | return m_theme; | ||
284 | } | ||
285 | |||
286 | fillin_type fillin_type() const | ||
287 | { | ||
288 | return m_fillin_type; | ||
289 | } | ||
290 | |||
291 | bool complete() const | ||
292 | { | ||
293 | return false; | ||
294 | } | ||
295 | |||
296 | std::string compile() const | ||
297 | { | ||
298 | return ""; | ||
299 | } | ||
300 | |||
301 | token* copy() const | ||
302 | { | ||
303 | return new fillin_token(*this); | ||
304 | } | ||
305 | }; | ||
306 | |||
307 | class string_token : public token { | ||
308 | private: | ||
309 | // String | ||
310 | std::string str; | ||
311 | |||
312 | public: | ||
313 | string_token(std::string str) : token(type::string), str(str) | ||
314 | { | ||
315 | |||
316 | } | ||
317 | |||
318 | bool complete() const | ||
319 | { | ||
320 | return true; | ||
321 | } | ||
322 | |||
323 | std::string compile() const | ||
324 | { | ||
325 | return str; | ||
326 | } | ||
327 | |||
328 | token* copy() const | ||
329 | { | ||
330 | return new string_token(*this); | ||
331 | } | ||
332 | }; | ||
333 | |||
334 | }; | ||
335 | |||
336 | #endif /* end of include guard: TOKEN_H_AD62C505 */ | ||
diff --git a/verbly/verb.h b/verbly/verb.h new file mode 100644 index 0000000..42c8dc2 --- /dev/null +++ b/verbly/verb.h | |||
@@ -0,0 +1,67 @@ | |||
1 | #ifndef VERB_H_BCC929AD | ||
2 | #define VERB_H_BCC929AD | ||
3 | |||
4 | #include <vector> | ||
5 | |||
6 | namespace verbly { | ||
7 | |||
8 | /*class frame_part { | ||
9 | |||
10 | }; | ||
11 | |||
12 | class frame { | ||
13 | private: | ||
14 | std::list<frame_part> content; | ||
15 | std::map<std::string, std::vector<std::list<frame_part>::iterator>> predicates; | ||
16 | |||
17 | public: | ||
18 | frame(std::list<frame_part> content) : content(content) | ||
19 | { | ||
20 | |||
21 | } | ||
22 | |||
23 | std::unique_ptr<token> make_utterance() const | ||
24 | { | ||
25 | |||
26 | } | ||
27 | };*/ | ||
28 | |||
29 | enum class conjugation { | ||
30 | present_participle, | ||
31 | past_participle, | ||
32 | infinitive | ||
33 | }; | ||
34 | |||
35 | class verb { | ||
36 | private: | ||
37 | int id; | ||
38 | |||
39 | public: | ||
40 | verb(int id) : id(id) | ||
41 | { | ||
42 | |||
43 | } | ||
44 | |||
45 | std::string infinitive; | ||
46 | std::string past_tense; | ||
47 | std::string past_participle; | ||
48 | std::string ing_form; | ||
49 | std::string s_form; | ||
50 | //std::vector<frame> frames; | ||
51 | |||
52 | std::string conjugate(conjugation infl) const | ||
53 | { | ||
54 | switch (infl) | ||
55 | { | ||
56 | case conjugation::infinitive: return infinitive; | ||
57 | case conjugation::past_participle: return past_participle; | ||
58 | case conjugation::present_participle: return ing_form; | ||
59 | } | ||
60 | } | ||
61 | }; | ||
62 | |||
63 | }; | ||
64 | |||
65 | #include "token.h" | ||
66 | |||
67 | #endif /* end of include guard: VERB_H_BCC929AD */ | ||
diff --git a/verbly/verbly.h b/verbly/verbly.h new file mode 100644 index 0000000..139d8f8 --- /dev/null +++ b/verbly/verbly.h | |||
@@ -0,0 +1,10 @@ | |||
1 | #ifndef VERBLY_H_5B39CE50 | ||
2 | #define VERBLY_H_5B39CE50 | ||
3 | |||
4 | #include "c++14.h" | ||
5 | #include "token.h" | ||
6 | #include "verb.h" | ||
7 | #include "adjective.h" | ||
8 | #include "data.h" | ||
9 | |||
10 | #endif /* end of include guard: VERBLY_H_5B39CE50 */ | ||