FST  openfst-1.8.3
OpenFst Library
map.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 #ifndef FST_SCRIPT_MAP_H_
19 #define FST_SCRIPT_MAP_H_
20 
21 #include <cstdint>
22 #include <memory>
23 #include <tuple>
24 #include <utility>
25 
26 #include <fst/arc-map.h>
27 #include <fst/arc.h>
28 #include <fst/fst.h>
29 #include <fst/state-map.h>
30 #include <fst/vector-fst.h>
31 #include <fst/script/arg-packs.h>
32 #include <fst/script/fst-class.h>
34 
35 namespace fst {
36 namespace script {
37 
38 template <class M>
39 std::unique_ptr<Fst<typename M::ToArc>> ArcMap(
40  const Fst<typename M::FromArc> &fst, const M &mapper) {
41  using ToArc = typename M::ToArc;
42  auto ofst = std::make_unique<VectorFst<ToArc>>();
43  ArcMap(fst, ofst.get(), mapper);
44  return ofst;
45 }
46 
47 template <class M>
48 std::unique_ptr<Fst<typename M::ToArc>> StateMap(
49  const Fst<typename M::FromArc> &fst, const M &mapper) {
50  using ToArc = typename M::ToArc;
51  auto ofst = std::make_unique<VectorFst<ToArc>>();
52  StateMap(fst, ofst.get(), mapper);
53  return ofst;
54 }
55 
56 enum class MapType : uint8_t {
57  ARC_SUM,
58  ARC_UNIQUE,
59  IDENTITY,
61  INVERT,
63  PLUS,
64  POWER,
65  QUANTIZE,
66  RMWEIGHT,
67  SUPERFINAL,
68  TIMES,
69  TO_LOG,
70  TO_LOG64,
71  TO_STD
72 };
73 
74 using FstMapInnerArgs =
75  std::tuple<const FstClass &, MapType, float, double, const WeightClass &>;
76 
78 
79 template <class Arc>
80 void Map(FstMapArgs *args) {
81  using Weight = typename Arc::Weight;
82  const Fst<Arc> &ifst = *std::get<0>(args->args).GetFst<Arc>();
83  const auto map_type = std::get<1>(args->args);
84  switch (map_type) {
85  case MapType::ARC_SUM: {
86  auto ofst = StateMap(ifst, ArcSumMapper<Arc>(ifst));
87  args->retval = std::make_unique<FstClass>(std::move(ofst));
88  return;
89  }
90  case MapType::ARC_UNIQUE: {
91  auto ofst = StateMap(ifst, ArcUniqueMapper<Arc>(ifst));
92  args->retval = std::make_unique<FstClass>(std::move(ofst));
93  return;
94  }
95  case MapType::IDENTITY: {
96  auto ofst = ArcMap(ifst, IdentityArcMapper<Arc>());
97  args->retval = std::make_unique<FstClass>(std::move(ofst));
98  return;
99  }
100  case MapType::INPUT_EPSILON: {
101  auto ofst = ArcMap(ifst, InputEpsilonMapper<Arc>());
102  args->retval = std::make_unique<FstClass>(std::move(ofst));
103  return;
104  }
105  case MapType::INVERT: {
106  auto ofst = ArcMap(ifst, InvertWeightMapper<Arc>());
107  args->retval = std::make_unique<FstClass>(std::move(ofst));
108  return;
109  }
111  auto ofst = ArcMap(ifst, OutputEpsilonMapper<Arc>());
112  args->retval = std::make_unique<FstClass>(std::move(ofst));
113  return;
114  }
115  case MapType::PLUS: {
116  const auto weight = *std::get<4>(args->args).GetWeight<Weight>();
117  auto ofst = ArcMap(ifst, PlusMapper<Arc>(weight));
118  args->retval = std::make_unique<FstClass>(std::move(ofst));
119  return;
120  }
121  case MapType::POWER: {
122  const auto power = std::get<3>(args->args);
123  auto ofst = ArcMap(ifst, PowerMapper<Arc>(power));
124  args->retval = std::make_unique<FstClass>(std::move(ofst));
125  return;
126  }
127  case MapType::QUANTIZE: {
128  const auto delta = std::get<2>(args->args);
129  auto ofst = ArcMap(ifst, QuantizeMapper<Arc>(delta));
130  args->retval = std::make_unique<FstClass>(std::move(ofst));
131  return;
132  }
133  case MapType::RMWEIGHT: {
134  auto ofst = ArcMap(ifst, RmWeightMapper<Arc>());
135  args->retval = std::make_unique<FstClass>(std::move(ofst));
136  return;
137  }
138  case MapType::SUPERFINAL: {
139  auto ofst = ArcMap(ifst, SuperFinalMapper<Arc>());
140  args->retval = std::make_unique<FstClass>(std::move(ofst));
141  return;
142  }
143  case MapType::TIMES: {
144  const auto weight = *std::get<4>(args->args).GetWeight<Weight>();
145  auto ofst = ArcMap(ifst, TimesMapper<Arc>(weight));
146  args->retval = std::make_unique<FstClass>(std::move(ofst));
147  return;
148  }
149  case MapType::TO_LOG: {
150  auto ofst = ArcMap(ifst, WeightConvertMapper<Arc, LogArc>());
151  args->retval = std::make_unique<FstClass>(std::move(ofst));
152  return;
153  }
154  case MapType::TO_LOG64: {
155  auto ofst = ArcMap(ifst, WeightConvertMapper<Arc, Log64Arc>());
156  args->retval = std::make_unique<FstClass>(std::move(ofst));
157  return;
158  }
159  case MapType::TO_STD: {
160  auto ofst = ArcMap(ifst, WeightConvertMapper<Arc, StdArc>());
161  args->retval = std::make_unique<FstClass>(std::move(ofst));
162  return;
163  }
164  }
165 }
166 
167 std::unique_ptr<FstClass> Map(const FstClass &ifst,
168  MapType map_type, float delta,
169  double power,
170  const WeightClass &weight);
171 
172 } // namespace script
173 } // namespace fst
174 
175 #endif // FST_SCRIPT_MAP_H_
MapType
Definition: map.h:56
void Map(FstMapArgs *args)
Definition: map.h:80
std::tuple< const FstClass &, MapType, float, double, const WeightClass & > FstMapInnerArgs
Definition: map.h:75
std::unique_ptr< Fst< typename M::ToArc > > ArcMap(const Fst< typename M::FromArc > &fst, const M &mapper)
Definition: map.h:39
std::unique_ptr< Fst< typename M::ToArc > > StateMap(const Fst< typename M::FromArc > &fst, const M &mapper)
Definition: map.h:48