// fstext/deterministic-fst-inl.h // Copyright 2011-2012 Gilles Boulianne // 2014 Telepoint Global Hosting Service, LLC. (Author: David Snyder) // 2012-2015 Johns Hopkins University (author: Daniel Povey) // See ../../COPYING for clarification regarding multiple authors // // 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 // // THIS CODE IS PROVIDED *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY // KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED // WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE, // MERCHANTABLITY OR NON-INFRINGEMENT. // See the Apache 2 License for the specific language governing permissions and // limitations under the License. #ifndef KALDI_FSTEXT_DETERMINISTIC_FST_INL_H_ #define KALDI_FSTEXT_DETERMINISTIC_FST_INL_H_ #include "base/kaldi-common.h" #include "fstext/fstext-utils.h" namespace fst { // Do not include this file directly. It is included by deterministic-fst.h. template typename Arc::StateId BackoffDeterministicOnDemandFst::GetBackoffState(StateId s, Weight *w) { ArcIterator > aiter(fst_, s); if (aiter.Done()) // no arcs. return kNoStateId; const Arc &arc = aiter.Value(); if (arc.ilabel == 0) { *w = arc.weight; return arc.nextstate; } else { return kNoStateId; } } template typename Arc::Weight BackoffDeterministicOnDemandFst::Final(StateId state) { Weight w = fst_.Final(state); if (w != Weight::Zero()) return w; Weight backoff_w; StateId backoff_state = GetBackoffState(state, &backoff_w); if (backoff_state == kNoStateId) return Weight::Zero(); else return Times(backoff_w, this->Final(backoff_state)); } template BackoffDeterministicOnDemandFst::BackoffDeterministicOnDemandFst( const Fst &fst): fst_(fst) { #ifdef KALDI_PARANOID KALDI_ASSERT(fst_.Properties(kILabelSorted|kIDeterministic, true) == (kILabelSorted|kIDeterministic) && "Input FST is not i-label sorted and deterministic."); #endif } template bool BackoffDeterministicOnDemandFst::GetArc( StateId s, Label ilabel, Arc *oarc) { KALDI_ASSERT(ilabel != 0); // We don't allow GetArc for epsilon. SortedMatcher > sm(fst_, MATCH_INPUT, 1); sm.SetState(s); if (sm.Find(ilabel)) { const Arc &arc = sm.Value(); *oarc = arc; return true; } else { Weight backoff_w; StateId backoff_state = GetBackoffState(s, &backoff_w); if (backoff_state == kNoStateId) return false; if (!this->GetArc(backoff_state, ilabel, oarc)) return false; oarc->weight = Times(oarc->weight, backoff_w); return true; } } template UnweightedNgramFst::UnweightedNgramFst(int n): n_(n) { // Starting state is an empty vector std::vector