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