00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #ifndef FST_LIB_PRODUCT_WEIGHT_H__
00022 #define FST_LIB_PRODUCT_WEIGHT_H__
00023
00024 #include <stack>
00025 #include <string>
00026 #include <fst/pair-weight.h>
00027 #include <fst/weight.h>
00028
00029 namespace fst {
00030
00031
00032 template<class W1, class W2>
00033 class ProductWeight : public PairWeight<W1, W2> {
00034 public:
00035 using PairWeight<W1, W2>::Zero;
00036 using PairWeight<W1, W2>::One;
00037 using PairWeight<W1, W2>::Quantize;
00038 using PairWeight<W1, W2>::Reverse;
00039
00040 typedef ProductWeight<typename W1::ReverseWeight, typename W2::ReverseWeight>
00041 ReverseWeight;
00042
00043 ProductWeight() {}
00044
00045 ProductWeight(const PairWeight<W1, W2>& w) : PairWeight<W1, W2>(w) {}
00046
00047 ProductWeight(W1 w1, W2 w2) : PairWeight<W1, W2>(w1, w2) {}
00048
00049 static const ProductWeight<W1, W2> &Zero() {
00050 static const ProductWeight<W1, W2> zero(PairWeight<W1, W2>::Zero());
00051 return zero;
00052 }
00053
00054 static const ProductWeight<W1, W2> &One() {
00055 static const ProductWeight<W1, W2> one(PairWeight<W1, W2>::One());
00056 return one;
00057 }
00058
00059 static const string &Type() {
00060 static const string type = W1::Type() + "_X_" + W2::Type();
00061 return type;
00062 }
00063
00064 static uint64 Properties() {
00065 uint64 props1 = W1::Properties();
00066 uint64 props2 = W2::Properties();
00067 return props1 & props2 & (kLeftSemiring | kRightSemiring |
00068 kCommutative | kIdempotent);
00069 }
00070
00071 ProductWeight<W1, W2> Quantize(float delta = kDelta) const {
00072 return PairWeight<W1, W2>::Quantize(delta);
00073 }
00074
00075 ReverseWeight Reverse() const {
00076 return PairWeight<W1, W2>::Reverse();
00077 }
00078
00079
00080 };
00081
00082 template <class W1, class W2>
00083 inline ProductWeight<W1, W2> Plus(const ProductWeight<W1, W2> &w,
00084 const ProductWeight<W1, W2> &v) {
00085 return ProductWeight<W1, W2>(Plus(w.Value1(), v.Value1()),
00086 Plus(w.Value2(), v.Value2()));
00087 }
00088
00089 template <class W1, class W2>
00090 inline ProductWeight<W1, W2> Times(const ProductWeight<W1, W2> &w,
00091 const ProductWeight<W1, W2> &v) {
00092 return ProductWeight<W1, W2>(Times(w.Value1(), v.Value1()),
00093 Times(w.Value2(), v.Value2()));
00094 }
00095
00096 template <class W1, class W2>
00097 inline ProductWeight<W1, W2> Divide(const ProductWeight<W1, W2> &w,
00098 const ProductWeight<W1, W2> &v,
00099 DivideType typ = DIVIDE_ANY) {
00100 return ProductWeight<W1, W2>(Divide(w.Value1(), v.Value1(), typ),
00101 Divide(w.Value2(), v.Value2(), typ));
00102 }
00103
00104 }
00105
00106 #endif /// FST_LIB_PRODUCT_WEIGHT_H__
00107