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