• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // product-weight.h
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 // Copyright 2005-2010 Google, Inc.
16 // Author: riley@google.com (Michael Riley)
17 //
18 // \file
19 // Product weight set and associated semiring operation definitions.
20 
21 #ifndef FST_LIB_PRODUCT_WEIGHT_H__
22 #define FST_LIB_PRODUCT_WEIGHT_H__
23 
24 #include <stack>
25 #include <string>
26 
27 #include <fst/pair-weight.h>
28 #include <fst/weight.h>
29 
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 PairWeight<W1, W2>::Zero;
38   using PairWeight<W1, W2>::One;
39   using PairWeight<W1, W2>::NoWeight;
40   using PairWeight<W1, W2>::Quantize;
41   using PairWeight<W1, W2>::Reverse;
42 
43   typedef ProductWeight<typename W1::ReverseWeight, typename W2::ReverseWeight>
44   ReverseWeight;
45 
ProductWeight()46   ProductWeight() {}
47 
ProductWeight(const PairWeight<W1,W2> & w)48   ProductWeight(const PairWeight<W1, W2>& w) : PairWeight<W1, W2>(w) {}
49 
ProductWeight(W1 w1,W2 w2)50   ProductWeight(W1 w1, W2 w2) : PairWeight<W1, W2>(w1, w2) {}
51 
Zero()52   static const ProductWeight<W1, W2> &Zero() {
53     static const ProductWeight<W1, W2> zero(PairWeight<W1, W2>::Zero());
54     return zero;
55   }
56 
One()57   static const ProductWeight<W1, W2> &One() {
58     static const ProductWeight<W1, W2> one(PairWeight<W1, W2>::One());
59     return one;
60   }
61 
NoWeight()62   static const ProductWeight<W1, W2> &NoWeight() {
63     static const ProductWeight<W1, W2> no_weight(
64         PairWeight<W1, W2>::NoWeight());
65     return no_weight;
66   }
67 
Type()68   static const string &Type() {
69     static const string type = W1::Type() + "_X_" + W2::Type();
70     return type;
71   }
72 
Properties()73   static uint64 Properties() {
74     uint64 props1 = W1::Properties();
75     uint64 props2 = W2::Properties();
76     return props1 & props2 & (kLeftSemiring | kRightSemiring |
77                               kCommutative | kIdempotent);
78   }
79 
80   ProductWeight<W1, W2> Quantize(float delta = kDelta) const {
81     return PairWeight<W1, W2>::Quantize(delta);
82   }
83 
Reverse()84   ReverseWeight Reverse() const {
85     return PairWeight<W1, W2>::Reverse();
86   }
87 
88 
89 };
90 
91 template <class W1, class W2>
Plus(const ProductWeight<W1,W2> & w,const ProductWeight<W1,W2> & v)92 inline ProductWeight<W1, W2> Plus(const ProductWeight<W1, W2> &w,
93                                   const ProductWeight<W1, W2> &v) {
94   return ProductWeight<W1, W2>(Plus(w.Value1(), v.Value1()),
95                                Plus(w.Value2(), v.Value2()));
96 }
97 
98 template <class W1, class W2>
Times(const ProductWeight<W1,W2> & w,const ProductWeight<W1,W2> & v)99 inline ProductWeight<W1, W2> Times(const ProductWeight<W1, W2> &w,
100                                    const ProductWeight<W1, W2> &v) {
101   return ProductWeight<W1, W2>(Times(w.Value1(), v.Value1()),
102                                Times(w.Value2(), v.Value2()));
103 }
104 
105 template <class W1, class W2>
106 inline ProductWeight<W1, W2> Divide(const ProductWeight<W1, W2> &w,
107                                     const ProductWeight<W1, W2> &v,
108                                     DivideType typ = DIVIDE_ANY) {
109   return ProductWeight<W1, W2>(Divide(w.Value1(), v.Value1(), typ),
110                                Divide(w.Value2(), v.Value2(), typ));
111 }
112 
113 }  // namespace fst
114 
115 #endif  // FST_LIB_PRODUCT_WEIGHT_H__
116