// 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 namespace fst { namespace script { template Fst *ArcMap(const Fst &fst, const M &mapper) { using ToArc = typename M::ToArc; auto *ofst = new VectorFst; ArcMap(fst, ofst, mapper); return ofst; } template Fst *StateMap(const Fst &fst, const M &mapper) { using ToArc = typename M::ToArc; auto *ofst = new VectorFst; StateMap(fst, ofst, mapper); return ofst; } enum MapType { ARC_SUM_MAPPER, ARC_UNIQUE_MAPPER, IDENTITY_MAPPER, INPUT_EPSILON_MAPPER, INVERT_MAPPER, OUTPUT_EPSILON_MAPPER, PLUS_MAPPER, POWER_MAPPER, QUANTIZE_MAPPER, RMWEIGHT_MAPPER, SUPERFINAL_MAPPER, TIMES_MAPPER, TO_LOG_MAPPER, TO_LOG64_MAPPER, TO_STD_MAPPER }; using MapInnerArgs = std::tuple; using MapArgs = WithReturnValue; template void Map(MapArgs *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 ARC_SUM_MAPPER: { std::unique_ptr> ofst(StateMap(ifst, ArcSumMapper(ifst))); args->retval = new FstClass(*ofst); return; } case ARC_UNIQUE_MAPPER: { std::unique_ptr> ofst( StateMap(ifst, ArcUniqueMapper(ifst))); args->retval = new FstClass(*ofst); return; } case IDENTITY_MAPPER: { std::unique_ptr> ofst(ArcMap(ifst, IdentityArcMapper())); args->retval = new FstClass(*ofst); return; } case INPUT_EPSILON_MAPPER: { std::unique_ptr> ofst(ArcMap(ifst, InputEpsilonMapper())); args->retval = new FstClass(*ofst); return; } case INVERT_MAPPER: { std::unique_ptr> ofst(ArcMap(ifst, InvertWeightMapper())); args->retval = new FstClass(*ofst); return; } case OUTPUT_EPSILON_MAPPER: { std::unique_ptr> ofst(ArcMap(ifst, OutputEpsilonMapper())); args->retval = new FstClass(*ofst); return; } case PLUS_MAPPER: { const auto weight = *(std::get<4>(args->args).GetWeight()); std::unique_ptr> ofst(ArcMap(ifst, PlusMapper(weight))); args->retval = new FstClass(*ofst); return; } case POWER_MAPPER: { const auto power = std::get<3>(args->args); std::unique_ptr> ofst(ArcMap(ifst, PowerMapper(power))); args->retval = new FstClass(*ofst); return; } case QUANTIZE_MAPPER: { const auto delta = std::get<2>(args->args); std::unique_ptr> ofst(ArcMap(ifst, QuantizeMapper(delta))); args->retval = new FstClass(*ofst); return; } case RMWEIGHT_MAPPER: { std::unique_ptr> ofst(ArcMap(ifst, RmWeightMapper())); args->retval = new FstClass(*ofst); return; } case SUPERFINAL_MAPPER: { std::unique_ptr> ofst(ArcMap(ifst, SuperFinalMapper())); args->retval = new FstClass(*ofst); return; } case TIMES_MAPPER: { const auto weight = *(std::get<4>(args->args).GetWeight()); std::unique_ptr> ofst(ArcMap(ifst, TimesMapper(weight))); args->retval = new FstClass(*ofst); return; } case TO_LOG_MAPPER: { std::unique_ptr> ofst( ArcMap(ifst, WeightConvertMapper())); args->retval = new FstClass(*ofst); return; } case TO_LOG64_MAPPER: { std::unique_ptr> ofst( ArcMap(ifst, WeightConvertMapper())); args->retval = new FstClass(*ofst); return; } case TO_STD_MAPPER: { std::unique_ptr> ofst( ArcMap(ifst, WeightConvertMapper())); args->retval = new FstClass(*ofst); return; } } } FstClass *Map(const FstClass &ifst, MapType map_type, float delta, double power, const WeightClass &weight); } // namespace script } // namespace fst #endif // FST_SCRIPT_MAP_H_