diff options
| -rw-r--r-- | lib/form.cpp | 44 | ||||
| -rw-r--r-- | lib/form.h | 84 |
2 files changed, 77 insertions, 51 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 | }; |
| diff --git a/lib/form.h b/lib/form.h index c6a1353..c9c2a9e 100644 --- a/lib/form.h +++ b/lib/form.h | |||
| @@ -6,144 +6,148 @@ | |||
| 6 | #include <string> | 6 | #include <string> |
| 7 | #include <stdexcept> | 7 | #include <stdexcept> |
| 8 | #include "field.h" | 8 | #include "field.h" |
| 9 | #include "pronunciation.h" | ||
| 9 | #include "filter.h" | 10 | #include "filter.h" |
| 10 | 11 | ||
| 11 | struct sqlite3_stmt; | 12 | struct sqlite3_stmt; |
| 12 | 13 | ||
| 13 | namespace verbly { | 14 | namespace verbly { |
| 14 | 15 | ||
| 15 | class pronunciation; | ||
| 16 | class database; | 16 | class database; |
| 17 | 17 | ||
| 18 | class form { | 18 | class form { |
| 19 | public: | 19 | public: |
| 20 | 20 | ||
| 21 | // Default constructor | 21 | // Default constructor |
| 22 | 22 | ||
| 23 | form() = default; | 23 | form() = default; |
| 24 | 24 | ||
| 25 | // Construct from database | 25 | // Construct from database |
| 26 | 26 | ||
| 27 | form(const database& db, sqlite3_stmt* row); | 27 | form(const database& db, sqlite3_stmt* row); |
| 28 | 28 | ||
| 29 | // Accessors | 29 | // Accessors |
| 30 | 30 | ||
| 31 | operator bool() const | 31 | operator bool() const |
| 32 | { | 32 | { |
| 33 | return valid_; | 33 | return valid_; |
| 34 | } | 34 | } |
| 35 | 35 | ||
| 36 | int getId() const | 36 | int getId() const |
| 37 | { | 37 | { |
| 38 | if (!valid_) | 38 | if (!valid_) |
| 39 | { | 39 | { |
| 40 | throw std::domain_error("Bad access to uninitialized form"); | 40 | throw std::domain_error("Bad access to uninitialized form"); |
| 41 | } | 41 | } |
| 42 | 42 | ||
| 43 | return id_; | 43 | return id_; |
| 44 | } | 44 | } |
| 45 | 45 | ||
| 46 | std::string getText() const | 46 | std::string getText() const |
| 47 | { | 47 | { |
| 48 | if (!valid_) | 48 | if (!valid_) |
| 49 | { | 49 | { |
| 50 | throw std::domain_error("Bad access to uninitialized form"); | 50 | throw std::domain_error("Bad access to uninitialized form"); |
| 51 | } | 51 | } |
| 52 | 52 | ||
| 53 | return text_; | 53 | return text_; |
| 54 | } | 54 | } |
| 55 | 55 | ||
| 56 | int getComplexity() const | 56 | int getComplexity() const |
| 57 | { | 57 | { |
| 58 | if (!valid_) | 58 | if (!valid_) |
| 59 | { | 59 | { |
| 60 | throw std::domain_error("Bad access to uninitialized form"); | 60 | throw std::domain_error("Bad access to uninitialized form"); |
| 61 | } | 61 | } |
| 62 | 62 | ||
| 63 | return complexity_; | 63 | return complexity_; |
| 64 | } | 64 | } |
| 65 | 65 | ||
| 66 | bool isProper() const | 66 | bool isProper() const |
| 67 | { | 67 | { |
| 68 | if (!valid_) | 68 | if (!valid_) |
| 69 | { | 69 | { |
| 70 | throw std::domain_error("Bad access to uninitialized form"); | 70 | throw std::domain_error("Bad access to uninitialized form"); |
| 71 | } | 71 | } |
| 72 | 72 | ||
| 73 | return proper_; | 73 | return proper_; |
| 74 | } | 74 | } |
| 75 | 75 | ||
| 76 | const std::vector<pronunciation>& getPronunciations() const; | 76 | const std::vector<pronunciation>& getPronunciations() const; |
| 77 | 77 | ||
| 78 | // Convenience | ||
| 79 | |||
| 80 | bool startsWithVowelSound() const; | ||
| 81 | |||
| 78 | // Type info | 82 | // Type info |
| 79 | 83 | ||
| 80 | static const object objectType; | 84 | static const object objectType; |
| 81 | 85 | ||
| 82 | static const std::list<std::string> select; | 86 | static const std::list<std::string> select; |
| 83 | 87 | ||
| 84 | // Query fields | 88 | // Query fields |
| 85 | 89 | ||
| 86 | static const field id; | 90 | static const field id; |
| 87 | static const field text; | 91 | static const field text; |
| 88 | static const field complexity; | 92 | static const field complexity; |
| 89 | static const field proper; | 93 | static const field proper; |
| 90 | 94 | ||
| 91 | operator filter() const | 95 | operator filter() const |
| 92 | { | 96 | { |
| 93 | if (!valid_) | 97 | if (!valid_) |
| 94 | { | 98 | { |
| 95 | throw std::domain_error("Bad access to uninitialized form"); | 99 | throw std::domain_error("Bad access to uninitialized form"); |
| 96 | } | 100 | } |
| 97 | 101 | ||
| 98 | return (id == id_); | 102 | return (id == id_); |
| 99 | } | 103 | } |
| 100 | 104 | ||
| 101 | // Relationships to other objects | 105 | // Relationships to other objects |
| 102 | 106 | ||
| 103 | static const field pronunciation; | 107 | static const field pronunciation; |
| 104 | 108 | ||
| 105 | class inflection_field { | 109 | class inflection_field { |
| 106 | public: | 110 | public: |
| 107 | 111 | ||
| 108 | inflection_field(inflection category) : category_(category) | 112 | inflection_field(inflection category) : category_(category) |
| 109 | { | 113 | { |
| 110 | } | 114 | } |
| 111 | 115 | ||
| 112 | const inflection getCategory() const | 116 | const inflection getCategory() const |
| 113 | { | 117 | { |
| 114 | return category_; | 118 | return category_; |
| 115 | } | 119 | } |
| 116 | 120 | ||
| 117 | private: | 121 | private: |
| 118 | 122 | ||
| 119 | const inflection category_; | 123 | const inflection category_; |
| 120 | }; | 124 | }; |
| 121 | 125 | ||
| 122 | static const inflection_field lemma(inflection category) | 126 | static const inflection_field lemma(inflection category) |
| 123 | { | 127 | { |
| 124 | return inflection_field(category); | 128 | return inflection_field(category); |
| 125 | } | 129 | } |
| 126 | 130 | ||
| 127 | friend filter operator%=(form::inflection_field check, filter joinCondition); | 131 | friend filter operator%=(form::inflection_field check, filter joinCondition); |
| 128 | 132 | ||
| 129 | private: | 133 | private: |
| 130 | bool valid_ = false; | 134 | bool valid_ = false; |
| 131 | 135 | ||
| 132 | int id_; | 136 | int id_; |
| 133 | std::string text_; | 137 | std::string text_; |
| 134 | int complexity_ ; | 138 | int complexity_ ; |
| 135 | bool proper_; | 139 | bool proper_; |
| 136 | 140 | ||
| 137 | const database* db_; | 141 | const database* db_; |
| 138 | 142 | ||
| 139 | mutable bool initializedPronunciations_ = false; | 143 | mutable bool initializedPronunciations_ = false; |
| 140 | mutable std::vector<class pronunciation> pronunciations_; | 144 | mutable std::vector<class pronunciation> pronunciations_; |
| 141 | 145 | ||
| 142 | static const field lemmaJoin; | 146 | static const field lemmaJoin; |
| 143 | static const field inflectionCategory; | 147 | static const field inflectionCategory; |
| 144 | 148 | ||
| 145 | }; | 149 | }; |
| 146 | 150 | ||
| 147 | }; | 151 | }; |
| 148 | 152 | ||
| 149 | #endif /* end of include guard: FORM_H_3A6C962C */ | 153 | #endif /* end of include guard: FORM_H_3A6C962C */ |
