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
109
110
111
112
113
114
115
116
117
118
119
120
121
|
#include <iostream>
#include <fstream>
#include <string>
#include <msgpack.hpp>
#include <msgpack/fbuffer.h>
#include <msgpack/fbuffer.hpp>
/*
* https://github.com/ascheglov/json-cpp
*
*/
#include "json-cpp/single_include/json-cpp.hpp"
using namespace std;
struct Node {
int id;
string cat;
vector<int> span;
MSGPACK_DEFINE(id, cat, span);
};
struct Vector {
double CountEF;
double EgivenFCoherent;
double Glue;
double IsSingletonF;
double IsSingletonFE;
double LanguageModel;
double LanguageModel_OOV;
double MaxLexFgivenE;
double MaxLexEgivenF;
double PassThrough;
double PassThrough_1;
double PassThrough_2;
double PassThrough_3;
double PassThrough_4;
double PassThrough_5;
double PassThrough_6;
double SampleCountF;
double WordPenalty;
MSGPACK_DEFINE(CountEF, EgivenFCoherent, Glue, IsSingletonF, IsSingletonFE, LanguageModel, LanguageModel_OOV, MaxLexEgivenF, MaxLexFgivenE, PassThrough, PassThrough_1, PassThrough_2, PassThrough_3, PassThrough_4, PassThrough_5, PassThrough_6, SampleCountF, WordPenalty);
};
struct Edge {
int head;
string rule;
vector<int> tails;
Vector f;
double weight;
MSGPACK_DEFINE(head, rule, tails, f, weight);
};
struct Hg {
Vector weights;
vector<Node> nodes;
vector<Edge> edges;
MSGPACK_DEFINE(weights, nodes, edges);
};
template<typename X> inline void
serialize(jsoncpp::Stream<X>& stream, Hg& o)
{
fields(o, stream, "weights", o.weights, "nodes", o.nodes, "edges", o.edges);
}
template<typename X> inline void
serialize(jsoncpp::Stream<X>& stream, Edge& 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, Vector& 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);
}
template<typename X> inline void
serialize(jsoncpp::Stream<X>& stream, Node& o)
{
fields(o, stream, "id", o.id, "cat", o.cat, "span", o.span);
}
int
main(int argc, char** argv)
{
ifstream ifs(argv[1]);
string json_str((istreambuf_iterator<char>(ifs) ),
(istreambuf_iterator<char>()));
Hg hg;
Vector w;
hg.weights = w;
vector<Node> nodes;
hg.nodes = nodes;
vector<Edge> edges;
hg.edges = edges;
jsoncpp::parse(hg, json_str);
FILE* file = fopen(argv[2], "wb");
msgpack::fbuffer fbuf(file);
msgpack::pack(fbuf, hg.nodes.size());
msgpack::pack(fbuf, hg.edges.size());
msgpack::pack(fbuf, hg.weights);
for (auto it = hg.nodes.begin(); it != hg.nodes.end(); it++)
msgpack::pack(fbuf, *it);
for (auto it = hg.edges.begin(); it != hg.edges.end(); it++)
msgpack::pack(fbuf, *it);
fclose(file);
return 0;
}
|