diff options
Diffstat (limited to 'lib/frame_query.cpp')
| -rw-r--r-- | lib/frame_query.cpp | 166 |
1 files changed, 0 insertions, 166 deletions
| diff --git a/lib/frame_query.cpp b/lib/frame_query.cpp deleted file mode 100644 index 11f0432..0000000 --- a/lib/frame_query.cpp +++ /dev/null | |||
| @@ -1,166 +0,0 @@ | |||
| 1 | #include "verbly.h" | ||
| 2 | #include <json.hpp> | ||
| 3 | |||
| 4 | using json = nlohmann::json; | ||
| 5 | |||
| 6 | namespace verbly { | ||
| 7 | |||
| 8 | frame_query::frame_query(const data& _data) : _data(_data) | ||
| 9 | { | ||
| 10 | |||
| 11 | } | ||
| 12 | |||
| 13 | frame_query& frame_query::for_verb(const verb& _v) | ||
| 14 | { | ||
| 15 | _for_verb.push_back(_v); | ||
| 16 | |||
| 17 | return *this; | ||
| 18 | } | ||
| 19 | |||
| 20 | frame::selrestr parse_selrestr(const json data) | ||
| 21 | { | ||
| 22 | if (data.find("children") != data.end()) | ||
| 23 | { | ||
| 24 | std::list<frame::selrestr> children; | ||
| 25 | std::transform(std::begin(data["children"]), std::end(data["children"]), std::back_inserter(children), &parse_selrestr); | ||
| 26 | |||
| 27 | return frame::selrestr{children, data["logic"] == "or"}; | ||
| 28 | } else if (data.find("type") != data.end()) | ||
| 29 | { | ||
| 30 | return frame::selrestr{data["type"].get<std::string>(), data["pos"].get<bool>()}; | ||
| 31 | } else { | ||
| 32 | return frame::selrestr{}; | ||
| 33 | } | ||
| 34 | } | ||
| 35 | |||
| 36 | std::list<frame> frame_query::run() const | ||
| 37 | { | ||
| 38 | std::stringstream construct; | ||
| 39 | construct << "SELECT frames.data, groups.data FROM frames INNER JOIN groups ON frames.group_id = groups.group_id"; | ||
| 40 | std::list<binding> bindings; | ||
| 41 | |||
| 42 | if (!_for_verb.empty()) | ||
| 43 | { | ||
| 44 | std::list<std::string> clauses(_for_verb.size(), "verb_id = ?"); | ||
| 45 | construct << " WHERE frames.group_id IN (SELECT group_id FROM verb_groups WHERE "; | ||
| 46 | construct << verbly::implode(std::begin(clauses), std::end(clauses), " OR "); | ||
| 47 | construct << ")"; | ||
| 48 | |||
| 49 | for (auto v : _for_verb) | ||
| 50 | { | ||
| 51 | bindings.emplace_back(v._id); | ||
| 52 | } | ||
| 53 | } | ||
| 54 | |||
| 55 | sqlite3_stmt* ppstmt; | ||
| 56 | std::string query = construct.str(); | ||
| 57 | if (sqlite3_prepare_v2(_data.ppdb, query.c_str(), query.length(), &ppstmt, NULL) != SQLITE_OK) | ||
| 58 | { | ||
| 59 | throw std::runtime_error(sqlite3_errmsg(_data.ppdb)); | ||
| 60 | } | ||
| 61 | |||
| 62 | int i = 1; | ||
| 63 | for (auto& binding : bindings) | ||
| 64 | { | ||
| 65 | switch (binding.get_type()) | ||
| 66 | { | ||
| 67 | case binding::type::integer: | ||
| 68 | { | ||
| 69 | sqlite3_bind_int(ppstmt, i, binding.get_integer()); | ||
| 70 | |||
| 71 | break; | ||
| 72 | } | ||
| 73 | |||
| 74 | case binding::type::string: | ||
| 75 | { | ||
| 76 | sqlite3_bind_text(ppstmt, i, binding.get_string().c_str(), binding.get_string().length(), SQLITE_TRANSIENT); | ||
| 77 | |||
| 78 | break; | ||
| 79 | } | ||
| 80 | } | ||
| 81 | |||
| 82 | i++; | ||
| 83 | } | ||
| 84 | |||
| 85 | std::list<frame> output; | ||
| 86 | while (sqlite3_step(ppstmt) == SQLITE_ROW) | ||
| 87 | { | ||
| 88 | frame f; | ||
| 89 | |||
| 90 | std::string fdatat(reinterpret_cast<const char*>(sqlite3_column_blob(ppstmt, 0))); | ||
| 91 | const json fdata = json::parse(fdatat); | ||
| 92 | for (const auto& part : fdata) | ||
| 93 | { | ||
| 94 | frame::part p; | ||
| 95 | |||
| 96 | if (part["type"] == "np") | ||
| 97 | { | ||
| 98 | p._type = frame::part::type::noun_phrase; | ||
| 99 | new(&p._noun_phrase.role) std::string(part["role"].get<std::string>()); | ||
| 100 | new(&p._noun_phrase.selrestrs) frame::selrestr(parse_selrestr(part["selrestrs"])); | ||
| 101 | new(&p._noun_phrase.synrestrs) std::set<std::string>(); | ||
| 102 | for (auto synrestr : part["synrestrs"]) | ||
| 103 | { | ||
| 104 | p._noun_phrase.synrestrs.insert(synrestr.get<std::string>()); | ||
| 105 | } | ||
| 106 | } else if (part["type"] == "pp") | ||
| 107 | { | ||
| 108 | if (!part["values"].empty()) | ||
| 109 | { | ||
| 110 | p._type = frame::part::type::literal_preposition; | ||
| 111 | new(&p._literal_preposition.choices) std::vector<std::string>(); | ||
| 112 | for (auto choice : part["values"]) | ||
| 113 | { | ||
| 114 | p._literal_preposition.choices.push_back(choice.get<std::string>()); | ||
| 115 | } | ||
| 116 | } else if (!part["preprestrs"].empty()) | ||
| 117 | { | ||
| 118 | p._type = frame::part::type::selection_preposition; | ||
| 119 | new(&p._selection_preposition.preprestrs) std::vector<std::string>(); | ||
| 120 | for (auto preprestr : part["preprestrs"]) | ||
| 121 | { | ||
| 122 | p._selection_preposition.preprestrs.push_back(preprestr.get<std::string>()); | ||
| 123 | } | ||
| 124 | } | ||
| 125 | } else if (part["type"] == "v") | ||
| 126 | { | ||
| 127 | p._type = frame::part::type::verb; | ||
| 128 | } else if (part["type"] == "adj") | ||
| 129 | { | ||
| 130 | p._type = frame::part::type::adjective; | ||
| 131 | } else if (part["type"] == "adv") | ||
| 132 | { | ||
| 133 | p._type = frame::part::type::adverb; | ||
| 134 | } else if (part["type"] == "lex") | ||
| 135 | { | ||
| 136 | p._type = frame::part::type::literal; | ||
| 137 | new(&p._literal.lexval) std::string(part["value"].get<std::string>()); | ||
| 138 | } | ||
| 139 | |||
| 140 | f._parts.push_back(p); | ||
| 141 | } | ||
| 142 | |||
| 143 | std::string rdatat(reinterpret_cast<const char*>(sqlite3_column_blob(ppstmt, 1))); | ||
| 144 | const json rdata = json::parse(rdatat); | ||
| 145 | for (const auto& role : rdata) | ||
| 146 | { | ||
| 147 | std::string rt = role["type"]; | ||
| 148 | frame::selrestr rs; | ||
| 149 | |||
| 150 | if (role.find("selrestrs") != role.end()) | ||
| 151 | { | ||
| 152 | rs = parse_selrestr(role["selrestrs"]); | ||
| 153 | } | ||
| 154 | |||
| 155 | f._roles[rt] = rs; | ||
| 156 | } | ||
| 157 | |||
| 158 | output.push_back(f); | ||
| 159 | } | ||
| 160 | |||
| 161 | sqlite3_finalize(ppstmt); | ||
| 162 | |||
| 163 | return output; | ||
| 164 | } | ||
| 165 | |||
| 166 | }; | ||
