From e4fa0cb86d97c23c24cd7bdd62c23f03eed312da Mon Sep 17 00:00:00 2001 From: Kelly Rauchenberger Date: Sun, 5 Feb 2017 08:56:39 -0500 Subject: Flattened selrestrs Now, selrestrs are, instead of logically being a tree of positive/negative restrictions that are ANDed/ORed together, they are a flat set of positive restrictions that are ORed together. They are stored as strings in a table called selrestrs, just like synrestrs, which makes them a lot more queryable now as well. This change required some changes to the VerbNet data, because we needed to consolidate any ANDed clauses into single selrestrs, as well as convert any negative selrestrs into positive ones. The changes made are detailed on the wiki. Preposition choices are now encoded as comma-separated lists instead of using JSON. This change, along with the selrestrs one, allows us to remove verbly's dependency on nlohmann::json. --- lib/selrestr.cpp | 309 ------------------------------------------------------- 1 file changed, 309 deletions(-) delete mode 100644 lib/selrestr.cpp (limited to 'lib/selrestr.cpp') diff --git a/lib/selrestr.cpp b/lib/selrestr.cpp deleted file mode 100644 index 8646871..0000000 --- a/lib/selrestr.cpp +++ /dev/null @@ -1,309 +0,0 @@ -#include "selrestr.h" - -namespace verbly { - - selrestr::selrestr(nlohmann::json data) - { - if (data.find("children") != data.end()) - { - type_ = type::group; - new(&group_.children) std::list(); - - for (const nlohmann::json& child : data["children"]) - { - group_.children.emplace_back(child); - } - - group_.orlogic = (data["logic"] == "or"); - } else if (data.find("type") != data.end()) - { - type_ = type::singleton; - singleton_.pos = data["pos"].get(); - new(&singleton_.restriction) std::string(data["type"].get()); - } else { - type_ = type::empty; - } - } - - selrestr::selrestr(const selrestr& other) - { - type_ = other.type_; - - switch (type_) - { - case type::singleton: - { - singleton_.pos = other.singleton_.pos; - new(&singleton_.restriction) std::string(other.singleton_.restriction); - - break; - } - - case type::group: - { - new(&group_.children) std::list(other.group_.children); - group_.orlogic = other.group_.orlogic; - - break; - } - - case type::empty: - { - break; - } - } - } - - selrestr::selrestr(selrestr&& other) : selrestr() - { - swap(*this, other); - } - - selrestr& selrestr::operator=(selrestr other) - { - swap(*this, other); - - return *this; - } - - void swap(selrestr& first, selrestr& second) - { - using type = selrestr::type; - - type tempType = first.type_; - int tempPos; - std::string tempRestriction; - std::list tempChildren; - bool tempOrlogic; - - switch (tempType) - { - case type::singleton: - { - tempPos = first.singleton_.pos; - tempRestriction = std::move(first.singleton_.restriction); - - break; - } - - case type::group: - { - tempChildren = std::move(first.group_.children); - tempOrlogic = first.group_.orlogic; - - break; - } - - case type::empty: - { - break; - } - } - - first.~selrestr(); - - first.type_ = second.type_; - - switch (first.type_) - { - case type::singleton: - { - first.singleton_.pos = second.singleton_.pos; - new(&first.singleton_.restriction) std::string(std::move(second.singleton_.restriction)); - - break; - } - - case type::group: - { - new(&first.group_.children) std::list(std::move(second.group_.children)); - first.group_.orlogic = second.group_.orlogic; - - break; - } - - case type::empty: - { - break; - } - } - - second.~selrestr(); - - second.type_ = tempType; - - switch (second.type_) - { - case type::singleton: - { - second.singleton_.pos = tempPos; - new(&second.singleton_.restriction) std::string(std::move(tempRestriction)); - - break; - } - - case type::group: - { - new(&second.group_.children) std::list(std::move(tempChildren)); - second.group_.orlogic = tempOrlogic; - - break; - } - - case type::empty: - { - break; - } - } - } - - selrestr::~selrestr() - { - switch (type_) - { - case type::singleton: - { - using string_type = std::string; - singleton_.restriction.~string_type(); - - break; - } - - case type::group: - { - using list_type = std::list; - group_.children.~list_type(); - - break; - } - - case type::empty: - { - break; - } - } - } - - selrestr::selrestr() : type_(type::empty) - { - } - - selrestr::selrestr( - std::string restriction, - bool pos) : - type_(type::singleton) - { - new(&singleton_.restriction) std::string(std::move(restriction)); - singleton_.pos = pos; - } - - std::string selrestr::getRestriction() const - { - if (type_ == type::singleton) - { - return singleton_.restriction; - } else { - throw std::domain_error("Only singleton selrestrs have restrictions"); - } - } - - bool selrestr::getPos() const - { - if (type_ == type::singleton) - { - return singleton_.pos; - } else { - throw std::domain_error("Only singleton selrestrs have positivity flags"); - } - } - - selrestr::selrestr( - std::list children, - bool orlogic) : - type_(type::group) - { - new(&group_.children) std::list(std::move(children)); - group_.orlogic = orlogic; - } - - std::list selrestr::getChildren() const - { - if (type_ == type::group) - { - return group_.children; - } else { - throw std::domain_error("Only group selrestrs have children"); - } - } - - std::list::const_iterator selrestr::begin() const - { - if (type_ == type::group) - { - return std::begin(group_.children); - } else { - throw std::domain_error("Only group selrestrs have children"); - } - } - - std::list::const_iterator selrestr::end() const - { - if (type_ == type::group) - { - return std::end(group_.children); - } else { - throw std::domain_error("Only group selrestrs have children"); - } - } - - bool selrestr::getOrlogic() const - { - if (type_ == type::group) - { - return group_.orlogic; - } else { - throw std::domain_error("Only group selrestrs have logic"); - } - } - - nlohmann::json selrestr::toJson() const - { - switch (type_) - { - case type::empty: - { - return {}; - } - - case type::singleton: - { - return { - {"type", singleton_.restriction}, - {"pos", singleton_.pos} - }; - } - - case type::group: - { - std::string logic; - if (group_.orlogic) - { - logic = "or"; - } else { - logic = "and"; - } - - std::list children; - std::transform(std::begin(group_.children), std::end(group_.children), std::back_inserter(children), [] (const selrestr& child) { - return child.toJson(); - }); - - return { - {"logic", logic}, - {"children", children} - }; - } - } - } - -}; -- cgit 1.4.1