diff options
author | Kelly Rauchenberger <fefferburbia@gmail.com> | 2017-02-06 20:58:37 -0500 |
---|---|---|
committer | Kelly Rauchenberger <fefferburbia@gmail.com> | 2017-02-06 20:58:37 -0500 |
commit | f1f67e62cebb4144f0599196263cd93b41fa972e (patch) | |
tree | 5d0f8fceeec63f47fb85846f5568bcb36f4a975f /lib/word.cpp | |
parent | 6cc23ba387d0813b801ba094709673a61bac889c (diff) | |
download | verbly-f1f67e62cebb4144f0599196263cd93b41fa972e.tar.gz verbly-f1f67e62cebb4144f0599196263cd93b41fa972e.tar.bz2 verbly-f1f67e62cebb4144f0599196263cd93b41fa972e.zip |
Made pronunciation::rhymes join dynamic
This involved adding a new type of filter; one that compares (currently only equality and inequality) a field with another field located in an enclosing join context. In the process, it was discovered that simplifying the lemma::forms join field earlier actually made some queries return inaccurate results because the inflection of the form was being ignored and anything in the lemma would be used because of the inner join. Because the existing condition join did not allow for the condition field to be on the from side of the join, two things were done: a condition version of joinThrough was made, and lemma was finally eliminated as a top-level object, replaced instead with a condition join between word and form through lemmas_forms. Queries are also now grouped by the first select field (assumed to be the primary ID) of the top table, in order to eliminate duplicates created by inner joins, so that there is a uniform distribution between results for random queries. Created a database index on pronunciations(rhyme) which decreases query time for rhyming filters. The new database version is backwards-compatible because no data or structure changed.
Diffstat (limited to 'lib/word.cpp')
-rw-r--r-- | lib/word.cpp | 65 |
1 files changed, 39 insertions, 26 deletions
diff --git a/lib/word.cpp b/lib/word.cpp index d75159c..4dea569 100644 --- a/lib/word.cpp +++ b/lib/word.cpp | |||
@@ -16,7 +16,6 @@ namespace verbly { | |||
16 | const field word::adjectivePosition = field::integerField(object::word, "position", true); | 16 | const field word::adjectivePosition = field::integerField(object::word, "position", true); |
17 | 17 | ||
18 | const field word::notions = field::joinField(object::word, "notion_id", object::notion); | 18 | const field word::notions = field::joinField(object::word, "notion_id", object::notion); |
19 | const field word::lemmas = field::joinField(object::word, "lemma_id", object::lemma); | ||
20 | const field word::frames = field::joinField(object::word, "group_id", object::frame, true); | 19 | const field word::frames = field::joinField(object::word, "group_id", object::frame, true); |
21 | 20 | ||
22 | const field word::antonyms = field::selfJoin(object::word, "word_id", "antonymy", "antonym_2_id", "antonym_1_id"); | 21 | const field word::antonyms = field::selfJoin(object::word, "word_id", "antonymy", "antonym_2_id", "antonym_1_id"); |
@@ -39,6 +38,11 @@ namespace verbly { | |||
39 | const field word::regionalTerms = field::selfJoin(object::word, "word_id", "regionality", "domain_id", "term_id"); | 38 | const field word::regionalTerms = field::selfJoin(object::word, "word_id", "regionality", "domain_id", "term_id"); |
40 | const field word::regionalDomains = field::selfJoin(object::word, "word_id", "regionality", "term_id", "domain_id"); | 39 | const field word::regionalDomains = field::selfJoin(object::word, "word_id", "regionality", "term_id", "domain_id"); |
41 | 40 | ||
41 | field word::forms(inflection category) | ||
42 | { | ||
43 | return field::joinThroughWhere(object::word, "lemma_id", object::form, "lemmas_forms", "form_id", "category", static_cast<int>(category)); | ||
44 | } | ||
45 | |||
42 | word::word(const database& db, sqlite3_stmt* row) : db_(&db), valid_(true) | 46 | word::word(const database& db, sqlite3_stmt* row) : db_(&db), valid_(true) |
43 | { | 47 | { |
44 | id_ = sqlite3_column_int(row, 0); | 48 | id_ = sqlite3_column_int(row, 0); |
@@ -78,21 +82,6 @@ namespace verbly { | |||
78 | return notion_; | 82 | return notion_; |
79 | } | 83 | } |
80 | 84 | ||
81 | const lemma& word::getLemma() const | ||
82 | { | ||
83 | if (!valid_) | ||
84 | { | ||
85 | throw std::domain_error("Bad access to uninitialized word"); | ||
86 | } | ||
87 | |||
88 | if (!lemma_) | ||
89 | { | ||
90 | lemma_ = db_->lemmas(lemma::id == lemmaId_).first(); | ||
91 | } | ||
92 | |||
93 | return lemma_; | ||
94 | } | ||
95 | |||
96 | bool word::hasFrames() const | 85 | bool word::hasFrames() const |
97 | { | 86 | { |
98 | if (!valid_) | 87 | if (!valid_) |
@@ -133,26 +122,50 @@ namespace verbly { | |||
133 | return frames_; | 122 | return frames_; |
134 | } | 123 | } |
135 | 124 | ||
136 | std::string word::getBaseForm() const | 125 | void word::initializeFrames() const |
137 | { | 126 | { |
138 | return getLemma().getBaseForm().getText(); | 127 | initializedFrames_ = true; |
128 | frames_ = db_->frames(*this, {}, -1).all(); | ||
139 | } | 129 | } |
140 | 130 | ||
141 | std::vector<std::string> word::getInflections(inflection category) const | 131 | const form& word::getBaseForm() const |
142 | { | 132 | { |
143 | std::vector<std::string> result; | 133 | if (!valid_) |
144 | for (const form& infl : getLemma().getInflections(category)) | ||
145 | { | 134 | { |
146 | result.push_back(infl.getText()); | 135 | throw std::domain_error("Bad access to uninitialized word"); |
147 | } | 136 | } |
148 | 137 | ||
149 | return result; | 138 | if (!forms_.count(inflection::base)) |
139 | { | ||
140 | initializeForm(inflection::base); | ||
141 | } | ||
142 | |||
143 | return forms_.at(inflection::base).front(); | ||
150 | } | 144 | } |
151 | 145 | ||
152 | void word::initializeFrames() const | 146 | bool word::hasInflection(inflection category) const |
153 | { | 147 | { |
154 | initializedFrames_ = true; | 148 | return !getInflections(category).empty(); |
155 | frames_ = db_->frames(*this, {}, -1).all(); | 149 | } |
150 | |||
151 | const std::vector<form>& word::getInflections(inflection category) const | ||
152 | { | ||
153 | if (!valid_) | ||
154 | { | ||
155 | throw std::domain_error("Bad access to uninitialized word"); | ||
156 | } | ||
157 | |||
158 | if (!forms_.count(category)) | ||
159 | { | ||
160 | initializeForm(category); | ||
161 | } | ||
162 | |||
163 | return forms_.at(category); | ||
164 | } | ||
165 | |||
166 | void word::initializeForm(inflection infl) const | ||
167 | { | ||
168 | forms_[infl] = db_->forms(form::words(infl) %= *this, verbly::form::id, -1).all(); | ||
156 | } | 169 | } |
157 | 170 | ||
158 | }; | 171 | }; |