diff options
Diffstat (limited to 'lib/form.cpp')
| -rw-r--r-- | lib/form.cpp | 44 |
1 files changed, 33 insertions, 11 deletions
| diff --git a/lib/form.cpp b/lib/form.cpp index 8ba3bd7..778e5d3 100644 --- a/lib/form.cpp +++ b/lib/form.cpp | |||
| @@ -1,26 +1,26 @@ | |||
| 1 | #include "form.h" | 1 | #include "form.h" |
| 2 | #include <sqlite3.h> | 2 | #include <sqlite3.h> |
| 3 | #include "filter.h" | 3 | #include "filter.h" |
| 4 | #include "pronunciation.h" | ||
| 5 | #include "database.h" | 4 | #include "database.h" |
| 6 | #include "query.h" | 5 | #include "query.h" |
| 6 | #include "util.h" | ||
| 7 | 7 | ||
| 8 | namespace verbly { | 8 | namespace verbly { |
| 9 | 9 | ||
| 10 | const object form::objectType = object::form; | 10 | const object form::objectType = object::form; |
| 11 | 11 | ||
| 12 | const std::list<std::string> form::select = {"form_id", "form", "complexity", "proper"}; | 12 | const std::list<std::string> form::select = {"form_id", "form", "complexity", "proper"}; |
| 13 | 13 | ||
| 14 | const field form::id = field::integerField(object::form, "form_id"); | 14 | const field form::id = field::integerField(object::form, "form_id"); |
| 15 | const field form::text = field::stringField(object::form, "form"); | 15 | const field form::text = field::stringField(object::form, "form"); |
| 16 | const field form::complexity = field::integerField(object::form, "complexity"); | 16 | const field form::complexity = field::integerField(object::form, "complexity"); |
| 17 | const field form::proper = field::booleanField(object::form, "proper"); | 17 | const field form::proper = field::booleanField(object::form, "proper"); |
| 18 | 18 | ||
| 19 | const field form::pronunciation = field::joinThrough(object::form, "form_id", object::pronunciation, "forms_pronunciations", "pronunciation_id"); | 19 | const field form::pronunciation = field::joinThrough(object::form, "form_id", object::pronunciation, "forms_pronunciations", "pronunciation_id"); |
| 20 | 20 | ||
| 21 | const field form::lemmaJoin = field::joinField(object::form, "form_id", object::lemma); | 21 | const field form::lemmaJoin = field::joinField(object::form, "form_id", object::lemma); |
| 22 | const field form::inflectionCategory = field::integerField("lemmas_forms", "category"); | 22 | const field form::inflectionCategory = field::integerField("lemmas_forms", "category"); |
| 23 | 23 | ||
| 24 | form::form(const database& db, sqlite3_stmt* row) : db_(&db), valid_(true) | 24 | form::form(const database& db, sqlite3_stmt* row) : db_(&db), valid_(true) |
| 25 | { | 25 | { |
| 26 | id_ = sqlite3_column_int(row, 0); | 26 | id_ = sqlite3_column_int(row, 0); |
| @@ -28,7 +28,7 @@ namespace verbly { | |||
| 28 | complexity_ = sqlite3_column_int(row, 2); | 28 | complexity_ = sqlite3_column_int(row, 2); |
| 29 | proper_ = (sqlite3_column_int(row, 3) == 1); | 29 | proper_ = (sqlite3_column_int(row, 3) == 1); |
| 30 | } | 30 | } |
| 31 | 31 | ||
| 32 | filter operator%=(form::inflection_field check, filter joinCondition) | 32 | filter operator%=(form::inflection_field check, filter joinCondition) |
| 33 | { | 33 | { |
| 34 | return (form::lemmaJoin %= (joinCondition && (form::inflectionCategory == check.getCategory()))); | 34 | return (form::lemmaJoin %= (joinCondition && (form::inflectionCategory == check.getCategory()))); |
| @@ -40,14 +40,36 @@ namespace verbly { | |||
| 40 | { | 40 | { |
| 41 | throw std::domain_error("Bad access to uninitialized form"); | 41 | throw std::domain_error("Bad access to uninitialized form"); |
| 42 | } | 42 | } |
| 43 | 43 | ||
| 44 | if (!initializedPronunciations_) | 44 | if (!initializedPronunciations_) |
| 45 | { | 45 | { |
| 46 | pronunciations_ = db_->pronunciations(pronunciation::form %= *this, false, -1).all(); | 46 | pronunciations_ = db_->pronunciations(pronunciation::form %= *this, false, -1).all(); |
| 47 | initializedPronunciations_ = true; | 47 | initializedPronunciations_ = true; |
| 48 | } | 48 | } |
| 49 | 49 | ||
| 50 | return pronunciations_; | 50 | return pronunciations_; |
| 51 | } | 51 | } |
| 52 | 52 | ||
| 53 | bool form::startsWithVowelSound() const | ||
| 54 | { | ||
| 55 | if (!valid_) | ||
| 56 | { | ||
| 57 | throw std::domain_error("Bad access to uninitialized form"); | ||
| 58 | } | ||
| 59 | |||
| 60 | const std::vector<verbly::pronunciation>& pronunciations = getPronunciations(); | ||
| 61 | if (!pronunciations.empty()) | ||
| 62 | { | ||
| 63 | return std::any_of(std::begin(pronunciations), std::end(pronunciations), [] (const verbly::pronunciation& p) { | ||
| 64 | std::cout << "phonemes: " << implode(std::begin(p.getPhonemes()), std::end(p.getPhonemes()), ",") << std::endl; | ||
| 65 | return p.getPhonemes().front().find_first_of("012") != std::string::npos; | ||
| 66 | }); | ||
| 67 | } else { | ||
| 68 | // If the word is not in CMUDICT, fall back to checking whether the first letter is a vowel. | ||
| 69 | // Not perfect but will work in most cases. | ||
| 70 | char ch = std::tolower(text_.front()); | ||
| 71 | return (ch == 'a') || (ch == 'e') || (ch == 'i') || (ch == 'o') || (ch == 'u'); | ||
| 72 | } | ||
| 73 | } | ||
| 74 | |||
| 53 | }; | 75 | }; |
