diff options
Diffstat (limited to 'verbly/lib/verb.cpp')
-rw-r--r-- | verbly/lib/verb.cpp | 193 |
1 files changed, 0 insertions, 193 deletions
diff --git a/verbly/lib/verb.cpp b/verbly/lib/verb.cpp deleted file mode 100644 index 23f7c92..0000000 --- a/verbly/lib/verb.cpp +++ /dev/null | |||
@@ -1,193 +0,0 @@ | |||
1 | #include "verbly.h" | ||
2 | |||
3 | namespace verbly { | ||
4 | |||
5 | verb::verb(const data& _data, int _id) : word(_data, _id) | ||
6 | { | ||
7 | |||
8 | } | ||
9 | |||
10 | std::string verb::base_form() const | ||
11 | { | ||
12 | return _infinitive; | ||
13 | } | ||
14 | |||
15 | std::string verb::infinitive_form() const | ||
16 | { | ||
17 | return _infinitive; | ||
18 | } | ||
19 | |||
20 | std::string verb::past_tense_form() const | ||
21 | { | ||
22 | return _past_tense; | ||
23 | } | ||
24 | |||
25 | std::string verb::past_participle_form() const | ||
26 | { | ||
27 | return _past_participle; | ||
28 | } | ||
29 | |||
30 | std::string verb::ing_form() const | ||
31 | { | ||
32 | return _ing_form; | ||
33 | } | ||
34 | |||
35 | std::string verb::s_form() const | ||
36 | { | ||
37 | return _s_form; | ||
38 | } | ||
39 | |||
40 | verb_query::verb_query(const data& _data) : _data(_data) | ||
41 | { | ||
42 | |||
43 | } | ||
44 | |||
45 | verb_query& verb_query::limit(int _limit) | ||
46 | { | ||
47 | if ((_limit > 0) || (_limit == unlimited)) | ||
48 | { | ||
49 | this->_limit = _limit; | ||
50 | } | ||
51 | |||
52 | return *this; | ||
53 | } | ||
54 | |||
55 | verb_query& verb_query::random(bool _random) | ||
56 | { | ||
57 | this->_random = _random; | ||
58 | |||
59 | return *this; | ||
60 | } | ||
61 | |||
62 | verb_query& verb_query::except(const verb& _word) | ||
63 | { | ||
64 | _except.push_back(_word); | ||
65 | |||
66 | return *this; | ||
67 | } | ||
68 | |||
69 | verb_query& verb_query::rhymes_with(const word& _word) | ||
70 | { | ||
71 | for (auto rhyme : _word.rhyme_phonemes()) | ||
72 | { | ||
73 | _rhymes.push_back(rhyme); | ||
74 | } | ||
75 | |||
76 | if (dynamic_cast<const verb*>(&_word) != nullptr) | ||
77 | { | ||
78 | _except.push_back(dynamic_cast<const verb&>(_word)); | ||
79 | } | ||
80 | |||
81 | return *this; | ||
82 | } | ||
83 | |||
84 | verb_query& verb_query::has_pronunciation(bool _has_prn) | ||
85 | { | ||
86 | this->_has_prn = _has_prn; | ||
87 | |||
88 | return *this; | ||
89 | } | ||
90 | |||
91 | std::list<verb> verb_query::run() const | ||
92 | { | ||
93 | std::stringstream construct; | ||
94 | construct << "SELECT verb_id, infinitive, past_tense, past_participle, ing_form, s_form FROM verbs"; | ||
95 | std::list<std::string> conditions; | ||
96 | |||
97 | if (_has_prn) | ||
98 | { | ||
99 | conditions.push_back("verb_id IN (SELECT verb_id FROM verb_pronunciations)"); | ||
100 | } | ||
101 | |||
102 | if (!_rhymes.empty()) | ||
103 | { | ||
104 | std::list<std::string> clauses(_rhymes.size(), "pronunciation LIKE @RHMPRN"); | ||
105 | std::string cond = "verb_id IN (SELECT verb_id FROM verb_pronunciations WHERE " + verbly::implode(std::begin(clauses), std::end(clauses), " OR ") + ")"; | ||
106 | conditions.push_back(cond); | ||
107 | } | ||
108 | |||
109 | for (auto except : _except) | ||
110 | { | ||
111 | conditions.push_back("verb_id != @EXCID"); | ||
112 | } | ||
113 | |||
114 | if (!conditions.empty()) | ||
115 | { | ||
116 | construct << " WHERE "; | ||
117 | construct << verbly::implode(std::begin(conditions), std::end(conditions), " AND "); | ||
118 | } | ||
119 | |||
120 | if (_random) | ||
121 | { | ||
122 | construct << " ORDER BY RANDOM()"; | ||
123 | } | ||
124 | |||
125 | if (_limit != unlimited) | ||
126 | { | ||
127 | construct << " LIMIT " << _limit; | ||
128 | } | ||
129 | |||
130 | sqlite3_stmt* ppstmt; | ||
131 | std::string query = construct.str(); | ||
132 | if (sqlite3_prepare_v2(_data.ppdb, query.c_str(), query.length(), &ppstmt, NULL) != SQLITE_OK) | ||
133 | { | ||
134 | throw std::runtime_error(sqlite3_errmsg(_data.ppdb)); | ||
135 | } | ||
136 | |||
137 | if (!_rhymes.empty()) | ||
138 | { | ||
139 | int i = 0; | ||
140 | for (auto rhyme : _rhymes) | ||
141 | { | ||
142 | std::string rhymer = "%" + rhyme; | ||
143 | sqlite3_bind_text(ppstmt, sqlite3_bind_parameter_index(ppstmt, "@RHMPRN"), rhymer.c_str(), rhymer.length(), SQLITE_STATIC); | ||
144 | |||
145 | i++; | ||
146 | } | ||
147 | } | ||
148 | |||
149 | for (auto except : _except) | ||
150 | { | ||
151 | sqlite3_bind_int(ppstmt, sqlite3_bind_parameter_index(ppstmt, "@EXCID"), except._id); | ||
152 | } | ||
153 | |||
154 | std::list<verb> output; | ||
155 | while (sqlite3_step(ppstmt) == SQLITE_ROW) | ||
156 | { | ||
157 | verb tnc {_data, sqlite3_column_int(ppstmt, 0)}; | ||
158 | tnc._infinitive = std::string(reinterpret_cast<const char*>(sqlite3_column_text(ppstmt, 1))); | ||
159 | tnc._past_tense = std::string(reinterpret_cast<const char*>(sqlite3_column_text(ppstmt, 2))); | ||
160 | tnc._past_participle = std::string(reinterpret_cast<const char*>(sqlite3_column_text(ppstmt, 3))); | ||
161 | tnc._ing_form = std::string(reinterpret_cast<const char*>(sqlite3_column_text(ppstmt, 4))); | ||
162 | tnc._s_form = std::string(reinterpret_cast<const char*>(sqlite3_column_text(ppstmt, 5))); | ||
163 | |||
164 | output.push_back(tnc); | ||
165 | } | ||
166 | |||
167 | sqlite3_finalize(ppstmt); | ||
168 | |||
169 | for (auto& verb : output) | ||
170 | { | ||
171 | query = "SELECT pronunciation FROM verb_pronunciations WHERE verb_id = ?"; | ||
172 | if (sqlite3_prepare_v2(_data.ppdb, query.c_str(), query.length(), &ppstmt, NULL) != SQLITE_OK) | ||
173 | { | ||
174 | throw std::runtime_error(sqlite3_errmsg(_data.ppdb)); | ||
175 | } | ||
176 | |||
177 | sqlite3_bind_int(ppstmt, 1, verb._id); | ||
178 | |||
179 | while (sqlite3_step(ppstmt) == SQLITE_ROW) | ||
180 | { | ||
181 | std::string pronunciation(reinterpret_cast<const char*>(sqlite3_column_text(ppstmt, 0))); | ||
182 | auto phonemes = verbly::split<std::list<std::string>>(pronunciation, " "); | ||
183 | |||
184 | verb.pronunciations.push_back(phonemes); | ||
185 | } | ||
186 | |||
187 | sqlite3_finalize(ppstmt); | ||
188 | } | ||
189 | |||
190 | return output; | ||
191 | } | ||
192 | |||
193 | }; | ||