// 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. #ifndef FST_SCRIPT_MAP_H_ #define FST_SCRIPT_MAP_H_ #include #include #include #include #include #include #include #include #include #include #include #include namespace fst { namespace script { template std::unique_ptr> ArcMap( const Fst &fst, const M &mapper) { using ToArc = typename M::ToArc; auto ofst = std::make_unique>(); ArcMap(fst, ofst.get(), mapper); return ofst; } template std::unique_ptr> StateMap( const Fst &fst, const M &mapper) { using ToArc = typename M::ToArc; auto ofst = std::make_unique>(); StateMap(fst, ofst.get(), mapper); return ofst; } enum class MapType : uint8_t { ARC_SUM, ARC_UNIQUE, IDENTITY, INPUT_EPSILON, INVERT, OUTPUT_EPSILON, PLUS, POWER, QUANTIZE, RMWEIGHT, SUPERFINAL, TIMES, TO_LOG, TO_LOG64, TO_STD }; using FstMapInnerArgs = std::tuple; using FstMapArgs = WithReturnValue, FstMapInnerArgs>; template void Map(FstMapArgs *args) { using Weight = typename Arc::Weight; const Fst &ifst = *std::get<0>(args->args).GetFst(); const auto map_type = std::get<1>(args->args); switch (map_type) { case MapType::ARC_SUM: { auto ofst = StateMap(ifst, ArcSumMapper(ifst)); args->retval = std::make_unique(std::move(ofst)); return; } case MapType::ARC_UNIQUE: { auto ofst = StateMap(ifst, ArcUniqueMapper(ifst)); args->retval = std::make_unique(std::move(ofst)); return; } case MapType::IDENTITY: { auto ofst = ArcMap(ifst, IdentityArcMapper()); args->retval = std::make_unique(std::move(ofst)); return; } case MapType::INPUT_EPSILON: { auto ofst = ArcMap(ifst, InputEpsilonMapper()); args->retval = std::make_unique(std::move(ofst)); return; } case MapType::INVERT: { auto ofst = ArcMap(ifst, InvertWeightMapper()); args->retval = std::make_unique(std::move(ofst)); return; } case MapType::OUTPUT_EPSILON: { auto ofst = ArcMap(ifst, OutputEpsilonMapper()); args->retval = std::make_unique(std::move(ofst)); return; } case MapType::PLUS: { const auto weight = *std::get<4>(args->args).GetWeight(); auto ofst = ArcMap(ifst, PlusMapper(weight)); args->retval = std::make_unique(std::move(ofst)); return; } case MapType::POWER: { const auto power = std::get<3>(args->args); auto ofst = ArcMap(ifst, PowerMapper(power)); args->retval = std::make_unique(std::move(ofst)); return; } case MapType::QUANTIZE: { const auto delta = std::get<2>(args->args); auto ofst = ArcMap(ifst, QuantizeMapper(delta)); args->retval = std::make_unique(std::move(ofst)); return; } case MapType::RMWEIGHT: { auto ofst = ArcMap(ifst, RmWeightMapper()); args->retval = std::make_unique(std::move(ofst)); return; } case MapType::SUPERFINAL: { auto ofst = ArcMap(ifst, SuperFinalMapper()); args->retval = std::make_unique(std::move(ofst)); return; } case MapType::TIMES: { const auto weight = *std::get<4>(args->args).GetWeight(); auto ofst = ArcMap(ifst, TimesMapper(weight)); args->retval = std::make_unique(std::move(ofst)); return; } case MapType::TO_LOG: { auto ofst = ArcMap(ifst, WeightConvertMapper()); args->retval = std::make_unique(std::move(ofst)); return; } case MapType::TO_LOG64: { auto ofst = ArcMap(ifst, WeightConvertMapper()); args->retval = std::make_unique(std::move(ofst)); return; } case MapType::TO_STD: { auto ofst = ArcMap(ifst, WeightConvertMapper()); args->retval = std::make_unique(std::move(ofst)); return; } } } std::unique_ptr Map(const FstClass &ifst, MapType map_type, float delta, double power, const WeightClass &weight); } // namespace script } // namespace fst #endif // FST_SCRIPT_MAP_H_