diff options
Diffstat (limited to 'lib/verb_query.cpp')
| -rw-r--r-- | lib/verb_query.cpp | 68 | 
1 files changed, 62 insertions, 6 deletions
| diff --git a/lib/verb_query.cpp b/lib/verb_query.cpp index 929ecc7..654bc33 100644 --- a/lib/verb_query.cpp +++ b/lib/verb_query.cpp | |||
| @@ -33,7 +33,7 @@ namespace verbly { | |||
| 33 | 33 | ||
| 34 | verb_query& verb_query::rhymes_with(const word& _word) | 34 | verb_query& verb_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 | verb_query& verb_query::has_rhyming_noun() | ||
| 57 | { | ||
| 58 | _has_rhyming_noun = true; | ||
| 59 | |||
| 60 | return *this; | ||
| 61 | } | ||
| 62 | |||
| 63 | verb_query& verb_query::has_rhyming_adjective() | ||
| 64 | { | ||
| 65 | _has_rhyming_adjective = true; | ||
| 66 | |||
| 67 | return *this; | ||
| 68 | } | ||
| 69 | |||
| 70 | verb_query& verb_query::has_rhyming_adverb() | ||
| 71 | { | ||
| 72 | _has_rhyming_adverb = true; | ||
| 73 | |||
| 74 | return *this; | ||
| 75 | } | ||
| 76 | |||
| 77 | verb_query& verb_query::has_rhyming_verb() | ||
| 78 | { | ||
| 79 | _has_rhyming_verb = true; | ||
| 80 | |||
| 81 | return *this; | ||
| 82 | } | ||
| 83 | |||
| 56 | verb_query& verb_query::has_frames() | 84 | verb_query& verb_query::has_frames() | 
| 57 | { | 85 | { | 
| 58 | this->_has_frames = true; | 86 | this->_has_frames = true; | 
| @@ -74,16 +102,37 @@ namespace verbly { | |||
| 74 | 102 | ||
| 75 | if (!_rhymes.empty()) | 103 | if (!_rhymes.empty()) | 
| 76 | { | 104 | { | 
| 77 | std::list<std::string> clauses(_rhymes.size(), "pronunciation LIKE ?"); | 105 | std::list<std::string> clauses(_rhymes.size(), "(prerhyme != ? AND rhyme = ?)"); | 
| 78 | std::string cond = "verb_id IN (SELECT verb_id FROM verb_pronunciations WHERE " + verbly::implode(std::begin(clauses), std::end(clauses), " OR ") + ")"; | 106 | std::string cond = "verb_id IN (SELECT verb_id FROM verb_pronunciations WHERE " + verbly::implode(std::begin(clauses), std::end(clauses), " OR ") + ")"; | 
| 79 | conditions.push_back(cond); | 107 | conditions.push_back(cond); | 
| 80 | 108 | ||
| 81 | for (auto rhyme : _rhymes) | 109 | for (auto rhy : _rhymes) | 
| 82 | { | 110 | { | 
| 83 | bindings.emplace_back("%" + rhyme); | 111 | bindings.emplace_back(rhy.get_prerhyme()); | 
| 112 | bindings.emplace_back(rhy.get_rhyme()); | ||
| 84 | } | 113 | } | 
| 85 | } | 114 | } | 
| 86 | 115 | ||
| 116 | if (_has_rhyming_noun) | ||
| 117 | { | ||
| 118 | conditions.push_back("verb_id IN (SELECT a.verb_id FROM verbs AS a INNER JOIN verb_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)"); | ||
| 119 | } | ||
| 120 | |||
| 121 | if (_has_rhyming_adjective) | ||
| 122 | { | ||
| 123 | conditions.push_back("verb_id IN (SELECT a.verb_id FROM verbs AS a INNER JOIN verb_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)"); | ||
| 124 | } | ||
| 125 | |||
| 126 | if (_has_rhyming_adverb) | ||
| 127 | { | ||
| 128 | conditions.push_back("verb_id IN (SELECT a.verb_id FROM verbs AS a INNER JOIN verb_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)"); | ||
| 129 | } | ||
| 130 | |||
| 131 | if (_has_rhyming_verb) | ||
| 132 | { | ||
| 133 | conditions.push_back("verb_id IN (SELECT a.verb_id FROM verbs AS a INNER JOIN verb_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 AND rhmp.verb_id != curp.verb_id)"); | ||
| 134 | } | ||
| 135 | |||
| 87 | for (auto except : _except) | 136 | for (auto except : _except) | 
| 88 | { | 137 | { | 
| 89 | conditions.push_back("verb_id != ?"); | 138 | conditions.push_back("verb_id != ?"); | 
| @@ -132,7 +181,7 @@ namespace verbly { | |||
| 132 | 181 | ||
| 133 | case binding::type::string: | 182 | case binding::type::string: | 
| 134 | { | 183 | { | 
| 135 | sqlite3_bind_text(ppstmt, i, binding.get_string().c_str(), binding.get_string().length(), SQLITE_STATIC); | 184 | sqlite3_bind_text(ppstmt, i, binding.get_string().c_str(), binding.get_string().length(), SQLITE_TRANSIENT); | 
| 136 | 185 | ||
| 137 | break; | 186 | break; | 
| 138 | } | 187 | } | 
| @@ -158,7 +207,7 @@ namespace verbly { | |||
| 158 | 207 | ||
| 159 | for (auto& verb : output) | 208 | for (auto& verb : output) | 
| 160 | { | 209 | { | 
| 161 | query = "SELECT pronunciation FROM verb_pronunciations WHERE verb_id = ?"; | 210 | query = "SELECT pronunciation, prerhyme, rhyme FROM verb_pronunciations WHERE verb_id = ?"; | 
| 162 | if (sqlite3_prepare_v2(_data.ppdb, query.c_str(), query.length(), &ppstmt, NULL) != SQLITE_OK) | 211 | if (sqlite3_prepare_v2(_data.ppdb, query.c_str(), query.length(), &ppstmt, NULL) != SQLITE_OK) | 
| 163 | { | 212 | { | 
| 164 | throw std::runtime_error(sqlite3_errmsg(_data.ppdb)); | 213 | throw std::runtime_error(sqlite3_errmsg(_data.ppdb)); | 
| @@ -172,6 +221,13 @@ namespace verbly { | |||
| 172 | auto phonemes = verbly::split<std::list<std::string>>(pronunciation, " "); | 221 | auto phonemes = verbly::split<std::list<std::string>>(pronunciation, " "); | 
| 173 | 222 | ||
| 174 | verb.pronunciations.push_back(phonemes); | 223 | verb.pronunciations.push_back(phonemes); | 
| 224 | |||
| 225 | if ((sqlite3_column_type(ppstmt, 1) != SQLITE_NULL) && (sqlite3_column_type(ppstmt, 2) != SQLITE_NULL)) | ||
| 226 | { | ||
| 227 | std::string prerhyme(reinterpret_cast<const char*>(sqlite3_column_text(ppstmt, 1))); | ||
| 228 | std::string rhyming(reinterpret_cast<const char*>(sqlite3_column_text(ppstmt, 2))); | ||
| 229 | verb.rhymes.emplace_back(prerhyme, rhyming); | ||
| 230 | } | ||
| 175 | } | 231 | } | 
| 176 | 232 | ||
| 177 | sqlite3_finalize(ppstmt); | 233 | sqlite3_finalize(ppstmt); | 
