summary refs log tree commit diff stats
path: root/lib/part.cpp
diff options
context:
space:
mode:
authorKelly Rauchenberger <fefferburbia@gmail.com>2017-02-05 08:56:39 -0500
committerKelly Rauchenberger <fefferburbia@gmail.com>2017-02-05 08:56:39 -0500
commite4fa0cb86d97c23c24cd7bdd62c23f03eed312da (patch)
tree70a20fdf684b1724659196a7de8d21a4a6ca194f /lib/part.cpp
parentbea3673ae1b3d19585dec56e96dbcd8a56b96e6d (diff)
downloadverbly-e4fa0cb86d97c23c24cd7bdd62c23f03eed312da.tar.gz
verbly-e4fa0cb86d97c23c24cd7bdd62c23f03eed312da.tar.bz2
verbly-e4fa0cb86d97c23c24cd7bdd62c23f03eed312da.zip
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.
Diffstat (limited to 'lib/part.cpp')
-rw-r--r--lib/part.cpp41
1 files changed, 20 insertions, 21 deletions
diff --git a/lib/part.cpp b/lib/part.cpp index cbd951b..341d4bb 100644 --- a/lib/part.cpp +++ b/lib/part.cpp
@@ -1,14 +1,14 @@
1#include "part.h" 1#include "part.h"
2#include <stdexcept> 2#include <stdexcept>
3#include <sqlite3.h> 3#include <sqlite3.h>
4#include "selrestr.h"
5#include "database.h" 4#include "database.h"
5#include "util.h"
6 6
7namespace verbly { 7namespace verbly {
8 8
9 const object part::objectType = object::part; 9 const object part::objectType = object::part;
10 10
11 const std::list<std::string> part::select = {"part_id", "frame_id", "part_index", "type", "role", "selrestrs", "prepositions", "preposition_literality", "literal_value"}; 11 const std::list<std::string> part::select = {"part_id", "frame_id", "part_index", "type", "role", "prepositions", "preposition_literality", "literal_value"};
12 12
13 const field part::index = field::integerField(object::part, "part_index"); 13 const field part::index = field::integerField(object::part, "part_index");
14 const field part::type = field::integerField(object::part, "type"); 14 const field part::type = field::integerField(object::part, "type");
@@ -17,17 +17,21 @@ namespace verbly {
17 17
18 const field part::frames = field::joinField(object::part, "frame_id", object::frame); 18 const field part::frames = field::joinField(object::part, "frame_id", object::frame);
19 19
20 const field part::selrestr_field::selrestrJoin = field::joinField(object::part, "part_id", "selrestrs");
21 const field part::selrestr_field::selrestrField = field::stringField("selrestrs", "selrestr");
22
20 const field part::synrestr_field::synrestrJoin = field::joinField(object::part, "part_id", "synrestrs"); 23 const field part::synrestr_field::synrestrJoin = field::joinField(object::part, "part_id", "synrestrs");
21 const field part::synrestr_field::synrestrField = field::stringField("synrestrs", "synrestr"); 24 const field part::synrestr_field::synrestrField = field::stringField("synrestrs", "synrestr");
22 25
26 const part::selrestr_field part::selrestrs = {};
23 const part::synrestr_field part::synrestrs = {}; 27 const part::synrestr_field part::synrestrs = {};
24 28
25 part part::createNounPhrase(std::string role, selrestr selrestrs, std::set<std::string> synrestrs) 29 part part::createNounPhrase(std::string role, std::set<std::string> selrestrs, std::set<std::string> synrestrs)
26 { 30 {
27 part p(part_type::noun_phrase); 31 part p(part_type::noun_phrase);
28 32
29 new(&p.noun_phrase_.role) std::string(std::move(role)); 33 new(&p.noun_phrase_.role) std::string(std::move(role));
30 new(&p.noun_phrase_.selrestrs) selrestr(std::move(selrestrs)); 34 new(&p.noun_phrase_.selrestrs) std::set<std::string>(std::move(selrestrs));
31 new(&p.noun_phrase_.synrestrs) std::set<std::string>(std::move(synrestrs)); 35 new(&p.noun_phrase_.synrestrs) std::set<std::string>(std::move(synrestrs));
32 36
33 return p; 37 return p;
@@ -78,7 +82,7 @@ namespace verbly {
78 case part_type::noun_phrase: 82 case part_type::noun_phrase:
79 { 83 {
80 new(&noun_phrase_.role) std::string(reinterpret_cast<const char*>(sqlite3_column_blob(row, 4))); 84 new(&noun_phrase_.role) std::string(reinterpret_cast<const char*>(sqlite3_column_blob(row, 4)));
81 new(&noun_phrase_.selrestrs) selrestr(nlohmann::json::parse(reinterpret_cast<const char*>(sqlite3_column_blob(row, 5)))); 85 new(&noun_phrase_.selrestrs) std::set<std::string>(db.selrestrs(id));
82 new(&noun_phrase_.synrestrs) std::set<std::string>(db.synrestrs(id)); 86 new(&noun_phrase_.synrestrs) std::set<std::string>(db.synrestrs(id));
83 87
84 break; 88 break;
@@ -86,22 +90,17 @@ namespace verbly {
86 90
87 case part_type::preposition: 91 case part_type::preposition:
88 { 92 {
89 new(&preposition_.choices) std::vector<std::string>(); 93 std::string serializedChoices(reinterpret_cast<const char*>(sqlite3_column_blob(row, 5)));
90 preposition_.literal = (sqlite3_column_int(row, 7) == 1); 94 new(&preposition_.choices) std::vector<std::string>(split<std::vector<std::string>>(serializedChoices, ","));
91 95
92 std::string choicesJsonStr(reinterpret_cast<const char*>(sqlite3_column_blob(row, 6))); 96 preposition_.literal = (sqlite3_column_int(row, 6) == 1);
93 nlohmann::json choicesJson = nlohmann::json::parse(std::move(choicesJsonStr));
94 for (const nlohmann::json& choiceJson : choicesJson)
95 {
96 preposition_.choices.push_back(choiceJson.get<std::string>());
97 }
98 97
99 break; 98 break;
100 } 99 }
101 100
102 case part_type::literal: 101 case part_type::literal:
103 { 102 {
104 new(&literal_) std::string(reinterpret_cast<const char*>(sqlite3_column_blob(row, 8))); 103 new(&literal_) std::string(reinterpret_cast<const char*>(sqlite3_column_blob(row, 7)));
105 104
106 break; 105 break;
107 } 106 }
@@ -125,7 +124,7 @@ namespace verbly {
125 case part_type::noun_phrase: 124 case part_type::noun_phrase:
126 { 125 {
127 new(&noun_phrase_.role) std::string(other.noun_phrase_.role); 126 new(&noun_phrase_.role) std::string(other.noun_phrase_.role);
128 new(&noun_phrase_.selrestrs) selrestr(other.noun_phrase_.selrestrs); 127 new(&noun_phrase_.selrestrs) std::set<std::string>(other.noun_phrase_.selrestrs);
129 new(&noun_phrase_.synrestrs) std::set<std::string>(other.noun_phrase_.synrestrs); 128 new(&noun_phrase_.synrestrs) std::set<std::string>(other.noun_phrase_.synrestrs);
130 129
131 break; 130 break;
@@ -174,7 +173,7 @@ namespace verbly {
174 173
175 type tempType = first.type_; 174 type tempType = first.type_;
176 std::string tempRole; 175 std::string tempRole;
177 selrestr tempSelrestrs; 176 std::set<std::string> tempSelrestrs;
178 std::set<std::string> tempSynrestrs; 177 std::set<std::string> tempSynrestrs;
179 std::vector<std::string> tempChoices; 178 std::vector<std::string> tempChoices;
180 bool tempPrepLiteral; 179 bool tempPrepLiteral;
@@ -224,7 +223,7 @@ namespace verbly {
224 case type::noun_phrase: 223 case type::noun_phrase:
225 { 224 {
226 new(&first.noun_phrase_.role) std::string(std::move(second.noun_phrase_.role)); 225 new(&first.noun_phrase_.role) std::string(std::move(second.noun_phrase_.role));
227 new(&first.noun_phrase_.selrestrs) selrestr(std::move(second.noun_phrase_.selrestrs)); 226 new(&first.noun_phrase_.selrestrs) std::set<std::string>(std::move(second.noun_phrase_.selrestrs));
228 new(&first.noun_phrase_.synrestrs) std::set<std::string>(std::move(second.noun_phrase_.synrestrs)); 227 new(&first.noun_phrase_.synrestrs) std::set<std::string>(std::move(second.noun_phrase_.synrestrs));
229 228
230 break; 229 break;
@@ -263,7 +262,7 @@ namespace verbly {
263 case type::noun_phrase: 262 case type::noun_phrase:
264 { 263 {
265 new(&second.noun_phrase_.role) std::string(std::move(tempRole)); 264 new(&second.noun_phrase_.role) std::string(std::move(tempRole));
266 new(&second.noun_phrase_.selrestrs) selrestr(std::move(tempSelrestrs)); 265 new(&second.noun_phrase_.selrestrs) std::set<std::string>(std::move(tempSelrestrs));
267 new(&second.noun_phrase_.synrestrs) std::set<std::string>(std::move(tempSynrestrs)); 266 new(&second.noun_phrase_.synrestrs) std::set<std::string>(std::move(tempSynrestrs));
268 267
269 break; 268 break;
@@ -304,7 +303,7 @@ namespace verbly {
304 using set_type = std::set<std::string>; 303 using set_type = std::set<std::string>;
305 304
306 noun_phrase_.role.~string_type(); 305 noun_phrase_.role.~string_type();
307 noun_phrase_.selrestrs.~selrestr(); 306 noun_phrase_.selrestrs.~set_type();
308 noun_phrase_.synrestrs.~set_type(); 307 noun_phrase_.synrestrs.~set_type();
309 308
310 break; 309 break;
@@ -348,7 +347,7 @@ namespace verbly {
348 } 347 }
349 } 348 }
350 349
351 selrestr part::getNounSelrestrs() const 350 std::set<std::string> part::getNounSelrestrs() const
352 { 351 {
353 if (type_ == part_type::noun_phrase) 352 if (type_ == part_type::noun_phrase)
354 { 353 {