diff options
Diffstat (limited to 'lib/frame.cpp')
| -rw-r--r-- | lib/frame.cpp | 95 |
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 | ||
| 5 | namespace verbly { | 6 | namespace 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 | }; |
