// 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) // Inspiration: shumash@google.com (Masha Maria Shugrina) // \file // Expectation semiring as described by Jason Eisner: // See: doi=10.1.1.22.9398 // Multiplex semiring operations and identities: // One: // Zero: // Plus: + = < (a1 + a2) , (b1 + b2) > // Times: * = < (a1 * a2) , [(a1 * b2) + (a2 * b1)] > // Division: Undefined (currently) // // Usually used to store the pair so that // ShortestDistance[Fst > >] // == < PosteriorProbability, Expected_Value[V] > #ifndef FST_LIB_EXPECTATION_WEIGHT_H_ #define FST_LIB_EXPECTATION_WEIGHT_H_ #include #include namespace fst { // X1 is usually a probability weight like LogWeight // X2 is usually a random variable or vector // see SignedLogWeight or SparsePowerWeight // // If X1 is distinct from X2, it is required that there is an external // product between X1 and X2 and if both semriring are commutative, or // left or right semirings, then result must have those properties. template class ExpectationWeight : public PairWeight { public: using PairWeight::Value1; using PairWeight::Value2; using PairWeight::Reverse; using PairWeight::Quantize; using PairWeight::Member; typedef X1 W1; typedef X2 W2; typedef ExpectationWeight ReverseWeight; ExpectationWeight() : PairWeight(Zero()) { } ExpectationWeight(const ExpectationWeight& w) : PairWeight (w) { } ExpectationWeight(const PairWeight& w) : PairWeight (w) { } ExpectationWeight(const X1& x1, const X2& x2) : PairWeight(x1, x2) { } static const ExpectationWeight &Zero() { static const ExpectationWeight zero(X1::Zero(), X2::Zero()); return zero; } static const ExpectationWeight &One() { static const ExpectationWeight one(X1::One(), X2::Zero()); return one; } static const ExpectationWeight &NoWeight() { static const ExpectationWeight no_weight(X1::NoWeight(), X2::NoWeight()); return no_weight; } static const string &Type() { static const string type = "expectation_" + X1::Type() + "_" + X2::Type(); return type; } PairWeight Quantize(float delta = kDelta) const { return PairWeight::Quantize(); } ReverseWeight Reverse() const { return PairWeight::Reverse(); } bool Member() const { return PairWeight::Member(); } static uint64 Properties() { uint64 props1 = W1::Properties(); uint64 props2 = W2::Properties(); return props1 & props2 & (kLeftSemiring | kRightSemiring | kCommutative | kIdempotent); } }; template inline ExpectationWeight Plus(const ExpectationWeight &w, const ExpectationWeight &v) { return ExpectationWeight(Plus(w.Value1(), v.Value1()), Plus(w.Value2(), v.Value2())); } template inline ExpectationWeight Times(const ExpectationWeight &w, const ExpectationWeight &v) { return ExpectationWeight(Times(w.Value1(), v.Value1()), Plus(Times(w.Value1(), v.Value2()), Times(w.Value2(), v.Value1()))); } template inline ExpectationWeight Divide(const ExpectationWeight &w, const ExpectationWeight &v, DivideType typ = DIVIDE_ANY) { FSTERROR() << "ExpectationWeight::Divide: not implemented"; return ExpectationWeight::NoWeight(); } } // namespace fst #endif // FST_LIB_EXPECTATION_WEIGHT_H_