From 129a22cfcc7651daa4b11ed52e7870249f6373a5 Mon Sep 17 00:00:00 2001
From: Patrick Simianer
Date: Tue, 16 Sep 2014 10:23:14 +0100
Subject: spring cleaning
---
src/make_pak.cc | 103 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 103 insertions(+)
create mode 100644 src/make_pak.cc
(limited to 'src/make_pak.cc')
diff --git a/src/make_pak.cc b/src/make_pak.cc
new file mode 100644
index 0000000..db3a8a4
--- /dev/null
+++ b/src/make_pak.cc
@@ -0,0 +1,103 @@
+#include
+#include
+#include
+#include
+#include
+
+#include "json-cpp/single_include/json-cpp.hpp"
+#include "hypergraph.hh"
+#include "types.hh"
+
+using namespace std;
+
+struct DummyNode {
+ size_t id;
+ string symbol;
+ vector span;
+};
+
+struct DummyEdge {
+ size_t head_id;
+ size_t rule_id;
+ vector tails_ids;
+ string f;
+ score_t score;
+};
+
+struct DummyHg {
+ vector rules;
+ vector nodes;
+ vector edges;
+};
+
+template inline void
+serialize(jsoncpp::Stream& stream, DummyNode& o)
+{
+ fields(o, stream, "id", o.id, "symbol", o.symbol, "span", o.span);
+}
+
+template inline void
+serialize(jsoncpp::Stream& stream, DummyEdge& o)
+{
+ fields(o, stream, "head", o.head_id, "rule", o.rule_id, "tails", o.tails_ids, "score", o.score);
+}
+
+template inline void
+serialize(jsoncpp::Stream& stream, DummyHg& o)
+{
+ fields(o, stream, "rules", o.rules, "nodes", o.nodes, "edges", o.edges);
+}
+
+int
+main(int argc, char** argv)
+{
+ // read from json
+ ifstream ifs(argv[1]);
+ string json_str((istreambuf_iterator(ifs) ),
+ (istreambuf_iterator()));
+ DummyHg hg;
+ vector rules;
+ hg.rules = rules;
+ vector nodes;
+ hg.nodes = nodes;
+ vector edges;
+ hg.edges = edges;
+ jsoncpp::parse(hg, json_str);
+
+ // convert to proper objects
+ vector nodes_conv;
+ for (const auto it: hg.nodes) {
+ Hg::Node* n = new Hg::Node;
+ n->id = it.id;
+ n->symbol = it.symbol;
+ n->left = it.span[0];
+ n->right = it.span[1];
+ nodes_conv.push_back(n);
+ }
+ vector edges_conv;
+ for (const auto it: hg.edges) {
+ Hg::Edge* e = new Hg::Edge;
+ e->head_id_ = it.head_id;
+ e->tails_ids_ = it.tails_ids;
+ e->score = it.score;
+ e->rule_id_ = it.rule_id;
+ edges_conv.push_back(e);
+ }
+
+ // write to msgpack
+ FILE* file = fopen(argv[2], "wb");
+ msgpack::fbuffer fbuf(file);
+ msgpack::pack(fbuf, hg.rules.size());
+ msgpack::pack(fbuf, hg.nodes.size());
+ msgpack::pack(fbuf, hg.edges.size());
+ for (const auto it: hg.rules)
+ msgpack::pack(fbuf, it);
+ for (const auto it: nodes_conv)
+ msgpack::pack(fbuf, *it);
+ for (const auto it: edges_conv)
+ msgpack::pack(fbuf, *it);
+ fclose(file);
+
+ return 0;
+}
+
--
cgit v1.2.3