diff options
Diffstat (limited to 'lib/adverb_query.cpp')
| -rw-r--r-- | lib/adverb_query.cpp | 68 |
1 files changed, 62 insertions, 6 deletions
| diff --git a/lib/adverb_query.cpp b/lib/adverb_query.cpp index 30ba92b..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; |
| @@ -181,16 +209,37 @@ namespace verbly { | |||
| 181 | 209 | ||
| 182 | if (!_rhymes.empty()) | 210 | if (!_rhymes.empty()) |
| 183 | { | 211 | { |
| 184 | std::list<std::string> clauses(_rhymes.size(), "pronunciation LIKE ?"); | 212 | std::list<std::string> clauses(_rhymes.size(), "(prerhyme != ? AND rhyme = ?)"); |
| 185 | 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 ") + ")"; |
| 186 | conditions.push_back(cond); | 214 | conditions.push_back(cond); |
| 187 | 215 | ||
| 188 | for (auto rhyme : _rhymes) | 216 | for (auto rhy : _rhymes) |
| 189 | { | 217 | { |
| 190 | bindings.emplace_back("%" + rhyme); | 218 | bindings.emplace_back(rhy.get_prerhyme()); |
| 219 | bindings.emplace_back(rhy.get_rhyme()); | ||
| 191 | } | 220 | } |
| 192 | } | 221 | } |
| 193 | 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)"); | ||
| 241 | } | ||
| 242 | |||
| 194 | for (auto except : _except) | 243 | for (auto except : _except) |
| 195 | { | 244 | { |
| 196 | conditions.push_back("adverb_id != ?"); | 245 | conditions.push_back("adverb_id != ?"); |
| @@ -538,7 +587,7 @@ namespace verbly { | |||
| 538 | 587 | ||
| 539 | case binding::type::string: | 588 | case binding::type::string: |
| 540 | { | 589 | { |
| 541 | sqlite3_bind_text(ppstmt, i, binding.get_string().c_str(), binding.get_string().length(), SQLITE_STATIC); | 590 | sqlite3_bind_text(ppstmt, i, binding.get_string().c_str(), binding.get_string().length(), SQLITE_TRANSIENT); |
| 542 | 591 | ||
| 543 | break; | 592 | break; |
| 544 | } | 593 | } |
| @@ -601,7 +650,7 @@ namespace verbly { | |||
| 601 | 650 | ||
| 602 | for (auto& adverb : output) | 651 | for (auto& adverb : output) |
| 603 | { | 652 | { |
| 604 | query = "SELECT pronunciation FROM adverb_pronunciations WHERE adverb_id = ?"; | 653 | query = "SELECT pronunciation, prerhyme, rhyme FROM adverb_pronunciations WHERE adverb_id = ?"; |
| 605 | 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) |
| 606 | { | 655 | { |
| 607 | throw std::runtime_error(sqlite3_errmsg(_data.ppdb)); | 656 | throw std::runtime_error(sqlite3_errmsg(_data.ppdb)); |
| @@ -615,6 +664,13 @@ namespace verbly { | |||
| 615 | auto phonemes = verbly::split<std::list<std::string>>(pronunciation, " "); | 664 | auto phonemes = verbly::split<std::list<std::string>>(pronunciation, " "); |
| 616 | 665 | ||
| 617 | 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 | } | ||
| 618 | } | 674 | } |
| 619 | 675 | ||
| 620 | sqlite3_finalize(ppstmt); | 676 | sqlite3_finalize(ppstmt); |
