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. --- generator/group.cpp | 119 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 119 insertions(+) create mode 100644 generator/group.cpp (limited to 'generator/group.cpp') diff --git a/generator/group.cpp b/generator/group.cpp new file mode 100644 index 0000000..7cbd4c8 --- /dev/null +++ b/generator/group.cpp @@ -0,0 +1,119 @@ +#include "group.h" +#include +#include +#include +#include "database.h" +#include "field.h" +#include "frame.h" + +namespace verbly { + namespace generator { + + int group::nextId_ = 0; + + group::group() : id_(nextId_++) + { + } + + void group::setParent(const group& parent) + { + // Adding a group to itself is nonsensical. + assert(&parent != this); + + parent_ = &parent; + } + + void group::addRole(std::string name, role r) + { + roleNames_.insert(name); + roles_[name] = std::move(r); + } + + void group::addFrame(const frame& f) + { + frames_.insert(&f); + } + + std::set group::getRoles() const + { + std::set fullRoles = roleNames_; + + if (hasParent()) + { + for (std::string name : getParent().getRoles()) + { + fullRoles.insert(name); + } + } + + return fullRoles; + } + + const role& group::getRole(std::string name) const + { + if (roles_.count(name)) + { + return roles_.at(name); + } else if (hasParent()) + { + return getParent().getRole(name); + } else { + throw std::invalid_argument("Specified role not found in verb group"); + } + } + + std::set group::getFrames() const + { + std::set fullFrames = frames_; + + if (hasParent()) + { + for (const frame* f : getParent().getFrames()) + { + fullFrames.insert(f); + } + } + + return fullFrames; + } + + database& operator<<(database& db, const group& arg) + { + // Serialize the group first + { + std::list fields; + fields.emplace_back("group_id", arg.getId()); + + nlohmann::json jsonRoles; + for (std::string name : arg.getRoles()) + { + const role& r = arg.getRole(name); + + nlohmann::json jsonRole; + jsonRole["type"] = name; + jsonRole["selrestrs"] = r.getSelrestrs().toJson(); + + jsonRoles.emplace_back(std::move(jsonRole)); + } + + fields.emplace_back("data", jsonRoles.dump()); + + db.insertIntoTable("groups", std::move(fields)); + } + + // Then, serialize the group/frame relationship + for (const frame* f : arg.getFrames()) + { + std::list fields; + + fields.emplace_back("group_id", arg.getId()); + fields.emplace_back("frame_id", f->getId()); + + db.insertIntoTable("groups_frames", std::move(fields)); + } + + return db; + } + + }; +}; -- cgit 1.4.1