summary refs log tree commit diff stats
path: root/lib/frame.cpp
diff options
context:
space:
mode:
authorKelly Rauchenberger <fefferburbia@gmail.com>2017-01-28 12:59:42 -0500
committerKelly Rauchenberger <fefferburbia@gmail.com>2017-01-28 12:59:42 -0500
commita7645346293ed6a912c26d0c50b6f7943f1f3072 (patch)
treed4d144e03a5e2dfcebbad2692fa71e790719d8fd /lib/frame.cpp
parent6ba8989bbbd497f949a3e8b17abed1d0bd048347 (diff)
downloadverbly-a7645346293ed6a912c26d0c50b6f7943f1f3072.tar.gz
verbly-a7645346293ed6a912c26d0c50b6f7943f1f3072.tar.bz2
verbly-a7645346293ed6a912c26d0c50b6f7943f1f3072.zip
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.
Diffstat (limited to 'lib/frame.cpp')
-rw-r--r--lib/frame.cpp95
1 files changed, 18 insertions, 77 deletions
diff --git a/lib/frame.cpp b/lib/frame.cpp index 8cab56b..a73fbda 100644 --- a/lib/frame.cpp +++ b/lib/frame.cpp
@@ -1,95 +1,36 @@
1#include "frame.h" 1#include "frame.h"
2#include <sqlite3.h> 2#include <sqlite3.h>
3#include <json.hpp> 3#include "database.h"
4#include "query.h"
4 5
5namespace verbly { 6namespace verbly {
6 7
7 const object frame::objectType = object::frame; 8 const object frame::objectType = object::frame;
8 9
9 const std::list<std::string> frame::select = {"frame_id", "data"}; 10 const std::list<std::string> frame::select = {"frame_id", "group_id", "length"};
10 11
11 const field frame::id = field::integerField(object::frame, "frame_id"); 12 const field frame::id = field::integerField(object::frame, "frame_id");
13 const field frame::length = field::integerField(object::frame, "length");
12 14
13 const field frame::group = field::joinThrough(object::frame, "frame_id", object::group, "groups_frames", "group_id"); 15 const field frame::word = field::joinField(object::frame, "group_id", object::word);
14 16
15 frame::frame(const database& db, sqlite3_stmt* row) : db_(&db), valid_(true) 17 field frame::part()
16 { 18 {
17 id_ = sqlite3_column_int(row, 0); 19 return field::joinField(object::frame, "frame_id", object::part);
18 20 }
19 std::string partsJsonStr(reinterpret_cast<const char*>(sqlite3_column_blob(row, 1)));
20 nlohmann::json partsJson = nlohmann::json::parse(std::move(partsJsonStr));
21
22 for (const nlohmann::json& partJson : partsJson)
23 {
24 part::type partType = static_cast<part::type>(partJson["type"].get<int>());
25
26 switch (partType)
27 {
28 case part::type::noun_phrase:
29 {
30 std::set<std::string> synrestrs;
31 for (const nlohmann::json& synrestrJson : partJson["synrestrs"])
32 {
33 synrestrs.insert(synrestrJson.get<std::string>());
34 }
35
36 parts_.push_back(part::createNounPhrase(
37 partJson["role"].get<std::string>(),
38 selrestr(partJson["selrestrs"]),
39 std::move(synrestrs)));
40
41 break;
42 }
43
44 case part::type::preposition:
45 {
46 std::vector<std::string> choices;
47 for (const nlohmann::json& choiceJson : partJson["choices"])
48 {
49 choices.push_back(choiceJson.get<std::string>());
50 }
51
52 parts_.push_back(part::createPreposition(
53 std::move(choices),
54 partJson["literal"].get<bool>()));
55
56 break;
57 }
58
59 case part::type::verb:
60 {
61 parts_.push_back(part::createVerb());
62
63 break;
64 }
65
66 case part::type::adjective:
67 {
68 parts_.push_back(part::createAdjective());
69
70 break;
71 }
72
73 case part::type::adverb:
74 {
75 parts_.push_back(part::createAdverb());
76
77 break;
78 }
79 21
80 case part::type::literal: 22 field frame::part(int index)
81 { 23 {
82 parts_.push_back(part::createLiteral(partJson["value"].get<std::string>())); 24 return field::joinWhere(object::frame, "frame_id", object::part, part::index, index);
25 }
83 26
84 break; 27 frame::frame(const database& db, sqlite3_stmt* row) : db_(&db), valid_(true)
85 } 28 {
29 id_ = sqlite3_column_int(row, 0);
30 groupId_ = sqlite3_column_int(row, 1);
31 length_ = sqlite3_column_int(row, 2);
86 32
87 case part::type::invalid: 33 parts_ = db.parts(*this, verbly::part::index, -1).all();
88 {
89 throw std::domain_error("Invalid part data");
90 }
91 }
92 }
93 } 34 }
94 35
95}; 36};