FST  openfst-1.8.3
OpenFst Library
power-weight-mappers.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 // Conversions to/from PowerWeight and SparsePowerWeight.
19 
20 #ifndef FST_POWER_WEIGHT_MAPPERS_H_
21 #define FST_POWER_WEIGHT_MAPPERS_H_
22 
23 namespace fst {
24 
25 // Converts FromWeight to ToPowerWeight (with one component).
26 // ToPowerWeight may be PowerWeight or SparsePowerWeight.
27 template <class FromWeight_, class ToPowerWeight>
29  public:
30  using FromWeight = FromWeight_;
31  using ToWeight = ToPowerWeight;
32  using Index = typename ToPowerWeight::Index;
33 
34  explicit ToPowerWeightMapper(Index index = 0) : index_(index) {}
35 
36  ToPowerWeight operator()(const FromWeight &w) const {
37  return ToPowerWeight(index_, w.Value());
38  }
39 
40  private:
41  const Index index_;
42 };
43 
44 // Converts FromPowerWeight to ToWeight. Uses only one component.
45 // FromPowerWeight may be PowerWeight or SparsePowerWeight.
46 template <class FromPowerWeight, class ToWeight_>
48  public:
49  using FromWeight = FromPowerWeight;
50  using ToWeight = ToWeight_;
51  using Index = typename FromPowerWeight::Index;
52 
53  explicit FromPowerWeightMapper(Index index = 0) : index_(index) {}
54 
55  ToWeight operator()(const FromPowerWeight &w) const {
56  return ToWeight(w.Value(index_));
57  }
58 
59  private:
60  const Index index_;
61 };
62 
63 // Projects to one dimension of the weight vector, filling the indices
64 // with `default_weight`.
65 // PowerWeightT may be PowerWeight or SparsePowerWeight.
66 template <class PowerWeightT>
68  public:
69  using FromWeight = PowerWeightT;
70  using ToWeight = PowerWeightT;
71  using Index = typename PowerWeightT::Index;
72  using ComponentWeight = typename PowerWeightT::Weight;
73 
75  Index from_index = 0, Index to_index = 0,
76  const ComponentWeight &default_weight = ComponentWeight::Zero())
77  : from_index_(from_index),
78  to_index_(to_index),
79  default_weight_(default_weight) {}
80 
81  PowerWeightT operator()(const PowerWeightT &w) const {
82  return PowerWeightT(to_index_, w.Value(from_index_), default_weight_);
83  }
84 
85  private:
86  const Index from_index_;
87  const Index to_index_;
88  const ComponentWeight default_weight_;
89 };
90 
91 // Applies a transformation function to each weight vector.
92 // PowerWeightT may be PowerWeight or SparsePowerWeight.
93 template <class PowerWeightT, class TransformFn_>
95  public:
96  using TransformFn = TransformFn_;
97  using FromWeight = PowerWeightT;
98  using ToWeight = PowerWeightT;
99 
101  const TransformFn &transform = TransformFn())
102  : transform_(transform) {}
103 
104  PowerWeightT operator()(const PowerWeightT &w) const { return transform_(w); }
105 
106  private:
107  TransformFn transform_;
108 };
109 
110 } // namespace fst
111 
112 #endif // FST_POWER_WEIGHT_MAPPERS_H_
ProjectPowerWeightMapper(Index from_index=0, Index to_index=0, const ComponentWeight &default_weight=ComponentWeight::Zero())
typename FromPowerWeight::Index Index
ToPowerWeight operator()(const FromWeight &w) const
PowerWeightT operator()(const PowerWeightT &w) const
TransformPowerWeightMapper(const TransformFn &transform=TransformFn())
typename PowerWeightT::Weight ComponentWeight
typename ToPowerWeight::Index Index
typename PowerWeightT::Index Index
PowerWeightT operator()(const PowerWeightT &w) const
ToWeight operator()(const FromPowerWeight &w) const