// replace-util.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: riley@google.com (Michael Riley) // // \file // Utility classes for the recursive replacement of Fsts (RTNs). #ifndef FST_LIB_REPLACE_UTIL_H__ #define FST_LIB_REPLACE_UTIL_H__ #include using std::vector; #include using std::tr1::unordered_map; using std::tr1::unordered_multimap; #include using std::tr1::unordered_set; using std::tr1::unordered_multiset; #include #include #include #include namespace fst { template void Replace(const vector* > >&, MutableFst *, typename Arc::Label, bool); // Utility class for the recursive replacement of Fsts (RTNs). The // user provides a set of Label, Fst pairs at construction. These are // used by methods for testing cyclic dependencies and connectedness // and doing RTN connection and specific Fst replacement by label or // for various optimization properties. The modified results can be // obtained with the GetFstPairs() or GetMutableFstPairs() methods. template class ReplaceUtil { public: typedef typename Arc::Label Label; typedef typename Arc::Weight Weight; typedef typename Arc::StateId StateId; typedef pair*> FstPair; typedef pair*> MutableFstPair; typedef unordered_map NonTerminalHash; // Constructs from mutable Fsts; Fst ownership given to ReplaceUtil. ReplaceUtil(const vector &fst_pairs, Label root_label, bool epsilon_on_replace = false); // Constructs from Fsts; Fst ownership retained by caller. ReplaceUtil(const vector &fst_pairs, Label root_label, bool epsilon_on_replace = false); // Constructs from ReplaceFst internals; ownership retained by caller. ReplaceUtil(const vector *> &fst_array, const NonTerminalHash &nonterminal_hash, Label root_fst, bool epsilon_on_replace = false); ~ReplaceUtil() { for (Label i = 0; i < fst_array_.size(); ++i) delete fst_array_[i]; } // True if the non-terminal dependencies are cyclic. Cyclic // dependencies will result in an unexpandable replace fst. bool CyclicDependencies() const { GetDependencies(false); return depprops_ & kCyclic; } // Returns true if no useless Fsts, states or transitions. bool Connected() const { GetDependencies(false); uint64 props = kAccessible | kCoAccessible; for (Label i = 0; i < fst_array_.size(); ++i) { if (!fst_array_[i]) continue; if (fst_array_[i]->Properties(props, true) != props || !depaccess_[i]) return false; } return true; } // Removes useless Fsts, states and transitions. void Connect(); // Replaces Fsts specified by labels. // Does nothing if there are cyclic dependencies. void ReplaceLabels(const vector