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