// Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. // // Copyright 2005-2010 Google, Inc. // Author: krr@google.com (Kasturi Rangan Raghavan) // \file // LogWeight along with sign information that represents the value X in the // linear domain as // The sign is a TropicalWeight: // positive, TropicalWeight.Value() > 0.0, recommended value 1.0 // negative, TropicalWeight.Value() <= 0.0, recommended value -1.0 #ifndef FST_LIB_SIGNED_LOG_WEIGHT_H_ #define FST_LIB_SIGNED_LOG_WEIGHT_H_ #include #include namespace fst { template class SignedLogWeightTpl : public PairWeight > { public: typedef TropicalWeight X1; typedef LogWeightTpl X2; using PairWeight::Value1; using PairWeight::Value2; using PairWeight::Reverse; using PairWeight::Quantize; using PairWeight::Member; typedef SignedLogWeightTpl ReverseWeight; SignedLogWeightTpl() : PairWeight() {} SignedLogWeightTpl(const SignedLogWeightTpl& w) : PairWeight (w) { } SignedLogWeightTpl(const PairWeight& w) : PairWeight (w) { } SignedLogWeightTpl(const X1& x1, const X2& x2) : PairWeight(x1, x2) { } static const SignedLogWeightTpl &Zero() { static const SignedLogWeightTpl zero(X1(1.0), X2::Zero()); return zero; } static const SignedLogWeightTpl &One() { static const SignedLogWeightTpl one(X1(1.0), X2::One()); return one; } static const SignedLogWeightTpl &NoWeight() { static const SignedLogWeightTpl no_weight(X1(1.0), X2::NoWeight()); return no_weight; } static const string &Type() { static const string type = "signed_log_" + X1::Type() + "_" + X2::Type(); return type; } ProductWeight Quantize(float delta = kDelta) const { return PairWeight::Quantize(); } ReverseWeight Reverse() const { return PairWeight::Reverse(); } bool Member() const { return PairWeight::Member(); } static uint64 Properties() { // not idempotent nor path return kLeftSemiring | kRightSemiring | kCommutative; } size_t Hash() const { size_t h1; if (Value2() == X2::Zero() || Value1().Value() > 0.0) h1 = TropicalWeight(1.0).Hash(); else h1 = TropicalWeight(-1.0).Hash(); size_t h2 = Value2().Hash(); const int lshift = 5; const int rshift = CHAR_BIT * sizeof(size_t) - 5; return h1 << lshift ^ h1 >> rshift ^ h2; } }; template inline SignedLogWeightTpl Plus(const SignedLogWeightTpl &w1, const SignedLogWeightTpl &w2) { if (!w1.Member() || !w2.Member()) return SignedLogWeightTpl::NoWeight(); bool s1 = w1.Value1().Value() > 0.0; bool s2 = w2.Value1().Value() > 0.0; T f1 = w1.Value2().Value(); T f2 = w2.Value2().Value(); if (f1 == FloatLimits::PosInfinity()) return w2; else if (f2 == FloatLimits::PosInfinity()) return w1; else if (f1 == f2) { if (s1 == s2) return SignedLogWeightTpl(w1.Value1(), (f2 - log(2.0F))); else return SignedLogWeightTpl::Zero(); } else if (f1 > f2) { if (s1 == s2) { return SignedLogWeightTpl( w1.Value1(), (f2 - log(1.0F + exp(f2 - f1)))); } else { return SignedLogWeightTpl( w2.Value1(), (f2 - log(1.0F - exp(f2 - f1)))); } } else { if (s2 == s1) { return SignedLogWeightTpl( w2.Value1(), (f1 - log(1.0F + exp(f1 - f2)))); } else { return SignedLogWeightTpl( w1.Value1(), (f1 - log(1.0F - exp(f1 - f2)))); } } } template inline SignedLogWeightTpl Minus(const SignedLogWeightTpl &w1, const SignedLogWeightTpl &w2) { SignedLogWeightTpl minus_w2(-w2.Value1().Value(), w2.Value2()); return Plus(w1, minus_w2); } template inline SignedLogWeightTpl Times(const SignedLogWeightTpl &w1, const SignedLogWeightTpl &w2) { if (!w1.Member() || !w2.Member()) return SignedLogWeightTpl::NoWeight(); bool s1 = w1.Value1().Value() > 0.0; bool s2 = w2.Value1().Value() > 0.0; T f1 = w1.Value2().Value(); T f2 = w2.Value2().Value(); if (s1 == s2) return SignedLogWeightTpl(TropicalWeight(1.0), (f1 + f2)); else return SignedLogWeightTpl(TropicalWeight(-1.0), (f1 + f2)); } template inline SignedLogWeightTpl Divide(const SignedLogWeightTpl &w1, const SignedLogWeightTpl &w2, DivideType typ = DIVIDE_ANY) { if (!w1.Member() || !w2.Member()) return SignedLogWeightTpl::NoWeight(); bool s1 = w1.Value1().Value() > 0.0; bool s2 = w2.Value1().Value() > 0.0; T f1 = w1.Value2().Value(); T f2 = w2.Value2().Value(); if (f2 == FloatLimits::PosInfinity()) return SignedLogWeightTpl(TropicalWeight(1.0), FloatLimits::NumberBad()); else if (f1 == FloatLimits::PosInfinity()) return SignedLogWeightTpl(TropicalWeight(1.0), FloatLimits::PosInfinity()); else if (s1 == s2) return SignedLogWeightTpl(TropicalWeight(1.0), (f1 - f2)); else return SignedLogWeightTpl(TropicalWeight(-1.0), (f1 - f2)); } template inline bool ApproxEqual(const SignedLogWeightTpl &w1, const SignedLogWeightTpl &w2, float delta = kDelta) { bool s1 = w1.Value1().Value() > 0.0; bool s2 = w2.Value1().Value() > 0.0; if (s1 == s2) { return ApproxEqual(w1.Value2(), w2.Value2(), delta); } else { return w1.Value2() == LogWeightTpl::Zero() && w2.Value2() == LogWeightTpl::Zero(); } } template inline bool operator==(const SignedLogWeightTpl &w1, const SignedLogWeightTpl &w2) { bool s1 = w1.Value1().Value() > 0.0; bool s2 = w2.Value1().Value() > 0.0; if (s1 == s2) return w1.Value2() == w2.Value2(); else return (w1.Value2() == LogWeightTpl::Zero()) && (w2.Value2() == LogWeightTpl::Zero()); } // Single-precision signed-log weight typedef SignedLogWeightTpl SignedLogWeight; // Double-precision signed-log weight typedef SignedLogWeightTpl SignedLog64Weight; // // WEIGHT CONVERTER SPECIALIZATIONS. // template bool SignedLogConvertCheck(W1 w) { if (w.Value1().Value() < 0.0) { FSTERROR() << "WeightConvert: can't convert weight from \"" << W1::Type() << "\" to \"" << W2::Type(); return false; } return true; } // Convert to tropical template <> struct WeightConvert { TropicalWeight operator()(SignedLogWeight w) const { if (!SignedLogConvertCheck(w)) return TropicalWeight::NoWeight(); return w.Value2().Value(); } }; template <> struct WeightConvert { TropicalWeight operator()(SignedLog64Weight w) const { if (!SignedLogConvertCheck(w)) return TropicalWeight::NoWeight(); return w.Value2().Value(); } }; // Convert to log template <> struct WeightConvert { LogWeight operator()(SignedLogWeight w) const { if (!SignedLogConvertCheck(w)) return LogWeight::NoWeight(); return w.Value2().Value(); } }; template <> struct WeightConvert { LogWeight operator()(SignedLog64Weight w) const { if (!SignedLogConvertCheck(w)) return LogWeight::NoWeight(); return w.Value2().Value(); } }; // Convert to log64 template <> struct WeightConvert { Log64Weight operator()(SignedLogWeight w) const { if (!SignedLogConvertCheck(w)) return Log64Weight::NoWeight(); return w.Value2().Value(); } }; template <> struct WeightConvert { Log64Weight operator()(SignedLog64Weight w) const { if (!SignedLogConvertCheck(w)) return Log64Weight::NoWeight(); return w.Value2().Value(); } }; // Convert to signed log template <> struct WeightConvert { SignedLogWeight operator()(TropicalWeight w) const { TropicalWeight x1 = 1.0; LogWeight x2 = w.Value(); return SignedLogWeight(x1, x2); } }; template <> struct WeightConvert { SignedLogWeight operator()(LogWeight w) const { TropicalWeight x1 = 1.0; LogWeight x2 = w.Value(); return SignedLogWeight(x1, x2); } }; template <> struct WeightConvert { SignedLogWeight operator()(Log64Weight w) const { TropicalWeight x1 = 1.0; LogWeight x2 = w.Value(); return SignedLogWeight(x1, x2); } }; template <> struct WeightConvert { SignedLogWeight operator()(SignedLog64Weight w) const { TropicalWeight x1 = w.Value1(); LogWeight x2 = w.Value2().Value(); return SignedLogWeight(x1, x2); } }; // Convert to signed log64 template <> struct WeightConvert { SignedLog64Weight operator()(TropicalWeight w) const { TropicalWeight x1 = 1.0; Log64Weight x2 = w.Value(); return SignedLog64Weight(x1, x2); } }; template <> struct WeightConvert { SignedLog64Weight operator()(LogWeight w) const { TropicalWeight x1 = 1.0; Log64Weight x2 = w.Value(); return SignedLog64Weight(x1, x2); } }; template <> struct WeightConvert { SignedLog64Weight operator()(Log64Weight w) const { TropicalWeight x1 = 1.0; Log64Weight x2 = w.Value(); return SignedLog64Weight(x1, x2); } }; template <> struct WeightConvert { SignedLog64Weight operator()(SignedLogWeight w) const { TropicalWeight x1 = w.Value1(); Log64Weight x2 = w.Value2().Value(); return SignedLog64Weight(x1, x2); } }; } // namespace fst #endif // FST_LIB_SIGNED_LOG_WEIGHT_H_