// See www.openfst.org for extensive documentation on this weighted // finite-state transducer library. #ifndef FST_SCRIPT_SHORTEST_PATH_H_ #define FST_SCRIPT_SHORTEST_PATH_H_ #include #include #include #include #include #include namespace fst { namespace script { // Slightly simplified interface: `has_distance` and `first_path` are disabled. struct ShortestPathOptions : public ShortestDistanceOptions { const int32 nshortest; const bool unique; const WeightClass &weight_threshold; const int64 state_threshold; ShortestPathOptions(QueueType queue_type, int32 nshortest, bool unique, float delta, const WeightClass &weight_threshold, int64 state_threshold = kNoStateId) : ShortestDistanceOptions(queue_type, ANY_ARC_FILTER, kNoStateId, delta), nshortest(nshortest), unique(unique), weight_threshold(weight_threshold), state_threshold(state_threshold) {} }; namespace internal { // Code to implement switching on queue types. template void ShortestPath(const Fst &ifst, MutableFst *ofst, std::vector *distance, const ShortestPathOptions &opts) { using ArcFilter = AnyArcFilter; using Weight = typename Arc::Weight; const std::unique_ptr queue( QueueConstructor::Construct(ifst, distance)); const fst::ShortestPathOptions sopts( queue.get(), ArcFilter(), opts.nshortest, opts.unique, /* has_distance=*/false, opts.delta, /* first_path=*/false, *opts.weight_threshold.GetWeight(), opts.state_threshold); ShortestPath(ifst, ofst, distance, sopts); } template void ShortestPath(const Fst &ifst, MutableFst *ofst, const ShortestPathOptions &opts) { using StateId = typename Arc::StateId; using Weight = typename Arc::Weight; std::vector distance; switch (opts.queue_type) { case AUTO_QUEUE: { ShortestPath>(ifst, ofst, &distance, opts); return; } case FIFO_QUEUE: { ShortestPath>(ifst, ofst, &distance, opts); return; } case LIFO_QUEUE: { ShortestPath>(ifst, ofst, &distance, opts); return; } case SHORTEST_FIRST_QUEUE: { ShortestPath>(ifst, ofst, &distance, opts); return; } case STATE_ORDER_QUEUE: { ShortestPath>(ifst, ofst, &distance, opts); return; } case TOP_ORDER_QUEUE: { ShortestPath>(ifst, ofst, &distance, opts); return; } default: { FSTERROR() << "ShortestPath: Unknown queue type: " << opts.queue_type; ofst->SetProperties(kError, kError); return; } } } } // namespace internal using ShortestPathArgs = std::tuple; template void ShortestPath(ShortestPathArgs *args) { const Fst &ifst = *(std::get<0>(*args).GetFst()); MutableFst *ofst = std::get<1>(*args)->GetMutableFst(); const ShortestPathOptions &opts = std::get<2>(*args); internal::ShortestPath(ifst, ofst, opts); } void ShortestPath(const FstClass &ifst, MutableFstClass *ofst, const ShortestPathOptions &opts); } // namespace script } // namespace fst #endif // FST_SCRIPT_SHORTEST_PATH_H_