FST  openfst-1.7.2
OpenFst Library
product-weight.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 // Product weight set and associated semiring operation definitions.
5 
6 #ifndef FST_PRODUCT_WEIGHT_H_
7 #define FST_PRODUCT_WEIGHT_H_
8 
9 #include <string>
10 #include <utility>
11 
12 #include <fst/pair-weight.h>
13 #include <fst/weight.h>
14 
15 
16 namespace fst {
17 
18 // Product semiring: W1 * W2.
19 template <class W1, class W2>
20 class ProductWeight : public PairWeight<W1, W2> {
21  public:
22  using ReverseWeight =
24 
26 
27  explicit ProductWeight(const PairWeight<W1, W2> &weight)
28  : PairWeight<W1, W2>(weight) {}
29 
30  ProductWeight(W1 w1, W2 w2)
31  : PairWeight<W1, W2>(std::move(w1), std::move(w2)) {}
32 
33  static const ProductWeight &Zero() {
34  static const ProductWeight zero(PairWeight<W1, W2>::Zero());
35  return zero;
36  }
37 
38  static const ProductWeight &One() {
39  static const ProductWeight one(PairWeight<W1, W2>::One());
40  return one;
41  }
42 
43  static const ProductWeight &NoWeight() {
44  static const ProductWeight no_weight(PairWeight<W1, W2>::NoWeight());
45  return no_weight;
46  }
47 
48  static const string &Type() {
49  static const string *const type =
50  new string(W1::Type() + "_X_" + W2::Type());
51  return *type;
52  }
53 
54  static constexpr uint64 Properties() {
55  return W1::Properties() & W2::Properties() &
57  }
58 
59  ProductWeight Quantize(float delta = kDelta) const {
61  }
62 
65  }
66 };
67 
68 template <class W1, class W2>
70  const ProductWeight<W1, W2> &w2) {
71  return ProductWeight<W1, W2>(Plus(w1.Value1(), w2.Value1()),
72  Plus(w1.Value2(), w2.Value2()));
73 }
74 
75 template <class W1, class W2>
77  const ProductWeight<W1, W2> &w2) {
78  return ProductWeight<W1, W2>(Times(w1.Value1(), w2.Value1()),
79  Times(w1.Value2(), w2.Value2()));
80 }
81 
82 template <class W1, class W2>
84  const ProductWeight<W1, W2> &w2,
85  DivideType typ = DIVIDE_ANY) {
86  return ProductWeight<W1, W2>(Divide(w1.Value1(), w2.Value1(), typ),
87  Divide(w1.Value2(), w2.Value2(), typ));
88 }
89 
90 // This function object generates weights by calling the underlying generators
91 // for the template weight types, like all other pair weight types. This is
92 // intended primarily for testing.
93 template <class W1, class W2>
94 class WeightGenerate<ProductWeight<W1, W2>> :
95  public WeightGenerate<PairWeight<W1, W2>> {
96  public:
99 
100  explicit WeightGenerate(bool allow_zero = true) : Generate(allow_zero) {}
101 
102  Weight operator()() const { return Weight(Generate::operator()()); }
103 };
104 
105 } // namespace fst
106 
107 #endif // FST_PRODUCT_WEIGHT_H_
ProductWeight Quantize(float delta=kDelta) const
ExpectationWeight< X1, X2 > Divide(const ExpectationWeight< X1, X2 > &w1, const ExpectationWeight< X1, X2 > &w2, DivideType typ=DIVIDE_ANY)
uint64_t uint64
Definition: types.h:32
ProductWeight(const PairWeight< W1, W2 > &weight)
constexpr uint64 kRightSemiring
Definition: weight.h:115
static constexpr uint64 Properties()
ExpectationWeight< X1, X2 > Times(const ExpectationWeight< X1, X2 > &w1, const ExpectationWeight< X1, X2 > &w2)
const W2 & Value2() const
Definition: pair-weight.h:78
constexpr uint64 kCommutative
Definition: weight.h:120
static const ProductWeight & NoWeight()
static const ProductWeight & Zero()
constexpr uint64 kLeftSemiring
Definition: weight.h:112
static const ProductWeight & One()
static const string & Type()
ReverseWeight Reverse() const
constexpr uint64 kIdempotent
Definition: weight.h:123
ExpectationWeight< X1, X2 > Plus(const ExpectationWeight< X1, X2 > &w1, const ExpectationWeight< X1, X2 > &w2)
ProductWeight(W1 w1, W2 w2)
DivideType
Definition: weight.h:142
constexpr float kDelta
Definition: weight.h:109
ProductWeight< typename W1::ReverseWeight, typename W2::ReverseWeight > ReverseWeight
const W1 & Value1() const
Definition: pair-weight.h:76