00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #ifndef FST_LIB_LEXICOGRAPHIC_WEIGHT_H__
00025 #define FST_LIB_LEXICOGRAPHIC_WEIGHT_H__
00026
00027 #include <string>
00028 #include <fst/pair-weight.h>
00029 #include <fst/weight.h>
00030
00031 namespace fst {
00032
00033 template<class W1, class W2>
00034 class LexicographicWeight : public PairWeight<W1, W2> {
00035 public:
00036 using PairWeight<W1, W2>::Value1;
00037 using PairWeight<W1, W2>::Value2;
00038 using PairWeight<W1, W2>::Zero;
00039 using PairWeight<W1, W2>::One;
00040 using PairWeight<W1, W2>::Quantize;
00041 using PairWeight<W1, W2>::Reverse;
00042
00043 typedef LexicographicWeight<typename W1::ReverseWeight,
00044 typename W2::ReverseWeight>
00045 ReverseWeight;
00046
00047 LexicographicWeight() {}
00048
00049 LexicographicWeight(const PairWeight<W1, W2>& w)
00050 : PairWeight<W1, W2>(w) {}
00051
00052 LexicographicWeight(W1 w1, W2 w2) : PairWeight<W1, W2>(w1, w2) {
00053 uint64 props = kPath;
00054 if ((W1::Properties() & props) != props) {
00055 LOG(ERROR) << "LexicographicWeight must "
00056 << "have the path property: " << W1::Type();
00057 }
00058 if ((W2::Properties() & props) != props) {
00059 LOG(ERROR) << "LexicographicWeight must "
00060 << "have the path property: " << W2::Type();
00061 }
00062 }
00063
00064 static const LexicographicWeight<W1, W2> &Zero() {
00065 static const LexicographicWeight<W1, W2> zero(PairWeight<W1, W2>::Zero());
00066 return zero;
00067 }
00068
00069 static const LexicographicWeight<W1, W2> &One() {
00070 static const LexicographicWeight<W1, W2> one(PairWeight<W1, W2>::One());
00071 return one;
00072 }
00073
00074 static const string &Type() {
00075 static const string type = W1::Type() + "_<_" + W2::Type();
00076 return type;
00077 }
00078
00079 bool Member() const {
00080 if (!Value1().Member() || !Value2().Member()) return false;
00081
00082 if (Value1() == W1::Zero() && Value2() == W2::Zero()) return true;
00083 if (Value1() != W1::Zero() && Value2() != W2::Zero()) return true;
00084 return false;
00085 }
00086
00087 LexicographicWeight<W1, W2> Quantize(float delta = kDelta) const {
00088 return PairWeight<W1, W2>::Quantize();
00089 }
00090
00091 ReverseWeight Reverse() const {
00092 return PairWeight<W1, W2>::Reverse();
00093 }
00094
00095 static uint64 Properties() {
00096 uint64 props1 = W1::Properties();
00097 uint64 props2 = W2::Properties();
00098 return props1 & props2 & (kLeftSemiring | kRightSemiring | kPath |
00099 kIdempotent | kCommutative);
00100 }
00101 };
00102
00103 template <class W1, class W2>
00104 inline LexicographicWeight<W1, W2> Plus(const LexicographicWeight<W1, W2> &w,
00105 const LexicographicWeight<W1, W2> &v) {
00106 NaturalLess<W1> less1;
00107 NaturalLess<W2> less2;
00108 if (less1(w.Value1(), v.Value1())) return w;
00109 if (less1(v.Value1(), w.Value1())) return v;
00110 if (less2(w.Value2(), v.Value2())) return w;
00111 if (less2(v.Value2(), w.Value2())) return v;
00112 return w;
00113 }
00114
00115 template <class W1, class W2>
00116 inline LexicographicWeight<W1, W2> Times(const LexicographicWeight<W1, W2> &w,
00117 const LexicographicWeight<W1, W2> &v) {
00118 return LexicographicWeight<W1, W2>(Times(w.Value1(), v.Value1()),
00119 Times(w.Value2(), v.Value2()));
00120 }
00121
00122 template <class W1, class W2>
00123 inline LexicographicWeight<W1, W2> Divide(const LexicographicWeight<W1, W2> &w,
00124 const LexicographicWeight<W1, W2> &v,
00125 DivideType typ = DIVIDE_ANY) {
00126 return LexicographicWeight<W1, W2>(Divide(w.Value1(), v.Value1(), typ),
00127 Divide(w.Value2(), v.Value2(), typ));
00128 }
00129
00130 }
00131
00132 #endif // FST_LIB_LEXICOGRAPHIC_WEIGHT_H__