From 96a32415ab43377cf1575bd3f4f2980f58028209 Mon Sep 17 00:00:00 2001 From: Determinant Date: Fri, 14 Aug 2015 11:51:42 +0800 Subject: add implementation for kaldi io (by ymz) --- .../openfst/include/fst/lexicographic-weight.h | 151 +++++++++++++++++++++ 1 file changed, 151 insertions(+) create mode 100644 kaldi_io/src/tools/openfst/include/fst/lexicographic-weight.h (limited to 'kaldi_io/src/tools/openfst/include/fst/lexicographic-weight.h') diff --git a/kaldi_io/src/tools/openfst/include/fst/lexicographic-weight.h b/kaldi_io/src/tools/openfst/include/fst/lexicographic-weight.h new file mode 100644 index 0000000..4b55c50 --- /dev/null +++ b/kaldi_io/src/tools/openfst/include/fst/lexicographic-weight.h @@ -0,0 +1,151 @@ +// lexicographic-weight.h + +// 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: rws@google.com (Richard Sproat) +// +// \file +// Lexicographic weight set and associated semiring operation definitions. +// +// A lexicographic weight is a sequence of weights, each of which must have the +// path property and Times() must be (strongly) cancellative +// (for all a,b,c != Zero(): Times(c, a) = Times(c, b) => a = b, +// Times(a, c) = Times(b, c) => a = b). +// The + operation on two weights a and b is the lexicographically +// prior of a and b. + +#ifndef FST_LIB_LEXICOGRAPHIC_WEIGHT_H__ +#define FST_LIB_LEXICOGRAPHIC_WEIGHT_H__ + +#include + +#include +#include + + +namespace fst { + +template +class LexicographicWeight : public PairWeight { + public: + using PairWeight::Value1; + using PairWeight::Value2; + using PairWeight::SetValue1; + using PairWeight::SetValue2; + using PairWeight::Zero; + using PairWeight::One; + using PairWeight::NoWeight; + using PairWeight::Quantize; + using PairWeight::Reverse; + + typedef LexicographicWeight + ReverseWeight; + + LexicographicWeight() {} + + LexicographicWeight(const PairWeight& w) + : PairWeight(w) {} + + LexicographicWeight(W1 w1, W2 w2) : PairWeight(w1, w2) { + uint64 props = kPath; + if ((W1::Properties() & props) != props) { + FSTERROR() << "LexicographicWeight must " + << "have the path property: " << W1::Type(); + SetValue1(W1::NoWeight()); + } + if ((W2::Properties() & props) != props) { + FSTERROR() << "LexicographicWeight must " + << "have the path property: " << W2::Type(); + SetValue2(W2::NoWeight()); + } + } + + static const LexicographicWeight &Zero() { + static const LexicographicWeight zero(PairWeight::Zero()); + return zero; + } + + static const LexicographicWeight &One() { + static const LexicographicWeight one(PairWeight::One()); + return one; + } + + static const LexicographicWeight &NoWeight() { + static const LexicographicWeight no_weight( + PairWeight::NoWeight()); + return no_weight; + } + + static const string &Type() { + static const string type = W1::Type() + "_LT_" + W2::Type(); + return type; + } + + bool Member() const { + if (!Value1().Member() || !Value2().Member()) return false; + // Lexicographic weights cannot mix zeroes and non-zeroes. + if (Value1() == W1::Zero() && Value2() == W2::Zero()) return true; + if (Value1() != W1::Zero() && Value2() != W2::Zero()) return true; + return false; + } + + LexicographicWeight Quantize(float delta = kDelta) const { + return PairWeight::Quantize(); + } + + ReverseWeight Reverse() const { + return PairWeight::Reverse(); + } + + static uint64 Properties() { + uint64 props1 = W1::Properties(); + uint64 props2 = W2::Properties(); + return props1 & props2 & (kLeftSemiring | kRightSemiring | kPath | + kIdempotent | kCommutative); + } +}; + +template +inline LexicographicWeight Plus(const LexicographicWeight &w, + const LexicographicWeight &v) { + if (!w.Member() || !v.Member()) + return LexicographicWeight::NoWeight(); + NaturalLess less1; + NaturalLess less2; + if (less1(w.Value1(), v.Value1())) return w; + if (less1(v.Value1(), w.Value1())) return v; + if (less2(w.Value2(), v.Value2())) return w; + if (less2(v.Value2(), w.Value2())) return v; + return w; +} + +template +inline LexicographicWeight Times(const LexicographicWeight &w, + const LexicographicWeight &v) { + return LexicographicWeight(Times(w.Value1(), v.Value1()), + Times(w.Value2(), v.Value2())); +} + +template +inline LexicographicWeight Divide(const LexicographicWeight &w, + const LexicographicWeight &v, + DivideType typ = DIVIDE_ANY) { + return LexicographicWeight(Divide(w.Value1(), v.Value1(), typ), + Divide(w.Value2(), v.Value2(), typ)); +} + +} // namespace fst + +#endif // FST_LIB_LEXICOGRAPHIC_WEIGHT_H__ -- cgit v1.2.3