arc.h
7.98 KB
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
// See www.openfst.org for extensive documentation on this weighted
// finite-state transducer library.
//
// Commonly used FST arc types.
#ifndef FST_ARC_H_
#define FST_ARC_H_
#include <climits>
#include <string>
#include <utility>
#include <fst/expectation-weight.h>
#include <fst/float-weight.h>
#include <fst/lexicographic-weight.h>
#include <fst/power-weight.h>
#include <fst/product-weight.h>
#include <fst/signed-log-weight.h>
#include <fst/sparse-power-weight.h>
#include <fst/string-weight.h>
namespace fst {
template <class W>
struct ArcTpl {
public:
using Weight = W;
using Label = int;
using StateId = int;
Label ilabel;
Label olabel;
Weight weight;
StateId nextstate;
ArcTpl() {}
ArcTpl(Label ilabel, Label olabel, Weight weight, StateId nextstate)
: ilabel(ilabel),
olabel(olabel),
weight(std::move(weight)),
nextstate(nextstate) {}
static const string &Type() {
static const string *const type =
new string(Weight::Type() == "tropical" ? "standard" : Weight::Type());
return *type;
}
};
using StdArc = ArcTpl<TropicalWeight>;
using LogArc = ArcTpl<LogWeight>;
using Log64Arc = ArcTpl<Log64Weight>;
using SignedLogArc = ArcTpl<SignedLogWeight>;
using SignedLog64Arc = ArcTpl<SignedLog64Weight>;
using MinMaxArc = ArcTpl<MinMaxWeight>;
// Arc with integer labels and state IDs and string weights.
template <StringType S = STRING_LEFT>
struct StringArc {
public:
using Label = int;
using Weight = StringWeight<int, S>;
using StateId = int;
Label ilabel;
Label olabel;
Weight weight;
StateId nextstate;
StringArc() = default;
StringArc(Label ilabel, Label olabel, Weight weight, StateId nextstate)
: ilabel(ilabel),
olabel(olabel),
weight(std::move(weight)),
nextstate(nextstate) {}
static const string &Type() {
static const string *const type =
new string(S == STRING_LEFT
? "left_standard_string"
: (S == STRING_RIGHT ? "right_standard_string"
: "restricted_standard_string"));
return *type;
}
};
// Arc with label and state Id type the same as template arg and with
// weights over the Gallic semiring w.r.t the output labels and weights of A.
template <class A, GallicType G = GALLIC_LEFT>
struct GallicArc {
using Arc = A;
using Label = typename Arc::Label;
using StateId = typename Arc::StateId;
using Weight = GallicWeight<Label, typename Arc::Weight, G>;
Label ilabel;
Label olabel;
Weight weight;
StateId nextstate;
GallicArc() = default;
GallicArc(Label ilabel, Label olabel, Weight weight, StateId nextstate)
: ilabel(ilabel),
olabel(olabel),
weight(std::move(weight)),
nextstate(nextstate) {}
explicit GallicArc(const Arc &arc)
: ilabel(arc.ilabel), olabel(arc.ilabel), weight(arc.olabel, arc.weight),
nextstate(arc.nextstate) {}
static const string &Type() {
static const string *const type =
new string(
(G == GALLIC_LEFT
? "left_gallic_"
: (G == GALLIC_RIGHT
? "right_gallic_"
: (G == GALLIC_RESTRICT
? "restricted_gallic_"
: (G == GALLIC_MIN
? "min_gallic_" : "gallic_")))) +
Arc::Type());
return *type;
}
};
// Arc with the reverse of the weight found in its template arg.
template <class A>
struct ReverseArc {
using Arc = A;
using Label = typename Arc::Label;
using StateId = typename Arc::StateId;
using AWeight = typename Arc::Weight;
using Weight = typename AWeight::ReverseWeight;
Label ilabel;
Label olabel;
Weight weight;
StateId nextstate;
ReverseArc() = default;
ReverseArc(Label ilabel, Label olabel, Weight weight, StateId nextstate)
: ilabel(ilabel),
olabel(olabel),
weight(std::move(weight)),
nextstate(nextstate) {}
static const string &Type() {
static const string *const type = new string("reverse_" + Arc::Type());
return *type;
}
};
// Arc with integer labels and state IDs and lexicographic weights.
template <class Weight1, class Weight2>
struct LexicographicArc {
using Label = int;
using StateId = int;
using Weight = LexicographicWeight<Weight1, Weight2>;
Label ilabel;
Label olabel;
Weight weight;
StateId nextstate;
LexicographicArc() = default;
LexicographicArc(Label ilabel, Label olabel, Weight weight, StateId nextstate)
: ilabel(ilabel),
olabel(olabel),
weight(std::move(weight)),
nextstate(nextstate) {}
static const string &Type() {
static const string *const type = new string(Weight::Type());
return *type;
}
};
// Arc with integer labels and state IDs and product weights.
template <class Weight1, class Weight2>
struct ProductArc {
using Label = int;
using StateId = int;
using Weight = ProductWeight<Weight1, Weight2>;
Label ilabel;
Label olabel;
Weight weight;
StateId nextstate;
ProductArc() = default;
ProductArc(Label ilabel, Label olabel, Weight weight, StateId nextstate)
: ilabel(ilabel),
olabel(olabel),
weight(std::move(weight)),
nextstate(nextstate) {}
static const string &Type() {
static const string *const type = new string(Weight::Type());
return *type;
}
};
// Arc with label and state ID type the same as first template argument and with
// weights over the n-th Cartesian power of the weight type of the template
// argument.
template <class A, unsigned int N>
struct PowerArc {
using Arc = A;
using Label = typename Arc::Label;
using StateId = typename Arc::StateId;
using Weight = PowerWeight<typename Arc::Weight, N>;
Label ilabel;
Label olabel;
Weight weight;
StateId nextstate;
PowerArc() = default;
PowerArc(Label ilabel, Label olabel, Weight weight, StateId nextstate)
: ilabel(ilabel),
olabel(olabel),
weight(std::move(weight)),
nextstate(nextstate) {}
static const string &Type() {
static const string *const type =
new string(Arc::Type() + "_^" + std::to_string(N));
return *type;
}
};
// Arc with label and state ID type the same as first template argument and with
// weights over the arbitrary Cartesian power of the weight type.
template <class A, class K = int>
struct SparsePowerArc {
using Arc = A;
using Label = typename Arc::Label;
using StateId = typename Arc::Label;
using Weight = SparsePowerWeight<typename Arc::Weight, K>;
Label ilabel;
Label olabel;
Weight weight;
StateId nextstate;
SparsePowerArc() = default;
SparsePowerArc(Label ilabel, Label olabel, Weight weight, StateId nextstate)
: ilabel(ilabel),
olabel(olabel),
weight(std::move(weight)),
nextstate(nextstate) {}
static const string &Type() {
static const string *const type = [] {
string type = Arc::Type() + "_^n";
if (sizeof(K) != sizeof(uint32)) {
type += "_" + std::to_string(CHAR_BIT * sizeof(K));
}
return new string(type);
}();
return *type;
}
};
// Arc with label and state ID type the same as first template argument and with
// expectation weight over the first template argument's weight type and the
// second template argument.
template <class A, class X2>
struct ExpectationArc {
using Arc = A;
using Label = typename Arc::Label;
using StateId = typename Arc::StateId;
using X1 = typename Arc::Weight;
using Weight = ExpectationWeight<X1, X2>;
Label ilabel;
Label olabel;
Weight weight;
StateId nextstate;
ExpectationArc() = default;
ExpectationArc(Label ilabel, Label olabel, Weight weight, StateId nextstate)
: ilabel(ilabel),
olabel(olabel),
weight(std::move(weight)),
nextstate(nextstate) {}
static const string &Type() {
static const string *const type =
new string("expectation_" + Arc::Type() + "_" + X2::Type());
return *type;
}
};
} // namespace fst
#endif // FST_ARC_H_