diff options
author | Kelly Rauchenberger <fefferburbia@gmail.com> | 2017-01-16 18:02:50 -0500 |
---|---|---|
committer | Kelly Rauchenberger <fefferburbia@gmail.com> | 2017-01-16 18:02:50 -0500 |
commit | 6746da6edd7d9d50efe374eabbb79a3cac882d81 (patch) | |
tree | ff20917e08b08d36b9541c1371106596e7bec442 /generator/group.cpp | |
parent | 4af7e55733098ca42f75a4ffaca1b0f6bab4dd36 (diff) | |
download | verbly-6746da6edd7d9d50efe374eabbb79a3cac882d81.tar.gz verbly-6746da6edd7d9d50efe374eabbb79a3cac882d81.tar.bz2 verbly-6746da6edd7d9d50efe374eabbb79a3cac882d81.zip |
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.
Diffstat (limited to 'generator/group.cpp')
-rw-r--r-- | generator/group.cpp | 119 |
1 files changed, 119 insertions, 0 deletions
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 @@ | |||
1 | #include "group.h" | ||
2 | #include <stdexcept> | ||
3 | #include <list> | ||
4 | #include <json.hpp> | ||
5 | #include "database.h" | ||
6 | #include "field.h" | ||
7 | #include "frame.h" | ||
8 | |||
9 | namespace verbly { | ||
10 | namespace generator { | ||
11 | |||
12 | int group::nextId_ = 0; | ||
13 | |||
14 | group::group() : id_(nextId_++) | ||
15 | { | ||
16 | } | ||
17 | |||
18 | void group::setParent(const group& parent) | ||
19 | { | ||
20 | // Adding a group to itself is nonsensical. | ||
21 | assert(&parent != this); | ||
22 | |||
23 | parent_ = &parent; | ||
24 | } | ||
25 | |||
26 | void group::addRole(std::string name, role r) | ||
27 | { | ||
28 | roleNames_.insert(name); | ||
29 | roles_[name] = std::move(r); | ||
30 | } | ||
31 | |||
32 | void group::addFrame(const frame& f) | ||
33 | { | ||
34 | frames_.insert(&f); | ||
35 | } | ||
36 | |||
37 | std::set<std::string> group::getRoles() const | ||
38 | { | ||
39 | std::set<std::string> fullRoles = roleNames_; | ||
40 | |||
41 | if (hasParent()) | ||
42 | { | ||
43 | for (std::string name : getParent().getRoles()) | ||
44 | { | ||
45 | fullRoles.insert(name); | ||
46 | } | ||
47 | } | ||
48 | |||
49 | return fullRoles; | ||
50 | } | ||
51 | |||
52 | const role& group::getRole(std::string name) const | ||
53 | { | ||
54 | if (roles_.count(name)) | ||
55 | { | ||
56 | return roles_.at(name); | ||
57 | } else if (hasParent()) | ||
58 | { | ||
59 | return getParent().getRole(name); | ||
60 | } else { | ||
61 | throw std::invalid_argument("Specified role not found in verb group"); | ||
62 | } | ||
63 | } | ||
64 | |||
65 | std::set<const frame*> group::getFrames() const | ||
66 | { | ||
67 | std::set<const frame*> fullFrames = frames_; | ||
68 | |||
69 | if (hasParent()) | ||
70 | { | ||
71 | for (const frame* f : getParent().getFrames()) | ||
72 | { | ||
73 | fullFrames.insert(f); | ||
74 | } | ||
75 | } | ||
76 | |||
77 | return fullFrames; | ||
78 | } | ||
79 | |||
80 | database& operator<<(database& db, const group& arg) | ||
81 | { | ||
82 | // Serialize the group first | ||
83 | { | ||
84 | std::list<field> fields; | ||
85 | fields.emplace_back("group_id", arg.getId()); | ||
86 | |||
87 | nlohmann::json jsonRoles; | ||
88 | for (std::string name : arg.getRoles()) | ||
89 | { | ||
90 | const role& r = arg.getRole(name); | ||
91 | |||
92 | nlohmann::json jsonRole; | ||
93 | jsonRole["type"] = name; | ||
94 | jsonRole["selrestrs"] = r.getSelrestrs().toJson(); | ||
95 | |||
96 | jsonRoles.emplace_back(std::move(jsonRole)); | ||
97 | } | ||
98 | |||
99 | fields.emplace_back("data", jsonRoles.dump()); | ||
100 | |||
101 | db.insertIntoTable("groups", std::move(fields)); | ||
102 | } | ||
103 | |||
104 | // Then, serialize the group/frame relationship | ||
105 | for (const frame* f : arg.getFrames()) | ||
106 | { | ||
107 | std::list<field> fields; | ||
108 | |||
109 | fields.emplace_back("group_id", arg.getId()); | ||
110 | fields.emplace_back("frame_id", f->getId()); | ||
111 | |||
112 | db.insertIntoTable("groups_frames", std::move(fields)); | ||
113 | } | ||
114 | |||
115 | return db; | ||
116 | } | ||
117 | |||
118 | }; | ||
119 | }; | ||