FST  openfst-1.8.3
OpenFst Library
arc.h
Go to the documentation of this file.
1 // Copyright 2005-2024 Google LLC
2 //
3 // Licensed under the Apache License, Version 2.0 (the 'License');
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an 'AS IS' BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 //
15 // See www.openfst.org for extensive documentation on this weighted
16 // finite-state transducer library.
17 //
18 // Commonly used FST arc types.
19 
20 #ifndef FST_ARC_H_
21 #define FST_ARC_H_
22 
23 #include <climits>
24 #include <cstddef>
25 #include <cstdint>
26 #include <string>
27 #include <type_traits>
28 #include <utility>
29 
30 #include <fst/error-weight.h>
31 #include <fst/expectation-weight.h>
32 #include <fst/float-weight.h>
33 #include <fst/fst-decl.h> // For optional argument declarations
35 #include <fst/power-weight.h>
36 #include <fst/product-weight.h>
37 #include <fst/signed-log-weight.h>
39 #include <fst/string-weight.h>
40 
41 namespace fst {
42 
43 template <class W, class L /* = int */, class S /* = int */>
44 struct ArcTpl {
45  public:
46  using Weight = W;
47  using Label = L;
48  using StateId = S;
49 
54 
55  ArcTpl() noexcept(std::is_nothrow_default_constructible_v<Weight>) = default;
56 
57  template <class T>
58  ArcTpl(Label ilabel, Label olabel, T &&weight, StateId nextstate)
59  : ilabel(ilabel),
60  olabel(olabel),
61  weight(std::forward<T>(weight)),
62  nextstate(nextstate) {}
63 
64  // Arc with weight One.
65  ArcTpl(Label ilabel, Label olabel, StateId nextstate)
66  : ArcTpl(ilabel, olabel, Weight::One(), nextstate) {}
67 
68  static const std::string &Type() {
69  static const auto *const type = new std::string(
70  Weight::Type() == "tropical" ? "standard" : Weight::Type());
71  return *type;
72  }
73 };
74 
84 
85 // Arc with integer labels and state IDs and string weights.
86 template <StringType S = STRING_LEFT>
87 struct StringArc : public ArcTpl<StringWeight<int, S>> {
88  public:
90 
91  using Base::Base;
92 
93  static const std::string &Type() {
94  static const auto *const type = new std::string(
95  S == STRING_LEFT ? "left_standard_string"
96  : (S == STRING_RIGHT ? "right_standard_string"
97  : "restricted_standard_string"));
98  return *type;
99  }
100 };
101 
102 // Arc with label and state Id type the same as template arg and with
103 // weights over the Gallic semiring w.r.t the output labels and weights of A.
104 template <class A, GallicType G = GALLIC_LEFT>
105 struct GallicArc : public ArcTpl<GallicWeight<int, typename A::Weight, G>,
106  typename A::Label, typename A::StateId> {
108  typename A::Label, typename A::StateId>;
109  using Arc = A;
110 
111  using Base::Base;
112 
113  explicit GallicArc(const Arc &arc)
114  : Base(arc.ilabel, arc.ilabel, Weight(arc.olabel, arc.weight),
115  arc.nextstate) {}
116 
117  static const std::string &Type() {
118  static const auto *const type = new std::string(
119  (G == GALLIC_LEFT
120  ? "left_gallic_"
121  : (G == GALLIC_RIGHT
122  ? "right_gallic_"
123  : (G == GALLIC_RESTRICT
124  ? "restricted_gallic_"
125  : (G == GALLIC_MIN ? "min_gallic_" : "gallic_")))) +
126  Arc::Type());
127  return *type;
128  }
129 };
130 
131 // Arc with the reverse of the weight found in its template arg.
132 template <class A>
133 struct ReverseArc : public ArcTpl<typename A::Weight::ReverseWeight,
134  typename A::Label, typename A::StateId> {
135  using Base = ArcTpl<typename A::Weight::ReverseWeight, typename A::Label,
136  typename A::StateId>;
137  using Arc = A;
138 
139  using Base::Base;
140 
141  static const std::string &Type() {
142  static const auto *const type = new std::string("reverse_" + Arc::Type());
143  return *type;
144  }
145 };
146 
147 // Arc with integer labels and state IDs and lexicographic weights.
148 template <class Weight1, class Weight2>
150 
151 // Arc with integer labels and state IDs and product weights.
152 template <class Weight1, class Weight2>
154 
155 // Arc with label and state ID type the same as first template argument and with
156 // weights over the n-th Cartesian power of the weight type of the template
157 // argument.
158 template <class A, size_t n>
159 struct PowerArc : public ArcTpl<PowerWeight<typename A::Weight, n>,
160  typename A::Label, typename A::StateId> {
161  using Base = ArcTpl<PowerWeight<typename A::Weight, n>, typename A::Label,
162  typename A::StateId>;
163  using Arc = A;
164 
165  using Base::Base;
166 
167  static const std::string &Type() {
168  static const auto *const type =
169  new std::string(Arc::Type() + "_^" + std::to_string(n));
170  return *type;
171  }
172 };
173 
174 // Arc with label and state ID type the same as first template argument and with
175 // weights over the arbitrary Cartesian power of the weight type.
176 template <class A, class K = int>
177 struct SparsePowerArc : public ArcTpl<SparsePowerWeight<typename A::Weight, K>,
178  typename A::Label, typename A::StateId> {
180  typename A::Label, typename A::StateId>;
181  using Arc = A;
182 
183  using Base::Base;
184 
185  static const std::string &Type() {
186  static const std::string *const type = [] {
187  std::string type = Arc::Type() + "_^n";
188  if (sizeof(K) != sizeof(uint32_t)) {
189  type += "_" + std::to_string(CHAR_BIT * sizeof(K));
190  }
191  return new std::string(type);
192  }();
193  return *type;
194  }
195 };
196 
197 // Arc with label and state ID type the same as first template argument and with
198 // expectation weight over the first template argument's weight type and the
199 // second template argument.
200 template <class A, class X2>
201 struct ExpectationArc : public ArcTpl<ExpectationWeight<typename A::Weight, X2>,
202  typename A::Label, typename A::StateId> {
204  typename A::Label, typename A::StateId>;
205  using Arc = A;
206  using X1 = typename Arc::Weight;
207 
208  using Base::Base;
209 
210  static const std::string &Type() {
211  static const auto *const type =
212  new std::string("expectation_" + Arc::Type() + "_" + X2::Type());
213  return *type;
214  }
215 };
216 
217 } // namespace fst
218 
219 #endif // FST_ARC_H_
typename Arc::Weight X1
Definition: arc.h:206
static const std::string & Type()
Definition: arc.h:210
ArcTpl(Label ilabel, Label olabel, StateId nextstate)
Definition: arc.h:65
Label ilabel
Definition: arc.h:50
static const std::string & Type()
Definition: arc.h:68
ArcTpl() noexcept(std::is_nothrow_default_constructible_v< Weight >)=default
static const std::string & Type()
Definition: arc.h:185
static const std::string & Type()
Definition: arc.h:93
Label olabel
Definition: arc.h:51
static const std::string & Type()
Definition: arc.h:167
Weight weight
Definition: arc.h:52
static const std::string & Type()
Definition: arc.h:117
static const std::string & Type()
Definition: arc.h:141
GallicArc(const Arc &arc)
Definition: arc.h:113
StateId nextstate
Definition: arc.h:53