summary refs log tree commit diff stats
path: root/lib/adverb_query.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'lib/adverb_query.cpp')
-rw-r--r--lib/adverb_query.cpp119
1 files changed, 119 insertions, 0 deletions
diff --git a/lib/adverb_query.cpp b/lib/adverb_query.cpp index 639f16f..c9d0d09 100644 --- a/lib/adverb_query.cpp +++ b/lib/adverb_query.cpp
@@ -67,6 +67,29 @@ namespace verbly {
67 return *this; 67 return *this;
68 } 68 }
69 69
70 adverb_query& adverb_query::with_prefix(filter<std::string> _f)
71 {
72 _f.clean();
73 _with_prefix = _f;
74
75 return *this;
76 }
77
78 adverb_query& adverb_query::with_suffix(filter<std::string> _f)
79 {
80 _f.clean();
81 _with_suffix = _f;
82
83 return *this;
84 }
85
86 adverb_query& adverb_query::with_complexity(int _arg)
87 {
88 _with_complexity = _arg;
89
90 return *this;
91 }
92
70 adverb_query& adverb_query::has_antonyms() 93 adverb_query& adverb_query::has_antonyms()
71 { 94 {
72 _has_antonyms = true; 95 _has_antonyms = true;
@@ -177,6 +200,85 @@ namespace verbly {
177 conditions.push_back("superlative IS NOT NULL"); 200 conditions.push_back("superlative IS NOT NULL");
178 } 201 }
179 202
203 if (!_with_prefix.empty())
204 {
205 std::function<std::string (filter<std::string>, bool)> recur = [&] (filter<std::string> f, bool notlogic) -> std::string {
206 switch (f.get_type())
207 {
208 case filter<std::string>::type::singleton:
209 {
210 if (notlogic == f.get_notlogic())
211 {
212 return "base_form LIKE @PREFIX";
213 } else {
214 return "base_form NOT LIKE @PREFIX";
215 }
216 }
217
218 case filter<std::string>::type::group:
219 {
220 bool truelogic = notlogic != f.get_notlogic();
221
222 std::list<std::string> clauses;
223 std::transform(std::begin(f), std::end(f), std::back_inserter(clauses), [&] (filter<std::string> f2) {
224 return recur(f2, truelogic);
225 });
226
227 if (truelogic == f.get_orlogic())
228 {
229 return "(" + verbly::implode(std::begin(clauses), std::end(clauses), " AND ") + ")";
230 } else {
231 return "(" + verbly::implode(std::begin(clauses), std::end(clauses), " OR ") + ")";
232 }
233 }
234 }
235 };
236
237 conditions.push_back(recur(_with_prefix, false));
238 }
239
240 if (!_with_suffix.empty())
241 {
242 std::function<std::string (filter<std::string>, bool)> recur = [&] (filter<std::string> f, bool notlogic) -> std::string {
243 switch (f.get_type())
244 {
245 case filter<std::string>::type::singleton:
246 {
247 if (notlogic == f.get_notlogic())
248 {
249 return "base_form LIKE @SUFFIX";
250 } else {
251 return "base_form NOT LIKE @SUFFIX";
252 }
253 }
254
255 case filter<std::string>::type::group:
256 {
257 bool truelogic = notlogic != f.get_notlogic();
258
259 std::list<std::string> clauses;
260 std::transform(std::begin(f), std::end(f), std::back_inserter(clauses), [&] (filter<std::string> f2) {
261 return recur(f2, truelogic);
262 });
263
264 if (truelogic == f.get_orlogic())
265 {
266 return "(" + verbly::implode(std::begin(clauses), std::end(clauses), " AND ") + ")";
267 } else {
268 return "(" + verbly::implode(std::begin(clauses), std::end(clauses), " OR ") + ")";
269 }
270 }
271 }
272 };
273
274 conditions.push_back(recur(_with_suffix, false));
275 }
276
277 if (_with_complexity != unlimited)
278 {
279 conditions.push_back("complexity = @COMPLEX");
280 }
281
180 if (_has_antonyms) 282 if (_has_antonyms)
181 { 283 {
182 conditions.push_back("adverb_id IN (SELECT adverb_2_id FROM adverb_antonymy)"); 284 conditions.push_back("adverb_id IN (SELECT adverb_2_id FROM adverb_antonymy)");
@@ -421,6 +523,23 @@ namespace verbly {
421 sqlite3_bind_int(ppstmt, sqlite3_bind_parameter_index(ppstmt, "@EXCID"), except._id); 523 sqlite3_bind_int(ppstmt, sqlite3_bind_parameter_index(ppstmt, "@EXCID"), except._id);
422 } 524 }
423 525
526 for (auto prefix : _with_prefix.inorder_flatten())
527 {
528 std::string pfat = prefix + "%";
529 sqlite3_bind_text(ppstmt, sqlite3_bind_parameter_index(ppstmt, "@PREFIX"), pfat.c_str(), pfat.length(), SQLITE_STATIC);
530 }
531
532 for (auto suffix : _with_suffix.inorder_flatten())
533 {
534 std::string pfat = "%" + suffix;
535 sqlite3_bind_text(ppstmt, sqlite3_bind_parameter_index(ppstmt, "@SUFFIX"), pfat.c_str(), pfat.length(), SQLITE_STATIC);
536 }
537
538 if (_with_complexity != unlimited)
539 {
540 sqlite3_bind_int(ppstmt, sqlite3_bind_parameter_index(ppstmt, "@COMPLEX"), _with_complexity);
541 }
542
424 for (auto antonym : _antonym_of.inorder_flatten()) 543 for (auto antonym : _antonym_of.inorder_flatten())
425 { 544 {
426 sqlite3_bind_int(ppstmt, sqlite3_bind_parameter_index(ppstmt, "@ANTID"), antonym._id); 545 sqlite3_bind_int(ppstmt, sqlite3_bind_parameter_index(ppstmt, "@ANTID"), antonym._id);