// Copyright 2005-2024 Google LLC // // 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. // // See www.openfst.org for extensive documentation on this weighted // finite-state transducer library. // // Convenience file for including all MPDT operations at once, and/or // registering them for new arc types. #ifndef FST_EXTENSIONS_MPDT_MPDTSCRIPT_H_ #define FST_EXTENSIONS_MPDT_MPDTSCRIPT_H_ #include #include #include #include #include #include #include #include #include #include #include // For LabelClassPair, #include // for ComposeOptions #include #include #include #include #include #include // FstClassPair, and to detect // any collisions. namespace fst { namespace script { using MPdtComposeArgs = std::tuple> &, const std::vector &, MutableFstClass *, const MPdtComposeOptions &, bool>; template void Compose(MPdtComposeArgs *args) { const Fst &ifst1 = *(std::get<0>(*args).GetFst()); const Fst &ifst2 = *(std::get<1>(*args).GetFst()); MutableFst *ofst = std::get<4>(*args)->GetMutableFst(); std::vector> typed_parens( std::get<2>(*args).size()); std::copy(std::get<2>(*args).begin(), std::get<2>(*args).end(), typed_parens.begin()); using Level = typename Arc::Label; std::vector typed_assignments(std::get<3>(*args).size()); std::copy(std::get<3>(*args).begin(), std::get<3>(*args).end(), typed_assignments.begin()); if (std::get<6>(*args)) { Compose(ifst1, typed_parens, typed_assignments, ifst2, ofst, std::get<5>(*args)); } else { Compose(ifst1, ifst2, typed_parens, typed_assignments, ofst, std::get<5>(*args)); } } void Compose(const FstClass &ifst1, const FstClass &ifst2, const std::vector> &parens, const std::vector &assignments, MutableFstClass *ofst, const MPdtComposeOptions &copts, bool left_pdt); using MPdtExpandArgs = std::tuple< const FstClass &, const std::vector> &, const std::vector &, MutableFstClass *, const MPdtExpandOptions &>; template void Expand(MPdtExpandArgs *args) { const Fst &fst = *(std::get<0>(*args).GetFst()); MutableFst *ofst = std::get<3>(*args)->GetMutableFst(); // In case Arc::Label is not the same as FstClass::Label, we make copies. // Truncation may occur if FstClass::Label has more precision than // Arc::Label. std::vector> typed_parens( std::get<1>(*args).size()); std::copy(std::get<1>(*args).begin(), std::get<1>(*args).end(), typed_parens.begin()); using Level = typename Arc::Label; std::vector typed_assignments(std::get<2>(*args).size()); std::copy(std::get<2>(*args).begin(), std::get<2>(*args).end(), typed_assignments.begin()); Expand(fst, typed_parens, typed_assignments, ofst, MPdtExpandOptions(std::get<4>(*args).connect, std::get<4>(*args).keep_parentheses)); } void Expand(const FstClass &ifst, const std::vector> &parens, const std::vector &assignments, MutableFstClass *ofst, const MPdtExpandOptions &opts); using MPdtReverseArgs = std::tuple> &, std::vector *, MutableFstClass *>; template void Reverse(MPdtReverseArgs *args) { const Fst &fst = *(std::get<0>(*args).GetFst()); MutableFst *ofst = std::get<3>(*args)->GetMutableFst(); // In case Arc::Label is not the same as FstClass::Label, we make copies. // Truncation may occur if FstClass::Label has more precision than // Arc::Label. std::vector> typed_parens( std::get<1>(*args).size()); std::copy(std::get<1>(*args).begin(), std::get<1>(*args).end(), typed_parens.begin()); using Level = typename Arc::Label; std::vector typed_assignments(std::get<2>(*args)->size()); std::copy(std::get<2>(*args)->begin(), std::get<2>(*args)->end(), typed_assignments.begin()); Reverse(fst, typed_parens, &typed_assignments, ofst); // Reassign stack assignments to input assignment vector. std::copy(typed_assignments.begin(), typed_assignments.end(), std::get<2>(*args)->begin()); } void Reverse(const FstClass &ifst, const std::vector> &parens, std::vector *assignments, MutableFstClass *ofst); using MPdtInfoArgs = std::tuple> &, const std::vector &>; template void Info(MPdtInfoArgs *args) { const Fst &fst = *(std::get<0>(*args).GetFst()); // In case Arc::Label is not the same as FstClass::Label, we make copies. // Truncation may occur if FstClass::Label has more precision than // Arc::Label. std::vector> typed_parens( std::get<1>(*args).size()); std::copy(std::get<1>(*args).begin(), std::get<1>(*args).end(), typed_parens.begin()); using Level = typename Arc::Label; std::vector typed_assignments(std::get<2>(*args).size()); std::copy(std::get<2>(*args).begin(), std::get<2>(*args).end(), typed_assignments.begin()); MPdtInfo mpdtinfo(fst, typed_parens, typed_assignments); mpdtinfo.Print(); } void Info(const FstClass &ifst, const std::vector> &parens, const std::vector &assignments); } // namespace script } // namespace fst #endif // FST_EXTENSIONS_MPDT_MPDTSCRIPT_H_