// 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: jpr@google.com (Jake Ratkiewicz) // Convenience file for including all PDT operations at once, and/or // registering them for new arc types. #ifndef FST_EXTENSIONS_PDT_PDTSCRIPT_H_ #define FST_EXTENSIONS_PDT_PDTSCRIPT_H_ #include using std::pair; using std::make_pair; #include using std::vector; #include // for ComposeOptions #include #include #include #include #include #include #include #include #include #include namespace fst { namespace script { // PDT COMPOSE typedef args::Package >&, MutableFstClass *, const ComposeOptions &, bool> PdtComposeArgs; template void PdtCompose(PdtComposeArgs *args) { const Fst &ifst1 = *(args->arg1.GetFst()); const Fst &ifst2 = *(args->arg2.GetFst()); MutableFst *ofst = args->arg4->GetMutableFst(); vector > parens( args->arg3.size()); for (size_t i = 0; i < parens.size(); ++i) { parens[i].first = args->arg3[i].first; parens[i].second = args->arg3[i].second; } if (args->arg6) { Compose(ifst1, parens, ifst2, ofst, args->arg5); } else { Compose(ifst1, ifst2, parens, ofst, args->arg5); } } void PdtCompose(const FstClass & ifst1, const FstClass & ifst2, const vector > &parens, MutableFstClass *ofst, const ComposeOptions &copts, bool left_pdt); // PDT EXPAND struct PdtExpandOptions { bool connect; bool keep_parentheses; WeightClass weight_threshold; PdtExpandOptions(bool c = true, bool k = false, WeightClass w = WeightClass::Zero()) : connect(c), keep_parentheses(k), weight_threshold(w) {} }; typedef args::Package >&, MutableFstClass *, PdtExpandOptions> PdtExpandArgs; template void PdtExpand(PdtExpandArgs *args) { const Fst &fst = *(args->arg1.GetFst()); MutableFst *ofst = args->arg3->GetMutableFst(); vector > parens( args->arg2.size()); for (size_t i = 0; i < parens.size(); ++i) { parens[i].first = args->arg2[i].first; parens[i].second = args->arg2[i].second; } Expand(fst, parens, ofst, ExpandOptions( args->arg4.connect, args->arg4.keep_parentheses, *(args->arg4.weight_threshold.GetWeight()))); } void PdtExpand(const FstClass &ifst, const vector > &parens, MutableFstClass *ofst, const PdtExpandOptions &opts); void PdtExpand(const FstClass &ifst, const vector > &parens, MutableFstClass *ofst, bool connect); // PDT REPLACE typedef args::Package > &, MutableFstClass *, vector > *, const int64 &> PdtReplaceArgs; template void PdtReplace(PdtReplaceArgs *args) { vector *> > tuples( args->arg1.size()); for (size_t i = 0; i < tuples.size(); ++i) { tuples[i].first = args->arg1[i].first; tuples[i].second = (args->arg1[i].second)->GetFst(); } MutableFst *ofst = args->arg2->GetMutableFst(); vector > parens( args->arg3->size()); for (size_t i = 0; i < parens.size(); ++i) { parens[i].first = args->arg3->at(i).first; parens[i].second = args->arg3->at(i).second; } Replace(tuples, ofst, &parens, args->arg4); // now copy parens back args->arg3->resize(parens.size()); for (size_t i = 0; i < parens.size(); ++i) { (*args->arg3)[i].first = parens[i].first; (*args->arg3)[i].second = parens[i].second; } } void PdtReplace(const vector > &fst_tuples, MutableFstClass *ofst, vector > *parens, const int64 &root); // PDT REVERSE typedef args::Package >&, MutableFstClass *> PdtReverseArgs; template void PdtReverse(PdtReverseArgs *args) { const Fst &fst = *(args->arg1.GetFst()); MutableFst *ofst = args->arg3->GetMutableFst(); vector > parens( args->arg2.size()); for (size_t i = 0; i < parens.size(); ++i) { parens[i].first = args->arg2[i].first; parens[i].second = args->arg2[i].second; } Reverse(fst, parens, ofst); } void PdtReverse(const FstClass &ifst, const vector > &parens, MutableFstClass *ofst); // PDT SHORTESTPATH struct PdtShortestPathOptions { QueueType queue_type; bool keep_parentheses; bool path_gc; PdtShortestPathOptions(QueueType qt = FIFO_QUEUE, bool kp = false, bool gc = true) : queue_type(qt), keep_parentheses(kp), path_gc(gc) {} }; typedef args::Package >&, MutableFstClass *, const PdtShortestPathOptions &> PdtShortestPathArgs; template void PdtShortestPath(PdtShortestPathArgs *args) { typedef typename Arc::StateId StateId; typedef typename Arc::Label Label; typedef typename Arc::Weight Weight; const Fst &fst = *(args->arg1.GetFst()); MutableFst *ofst = args->arg3->GetMutableFst(); const PdtShortestPathOptions &opts = args->arg4; vector > parens(args->arg2.size()); for (size_t i = 0; i < parens.size(); ++i) { parens[i].first = args->arg2[i].first; parens[i].second = args->arg2[i].second; } switch (opts.queue_type) { default: FSTERROR() << "Unknown queue type: " << opts.queue_type; case FIFO_QUEUE: { typedef FifoQueue Queue; fst::PdtShortestPathOptions spopts(opts.keep_parentheses, opts.path_gc); ShortestPath(fst, parens, ofst, spopts); return; } case LIFO_QUEUE: { typedef LifoQueue Queue; fst::PdtShortestPathOptions spopts(opts.keep_parentheses, opts.path_gc); ShortestPath(fst, parens, ofst, spopts); return; } case STATE_ORDER_QUEUE: { typedef StateOrderQueue Queue; fst::PdtShortestPathOptions spopts(opts.keep_parentheses, opts.path_gc); ShortestPath(fst, parens, ofst, spopts); return; } } } void PdtShortestPath(const FstClass &ifst, const vector > &parens, MutableFstClass *ofst, const PdtShortestPathOptions &opts = PdtShortestPathOptions()); // PRINT INFO typedef args::Package > &> PrintPdtInfoArgs; template void PrintPdtInfo(PrintPdtInfoArgs *args) { const Fst &fst = *(args->arg1.GetFst()); vector > parens( args->arg2.size()); for (size_t i = 0; i < parens.size(); ++i) { parens[i].first = args->arg2[i].first; parens[i].second = args->arg2[i].second; } PdtInfo pdtinfo(fst, parens); PrintPdtInfo(pdtinfo); } void PrintPdtInfo(const FstClass &ifst, const vector > &parens); } // namespace script } // namespace fst #define REGISTER_FST_PDT_OPERATIONS(ArcType) \ REGISTER_FST_OPERATION(PdtCompose, ArcType, PdtComposeArgs); \ REGISTER_FST_OPERATION(PdtExpand, ArcType, PdtExpandArgs); \ REGISTER_FST_OPERATION(PdtReplace, ArcType, PdtReplaceArgs); \ REGISTER_FST_OPERATION(PdtReverse, ArcType, PdtReverseArgs); \ REGISTER_FST_OPERATION(PdtShortestPath, ArcType, PdtShortestPathArgs); \ REGISTER_FST_OPERATION(PrintPdtInfo, ArcType, PrintPdtInfoArgs) #endif // FST_EXTENSIONS_PDT_PDTSCRIPT_H_