summary refs log tree commit diff stats
path: root/lib/adjective_query.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'lib/adjective_query.cpp')
-rw-r--r--lib/adjective_query.cpp68
1 files changed, 62 insertions, 6 deletions
diff --git a/lib/adjective_query.cpp b/lib/adjective_query.cpp index a7f915c..2bea68f 100644 --- a/lib/adjective_query.cpp +++ b/lib/adjective_query.cpp
@@ -33,7 +33,7 @@ namespace verbly {
33 33
34 adjective_query& adjective_query::rhymes_with(const word& _word) 34 adjective_query& adjective_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 adjective_query& adjective_query::has_rhyming_noun()
57 {
58 _has_rhyming_noun = true;
59
60 return *this;
61 }
62
63 adjective_query& adjective_query::has_rhyming_adjective()
64 {
65 _has_rhyming_adjective = true;
66
67 return *this;
68 }
69
70 adjective_query& adjective_query::has_rhyming_adverb()
71 {
72 _has_rhyming_adverb = true;
73
74 return *this;
75 }
76
77 adjective_query& adjective_query::has_rhyming_verb()
78 {
79 _has_rhyming_verb = true;
80
81 return *this;
82 }
83
56 adjective_query& adjective_query::with_prefix(filter<std::string> _f) 84 adjective_query& adjective_query::with_prefix(filter<std::string> _f)
57 { 85 {
58 _f.clean(); 86 _f.clean();
@@ -227,16 +255,37 @@ namespace verbly {
227 255
228 if (!_rhymes.empty()) 256 if (!_rhymes.empty())
229 { 257 {
230 std::list<std::string> clauses(_rhymes.size(), "pronunciation LIKE ?"); 258 std::list<std::string> clauses(_rhymes.size(), "(prerhyme != ? AND rhyme = ?)");
231 std::string cond = "adjective_id IN (SELECT adjective_id FROM adjective_pronunciations WHERE " + verbly::implode(std::begin(clauses), std::end(clauses), " OR ") + ")"; 259 std::string cond = "adjective_id IN (SELECT adjective_id FROM adjective_pronunciations WHERE " + verbly::implode(std::begin(clauses), std::end(clauses), " OR ") + ")";
232 conditions.push_back(cond); 260 conditions.push_back(cond);
233 261
234 for (auto rhyme : _rhymes) 262 for (auto rhy : _rhymes)
235 { 263 {
236 bindings.emplace_back("%" + rhyme); 264 bindings.emplace_back(rhy.get_prerhyme());
265 bindings.emplace_back(rhy.get_rhyme());
237 } 266 }
238 } 267 }
239 268
269 if (_has_rhyming_noun)
270 {
271 conditions.push_back("adjective_id IN (SELECT a.adjective_id FROM adjectives AS a INNER JOIN adjective_pronunciations AS curp ON curp.adjective_id = a.adjective_id INNER JOIN noun_pronunciations AS rhmp ON rhmp.prerhyme != curp.prerhyme AND rhmp.rhyme = curp.rhyme)");
272 }
273
274 if (_has_rhyming_adjective)
275 {
276 conditions.push_back("adjective_id IN (SELECT a.adjective_id FROM adjectives AS a INNER JOIN adjective_pronunciations AS curp ON curp.adjective_id = a.adjective_id INNER JOIN adjective_pronunciations AS rhmp ON rhmp.prerhyme != curp.prerhyme AND rhmp.rhyme = curp.rhyme AND rhmp.adjective_id != curp.adjective_id)");
277 }
278
279 if (_has_rhyming_adverb)
280 {
281 conditions.push_back("adjective_id IN (SELECT a.adjective_id FROM adjectives AS a INNER JOIN adjective_pronunciations AS curp ON curp.adjective_id = a.adjective_id INNER JOIN adverb_pronunciations AS rhmp ON rhmp.prerhyme != curp.prerhyme AND rhmp.rhyme = curp.rhyme)");
282 }
283
284 if (_has_rhyming_verb)
285 {
286 conditions.push_back("adjective_id IN (SELECT a.adjective_id FROM adjectives AS a INNER JOIN adjective_pronunciations AS curp ON curp.adjective_id = a.adjective_id INNER JOIN verb_pronunciations AS rhmp ON rhmp.prerhyme != curp.prerhyme AND rhmp.rhyme = curp.rhyme)");
287 }
288
240 for (auto except : _except) 289 for (auto except : _except)
241 { 290 {
242 conditions.push_back("adjective_id != ?"); 291 conditions.push_back("adjective_id != ?");
@@ -816,7 +865,7 @@ namespace verbly {
816 865
817 case binding::type::string: 866 case binding::type::string:
818 { 867 {
819 sqlite3_bind_text(ppstmt, i, binding.get_string().c_str(), binding.get_string().length(), SQLITE_STATIC); 868 sqlite3_bind_text(ppstmt, i, binding.get_string().c_str(), binding.get_string().length(), SQLITE_TRANSIENT);
820 869
821 break; 870 break;
822 } 871 }
@@ -894,7 +943,7 @@ namespace verbly {
894 943
895 for (auto& adjective : output) 944 for (auto& adjective : output)
896 { 945 {
897 query = "SELECT pronunciation FROM adjective_pronunciations WHERE adjective_id = ?"; 946 query = "SELECT pronunciation, prerhyme, rhyme FROM adjective_pronunciations WHERE adjective_id = ?";
898 if (sqlite3_prepare_v2(_data.ppdb, query.c_str(), query.length(), &ppstmt, NULL) != SQLITE_OK) 947 if (sqlite3_prepare_v2(_data.ppdb, query.c_str(), query.length(), &ppstmt, NULL) != SQLITE_OK)
899 { 948 {
900 throw std::runtime_error(sqlite3_errmsg(_data.ppdb)); 949 throw std::runtime_error(sqlite3_errmsg(_data.ppdb));
@@ -908,6 +957,13 @@ namespace verbly {
908 auto phonemes = verbly::split<std::list<std::string>>(pronunciation, " "); 957 auto phonemes = verbly::split<std::list<std::string>>(pronunciation, " ");
909 958
910 adjective.pronunciations.push_back(phonemes); 959 adjective.pronunciations.push_back(phonemes);
960
961 if ((sqlite3_column_type(ppstmt, 1) != SQLITE_NULL) && (sqlite3_column_type(ppstmt, 2) != SQLITE_NULL))
962 {
963 std::string prerhyme(reinterpret_cast<const char*>(sqlite3_column_text(ppstmt, 1)));
964 std::string rhyming(reinterpret_cast<const char*>(sqlite3_column_text(ppstmt, 2)));
965 adjective.rhymes.emplace_back(prerhyme, rhyming);
966 }
911 } 967 }
912 968
913 sqlite3_finalize(ppstmt); 969 sqlite3_finalize(ppstmt);