From c4743dd4dd15681b1ff9d2be64c8307d0dce53b9 Mon Sep 17 00:00:00 2001 From: Kelly Rauchenberger Date: Mon, 23 Jan 2017 12:03:51 -0500 Subject: Added form::startsWithVowelSound convenience method --- lib/form.cpp | 44 +++++++++++++++++++++++-------- 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 @@ #include "form.h" #include #include "filter.h" -#include "pronunciation.h" #include "database.h" #include "query.h" +#include "util.h" namespace verbly { - + const object form::objectType = object::form; - + const std::list form::select = {"form_id", "form", "complexity", "proper"}; - + const field form::id = field::integerField(object::form, "form_id"); const field form::text = field::stringField(object::form, "form"); const field form::complexity = field::integerField(object::form, "complexity"); const field form::proper = field::booleanField(object::form, "proper"); - + const field form::pronunciation = field::joinThrough(object::form, "form_id", object::pronunciation, "forms_pronunciations", "pronunciation_id"); - + const field form::lemmaJoin = field::joinField(object::form, "form_id", object::lemma); const field form::inflectionCategory = field::integerField("lemmas_forms", "category"); - + form::form(const database& db, sqlite3_stmt* row) : db_(&db), valid_(true) { id_ = sqlite3_column_int(row, 0); @@ -28,7 +28,7 @@ namespace verbly { complexity_ = sqlite3_column_int(row, 2); proper_ = (sqlite3_column_int(row, 3) == 1); } - + filter operator%=(form::inflection_field check, filter joinCondition) { return (form::lemmaJoin %= (joinCondition && (form::inflectionCategory == check.getCategory()))); @@ -40,14 +40,36 @@ namespace verbly { { throw std::domain_error("Bad access to uninitialized form"); } - + if (!initializedPronunciations_) { pronunciations_ = db_->pronunciations(pronunciation::form %= *this, false, -1).all(); initializedPronunciations_ = true; } - + return pronunciations_; } - + + bool form::startsWithVowelSound() const + { + if (!valid_) + { + throw std::domain_error("Bad access to uninitialized form"); + } + + const std::vector& pronunciations = getPronunciations(); + if (!pronunciations.empty()) + { + return std::any_of(std::begin(pronunciations), std::end(pronunciations), [] (const verbly::pronunciation& p) { + std::cout << "phonemes: " << implode(std::begin(p.getPhonemes()), std::end(p.getPhonemes()), ",") << std::endl; + return p.getPhonemes().front().find_first_of("012") != std::string::npos; + }); + } else { + // If the word is not in CMUDICT, fall back to checking whether the first letter is a vowel. + // Not perfect but will work in most cases. + char ch = std::tolower(text_.front()); + return (ch == 'a') || (ch == 'e') || (ch == 'i') || (ch == 'o') || (ch == 'u'); + } + } + }; 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 @@ #include #include #include "field.h" +#include "pronunciation.h" #include "filter.h" struct sqlite3_stmt; namespace verbly { - - class pronunciation; + class database; - + class form { public: - + // Default constructor - + form() = default; - + // Construct from database - + form(const database& db, sqlite3_stmt* row); - + // Accessors - + operator bool() const { return valid_; } - + int getId() const { if (!valid_) { throw std::domain_error("Bad access to uninitialized form"); } - + return id_; } - + std::string getText() const { if (!valid_) { throw std::domain_error("Bad access to uninitialized form"); } - + return text_; } - + int getComplexity() const { if (!valid_) { throw std::domain_error("Bad access to uninitialized form"); } - + return complexity_; } - + bool isProper() const { if (!valid_) { throw std::domain_error("Bad access to uninitialized form"); } - + return proper_; } - + const std::vector& getPronunciations() const; - + + // Convenience + + bool startsWithVowelSound() const; + // Type info - + static const object objectType; - + static const std::list select; - + // Query fields - + static const field id; static const field text; static const field complexity; static const field proper; - + operator filter() const { if (!valid_) { throw std::domain_error("Bad access to uninitialized form"); } - + return (id == id_); } - + // Relationships to other objects - + static const field pronunciation; - + class inflection_field { public: - + inflection_field(inflection category) : category_(category) { } - + const inflection getCategory() const { return category_; } private: - + const inflection category_; }; - + static const inflection_field lemma(inflection category) { return inflection_field(category); } - + friend filter operator%=(form::inflection_field check, filter joinCondition); - + private: bool valid_ = false; - + int id_; std::string text_; int complexity_ ; bool proper_; - + const database* db_; - + mutable bool initializedPronunciations_ = false; mutable std::vector pronunciations_; - + static const field lemmaJoin; static const field inflectionCategory; - + }; - + }; #endif /* end of include guard: FORM_H_3A6C962C */ -- cgit 1.4.1