diff options
Diffstat (limited to 'lib/form.cpp')
-rw-r--r-- | lib/form.cpp | 52 |
1 files changed, 22 insertions, 30 deletions
diff --git a/lib/form.cpp b/lib/form.cpp index b2c424d..4983274 100644 --- a/lib/form.cpp +++ b/lib/form.cpp | |||
@@ -1,5 +1,4 @@ | |||
1 | #include "form.h" | 1 | #include "form.h" |
2 | #include <sqlite3.h> | ||
3 | #include <algorithm> | 2 | #include <algorithm> |
4 | #include "filter.h" | 3 | #include "filter.h" |
5 | #include "database.h" | 4 | #include "database.h" |
@@ -24,29 +23,15 @@ namespace verbly { | |||
24 | return field::joinThroughWhere(object::form, "form_id", object::word, "lemmas_forms", "lemma_id", "category", static_cast<int>(category)); | 23 | return field::joinThroughWhere(object::form, "form_id", object::word, "lemmas_forms", "lemma_id", "category", static_cast<int>(category)); |
25 | } | 24 | } |
26 | 25 | ||
27 | form::form(const database& db, sqlite3_stmt* row) : db_(&db), valid_(true) | 26 | form::form(const database& db, hatkirby::row row) : valid_(true) |
28 | { | 27 | { |
29 | id_ = sqlite3_column_int(row, 0); | 28 | id_ = mpark::get<int>(row[0]); |
30 | text_ = std::string(reinterpret_cast<const char*>(sqlite3_column_text(row, 1))); | 29 | text_ = mpark::get<std::string>(row[1]); |
31 | complexity_ = sqlite3_column_int(row, 2); | 30 | complexity_ = mpark::get<int>(row[2]); |
32 | proper_ = (sqlite3_column_int(row, 3) == 1); | 31 | proper_ = (mpark::get<int>(row[3]) == 1); |
33 | length_ = sqlite3_column_int(row, 4); | 32 | length_ = mpark::get<int>(row[4]); |
34 | } | ||
35 | |||
36 | const std::vector<pronunciation>& form::getPronunciations() const | ||
37 | { | ||
38 | if (!valid_) | ||
39 | { | ||
40 | throw std::domain_error("Bad access to uninitialized form"); | ||
41 | } | ||
42 | |||
43 | if (!initializedPronunciations_) | ||
44 | { | ||
45 | pronunciations_ = db_->pronunciations(pronunciation::forms %= *this, pronunciation::id, -1).all(); | ||
46 | initializedPronunciations_ = true; | ||
47 | } | ||
48 | 33 | ||
49 | return pronunciations_; | 34 | pronunciations_ = db.pronunciations(*this, pronunciation::id, -1).all(); |
50 | } | 35 | } |
51 | 36 | ||
52 | bool form::startsWithVowelSound() const | 37 | bool form::startsWithVowelSound() const |
@@ -56,17 +41,24 @@ namespace verbly { | |||
56 | throw std::domain_error("Bad access to uninitialized form"); | 41 | throw std::domain_error("Bad access to uninitialized form"); |
57 | } | 42 | } |
58 | 43 | ||
59 | const std::vector<pronunciation>& pronunciations = getPronunciations(); | 44 | if (!pronunciations_.empty()) |
60 | if (!pronunciations.empty()) | ||
61 | { | 45 | { |
62 | return std::any_of(std::begin(pronunciations), std::end(pronunciations), [] (const pronunciation& p) { | 46 | return std::any_of( |
63 | return p.getPhonemes().front().find_first_of("012") != std::string::npos; | 47 | std::begin(pronunciations_), |
64 | }); | 48 | std::end(pronunciations_), |
49 | [] (const pronunciation& p) { | ||
50 | return p.getPhonemes().front().find_first_of("012") != | ||
51 | std::string::npos; | ||
52 | }); | ||
65 | } else { | 53 | } else { |
66 | // If the word is not in CMUDICT, fall back to checking whether the first letter is a vowel. | 54 | // If the word is not in CMUDICT, fall back to checking whether the first |
67 | // Not perfect but will work in most cases. | 55 | // letter is a vowel. Not perfect but will work in most cases. |
68 | char ch = std::tolower(text_.front()); | 56 | char ch = std::tolower(text_.front()); |
69 | return (ch == 'a') || (ch == 'e') || (ch == 'i') || (ch == 'o') || (ch == 'u'); | 57 | return (ch == 'a') || |
58 | (ch == 'e') || | ||
59 | (ch == 'i') || | ||
60 | (ch == 'o') || | ||
61 | (ch == 'u'); | ||
70 | } | 62 | } |
71 | } | 63 | } |
72 | 64 | ||