Blame view

tools/openfst-1.6.7/src/include/fst/power-weight.h 4.63 KB
8dcb6dfcb   Yannick Estève   first commit
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
  // See www.openfst.org for extensive documentation on this weighted
  // finite-state transducer library.
  //
  // Cartesian power weight semiring operation definitions.
  
  #ifndef FST_POWER_WEIGHT_H_
  #define FST_POWER_WEIGHT_H_
  
  #include <string>
  
  #include <fst/tuple-weight.h>
  #include <fst/weight.h>
  
  
  namespace fst {
  
  // Cartesian power semiring: W ^ n
  //
  // Forms:
  //  - a left semimodule when W is a left semiring,
  //  - a right semimodule when W is a right semiring,
  //  - a bisemimodule when W is a semiring,
  //    the free semimodule of rank n over W
  // The Times operation is overloaded to provide the left and right scalar
  // products.
  template <class W, size_t n>
  class PowerWeight : public TupleWeight<W, n> {
   public:
    using ReverseWeight = PowerWeight<typename W::ReverseWeight, n>;
  
    PowerWeight() {}
  
    explicit PowerWeight(const TupleWeight<W, n> &weight)
        : TupleWeight<W, n>(weight) {}
  
    template <class Iterator>
    PowerWeight(Iterator begin, Iterator end) : TupleWeight<W, n>(begin, end) {}
  
    // Initialize component `index` to `weight`; initialize all other components
    // to `default_weight`
    PowerWeight(size_t index, const W &weight,
                const W &default_weight = W::Zero())
        : TupleWeight<W, n>(index, weight, default_weight) {}
  
    static const PowerWeight &Zero() {
      static const PowerWeight zero(TupleWeight<W, n>::Zero());
      return zero;
    }
  
    static const PowerWeight &One() {
      static const PowerWeight one(TupleWeight<W, n>::One());
      return one;
    }
  
    static const PowerWeight &NoWeight() {
      static const PowerWeight no_weight(TupleWeight<W, n>::NoWeight());
      return no_weight;
    }
  
    static const string &Type() {
      static const string *const type =
          new string(W::Type() + "_^" + std::to_string(n));
      return *type;
    }
  
    static constexpr uint64 Properties() {
      return W::Properties() &
             (kLeftSemiring | kRightSemiring | kCommutative | kIdempotent);
    }
  
    PowerWeight Quantize(float delta = kDelta) const {
      return PowerWeight(TupleWeight<W, n>::Quantize(delta));
    }
  
    ReverseWeight Reverse() const {
      return ReverseWeight(TupleWeight<W, n>::Reverse());
    }
  };
  
  // Semiring plus operation.
  template <class W, size_t n>
  inline PowerWeight<W, n> Plus(const PowerWeight<W, n> &w1,
                                const PowerWeight<W, n> &w2) {
    PowerWeight<W, n> result;
    for (size_t i = 0; i < n; ++i) {
      result.SetValue(i, Plus(w1.Value(i), w2.Value(i)));
    }
    return result;
  }
  
  // Semiring times operation.
  template <class W, size_t n>
  inline PowerWeight<W, n> Times(const PowerWeight<W, n> &w1,
                                 const PowerWeight<W, n> &w2) {
    PowerWeight<W, n> result;
    for (size_t i = 0; i < n; ++i) {
      result.SetValue(i, Times(w1.Value(i), w2.Value(i)));
    }
    return result;
  }
  
  // Semiring divide operation.
  template <class W, size_t n>
  inline PowerWeight<W, n> Divide(const PowerWeight<W, n> &w1,
                                  const PowerWeight<W, n> &w2,
                                  DivideType type = DIVIDE_ANY) {
    PowerWeight<W, n> result;
    for (size_t i = 0; i < n; ++i) {
      result.SetValue(i, Divide(w1.Value(i), w2.Value(i), type));
    }
    return result;
  }
  
  // Semimodule left scalar product.
  template <class W, size_t n>
  inline PowerWeight<W, n> Times(const W &scalar,
                                 const PowerWeight<W, n> &weight) {
    PowerWeight<W, n> result;
    for (size_t i = 0; i < n; ++i) {
      result.SetValue(i, Times(scalar, weight.Value(i)));
    }
    return result;
  }
  
  // Semimodule right scalar product.
  template <class W, size_t n>
  inline PowerWeight<W, n> Times(const PowerWeight<W, n> &weight,
                                 const W &scalar) {
    PowerWeight<W, n> result;
    for (size_t i = 0; i < n; ++i) {
      result.SetValue(i, Times(weight.Value(i), scalar));
    }
    return result;
  }
  
  // Semimodule dot product.
  template <class W, size_t n>
  inline W DotProduct(const PowerWeight<W, n> &w1, const PowerWeight<W, n> &w2) {
    W result(W::Zero());
    for (size_t i = 0; i < n; ++i) {
      result = Plus(result, Times(w1.Value(i), w2.Value(i)));
    }
    return result;
  }
  
  // This function object generates weights over the Cartesian power of rank
  // n over the underlying weight. This is intended primarily for testing.
  template <class W, size_t n>
  class WeightGenerate<PowerWeight<W, n>> {
   public:
    using Weight = PowerWeight<W, n>;
    using Generate = WeightGenerate<W>;
  
    explicit WeightGenerate(bool allow_zero = true) : generate_(allow_zero) {}
  
    Weight operator()() const {
      Weight result;
      for (size_t i = 0; i < n; ++i) result.SetValue(i, generate_());
      return result;
    }
  
   private:
    Generate generate_;
  };
  
  }  // namespace fst
  
  #endif  // FST_POWER_WEIGHT_H_