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 template <class W1, class W2>
Plus(const ProductWeight<W1,W2> & w,const ProductWeight<W1,W2> & v)90 inline ProductWeight<W1, W2> Plus(const ProductWeight<W1, W2> &w,
91                                   const ProductWeight<W1, W2> &v) {
92   return ProductWeight<W1, W2>(Plus(w.Value1(), v.Value1()),
93                                Plus(w.Value2(), v.Value2()));
94 }
95 
96 template <class W1, class W2>
Times(const ProductWeight<W1,W2> & w,const ProductWeight<W1,W2> & v)97 inline ProductWeight<W1, W2> Times(const ProductWeight<W1, W2> &w,
98                                    const ProductWeight<W1, W2> &v) {
99   return ProductWeight<W1, W2>(Times(w.Value1(), v.Value1()),
100                                Times(w.Value2(), v.Value2()));
101 }
102 
103 template <class W1, class W2>
104 inline ProductWeight<W1, W2> Divide(const ProductWeight<W1, W2> &w,
105                                     const ProductWeight<W1, W2> &v,
106                                     DivideType typ = DIVIDE_ANY) {
107   return ProductWeight<W1, W2>(Divide(w.Value1(), v.Value1(), typ),
108                                Divide(w.Value2(), v.Value2(), typ));
109 }
110 
111 }  // namespace fst
112 
113 #endif  // FST_LIB_PRODUCT_WEIGHT_H__
114