From 6746da6edd7d9d50efe374eabbb79a3cac882d81 Mon Sep 17 00:00:00 2001 From: Kelly Rauchenberger Date: Mon, 16 Jan 2017 18:02:50 -0500 Subject: Started structural rewrite The new object structure was designed to build on the existing WordNet structure, while also adding in all of the data that we get from other sources. More information about this can be found on the project wiki. The generator has already been completely rewritten to generate a datafile that uses the new structure. In addition, a number of indexes are created, which does double the size of the datafile, but also allows for much faster lookups. Finally, the new generator is written modularly and is a lot more readable than the old one. The verbly interface to the new object structure has mostly been completed, but has not been tested fully. There is a completely new search API which utilizes a lot of operator overloading; documentation on how to use it should go up at some point. Token processing and verb frames are currently unimplemented. Source for these have been left in the repository for now. --- lib/word.cpp | 120 ++++++++++++++++++++++++++++++++++++++++++----------------- 1 file changed, 86 insertions(+), 34 deletions(-) (limited to 'lib/word.cpp') diff --git a/lib/word.cpp b/lib/word.cpp index 49e34a1..3edf2d2 100644 --- a/lib/word.cpp +++ b/lib/word.cpp @@ -1,60 +1,112 @@ -#include "verbly.h" -#include +#include "word.h" +#include +#include "form.h" +#include "util.h" +#include "database.h" +#include "query.h" namespace verbly { - rhyme::rhyme(std::string prerhyme, std::string phonemes) : _prerhyme(prerhyme), _rhyme(phonemes) - { - - } + const object word::objectType = object::word; - std::string rhyme::get_prerhyme() const - { - return _prerhyme; - } + const std::list word::select = {"word_id", "notion_id", "lemma_id", "tag_count", "position", "group_id"}; - std::string rhyme::get_rhyme() const - { - return _rhyme; - } + const field word::id = field::integerField(object::word, "word_id"); + const field word::tagCount = field::integerField(object::word, "tag_count", true); + const field word::adjectivePosition = field::integerField(object::word, "position", true); + + const field word::notion = field::joinField(object::word, "notion_id", object::notion); + const field word::lemma = field::joinField(object::word, "lemma_id", object::lemma); + const field word::group = field::joinField(object::word, "group_id", object::group, true); + + const field word::antonyms = field::selfJoin(object::word, "word_id", "antonymy", "antonym_2_id", "antonym_1_id"); + + const field word::specifications = field::selfJoin(object::word, "word_id", "specification", "general_id", "specific_id"); + const field word::generalizations = field::selfJoin(object::word, "word_id", "specification", "specific_id", "general_id"); - bool rhyme::operator==(const rhyme& other) const + const field word::pertainyms = field::selfJoin(object::word, "word_id", "pertainymy", "noun_id", "pertainym_id"); + const field word::antiPertainyms = field::selfJoin(object::word, "word_id", "pertainymy", "pertainym_id", "noun_id"); + + const field word::mannernyms = field::selfJoin(object::word, "word_id", "mannernymy", "adjective_id", "mannernym_id"); + const field word::antiMannernyms = field::selfJoin(object::word, "word_id", "mannernymy", "mannernym_id", "adjective_id"); + + const field word::usageTerms = field::selfJoin(object::word, "word_id", "usage", "domain_id", "term_id"); + const field word::usageDomains = field::selfJoin(object::word, "word_id", "usage", "term_id", "domain_id"); + + const field word::topicalTerms = field::selfJoin(object::word, "word_id", "topicality", "domain_id", "term_id"); + const field word::topicalDomains = field::selfJoin(object::word, "word_id", "topicality", "term_id", "domain_id"); + + const field word::regionalTerms = field::selfJoin(object::word, "word_id", "regionality", "domain_id", "term_id"); + const field word::regionalDomains = field::selfJoin(object::word, "word_id", "regionality", "term_id", "domain_id"); + + word::word(const database& db, sqlite3_stmt* row) : db_(&db), valid_(true) { - return std::tie(_prerhyme, _rhyme) == std::tie(other._prerhyme, other._rhyme); + id_ = sqlite3_column_int(row, 0); + notionId_ = sqlite3_column_int(row, 1); + lemmaId_ = sqlite3_column_int(row, 2); + + if (sqlite3_column_type(row, 3) != SQLITE_NULL) + { + hasTagCount_ = true; + tagCount_ = sqlite3_column_int(row, 3); + } + + if (sqlite3_column_type(row, 4) != SQLITE_NULL) + { + adjectivePosition_ = static_cast(sqlite3_column_int(row, 4)); + } + + if (sqlite3_column_type(row, 5) != SQLITE_NULL) + { + hasGroup_ = true; + groupId_ = sqlite3_column_int(row, 5); + } } - word::word() + const notion& word::getNotion() const { + if (!valid_) + { + throw std::domain_error("Bad access to uninitialized word"); + } + + if (!notion_) + { + notion_ = db_->notions(notion::id == notionId_).first(); + } + return notion_; } - word::word(const data& _data, int _id) : _data(&_data), _id(_id), _valid(true) + const lemma& word::getLemma() const { + if (!valid_) + { + throw std::domain_error("Bad access to uninitialized word"); + } + if (!lemma_) + { + lemma_ = db_->lemmas(lemma::id == lemmaId_).first(); + } + + return lemma_; } - std::list word::get_rhymes() const + std::string word::getBaseForm() const { - assert(_valid == true); - - return rhymes; + return getLemma().getBaseForm().getText(); } - bool word::starts_with_vowel_sound() const + std::list word::getInflections(inflection category) const { - assert(_valid == true); - - if (pronunciations.size() > 0) + std::list result; + for (const form& infl : getLemma().getInflections(category)) { - return std::any_of(std::begin(pronunciations), std::end(pronunciations), [] (std::list phonemes) { - return (phonemes.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 = tolower(base_form().front()); - return (ch == 'a') || (ch == 'e') || (ch == 'i') || (ch == 'o') || (ch == 'u'); + result.push_back(infl.getText()); } + + return result; } }; -- cgit 1.4.1