// lat/determinize-lattice-pruned.cc // Copyright 2009-2012 Microsoft Corporation // 2012-2013 Johns Hopkins University (Author: Daniel Povey) // 2014 Guoguo Chen // 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. #include #include #include "fstext/determinize-lattice.h" // for LatticeStringRepository #include "fstext/fstext-utils.h" #include "lat/lattice-functions.h" // for PruneLattice #include "lat/minimize-lattice.h" // for minimization #include "lat/push-lattice.h" // for minimization #include "lat/determinize-lattice-pruned.h" namespace fst { // class LatticeDeterminizerPruned is templated on the same types that // CompactLatticeWeight is templated on: the base weight (Weight), typically // LatticeWeightTpl etc. but could also be e.g. TropicalWeight, and the // IntType, typically int32, used for the output symbols in the compact // representation of strings [note: the output symbols would usually be // p.d.f. id's in the anticipated use of this code] It has a special requirement // on the Weight type: that there should be a Compare function on the weights // such that Compare(w1, w2) returns -1 if w1 < w2, 0 if w1 == w2, and +1 if w1 > // w2. This requires that there be a total order on the weights. template class LatticeDeterminizerPruned { public: // Output to Gallic acceptor (so the strings go on weights, and there is a 1-1 correspondence // between our states and the states in ofst. If destroy == true, release memory as we go // (but we cannot output again). typedef CompactLatticeWeightTpl CompactWeight; typedef ArcTpl CompactArc; // arc in compact, acceptor form of lattice typedef ArcTpl Arc; // arc in non-compact version of lattice // Output to standard FST with CompactWeightTpl as its weight type (the // weight stores the original output-symbol strings). If destroy == true, // release memory as we go (but we cannot output again). void Output(MutableFst *ofst, bool destroy = true) { KALDI_ASSERT(determinized_); typedef typename Arc::StateId StateId; StateId nStates = static_cast(output_states_.size()); if (destroy) FreeMostMemory(); ofst->DeleteStates(); ofst->SetStart(kNoStateId); if (nStates == 0) { return; } for (StateId s = 0;s < nStates;s++) { OutputStateId news = ofst->AddState(); KALDI_ASSERT(news == s); } ofst->SetStart(0); // now process transitions. for (StateId this_state_id = 0; this_state_id < nStates; this_state_id++) { OutputState &this_state = *(output_states_[this_state_id]); vector &this_vec(this_state.arcs); typename vector::const_iterator iter = this_vec.begin(), end = this_vec.end(); for (;iter != end; ++iter) { const TempArc &temp_arc(*iter); CompactArc new_arc; vector