1 // See www.openfst.org for extensive documentation on this weighted
2 // finite-state transducer library.
4 // Commonly used FST arc types.
14 #include <fst/expectation-weight.h>
15 #include <fst/float-weight.h>
16 #include <fst/lexicographic-weight.h>
17 #include <fst/power-weight.h>
18 #include <fst/product-weight.h>
19 #include <fst/signed-log-weight.h>
20 #include <fst/sparse-power-weight.h>
21 #include <fst/string-weight.h>
40 ArcTpl(Label ilabel, Label olabel, Weight weight, StateId nextstate)
43 weight(std::move(weight)),
44 nextstate(nextstate) {}
46 static const string &Type() {
47 static const string *const type =
48 new string(Weight::Type() == "tropical" ? "standard" : Weight::Type());
53 using StdArc = ArcTpl<TropicalWeight>;
54 using LogArc = ArcTpl<LogWeight>;
55 using Log64Arc = ArcTpl<Log64Weight>;
56 using SignedLogArc = ArcTpl<SignedLogWeight>;
57 using SignedLog64Arc = ArcTpl<SignedLog64Weight>;
58 using MinMaxArc = ArcTpl<MinMaxWeight>;
60 // Arc with integer labels and state IDs and string weights.
61 template <StringType S = STRING_LEFT>
65 using Weight = StringWeight<int, S>;
73 StringArc() = default;
75 StringArc(Label ilabel, Label olabel, Weight weight, StateId nextstate)
78 weight(std::move(weight)),
79 nextstate(nextstate) {}
81 static const string &Type() {
82 static const string *const type =
83 new string(S == STRING_LEFT
84 ? "left_standard_string"
85 : (S == STRING_RIGHT ? "right_standard_string"
86 : "restricted_standard_string"));
91 // Arc with label and state Id type the same as template arg and with
92 // weights over the Gallic semiring w.r.t the output labels and weights of A.
93 template <class A, GallicType G = GALLIC_LEFT>
96 using Label = typename Arc::Label;
97 using StateId = typename Arc::StateId;
98 using Weight = GallicWeight<Label, typename Arc::Weight, G>;
105 GallicArc() = default;
107 GallicArc(Label ilabel, Label olabel, Weight weight, StateId nextstate)
110 weight(std::move(weight)),
111 nextstate(nextstate) {}
113 explicit GallicArc(const Arc &arc)
114 : ilabel(arc.ilabel), olabel(arc.ilabel), weight(arc.olabel, arc.weight),
115 nextstate(arc.nextstate) {}
117 static const string &Type() {
118 static const string *const type =
124 : (G == GALLIC_RESTRICT
125 ? "restricted_gallic_"
127 ? "min_gallic_" : "gallic_")))) +
133 // Arc with the reverse of the weight found in its template arg.
137 using Label = typename Arc::Label;
138 using StateId = typename Arc::StateId;
139 using AWeight = typename Arc::Weight;
140 using Weight = typename AWeight::ReverseWeight;
147 ReverseArc() = default;
149 ReverseArc(Label ilabel, Label olabel, Weight weight, StateId nextstate)
152 weight(std::move(weight)),
153 nextstate(nextstate) {}
155 static const string &Type() {
156 static const string *const type = new string("reverse_" + Arc::Type());
161 // Arc with integer labels and state IDs and lexicographic weights.
162 template <class Weight1, class Weight2>
163 struct LexicographicArc {
166 using Weight = LexicographicWeight<Weight1, Weight2>;
173 LexicographicArc() = default;
175 LexicographicArc(Label ilabel, Label olabel, Weight weight, StateId nextstate)
178 weight(std::move(weight)),
179 nextstate(nextstate) {}
181 static const string &Type() {
182 static const string *const type = new string(Weight::Type());
187 // Arc with integer labels and state IDs and product weights.
188 template <class Weight1, class Weight2>
192 using Weight = ProductWeight<Weight1, Weight2>;
199 ProductArc() = default;
201 ProductArc(Label ilabel, Label olabel, Weight weight, StateId nextstate)
204 weight(std::move(weight)),
205 nextstate(nextstate) {}
207 static const string &Type() {
208 static const string *const type = new string(Weight::Type());
213 // Arc with label and state ID type the same as first template argument and with
214 // weights over the n-th Cartesian power of the weight type of the template
216 template <class A, unsigned int N>
219 using Label = typename Arc::Label;
220 using StateId = typename Arc::StateId;
221 using Weight = PowerWeight<typename Arc::Weight, N>;
228 PowerArc() = default;
230 PowerArc(Label ilabel, Label olabel, Weight weight, StateId nextstate)
233 weight(std::move(weight)),
234 nextstate(nextstate) {}
236 static const string &Type() {
237 static string *const type =
238 new string(Arc::Type() + "_^" + std::to_string(N));
243 // Arc with label and state ID type the same as first template argument and with
244 // weights over the arbitrary Cartesian power of the weight type.
245 template <class A, class K = int>
246 struct SparsePowerArc {
248 using Label = typename Arc::Label;
249 using StateId = typename Arc::Label;
250 using Weight = SparsePowerWeight<typename Arc::Weight, K>;
257 SparsePowerArc() = default;
259 SparsePowerArc(Label ilabel, Label olabel, Weight weight, StateId nextstate)
262 weight(std::move(weight)),
263 nextstate(nextstate) {}
265 static const string &Type() {
266 static string *const type = [] {
267 string type = Arc::Type() + "_^n";
268 if (sizeof(K) != sizeof(uint32)) {
269 type += "_" + std::to_string(CHAR_BIT * sizeof(K));
271 return new string(type);
277 // Arc with label and state ID type the same as first template argument and with
278 // expectation weight over the first template argument's weight type and the
279 // second template argument.
280 template <class A, class X2>
281 struct ExpectationArc {
283 using Label = typename Arc::Label;
284 using StateId = typename Arc::StateId;
285 using X1 = typename Arc::Weight;
286 using Weight = ExpectationWeight<X1, X2>;
293 ExpectationArc() = default;
295 ExpectationArc(Label ilabel, Label olabel, Weight weight, StateId nextstate)
298 weight(std::move(weight)),
299 nextstate(nextstate) {}
301 static const string &Type() {
302 static string *const type =
303 new string("expectation_" + Arc::Type() + "_" + X2::Type());
310 #endif // FST_LIB_ARC_H_