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
|
#pragma once
#include <iostream>
#include <fstream>
#include <map>
#include <sstream>
#include <string>
#include <vector>
#include <msgpack.hpp>
#include "sparse_vector.hh"
#include "util.hh"
using namespace std;
namespace G {
enum item_type {
UNDEFINED,
TERMINAL,
NON_TERMINAL
};
struct Item {
virtual size_t index() const { return 0; }
virtual symbol_t symbol() const { return ""; }
virtual item_type type() const { return UNDEFINED; }
virtual ostream& repr(ostream& os) const { return os << "Item<>"; }
virtual ostream& escaped(ostream& os) const { return os << ""; }
friend ostream&
operator<<(ostream& os, const Item& i)
{
return i.repr(os);
};
};
struct NT : public Item {
symbol_t symbol_;
size_t index_;
NT() {}
NT(string const& s)
{
index_ = 0; // default
string t(s);
t.erase(0, 1); t.pop_back(); // remove '[' and ']'
istringstream ss(s);
if (ss >> index_) { // [i]
symbol_ = "";
index_ = stoi(s);
return;
} else { // [X]
symbol_ = s;
return;
}
string buf;
size_t j = 0;
while (ss.good() && getline(ss, buf, ',')) {
if (j == 0) {
symbol_ = buf;
} else {
index_ = stoi(buf);
}
j++;
}
}
virtual size_t index() const { return index_; }
virtual symbol_t symbol() const { return symbol_; }
virtual item_type type() { return NON_TERMINAL; }
virtual ostream&
repr(ostream& os) const
{
return os << "NT<" << symbol_ << "," << index_ << ">";
}
virtual ostream&
escaped(ostream& os) const
{
os << "[" << symbol_;
if (index_ > 0)
os << "," << index_;
os << "]";
return os;
}
};
struct T : public Item {
symbol_t symbol_;
T(string const& s)
{
symbol_ = s;
}
virtual symbol_t symbol() const { return symbol_; }
virtual item_type type() { return TERMINAL; }
virtual ostream&
repr(ostream& os) const
{
return os << "T<" << symbol_ << ">";
}
virtual ostream&
escaped(ostream& os) const
{
os << util::json_escape(symbol_);
}
};
struct Vocabulary
{
unordered_map<symbol_t, size_t> m_;
vector<Item*> items_;
bool
is_non_terminal(string const& s)
{
return s.front() == '[' && s.back() == ']';
}
Item*
get(symbol_t const& s)
{
if (is_non_terminal(s))
return new NT(s);
if (m_.find(s) != m_.end())
return items_[m_[s]];
return add(s);
}
Item*
add(symbol_t const& s)
{
size_t next_index_ = items_.size();
T* item = new T(s);
items_.push_back(item);
m_[s] = next_index_;
return item;
}
};
struct Rule {
NT* lhs;
vector<Item*> rhs;
vector<Item*> target;
size_t arity;
Sv::SparseVector<string, score_t>* f;
map<size_t, size_t> order;
string as_str_;
Rule() {}
Rule(string const& s, Vocabulary& vocab) { from_s(this, s, vocab); }
static void
from_s(Rule* r, string const& s, Vocabulary& vocab)
{
istringstream ss(s);
string buf;
size_t j = 0, i = 0;
r->arity = 0;
vector<NT*> rhs_non_terminals;
r->f = new Sv::SparseVector<string, score_t>();
while (ss >> buf) {
if (buf == "|||") { j++; continue; }
if (j == 0) { // left-hand side
r->lhs = new NT(buf);
} else if (j == 1) { // right-hand side
Item* item = vocab.get(buf);
r->rhs.push_back(item);
if (item->type() == NON_TERMINAL) {
r->arity++;
rhs_non_terminals.push_back(reinterpret_cast<NT*>(item));
}
} else if (j == 2) { // target
Item* item = vocab.get(buf);
if (item->type() == NON_TERMINAL) {
r->order[i] = item->index();
i++;
if (item->symbol() == "") { // only [1], [2] ... on target
reinterpret_cast<NT*>(item)->symbol_ = \
rhs_non_terminals[item->index()-1]->symbol();
}
}
r->target.push_back(item);
} else if (j == 3) { // feature vector
Sv::SparseVector<string, score_t>::from_s(r->f, buf);
// FIXME: this is slow!!! ^^^
} else if (j == 4) { // alignment
} else {
// error
}
if (j == 4) break;
}
}
ostream&
repr(ostream& os) const
{
os << "Rule<lhs=";
lhs->repr(os);
os << ", rhs:{";
for (auto it = rhs.begin(); it != rhs.end(); it++) {
(**it).repr(os);
if (next(it) != rhs.end()) os << " ";
}
os << "}, target:{";
for (auto it = target.begin(); it != target.end(); it++) {
(**it).repr(os);
if (next(it) != target.end()) os << " ";
}
os << "}, f:";
f->repr(os);
os << ", arity=" << arity << \
", map:" << "TODO" << \
">";
return os;
}
ostream&
escaped(ostream& os) const
{
lhs->escaped(os);
os << " ||| ";
for (auto it = rhs.begin(); it != rhs.end(); it++) {
(**it).escaped(os);
if (next(it) != rhs.end()) os << " ";
}
os << " ||| ";
for (auto it = target.begin(); it != target.end(); it++) {
(**it).escaped(os);
if (next(it) != target.end()) os << " ";
}
os << " ||| ";
f->escaped(os);
os << " ||| ";
os << "TODO";
return os;
};
friend ostream&
operator<<(ostream& os, const Rule& r)
{
return r.repr(os);
};
// --
void
prep_for_serialization_()
{
ostringstream os;
escaped(os);
as_str_ = os.str();
};
MSGPACK_DEFINE(as_str_);
// ^^^ FIXME
};
struct Grammar {
vector<Rule*> rules;
vector<Rule*> flat;
vector<Rule*> start_non_terminal;
vector<Rule*> start_terminal;
Grammar() {}
Grammar(string const& fn, Vocabulary& vocab)
{
ifstream ifs(fn);
string line;
while (getline(ifs, line)) {
G::Rule* r = new G::Rule(line, vocab);
rules.push_back(r);
if (r->arity == 0)
flat.push_back(r);
else if (r->rhs.front()->type() == NON_TERMINAL)
start_non_terminal.push_back(r);
else
start_terminal.push_back(r);
}
}
void add_glue();
// ^^^ TODO
void add_pass_through(const string& input);
// ^^^ TODO
friend ostream&
operator<<(ostream& os, Grammar const& g)
{
for (const auto it: g.rules) {
it->repr(os);
os << endl;
}
return os;
}
};
} // namespace G
|