// replace.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 // Recursively replace Fst arcs with other Fst(s) returning a PDT. #ifndef FST_EXTENSIONS_PDT_REPLACE_H__ #define FST_EXTENSIONS_PDT_REPLACE_H__ #include namespace fst { // Hash to paren IDs template struct ReplaceParenHash { size_t operator()(const pair &p) const { return p.first + p.second * kPrime; } private: static const size_t kPrime = 7853; }; template const size_t ReplaceParenHash::kPrime; // Builds a pushdown transducer (PDT) from an RTN specification // identical to that in fst/lib/replace.h. The result is a PDT // encoded as the FST 'ofst' where some transitions are labeled with // open or close parentheses. To be interpreted as a PDT, the parens // must balance on a path (see PdtExpand()). The open/close // parenthesis label pairs are returned in 'parens'. template void Replace(const vector* > >& ifst_array, MutableFst *ofst, vector > *parens, typename Arc::Label root) { typedef typename Arc::Label Label; typedef typename Arc::StateId StateId; typedef typename Arc::Weight Weight; ofst->DeleteStates(); parens->clear(); unordered_map label2id; for (size_t i = 0; i < ifst_array.size(); ++i) label2id[ifst_array[i].first] = i; Label max_label = kNoLabel; deque non_term_queue; // Queue of non-terminals to replace unordered_set