diff options
author | Kelly Rauchenberger <fefferburbia@gmail.com> | 2016-04-17 13:44:37 -0400 |
---|---|---|
committer | Kelly Rauchenberger <fefferburbia@gmail.com> | 2016-04-17 13:44:37 -0400 |
commit | 04338f2b040fee5142904c062e0e38c836601034 (patch) | |
tree | a3ca42f738839ae4f6c83d599277c33203beb733 /lib/noun_query.cpp | |
parent | 040ee58fecdc9c478004bc2e554e1ae126ec4602 (diff) | |
download | verbly-04338f2b040fee5142904c062e0e38c836601034.tar.gz verbly-04338f2b040fee5142904c062e0e38c836601034.tar.bz2 verbly-04338f2b040fee5142904c062e0e38c836601034.zip |
Fixed perfect rhyming
Rhyme detection now ensures that any rhymes it finds are perfect rhymes and not identical rhymes. Rhyme detection is also now a lot faster because additional information is stored in the datafile. Also fixed a bug in the query interface (and the generator) that could cause incorrect queries to be executed.
Diffstat (limited to 'lib/noun_query.cpp')
-rw-r--r-- | lib/noun_query.cpp | 73 |
1 files changed, 65 insertions, 8 deletions
diff --git a/lib/noun_query.cpp b/lib/noun_query.cpp index 19a1297..b4336b6 100644 --- a/lib/noun_query.cpp +++ b/lib/noun_query.cpp | |||
@@ -33,7 +33,7 @@ namespace verbly { | |||
33 | 33 | ||
34 | noun_query& noun_query::rhymes_with(const word& _word) | 34 | noun_query& noun_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 | noun_query& noun_query::has_rhyming_noun() | ||
57 | { | ||
58 | _has_rhyming_noun = true; | ||
59 | |||
60 | return *this; | ||
61 | } | ||
62 | |||
63 | noun_query& noun_query::has_rhyming_adjective() | ||
64 | { | ||
65 | _has_rhyming_adjective = true; | ||
66 | |||
67 | return *this; | ||
68 | } | ||
69 | |||
70 | noun_query& noun_query::has_rhyming_adverb() | ||
71 | { | ||
72 | _has_rhyming_adverb = true; | ||
73 | |||
74 | return *this; | ||
75 | } | ||
76 | |||
77 | noun_query& noun_query::has_rhyming_verb() | ||
78 | { | ||
79 | _has_rhyming_verb = true; | ||
80 | |||
81 | return *this; | ||
82 | } | ||
83 | |||
56 | noun_query& noun_query::with_singular_form(std::string _arg) | 84 | noun_query& noun_query::with_singular_form(std::string _arg) |
57 | { | 85 | { |
58 | _with_singular_form.push_back(_arg); | 86 | _with_singular_form.push_back(_arg); |
@@ -483,16 +511,37 @@ namespace verbly { | |||
483 | 511 | ||
484 | if (!_rhymes.empty()) | 512 | if (!_rhymes.empty()) |
485 | { | 513 | { |
486 | std::list<std::string> clauses(_rhymes.size(), "pronunciation LIKE ?"); | 514 | std::list<std::string> clauses(_rhymes.size(), "(prerhyme != ? AND rhyme = ?)"); |
487 | std::string cond = "noun_id IN (SELECT noun_id FROM noun_pronunciations WHERE " + verbly::implode(std::begin(clauses), std::end(clauses), " OR ") + ")"; | 515 | std::string cond = "noun_id IN (SELECT noun_id FROM noun_pronunciations WHERE " + verbly::implode(std::begin(clauses), std::end(clauses), " OR ") + ")"; |
488 | conditions.push_back(cond); | 516 | conditions.push_back(cond); |
489 | 517 | ||
490 | for (auto rhyme : _rhymes) | 518 | for (auto rhy : _rhymes) |
491 | { | 519 | { |
492 | bindings.emplace_back("%" + rhyme); | 520 | bindings.emplace_back(rhy.get_prerhyme()); |
521 | bindings.emplace_back(rhy.get_rhyme()); | ||
493 | } | 522 | } |
494 | } | 523 | } |
495 | 524 | ||
525 | if (_has_rhyming_noun) | ||
526 | { | ||
527 | conditions.push_back("noun_id IN (SELECT a.noun_id FROM nouns AS a INNER JOIN noun_pronunciations AS curp ON curp.noun_id = a.noun_id INNER JOIN noun_pronunciations AS rhmp ON rhmp.prerhyme != curp.prerhyme AND rhmp.rhyme = curp.rhyme AND rhmp.noun_id != curp.noun_id)"); | ||
528 | } | ||
529 | |||
530 | if (_has_rhyming_adjective) | ||
531 | { | ||
532 | conditions.push_back("noun_id IN (SELECT a.noun_id FROM nouns AS a INNER JOIN noun_pronunciations AS curp ON curp.noun_id = a.noun_id INNER JOIN adjective_pronunciations AS rhmp ON rhmp.prerhyme != curp.prerhyme AND rhmp.rhyme = curp.rhyme)"); | ||
533 | } | ||
534 | |||
535 | if (_has_rhyming_adverb) | ||
536 | { | ||
537 | conditions.push_back("noun_id IN (SELECT a.noun_id FROM nouns AS a INNER JOIN noun_pronunciations AS curp ON curp.noun_id = a.noun_id INNER JOIN adverb_pronunciations AS rhmp ON rhmp.prerhyme != curp.prerhyme AND rhmp.rhyme = curp.rhyme)"); | ||
538 | } | ||
539 | |||
540 | if (_has_rhyming_verb) | ||
541 | { | ||
542 | conditions.push_back("noun_id IN (SELECT a.noun_id FROM nouns AS a INNER JOIN noun_pronunciations AS curp ON curp.noun_id = a.noun_id INNER JOIN verb_pronunciations AS rhmp ON rhmp.prerhyme != curp.prerhyme AND rhmp.rhyme = curp.rhyme)"); | ||
543 | } | ||
544 | |||
496 | for (auto except : _except) | 545 | for (auto except : _except) |
497 | { | 546 | { |
498 | conditions.push_back("noun_id != ?"); | 547 | conditions.push_back("noun_id != ?"); |
@@ -1768,7 +1817,7 @@ namespace verbly { | |||
1768 | { | 1817 | { |
1769 | throw std::runtime_error(sqlite3_errmsg(_data.ppdb)); | 1818 | throw std::runtime_error(sqlite3_errmsg(_data.ppdb)); |
1770 | } | 1819 | } |
1771 | 1820 | ||
1772 | int i = 1; | 1821 | int i = 1; |
1773 | for (auto& binding : bindings) | 1822 | for (auto& binding : bindings) |
1774 | { | 1823 | { |
@@ -1783,7 +1832,7 @@ namespace verbly { | |||
1783 | 1832 | ||
1784 | case binding::type::string: | 1833 | case binding::type::string: |
1785 | { | 1834 | { |
1786 | sqlite3_bind_text(ppstmt, i, binding.get_string().c_str(), binding.get_string().length(), SQLITE_STATIC); | 1835 | sqlite3_bind_text(ppstmt, i, binding.get_string().c_str(), binding.get_string().length(), SQLITE_TRANSIENT); |
1787 | 1836 | ||
1788 | break; | 1837 | break; |
1789 | } | 1838 | } |
@@ -1791,7 +1840,7 @@ namespace verbly { | |||
1791 | 1840 | ||
1792 | i++; | 1841 | i++; |
1793 | } | 1842 | } |
1794 | 1843 | ||
1795 | /* | 1844 | /* |
1796 | for (auto adj : _derived_from_adjective) | 1845 | for (auto adj : _derived_from_adjective) |
1797 | { | 1846 | { |
@@ -1843,7 +1892,7 @@ namespace verbly { | |||
1843 | 1892 | ||
1844 | for (auto& noun : output) | 1893 | for (auto& noun : output) |
1845 | { | 1894 | { |
1846 | query = "SELECT pronunciation FROM noun_pronunciations WHERE noun_id = ?"; | 1895 | query = "SELECT pronunciation, prerhyme, rhyme FROM noun_pronunciations WHERE noun_id = ?"; |
1847 | if (sqlite3_prepare_v2(_data.ppdb, query.c_str(), query.length(), &ppstmt, NULL) != SQLITE_OK) | 1896 | if (sqlite3_prepare_v2(_data.ppdb, query.c_str(), query.length(), &ppstmt, NULL) != SQLITE_OK) |
1848 | { | 1897 | { |
1849 | throw std::runtime_error(sqlite3_errmsg(_data.ppdb)); | 1898 | throw std::runtime_error(sqlite3_errmsg(_data.ppdb)); |
@@ -1857,6 +1906,14 @@ namespace verbly { | |||
1857 | auto phonemes = verbly::split<std::list<std::string>>(pronunciation, " "); | 1906 | auto phonemes = verbly::split<std::list<std::string>>(pronunciation, " "); |
1858 | 1907 | ||
1859 | noun.pronunciations.push_back(phonemes); | 1908 | noun.pronunciations.push_back(phonemes); |
1909 | |||
1910 | if ((sqlite3_column_type(ppstmt, 1) != SQLITE_NULL) && (sqlite3_column_type(ppstmt, 2) != SQLITE_NULL)) | ||
1911 | { | ||
1912 | std::string prerhyme(reinterpret_cast<const char*>(sqlite3_column_text(ppstmt, 1))); | ||
1913 | std::string rhyming(reinterpret_cast<const char*>(sqlite3_column_text(ppstmt, 2))); | ||
1914 | |||
1915 | noun.rhymes.emplace_back(prerhyme, rhyming); | ||
1916 | } | ||
1860 | } | 1917 | } |
1861 | 1918 | ||
1862 | sqlite3_finalize(ppstmt); | 1919 | sqlite3_finalize(ppstmt); |