From 9bd863c9002b525b7827f9158d9136143393be5c Mon Sep 17 00:00:00 2001 From: Kelly Rauchenberger Date: Mon, 23 Jan 2017 11:49:51 -0500 Subject: Added verb frame parsing --- lib/frame_query.cpp | 166 ---------------------------------------------------- 1 file changed, 166 deletions(-) delete mode 100644 lib/frame_query.cpp (limited to 'lib/frame_query.cpp') 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 @@ -#include "verbly.h" -#include - -using json = nlohmann::json; - -namespace verbly { - - frame_query::frame_query(const data& _data) : _data(_data) - { - - } - - frame_query& frame_query::for_verb(const verb& _v) - { - _for_verb.push_back(_v); - - return *this; - } - - frame::selrestr parse_selrestr(const json data) - { - if (data.find("children") != data.end()) - { - std::list children; - std::transform(std::begin(data["children"]), std::end(data["children"]), std::back_inserter(children), &parse_selrestr); - - return frame::selrestr{children, data["logic"] == "or"}; - } else if (data.find("type") != data.end()) - { - return frame::selrestr{data["type"].get(), data["pos"].get()}; - } else { - return frame::selrestr{}; - } - } - - std::list frame_query::run() const - { - std::stringstream construct; - construct << "SELECT frames.data, groups.data FROM frames INNER JOIN groups ON frames.group_id = groups.group_id"; - std::list bindings; - - if (!_for_verb.empty()) - { - std::list clauses(_for_verb.size(), "verb_id = ?"); - construct << " WHERE frames.group_id IN (SELECT group_id FROM verb_groups WHERE "; - construct << verbly::implode(std::begin(clauses), std::end(clauses), " OR "); - construct << ")"; - - for (auto v : _for_verb) - { - bindings.emplace_back(v._id); - } - } - - sqlite3_stmt* ppstmt; - std::string query = construct.str(); - if (sqlite3_prepare_v2(_data.ppdb, query.c_str(), query.length(), &ppstmt, NULL) != SQLITE_OK) - { - throw std::runtime_error(sqlite3_errmsg(_data.ppdb)); - } - - int i = 1; - for (auto& binding : bindings) - { - switch (binding.get_type()) - { - case binding::type::integer: - { - sqlite3_bind_int(ppstmt, i, binding.get_integer()); - - break; - } - - case binding::type::string: - { - sqlite3_bind_text(ppstmt, i, binding.get_string().c_str(), binding.get_string().length(), SQLITE_TRANSIENT); - - break; - } - } - - i++; - } - - std::list output; - while (sqlite3_step(ppstmt) == SQLITE_ROW) - { - frame f; - - std::string fdatat(reinterpret_cast(sqlite3_column_blob(ppstmt, 0))); - const json fdata = json::parse(fdatat); - for (const auto& part : fdata) - { - frame::part p; - - if (part["type"] == "np") - { - p._type = frame::part::type::noun_phrase; - new(&p._noun_phrase.role) std::string(part["role"].get()); - new(&p._noun_phrase.selrestrs) frame::selrestr(parse_selrestr(part["selrestrs"])); - new(&p._noun_phrase.synrestrs) std::set(); - for (auto synrestr : part["synrestrs"]) - { - p._noun_phrase.synrestrs.insert(synrestr.get()); - } - } else if (part["type"] == "pp") - { - if (!part["values"].empty()) - { - p._type = frame::part::type::literal_preposition; - new(&p._literal_preposition.choices) std::vector(); - for (auto choice : part["values"]) - { - p._literal_preposition.choices.push_back(choice.get()); - } - } else if (!part["preprestrs"].empty()) - { - p._type = frame::part::type::selection_preposition; - new(&p._selection_preposition.preprestrs) std::vector(); - for (auto preprestr : part["preprestrs"]) - { - p._selection_preposition.preprestrs.push_back(preprestr.get()); - } - } - } else if (part["type"] == "v") - { - p._type = frame::part::type::verb; - } else if (part["type"] == "adj") - { - p._type = frame::part::type::adjective; - } else if (part["type"] == "adv") - { - p._type = frame::part::type::adverb; - } else if (part["type"] == "lex") - { - p._type = frame::part::type::literal; - new(&p._literal.lexval) std::string(part["value"].get()); - } - - f._parts.push_back(p); - } - - std::string rdatat(reinterpret_cast(sqlite3_column_blob(ppstmt, 1))); - const json rdata = json::parse(rdatat); - for (const auto& role : rdata) - { - std::string rt = role["type"]; - frame::selrestr rs; - - if (role.find("selrestrs") != role.end()) - { - rs = parse_selrestr(role["selrestrs"]); - } - - f._roles[rt] = rs; - } - - output.push_back(f); - } - - sqlite3_finalize(ppstmt); - - return output; - } - -}; -- cgit 1.4.1