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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
|
#include "hg_io.h"
#include <cstdio>
#include <cstdlib>
#include <fstream>
#include <sstream>
#include <iostream>
#include <boost/archive/binary_iarchive.hpp>
#include <boost/archive/binary_oarchive.hpp>
#include <boost/serialization/shared_ptr.hpp>
#include "fast_lexical_cast.hpp"
#include "tdict.h"
#include "hg.h"
using namespace std;
bool HypergraphIO::ReadFromBinary(istream* in, Hypergraph* hg) {
boost::archive::binary_iarchive oa(*in);
hg->clear();
oa >> *hg;
return true;
}
bool HypergraphIO::WriteToBinary(const Hypergraph& hg, ostream* out) {
boost::archive::binary_oarchive oa(*out);
oa << hg;
return true;
}
bool needs_escape[128];
void InitEscapes() {
memset(needs_escape, false, 128);
needs_escape[static_cast<size_t>('\'')] = true;
needs_escape[static_cast<size_t>('\\')] = true;
}
string HypergraphIO::Escape(const string& s) {
size_t len = s.size();
for (int i = 0; i < s.size(); ++i) {
unsigned char c = s[i];
if (c < 128 && needs_escape[c]) ++len;
}
if (len == s.size()) return s;
string res(len, ' ');
size_t o = 0;
for (int i = 0; i < s.size(); ++i) {
unsigned char c = s[i];
if (c < 128 && needs_escape[c])
res[o++] = '\\';
res[o++] = c;
}
assert(o == len);
return res;
}
string HypergraphIO::AsPLF(const Hypergraph& hg, bool include_global_parentheses) {
static bool first = true;
if (first) { InitEscapes(); first = false; }
if (hg.nodes_.empty()) return "()";
ostringstream os;
if (include_global_parentheses) os << '(';
static const string EPS="*EPS*";
for (int i = 0; i < hg.nodes_.size()-1; ++i) {
if (hg.nodes_[i].out_edges_.empty()) abort();
const bool last_node = (i == hg.nodes_.size() - 2);
const int out_edges_size = hg.nodes_[i].out_edges_.size();
// compound splitter adds an extra goal transition which we suppress with
// the following conditional
if (!last_node || out_edges_size != 1 ||
hg.edges_[hg.nodes_[i].out_edges_[0]].rule_->EWords() == 1) {
os << '(';
for (int j = 0; j < out_edges_size; ++j) {
const Hypergraph::Edge& e = hg.edges_[hg.nodes_[i].out_edges_[j]];
const string output = e.rule_->e_.size() ==2 ? Escape(TD::Convert(e.rule_->e_[1])) : EPS;
double prob = log(e.edge_prob_);
if (std::isinf(prob)) { prob = -9e20; }
if (std::isnan(prob)) { prob = 0; }
os << "('" << output << "'," << prob << "," << e.head_node_ - i << "),";
}
os << "),";
}
}
if (include_global_parentheses) os << ')';
return os.str();
}
// TODO this should write out the PLF with the Python dictionary format
// rather than just the single "LatticeCost" feature
double PLFFeatureDictionary(const SparseVector<double>& f) {
return f.get(FD::Convert("LatticeCost"));
}
string HypergraphIO::AsPLF(const Lattice& lat, bool include_global_parentheses) {
static bool first = true;
if (first) { InitEscapes(); first = false; }
if (lat.empty()) return "()";
ostringstream os;
if (include_global_parentheses) os << '(';
static const string EPS="*EPS*";
for (int i = 0; i < lat.size(); ++i) {
const vector<LatticeArc> arcs = lat[i];
os << '(';
for (int j = 0; j < arcs.size(); ++j) {
os << "('" << Escape(TD::Convert(arcs[j].label)) << "',"
<< PLFFeatureDictionary(arcs[j].features) << ',' << arcs[j].dist2next << "),";
}
os << "),";
}
if (include_global_parentheses) os << ')';
return os.str();
}
namespace PLF {
const string chars = "'\\";
const char& quote = chars[0];
const char& slash = chars[1];
// safe get
inline char get(const std::string& in, int c) {
if (c < 0 || c >= (int)in.size()) return 0;
else return in[(size_t)c];
}
// consume whitespace
inline void eatws(const std::string& in, int& c) {
while (get(in,c) == ' ') { c++; }
}
// from 'foo' return foo
std::string getEscapedString(const std::string& in, int &c)
{
eatws(in,c);
if (get(in,c++) != quote) {
cerr << "Expected escaped string to begin with " << quote << ". Got " << get(in, c - 1) << "\n";
abort();
}
std::string res;
char cur = 0;
do {
cur = get(in,c++);
if (cur == slash) { res += get(in,c++); }
else if (cur != quote) { res += cur; }
} while (get(in,c) != quote && (c < (int)in.size()));
c++;
eatws(in,c);
return res;
}
// basically atof
float getFloat(const std::string& in, int &c)
{
std::string tmp;
eatws(in,c);
while (c < (int)in.size() && get(in,c) != ' ' && get(in,c) != ')' && get(in,c) != ',' && get(in,c) != '}') {
tmp += get(in,c++);
}
eatws(in,c);
if (tmp.empty()) {
cerr << "Syntax error while reading number! col=" << c << endl;
abort();
}
return atof(tmp.c_str());
}
// basically atoi
int getInt(const std::string& in, int &c)
{
std::string tmp;
eatws(in,c);
while (c < (int)in.size() && get(in,c) != ' ' && get(in,c) != ')' && get(in,c) != ',') {
tmp += get(in,c++);
}
eatws(in,c);
return atoi(tmp.c_str());
}
// maximum number of nodes permitted
#define MAX_NODES 100000000
void ReadPLFFeature(const std::string& in, int &c, map<string, float>& features) {
eatws(in,c);
string name = getEscapedString(in,c);
eatws(in,c);
if (get(in,c++) != ':') { cerr << "PCN/PLF parse error: expected : after feature name " << name << "\n"; abort(); }
float value = getFloat(in, c);
eatws(in,c);
features[name] = value;
}
// parse ('foo', 0.23, 1)
void ReadPLFEdge(const std::string& in, int &c, int cur_node, Hypergraph* hg) {
if (get(in,c++) != '(') { cerr << "PCN/PLF parse error: expected (\n"; abort(); }
vector<WordID> ewords(2, 0);
ewords[1] = TD::Convert(getEscapedString(in,c));
TRulePtr r(new TRule(ewords));
r->ComputeArity();
// cerr << "RULE: " << r->AsString() << endl;
if (get(in,c++) != ',') { cerr << in << endl; cerr << "PCN/PLF parse error: expected , after string\n"; abort(); }
eatws(in,c);
map<string, float> features;
size_t cnNext = 1;
// Read in sparse feature format
if (get(in,c) == '{') {
c++;
ReadPLFFeature(in, c, features);
while (get(in,c) == ',') {
c++;
if (get(in,c) == '}') { break; }
ReadPLFFeature(in, c, features);
}
if (get(in,c++) != '}') { cerr << "PCN/PLF parse error: expected } after feature dictionary\n"; abort(); }
eatws(in,c);
if (get(in, c++) != ',') { cerr << "PCN/PLF parse error: expected , after feature dictionary\n"; abort(); }
cnNext = static_cast<size_t>(getFloat(in, c));
}
// Read in dense feature format
else {
std::vector<float> probs;
probs.push_back(getFloat(in,c));
while (get(in,c) == ',') {
c++;
float val = getFloat(in,c);
probs.push_back(val);
// cerr << val << endl; //REMO
}
if (probs.size() == 0) { cerr << "PCN/PLF parse error: missing destination state increment\n"; abort(); }
// the last item was column increment
cnNext = static_cast<size_t>(probs.back());
probs.pop_back();
for (unsigned i = 0; i < probs.size(); ++i) {
features["LatticeCost_" + to_string(i)] = probs[i];
}
}
if (get(in,c++) != ')') { cerr << "PCN/PLF parse error: expected ) at end of cn alt block. Got " << get(in, c-1) << "\n"; abort(); }
if (cnNext < 1) { cerr << cnNext << endl << "PCN/PLF parse error: bad link length at last element of cn alt block\n"; abort(); }
eatws(in,c);
Hypergraph::TailNodeVector tail(1, cur_node);
Hypergraph::Edge* edge = hg->AddEdge(r, tail);
//cerr << " <--" << cur_node << endl;
int head_node = cur_node + cnNext;
assert(head_node < MAX_NODES); // prevent malicious PLFs from using all the memory
if (hg->nodes_.size() < (head_node + 1)) { hg->ResizeNodes(head_node + 1); }
hg->ConnectEdgeToHeadNode(edge, &hg->nodes_[head_node]);
for (map<string, float>::iterator it = features.begin(); it != features.end(); ++it) {
edge->feature_values_.set_value(FD::Convert(it->first), it->second);
}
}
// parse (('foo', 0.23, 1), ('bar', 0.77, 1))
void ReadPLFNode(const std::string& in, int &c, int cur_node, int line, Hypergraph* hg) {
if (hg->nodes_.size() < (cur_node + 1)) { hg->ResizeNodes(cur_node + 1); }
if (get(in,c++) != '(') { cerr << line << ": Syntax error 1 in PLF\n"; abort(); }
eatws(in,c);
while (1) {
if (c > (int)in.size()) { break; }
if (get(in,c) == ')') {
c++;
eatws(in,c);
break;
}
if (get(in,c) == ',' && get(in,c+1) == ')') {
c+=2;
eatws(in,c);
break;
}
if (get(in,c) == ',') { c++; eatws(in,c); }
ReadPLFEdge(in, c, cur_node, hg);
}
}
} // namespace PLF
void HypergraphIO::ReadFromPLF(const std::string& in, Hypergraph* hg, int line) {
hg->clear();
int c = 0;
int cur_node = 0;
if (in[c++] != '(') { cerr << line << ": Syntax error in PLF!\n"; abort(); }
while (1) {
if (c > (int)in.size()) { break; }
if (PLF::get(in,c) == ')') {
c++;
PLF::eatws(in,c);
break;
}
if (PLF::get(in,c) == ',' && PLF::get(in,c+1) == ')') {
c+=2;
PLF::eatws(in,c);
break;
}
if (PLF::get(in,c) == ',') { c++; PLF::eatws(in,c); }
PLF::ReadPLFNode(in, c, cur_node, line, hg);
++cur_node;
}
assert(cur_node == hg->nodes_.size() - 1);
}
void HypergraphIO::PLFtoLattice(const string& plf, Lattice* pl) {
Lattice& l = *pl;
Hypergraph g;
ReadFromPLF(plf, &g, 0);
const int num_nodes = g.nodes_.size() - 1;
l.resize(num_nodes);
for (int i = 0; i < num_nodes; ++i) {
vector<LatticeArc>& alts = l[i];
const Hypergraph::Node& node = g.nodes_[i];
const int num_alts = node.out_edges_.size();
alts.resize(num_alts);
for (int j = 0; j < num_alts; ++j) {
const Hypergraph::Edge& edge = g.edges_[node.out_edges_[j]];
alts[j].label = edge.rule_->e_[1];
alts[j].features = edge.feature_values_;
alts[j].dist2next = edge.head_node_ - node.id_;
}
}
}
void HypergraphIO::WriteAsCFG(const Hypergraph& hg) {
vector<int> cats(hg.nodes_.size());
// each node in the translation forest becomes a "non-terminal" in the new
// grammar, create the labels here
const string kSEP = "_";
for (int i = 0; i < hg.nodes_.size(); ++i) {
string pstr = "CAT";
if (hg.nodes_[i].cat_ < 0)
pstr = TD::Convert(-hg.nodes_[i].cat_);
cats[i] = TD::Convert(pstr + kSEP + boost::lexical_cast<string>(i)) * -1;
}
for (int i = 0; i < hg.edges_.size(); ++i) {
const Hypergraph::Edge& edge = hg.edges_[i];
const vector<WordID>& tgt = edge.rule_->e();
const vector<WordID>& src = edge.rule_->f();
TRulePtr rule(new TRule);
rule->prev_i = edge.i_;
rule->prev_j = edge.j_;
rule->lhs_ = cats[edge.head_node_];
vector<WordID>& f = rule->f_;
vector<WordID>& e = rule->e_;
f.resize(tgt.size()); // swap source and target, since the parser
e.resize(src.size()); // parses using the source side!
Hypergraph::TailNodeVector tn(edge.tail_nodes_.size());
int ntc = 0;
for (int j = 0; j < tgt.size(); ++j) {
const WordID& cur = tgt[j];
if (cur > 0) {
f[j] = cur;
} else {
tn[ntc++] = cur;
f[j] = cats[edge.tail_nodes_[-cur]];
}
}
ntc = 0;
for (int j = 0; j < src.size(); ++j) {
const WordID& cur = src[j];
if (cur > 0) {
e[j] = cur;
} else {
e[j] = tn[ntc++];
}
}
rule->scores_ = edge.feature_values_;
rule->parent_rule_ = edge.rule_;
rule->ComputeArity();
cout << rule->AsString() << endl;
}
}
/* Output format:
* #vertices
* for each vertex in bottom-up topological order:
* #downward_edges
* for each downward edge:
* RHS with [vertex_index] for NTs ||| scores
*/
void HypergraphIO::WriteTarget(const std::string &base, unsigned int id, const Hypergraph& hg) {
std::string name(base);
name += '/';
name += boost::lexical_cast<std::string>(id);
std::fstream out(name.c_str(), std::fstream::out);
out << hg.nodes_.size() << ' ' << hg.edges_.size() << '\n';
for (unsigned int i = 0; i < hg.nodes_.size(); ++i) {
const Hypergraph::EdgesVector &edges = hg.nodes_[i].in_edges_;
out << edges.size() << '\n';
for (unsigned int j = 0; j < edges.size(); ++j) {
const Hypergraph::Edge &edge = hg.edges_[edges[j]];
const std::vector<WordID> &e = edge.rule_->e();
for (std::vector<WordID>::const_iterator word = e.begin(); word != e.end(); ++word) {
if (*word <= 0) {
out << '[' << edge.tail_nodes_[-*word] << "] ";
} else {
out << TD::Convert(*word) << ' ';
}
}
out << "||| " << edge.rule_->scores_ << '\n';
}
}
}
|