FST  openfst-1.8.3
OpenFst Library
product-weight.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 // Product weight set and associated semiring operation definitions.
19 
20 #ifndef FST_PRODUCT_WEIGHT_H_
21 #define FST_PRODUCT_WEIGHT_H_
22 
23 #include <cstdint>
24 #include <random>
25 #include <string>
26 #include <utility>
27 
28 #include <fst/pair-weight.h>
29 #include <fst/weight.h>
30 
31 namespace fst {
32 
33 // Product semiring: W1 * W2.
34 template <class W1, class W2>
35 class ProductWeight : public PairWeight<W1, W2> {
36  public:
37  using ReverseWeight =
39 
40  ProductWeight() = default;
41 
42  explicit ProductWeight(const PairWeight<W1, W2> &weight)
43  : PairWeight<W1, W2>(weight) {}
44 
45  ProductWeight(W1 w1, W2 w2)
46  : PairWeight<W1, W2>(std::move(w1), std::move(w2)) {}
47 
48  static const ProductWeight &Zero() {
49  static const ProductWeight zero(PairWeight<W1, W2>::Zero());
50  return zero;
51  }
52 
53  static const ProductWeight &One() {
54  static const ProductWeight one(PairWeight<W1, W2>::One());
55  return one;
56  }
57 
58  static const ProductWeight &NoWeight() {
59  static const ProductWeight no_weight(PairWeight<W1, W2>::NoWeight());
60  return no_weight;
61  }
62 
63  static const std::string &Type() {
64  static const std::string *const type =
65  new std::string(W1::Type() + "_X_" + W2::Type());
66  return *type;
67  }
68 
69  static constexpr uint64_t Properties() {
70  return W1::Properties() & W2::Properties() &
72  }
73 
74  ProductWeight Quantize(float delta = kDelta) const {
76  }
77 
80  }
81 };
82 
83 template <class W1, class W2>
85  const ProductWeight<W1, W2> &w2) {
86  return ProductWeight<W1, W2>(Plus(w1.Value1(), w2.Value1()),
87  Plus(w1.Value2(), w2.Value2()));
88 }
89 
90 template <class W1, class W2>
92  const ProductWeight<W1, W2> &w2) {
93  return ProductWeight<W1, W2>(Times(w1.Value1(), w2.Value1()),
94  Times(w1.Value2(), w2.Value2()));
95 }
96 
97 template <class W1, class W2>
99  const ProductWeight<W1, W2> &w2,
100  DivideType typ = DIVIDE_ANY) {
101  return ProductWeight<W1, W2>(Divide(w1.Value1(), w2.Value1(), typ),
102  Divide(w1.Value2(), w2.Value2(), typ));
103 }
104 
105 // Specialization for product weight
106 template <class W1, class W2>
107 class Adder<ProductWeight<W1, W2>> {
108  public:
110 
111  Adder() = default;
112 
113  explicit Adder(Weight w) : adder1_(w.Value1()), adder2_(w.Value2()) {}
114 
115  Weight Add(const Weight &w) {
116  adder1_.Add(w.Value1());
117  adder2_.Add(w.Value2());
118  return Sum();
119  }
120 
121  Weight Sum() const { return Weight(adder1_.Sum(), adder2_.Sum()); }
122 
123  void Reset(Weight w = Weight::Zero()) {
124  adder1_.Reset(w.Value1());
125  adder2_.Reset(w.Value2());
126  }
127 
128  private:
129  Adder<W1> adder1_;
130  Adder<W2> adder2_;
131 };
132 
133 // This function object generates weights by calling the underlying generators
134 // for the template weight types, like all other pair weight types. This is
135 // intended primarily for testing.
136 template <class W1, class W2>
138  public:
141 
142  explicit WeightGenerate(uint64_t seed = std::random_device()(),
143  bool allow_zero = true)
144  : generate_(seed, allow_zero) {}
145 
146  Weight operator()() const { return Weight(generate_()); }
147 
148  private:
149  const Generate generate_;
150 };
151 
152 } // namespace fst
153 
154 #endif // FST_PRODUCT_WEIGHT_H_
ProductWeight Quantize(float delta=kDelta) const
static const std::string & Type()
WeightGenerate(uint64_t seed=std::random_device()(), bool allow_zero=true)
ErrorWeight Plus(const ErrorWeight &, const ErrorWeight &)
Definition: error-weight.h:61
ProductWeight(const PairWeight< W1, W2 > &weight)
ErrorWeight Times(const ErrorWeight &, const ErrorWeight &)
Definition: error-weight.h:64
ProductWeight()=default
const W2 & Value2() const
Definition: pair-weight.h:95
static constexpr uint64_t Properties()
constexpr uint64_t kIdempotent
Definition: weight.h:147
static const ProductWeight & NoWeight()
static const ProductWeight & Zero()
static const ProductWeight & One()
void Reset(Weight w=Weight::Zero())
constexpr uint64_t kRightSemiring
Definition: weight.h:139
ReverseWeight Reverse() const
constexpr uint64_t kCommutative
Definition: weight.h:144
ErrorWeight Divide(const ErrorWeight &, const ErrorWeight &)
Definition: error-weight.h:67
ProductWeight(W1 w1, W2 w2)
DivideType
Definition: weight.h:165
constexpr uint64_t kLeftSemiring
Definition: weight.h:136
constexpr float kDelta
Definition: weight.h:133
ProductWeight< typename W1::ReverseWeight, typename W2::ReverseWeight > ReverseWeight
const W1 & Value1() const
Definition: pair-weight.h:93