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.cpp163
1 files changed, 106 insertions, 57 deletions
diff --git a/lib/adverb_query.cpp b/lib/adverb_query.cpp index c9d0d09..797e6a6 100644 --- a/lib/adverb_query.cpp +++ b/lib/adverb_query.cpp
@@ -33,7 +33,7 @@ namespace verbly {
33 33
34 adverb_query& adverb_query::rhymes_with(const word& _word) 34 adverb_query& adverb_query::rhymes_with(const word& _word)
35 { 35 {
36 for (auto rhyme : _word.rhyme_phonemes()) 36 for (auto rhyme : _word.get_rhymes())
37 { 37 {
38 _rhymes.push_back(rhyme); 38 _rhymes.push_back(rhyme);
39 } 39 }
@@ -53,6 +53,34 @@ namespace verbly {
53 return *this; 53 return *this;
54 } 54 }
55 55
56 adverb_query& adverb_query::has_rhyming_noun()
57 {
58 _has_rhyming_noun = true;
59
60 return *this;
61 }
62
63 adverb_query& adverb_query::has_rhyming_adjective()
64 {
65 _has_rhyming_adjective = true;
66
67 return *this;
68 }
69
70 adverb_query& adverb_query::has_rhyming_adverb()
71 {
72 _has_rhyming_adverb = true;
73
74 return *this;
75 }
76
77 adverb_query& adverb_query::has_rhyming_verb()
78 {
79 _has_rhyming_verb = true;
80
81 return *this;
82 }
83
56 adverb_query& adverb_query::requires_comparative_form() 84 adverb_query& adverb_query::requires_comparative_form()
57 { 85 {
58 _requires_comparative_form = true; 86 _requires_comparative_form = true;
@@ -172,6 +200,7 @@ namespace verbly {
172 std::stringstream construct; 200 std::stringstream construct;
173 construct << "SELECT adverb_id, base_form, comparative, superlative FROM adverbs"; 201 construct << "SELECT adverb_id, base_form, comparative, superlative FROM adverbs";
174 std::list<std::string> conditions; 202 std::list<std::string> conditions;
203 std::list<binding> bindings;
175 204
176 if (_has_prn) 205 if (_has_prn)
177 { 206 {
@@ -180,14 +209,41 @@ namespace verbly {
180 209
181 if (!_rhymes.empty()) 210 if (!_rhymes.empty())
182 { 211 {
183 std::list<std::string> clauses(_rhymes.size(), "pronunciation LIKE @RHMPRN"); 212 std::list<std::string> clauses(_rhymes.size(), "(prerhyme != ? AND rhyme = ?)");
184 std::string cond = "adverb_id IN (SELECT adverb_id FROM adverb_pronunciations WHERE " + verbly::implode(std::begin(clauses), std::end(clauses), " OR ") + ")"; 213 std::string cond = "adverb_id IN (SELECT adverb_id FROM adverb_pronunciations WHERE " + verbly::implode(std::begin(clauses), std::end(clauses), " OR ") + ")";
185 conditions.push_back(cond); 214 conditions.push_back(cond);
215
216 for (auto rhy : _rhymes)
217 {
218 bindings.emplace_back(rhy.get_prerhyme());
219 bindings.emplace_back(rhy.get_rhyme());
220 }
221 }
222
223 if (_has_rhyming_noun)
224 {
225 conditions.push_back("adverb_id IN (SELECT a.adverb_id FROM adverbs AS a INNER JOIN adverb_pronunciations AS curp ON curp.noun_id = a.adverb_id INNER JOIN noun_pronunciations AS rhmp ON rhmp.prerhyme != curp.prerhyme AND rhmp.rhyme = curp.rhyme)");
226 }
227
228 if (_has_rhyming_adjective)
229 {
230 conditions.push_back("adverb_id IN (SELECT a.adverb_id FROM adverbs AS a INNER JOIN adverb_pronunciations AS curp ON curp.noun_id = a.adverb_id INNER JOIN adjective_pronunciations AS rhmp ON rhmp.prerhyme != curp.prerhyme AND rhmp.rhyme = curp.rhyme)");
231 }
232
233 if (_has_rhyming_adverb)
234 {
235 conditions.push_back("adverb_id IN (SELECT a.adverb_id FROM adverbs AS a INNER JOIN adverb_pronunciations AS curp ON curp.noun_id = a.adverb_id INNER JOIN adverb_pronunciations AS rhmp ON rhmp.prerhyme != curp.prerhyme AND rhmp.rhyme = curp.rhyme AND rhmp.adverb_id != curp.adverb_id)");
236 }
237
238 if (_has_rhyming_verb)
239 {
240 conditions.push_back("adverb_id IN (SELECT a.adverb_id FROM adverbs AS a INNER JOIN adverb_pronunciations AS curp ON curp.noun_id = a.adverb_id INNER JOIN verb_pronunciations AS rhmp ON rhmp.prerhyme != curp.prerhyme AND rhmp.rhyme = curp.rhyme)");
186 } 241 }
187 242
188 for (auto except : _except) 243 for (auto except : _except)
189 { 244 {
190 conditions.push_back("adverb_id != @EXCID"); 245 conditions.push_back("adverb_id != ?");
246 bindings.emplace_back(except._id);
191 } 247 }
192 248
193 if (_requires_comparative_form) 249 if (_requires_comparative_form)
@@ -207,11 +263,13 @@ namespace verbly {
207 { 263 {
208 case filter<std::string>::type::singleton: 264 case filter<std::string>::type::singleton:
209 { 265 {
266 bindings.emplace_back(f.get_elem() + "%");
267
210 if (notlogic == f.get_notlogic()) 268 if (notlogic == f.get_notlogic())
211 { 269 {
212 return "base_form LIKE @PREFIX"; 270 return "base_form LIKE ?";
213 } else { 271 } else {
214 return "base_form NOT LIKE @PREFIX"; 272 return "base_form NOT LIKE ?";
215 } 273 }
216 } 274 }
217 275
@@ -244,11 +302,13 @@ namespace verbly {
244 { 302 {
245 case filter<std::string>::type::singleton: 303 case filter<std::string>::type::singleton:
246 { 304 {
305 bindings.emplace_back("%" + f.get_elem());
306
247 if (notlogic == f.get_notlogic()) 307 if (notlogic == f.get_notlogic())
248 { 308 {
249 return "base_form LIKE @SUFFIX"; 309 return "base_form LIKE ?";
250 } else { 310 } else {
251 return "base_form NOT LIKE @SUFFIX"; 311 return "base_form NOT LIKE ?";
252 } 312 }
253 } 313 }
254 314
@@ -276,7 +336,8 @@ namespace verbly {
276 336
277 if (_with_complexity != unlimited) 337 if (_with_complexity != unlimited)
278 { 338 {
279 conditions.push_back("complexity = @COMPLEX"); 339 conditions.push_back("complexity = ?");
340 bindings.emplace_back(_with_complexity);
280 } 341 }
281 342
282 if (_has_antonyms) 343 if (_has_antonyms)
@@ -301,11 +362,13 @@ namespace verbly {
301 { 362 {
302 case filter<adverb>::type::singleton: 363 case filter<adverb>::type::singleton:
303 { 364 {
365 bindings.emplace_back(f.get_elem()._id);
366
304 if (notlogic == f.get_notlogic()) 367 if (notlogic == f.get_notlogic())
305 { 368 {
306 return "adverb_1_id = @ANTID"; 369 return "adverb_1_id = ?";
307 } else { 370 } else {
308 return "adverb_1_id != @ANTID"; 371 return "adverb_1_id != ?";
309 } 372 }
310 } 373 }
311 374
@@ -355,11 +418,13 @@ namespace verbly {
355 { 418 {
356 case filter<adverb>::type::singleton: 419 case filter<adverb>::type::singleton:
357 { 420 {
421 bindings.emplace_back(f.get_elem()._id);
422
358 if (notlogic == f.get_notlogic()) 423 if (notlogic == f.get_notlogic())
359 { 424 {
360 return "adverb_1_id = @SYNID"; 425 return "adverb_1_id = ?";
361 } else { 426 } else {
362 return "adverb_1_id != @SYNID"; 427 return "adverb_1_id != ?";
363 } 428 }
364 } 429 }
365 430
@@ -409,11 +474,13 @@ namespace verbly {
409 { 474 {
410 case filter<adjective>::type::singleton: 475 case filter<adjective>::type::singleton:
411 { 476 {
477 bindings.emplace_back(f.get_elem()._id);
478
412 if (notlogic == f.get_notlogic()) 479 if (notlogic == f.get_notlogic())
413 { 480 {
414 return "adjective_id = @AMANID"; 481 return "adjective_id = ?";
415 } else { 482 } else {
416 return "adjective_id != @AMANID"; 483 return "adjective_id != ?";
417 } 484 }
418 } 485 }
419 486
@@ -506,54 +573,29 @@ namespace verbly {
506 throw std::runtime_error(sqlite3_errmsg(_data.ppdb)); 573 throw std::runtime_error(sqlite3_errmsg(_data.ppdb));
507 } 574 }
508 575
509 if (!_rhymes.empty()) 576 int i = 1;
577 for (auto& binding : bindings)
510 { 578 {
511 int i = 0; 579 switch (binding.get_type())
512 for (auto rhyme : _rhymes)
513 { 580 {
514 std::string rhymer = "%" + rhyme; 581 case binding::type::integer:
515 sqlite3_bind_text(ppstmt, sqlite3_bind_parameter_index(ppstmt, "@RHMPRN"), rhymer.c_str(), rhymer.length(), SQLITE_STATIC); 582 {
583 sqlite3_bind_int(ppstmt, i, binding.get_integer());
584
585 break;
586 }
516 587
517 i++; 588 case binding::type::string:
589 {
590 sqlite3_bind_text(ppstmt, i, binding.get_string().c_str(), binding.get_string().length(), SQLITE_TRANSIENT);
591
592 break;
593 }
518 } 594 }
595
596 i++;
519 } 597 }
520 598
521 for (auto except : _except)
522 {
523 sqlite3_bind_int(ppstmt, sqlite3_bind_parameter_index(ppstmt, "@EXCID"), except._id);
524 }
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
543 for (auto antonym : _antonym_of.inorder_flatten())
544 {
545 sqlite3_bind_int(ppstmt, sqlite3_bind_parameter_index(ppstmt, "@ANTID"), antonym._id);
546 }
547
548 for (auto synonym : _synonym_of.inorder_flatten())
549 {
550 sqlite3_bind_int(ppstmt, sqlite3_bind_parameter_index(ppstmt, "@SYNID"), synonym._id);
551 }
552
553 for (auto adj : _mannernym_of.inorder_flatten())
554 {
555 sqlite3_bind_int(ppstmt, sqlite3_bind_parameter_index(ppstmt, "@AMANID"), adj._id);
556 }
557 /* 599 /*
558 for (auto adj : _derived_from_adjective) 600 for (auto adj : _derived_from_adjective)
559 { 601 {
@@ -608,7 +650,7 @@ namespace verbly {
608 650
609 for (auto& adverb : output) 651 for (auto& adverb : output)
610 { 652 {
611 query = "SELECT pronunciation FROM adverb_pronunciations WHERE adverb_id = ?"; 653 query = "SELECT pronunciation, prerhyme, rhyme FROM adverb_pronunciations WHERE adverb_id = ?";
612 if (sqlite3_prepare_v2(_data.ppdb, query.c_str(), query.length(), &ppstmt, NULL) != SQLITE_OK) 654 if (sqlite3_prepare_v2(_data.ppdb, query.c_str(), query.length(), &ppstmt, NULL) != SQLITE_OK)
613 { 655 {
614 throw std::runtime_error(sqlite3_errmsg(_data.ppdb)); 656 throw std::runtime_error(sqlite3_errmsg(_data.ppdb));
@@ -622,6 +664,13 @@ namespace verbly {
622 auto phonemes = verbly::split<std::list<std::string>>(pronunciation, " "); 664 auto phonemes = verbly::split<std::list<std::string>>(pronunciation, " ");
623 665
624 adverb.pronunciations.push_back(phonemes); 666 adverb.pronunciations.push_back(phonemes);
667
668 if ((sqlite3_column_type(ppstmt, 1) != SQLITE_NULL) && (sqlite3_column_type(ppstmt, 2) != SQLITE_NULL))
669 {
670 std::string prerhyme(reinterpret_cast<const char*>(sqlite3_column_text(ppstmt, 1)));
671 std::string rhyming(reinterpret_cast<const char*>(sqlite3_column_text(ppstmt, 2)));
672 adverb.rhymes.emplace_back(prerhyme, rhyming);
673 }
625 } 674 }
626 675
627 sqlite3_finalize(ppstmt); 676 sqlite3_finalize(ppstmt);