From a7645346293ed6a912c26d0c50b6f7943f1f3072 Mon Sep 17 00:00:00 2001 From: Kelly Rauchenberger Date: Sat, 28 Jan 2017 12:59:42 -0500 Subject: Restructured verb frame schema to be more queryable Groups are much less significant now, and they no longer have a database table, nor are they considered a top level object anymore. Instead of containing their own role data, that data is folded into the frames so that it's easier to query; as a result, each group has its own copy of the frames that it contains. Additionally, parts are considered top level objects now, and you can query for frames based on attributes of their indexed parts. Synrestrs are also contained in their own table now, so that parts can be filtered against their synrestrs; they are however not considered top level objects. Created a new type of field, the "join where" or "condition join" field, which is a normal join field that has a built in condition on a specified field. This is used to allow creating multiple distinct join fields from one object to another. This is required for the lemma::form and frame::part joins, because filters for forms of separate inflections should not be coalesced; similarly, filters on differently indexed frame parts should not be coalesced. Queries can now be ordered, ascending or descending, by a field, in addition to randomly as before. This is necessary for accessing the parts of a verb frame in the correct order, but may be useful to an end user as well. Fixed a bug with statement generation in that condition groups were not being surrounded in parentheses, which made mixing OR groups and AND groups generate inaccurate statements. This has been fixed; additionally, parentheses are not placed around the top level condition, and nested condition groups with the same logic type are coalesced, to make query strings as easy to read as possible. Also simplified the form::lemma field; it no longer conditions on the inflection of the form like the lemma::form field does. Also added a debug flag to statement::getQueryString that makes it return a query string with all of the bindings filled in, for debug use only. --- lib/part.cpp | 128 ++++++++++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 100 insertions(+), 28 deletions(-) (limited to 'lib/part.cpp') diff --git a/lib/part.cpp b/lib/part.cpp index e66d151..1fbb24d 100644 --- a/lib/part.cpp +++ b/lib/part.cpp @@ -1,12 +1,30 @@ #include "part.h" #include +#include #include "selrestr.h" +#include "database.h" namespace verbly { + const object part::objectType = object::part; + + const std::list part::select = {"part_id", "frame_id", "part_index", "type", "role", "selrestrs", "prepositions", "preposition_literality", "literal_value"}; + + const field part::index = field::integerField(object::part, "part_index"); + const field part::type = field::integerField(object::part, "type"); + + const field part::role = field::stringField(object::part, "role", true); + + const field part::frame = field::joinField(object::part, "frame_id", object::frame); + + const field part::synrestr_field::synrestrJoin = field::joinField(object::part, "part_id", "synrestrs"); + const field part::synrestr_field::synrestrField = field::stringField("synrestrs", "synrestr"); + + const part::synrestr_field part::synrestr = {}; + part part::createNounPhrase(std::string role, selrestr selrestrs, std::set synrestrs) { - part p(type::noun_phrase); + part p(part_type::noun_phrase); new(&p.noun_phrase_.role) std::string(std::move(role)); new(&p.noun_phrase_.selrestrs) selrestr(std::move(selrestrs)); @@ -17,12 +35,12 @@ namespace verbly { part part::createVerb() { - return part(type::verb); + return part(part_type::verb); } part part::createPreposition(std::vector choices, bool literal) { - part p(type::preposition); + part p(part_type::preposition); new(&p.preposition_.choices) std::vector(std::move(choices)); p.preposition_.literal = literal; @@ -32,30 +50,79 @@ namespace verbly { part part::createAdjective() { - return part(type::adjective); + return part(part_type::adjective); } part part::createAdverb() { - return part(type::adverb); + return part(part_type::adverb); } part part::createLiteral(std::string value) { - part p(type::literal); + part p(part_type::literal); new(&p.literal_) std::string(std::move(value)); return p; } + part::part(const database& db, sqlite3_stmt* row) + { + int id = sqlite3_column_int(row, 0); + + type_ = static_cast(sqlite3_column_int(row, 3)); + + switch (type_) + { + case part_type::noun_phrase: + { + new(&noun_phrase_.role) std::string(reinterpret_cast(sqlite3_column_blob(row, 4))); + new(&noun_phrase_.selrestrs) selrestr(nlohmann::json::parse(reinterpret_cast(sqlite3_column_blob(row, 5)))); + new(&noun_phrase_.synrestrs) std::set(db.synrestrs(id)); + + break; + } + + case part_type::preposition: + { + new(&preposition_.choices) std::vector(); + preposition_.literal = (sqlite3_column_int(row, 7) == 1); + + std::string choicesJsonStr(reinterpret_cast(sqlite3_column_blob(row, 6))); + nlohmann::json choicesJson = nlohmann::json::parse(std::move(choicesJsonStr)); + for (const nlohmann::json& choiceJson : choicesJson) + { + preposition_.choices.push_back(choiceJson.get()); + } + + break; + } + + case part_type::literal: + { + new(&literal_) std::string(reinterpret_cast(sqlite3_column_blob(row, 8))); + + break; + } + + case part_type::verb: + case part_type::adjective: + case part_type::adverb: + case part_type::invalid: + { + break; + } + } + } + part::part(const part& other) { type_ = other.type_; switch (type_) { - case type::noun_phrase: + case part_type::noun_phrase: { new(&noun_phrase_.role) std::string(other.noun_phrase_.role); new(&noun_phrase_.selrestrs) selrestr(other.noun_phrase_.selrestrs); @@ -64,7 +131,7 @@ namespace verbly { break; } - case type::preposition: + case part_type::preposition: { new(&preposition_.choices) std::vector(other.preposition_.choices); preposition_.literal = other.preposition_.literal; @@ -72,17 +139,17 @@ namespace verbly { break; } - case type::literal: + case part_type::literal: { new(&literal_) std::string(other.literal_); break; } - case type::verb: - case type::adjective: - case type::adverb: - case type::invalid: + case part_type::verb: + case part_type::adjective: + case part_type::adverb: + case part_type::invalid: { break; } @@ -103,7 +170,7 @@ namespace verbly { void swap(part& first, part& second) { - using type = part::type; + using type = part_type; type tempType = first.type_; std::string tempRole; @@ -231,7 +298,7 @@ namespace verbly { { switch (type_) { - case type::noun_phrase: + case part_type::noun_phrase: { using string_type = std::string; using set_type = std::set; @@ -243,7 +310,7 @@ namespace verbly { break; } - case type::preposition: + case part_type::preposition: { using vector_type = std::vector; @@ -252,7 +319,7 @@ namespace verbly { break; } - case type::literal: + case part_type::literal: { using string_type = std::string; @@ -261,10 +328,10 @@ namespace verbly { break; } - case type::verb: - case type::adjective: - case type::adverb: - case type::invalid: + case part_type::verb: + case part_type::adjective: + case part_type::adverb: + case part_type::invalid: { break; } @@ -273,7 +340,7 @@ namespace verbly { std::string part::getNounRole() const { - if (type_ == type::noun_phrase) + if (type_ == part_type::noun_phrase) { return noun_phrase_.role; } else { @@ -283,7 +350,7 @@ namespace verbly { selrestr part::getNounSelrestrs() const { - if (type_ == type::noun_phrase) + if (type_ == part_type::noun_phrase) { return noun_phrase_.selrestrs; } else { @@ -293,7 +360,7 @@ namespace verbly { std::set part::getNounSynrestrs() const { - if (type_ == type::noun_phrase) + if (type_ == part_type::noun_phrase) { return noun_phrase_.synrestrs; } else { @@ -303,7 +370,7 @@ namespace verbly { bool part::nounHasSynrestr(std::string synrestr) const { - if (type_ != type::noun_phrase) + if (type_ != part_type::noun_phrase) { throw std::domain_error("part::nounHasSynrestr is only valid for noun phrase parts"); } @@ -313,7 +380,7 @@ namespace verbly { std::vector part::getPrepositionChoices() const { - if (type_ == type::preposition) + if (type_ == part_type::preposition) { return preposition_.choices; } else { @@ -323,7 +390,7 @@ namespace verbly { bool part::isPrepositionLiteral() const { - if (type_ == type::preposition) + if (type_ == part_type::preposition) { return preposition_.literal; } else { @@ -333,7 +400,7 @@ namespace verbly { std::string part::getLiteralValue() const { - if (type_ == type::literal) + if (type_ == part_type::literal) { return literal_; } else { @@ -341,4 +408,9 @@ namespace verbly { } } + filter part::synrestr_field::operator%=(std::string synrestr) const + { + return (synrestrJoin %= (synrestrField == synrestr)); + } + }; -- cgit 1.4.1