1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
|
#include <iostream>
#include <fstream>
#include <string>
#include <msgpack.hpp>
#include <msgpack/fbuffer.hpp>
#include "json-cpp.hpp"
#include "../fast/dummyvector.h"
#include "../fast/hypergraph.hh"
using namespace std;
struct DummyNode {
size_t id;
string cat;
vector<short> span;
};
struct DummyEdge {
size_t head;
string rule;
vector<size_t> tails;
DummyVector f;
score_t weight;
};
struct DummyHg {
vector<DummyNode> nodes;
vector<DummyEdge> edges;
DummyVector weights;
};
template<typename X> inline void
serialize(jsoncpp::Stream<X>& stream, DummyNode& o)
{
fields(o, stream, "id", o.id, "cat", o.cat, "span", o.span);
}
template<typename X> inline void
serialize(jsoncpp::Stream<X>& stream, DummyEdge& o)
{
fields(o, stream, "head", o.head, "rule", o.rule, "tails", o.tails, "f", o.f, "weight", o.weight);
}
template<typename X> inline void
serialize(jsoncpp::Stream<X>& stream, DummyHg& o)
{
fields(o, stream, "nodes", o.nodes, "edges", o.edges, "weights", o.weights);
}
template<typename X> inline void
serialize(jsoncpp::Stream<X>& stream, DummyVector& o)
{
fields(o, stream, "EgivenFCoherent", o.EgivenFCoherent, "SampleCountF", o.SampleCountF, "CountEF", o.CountEF, "MaxLexFgivenE", o.MaxLexFgivenE, "MaxLexEgivenF", o.MaxLexEgivenF, "IsSingletonF", o.IsSingletonF, "IsSingletonFE", o.IsSingletonFE, "LanguageModel", o.LanguageModel, "LanguageModel_OOV", o.LanguageModel_OOV, "PassThrough", o.PassThrough, "PassThrough_1", o.PassThrough_1, "PassThrough_2", o.PassThrough_2, "PassThrough_3", o.PassThrough_3, "PassThrough_4", o.PassThrough_4, "PassThrough_5", o.PassThrough_5, "PassThrough_6", o.PassThrough_6, "WordPenalty", o.WordPenalty, "Glue", o.Glue);
}
int
main(int argc, char** argv)
{
// read from json
ifstream ifs(argv[1]);
string json_str((istreambuf_iterator<char>(ifs) ),
(istreambuf_iterator<char>()));
DummyHg hg;
vector<DummyNode> nodes;
hg.nodes = nodes;
vector<DummyEdge> edges;
hg.edges = edges;
DummyVector w;
hg.weights = w;
jsoncpp::parse(hg, json_str);
// convert objects
vector<Hg::Node*> nodes_conv;
for (auto it = hg.nodes.begin(); it != hg.nodes.end(); ++it) {
Hg::Node* n = new Hg::Node;
n->id = it->id;
n->symbol = it->cat;
n->left = it->span[0];
n->right = it->span[1];
nodes_conv.push_back(n);
}
vector<Hg::Edge*> edges_conv;
for (auto it = hg.edges.begin(); it != hg.edges.end(); ++it) {
Hg::Edge* e = new Hg::Edge;
e->head_id_ = it->head;
e->tails_ids_ = it->tails;
e->score = it->weight;
e->rule = it->rule;
e->f = it->f;
edges_conv.push_back(e);
}
// write to msgpack
FILE* file = fopen(argv[2], "wb");
msgpack::fbuffer fbuf(file);
msgpack::pack(fbuf, hg.nodes.size());
msgpack::pack(fbuf, hg.edges.size());
for (auto it = nodes_conv.begin(); it != nodes_conv.end(); ++it)
msgpack::pack(fbuf, **it);
for (auto it = edges_conv.begin(); it != edges_conv.end(); ++it)
msgpack::pack(fbuf, **it);
fclose(file);
return 0;
}
|