FST  openfst-1.7.2
OpenFst Library
map.h
Go to the documentation of this file.
1 // See www.openfst.org for extensive documentation on this weighted
2 // finite-state transducer library.
3 
4 #ifndef FST_SCRIPT_MAP_H_
5 #define FST_SCRIPT_MAP_H_
6 
7 #include <memory>
8 #include <tuple>
9 
10 #include <fst/arc-map.h>
11 #include <fst/state-map.h>
12 #include <fst/script/arg-packs.h>
13 #include <fst/script/fst-class.h>
15 
16 namespace fst {
17 namespace script {
18 
19 template <class M>
21  const M &mapper) {
22  using ToArc = typename M::ToArc;
23  auto *ofst = new VectorFst<ToArc>;
24  ArcMap(fst, ofst, mapper);
25  return ofst;
26 }
27 
28 template <class M>
30  const M &mapper) {
31  using ToArc = typename M::ToArc;
32  auto *ofst = new VectorFst<ToArc>;
33  StateMap(fst, ofst, mapper);
34  return ofst;
35 }
36 
37 enum MapType {
53 };
54 
55 using MapInnerArgs =
56  std::tuple<const FstClass &, MapType, float, double, const WeightClass &>;
57 
59 
60 template <class Arc>
61 void Map(MapArgs *args) {
62  using Weight = typename Arc::Weight;
63  const Fst<Arc> &ifst = *(std::get<0>(args->args).GetFst<Arc>());
64  const auto map_type = std::get<1>(args->args);
65  switch (map_type) {
66  case ARC_SUM_MAPPER: {
67  std::unique_ptr<Fst<Arc>> ofst(StateMap(ifst, ArcSumMapper<Arc>(ifst)));
68  args->retval = new FstClass(*ofst);
69  return;
70  }
71  case ARC_UNIQUE_MAPPER: {
72  std::unique_ptr<Fst<Arc>> ofst(
73  StateMap(ifst, ArcUniqueMapper<Arc>(ifst)));
74  args->retval = new FstClass(*ofst);
75  return;
76  }
77  case IDENTITY_MAPPER: {
78  std::unique_ptr<Fst<Arc>> ofst(ArcMap(ifst, IdentityArcMapper<Arc>()));
79  args->retval = new FstClass(*ofst);
80  return;
81  }
82  case INPUT_EPSILON_MAPPER: {
83  std::unique_ptr<Fst<Arc>> ofst(ArcMap(ifst, InputEpsilonMapper<Arc>()));
84  args->retval = new FstClass(*ofst);
85  return;
86  }
87  case INVERT_MAPPER: {
88  std::unique_ptr<Fst<Arc>> ofst(ArcMap(ifst, InvertWeightMapper<Arc>()));
89  args->retval = new FstClass(*ofst);
90  return;
91  }
92  case OUTPUT_EPSILON_MAPPER: {
93  std::unique_ptr<Fst<Arc>> ofst(ArcMap(ifst, OutputEpsilonMapper<Arc>()));
94  args->retval = new FstClass(*ofst);
95  return;
96  }
97  case PLUS_MAPPER: {
98  const auto weight = *(std::get<4>(args->args).GetWeight<Weight>());
99  std::unique_ptr<Fst<Arc>> ofst(ArcMap(ifst, PlusMapper<Arc>(weight)));
100  args->retval = new FstClass(*ofst);
101  return;
102  }
103  case POWER_MAPPER: {
104  const auto power = std::get<3>(args->args);
105  std::unique_ptr<Fst<Arc>> ofst(ArcMap(ifst, PowerMapper<Arc>(power)));
106  args->retval = new FstClass(*ofst);
107  return;
108  }
109  case QUANTIZE_MAPPER: {
110  const auto delta = std::get<2>(args->args);
111  std::unique_ptr<Fst<Arc>> ofst(ArcMap(ifst, QuantizeMapper<Arc>(delta)));
112  args->retval = new FstClass(*ofst);
113  return;
114  }
115  case RMWEIGHT_MAPPER: {
116  std::unique_ptr<Fst<Arc>> ofst(ArcMap(ifst, RmWeightMapper<Arc>()));
117  args->retval = new FstClass(*ofst);
118  return;
119  }
120  case SUPERFINAL_MAPPER: {
121  std::unique_ptr<Fst<Arc>> ofst(ArcMap(ifst, SuperFinalMapper<Arc>()));
122  args->retval = new FstClass(*ofst);
123  return;
124  }
125  case TIMES_MAPPER: {
126  const auto weight = *(std::get<4>(args->args).GetWeight<Weight>());
127  std::unique_ptr<Fst<Arc>> ofst(ArcMap(ifst, TimesMapper<Arc>(weight)));
128  args->retval = new FstClass(*ofst);
129  return;
130  }
131  case TO_LOG_MAPPER: {
132  std::unique_ptr<Fst<LogArc>> ofst(
134  args->retval = new FstClass(*ofst);
135  return;
136  }
137  case TO_LOG64_MAPPER: {
138  std::unique_ptr<Fst<Log64Arc>> ofst(
140  args->retval = new FstClass(*ofst);
141  return;
142  }
143  case TO_STD_MAPPER: {
144  std::unique_ptr<Fst<StdArc>> ofst(
146  args->retval = new FstClass(*ofst);
147  return;
148  }
149  }
150 }
151 
152 FstClass *Map(const FstClass &ifst, MapType map_type, float delta, double power,
153  const WeightClass &weight);
154 
155 } // namespace script
156 } // namespace fst
157 
158 #endif // FST_SCRIPT_MAP_H_
MapType
Definition: map.h:37
Fst< typename M::ToArc > * ArcMap(const Fst< typename M::FromArc > &fst, const M &mapper)
Definition: map.h:20
std::tuple< const FstClass &, MapType, float, double, const WeightClass & > MapInnerArgs
Definition: map.h:56
Fst< typename M::ToArc > * StateMap(const Fst< typename M::FromArc > &fst, const M &mapper)
Definition: map.h:29
void Map(MapArgs *args)
Definition: map.h:61