// 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) #ifndef FST_SCRIPT_SHORTEST_DISTANCE_H_ #define FST_SCRIPT_SHORTEST_DISTANCE_H_ #include using std::vector; #include #include #include #include // for ArcFilterType #include // for QueueType #include namespace fst { namespace script { enum ArcFilterType { ANY_ARC_FILTER, EPSILON_ARC_FILTER, INPUT_EPSILON_ARC_FILTER, OUTPUT_EPSILON_ARC_FILTER }; // See nlp/fst/lib/shortest-distance.h for the template options class // that this one shadows struct ShortestDistanceOptions { const QueueType queue_type; const ArcFilterType arc_filter_type; const int64 source; const float delta; const bool first_path; ShortestDistanceOptions(QueueType qt, ArcFilterType aft, int64 s, float d) : queue_type(qt), arc_filter_type(aft), source(s), delta(d), first_path(false) { } }; // 1 typedef args::Package *, const ShortestDistanceOptions &> ShortestDistanceArgs1; template struct QueueConstructor { // template static Queue *Construct(const Fst &, const vector *) { return new Queue(); } }; // Specializations to deal with AutoQueue, NaturalShortestFirstQueue, // and TopOrderQueue's different constructors template struct QueueConstructor, Arc, ArcFilter> { // template static AutoQueue *Construct( const Fst &fst, const vector *distance) { return new AutoQueue(fst, distance, ArcFilter()); } }; template struct QueueConstructor, Arc, ArcFilter> { // template static NaturalShortestFirstQueue *Construct(const Fst &fst, const vector *distance) { return new NaturalShortestFirstQueue(*distance); } }; template struct QueueConstructor, Arc, ArcFilter> { // template static TopOrderQueue *Construct( const Fst &fst, const vector *weights) { return new TopOrderQueue(fst, ArcFilter()); } }; template void ShortestDistanceHelper(ShortestDistanceArgs1 *args) { const Fst &fst = *(args->arg1.GetFst()); const ShortestDistanceOptions &opts = args->arg3; vector weights; switch (opts.arc_filter_type) { case ANY_ARC_FILTER: { Queue *queue = QueueConstructor >::Construct( fst, &weights); fst::ShortestDistanceOptions > sdopts( queue, AnyArcFilter(), opts.source, opts.delta); ShortestDistance(fst, &weights, sdopts); delete queue; break; } case EPSILON_ARC_FILTER: { Queue *queue = QueueConstructor >::Construct( fst, &weights); fst::ShortestDistanceOptions > sdopts( queue, EpsilonArcFilter(), opts.source, opts.delta); ShortestDistance(fst, &weights, sdopts); delete queue; break; } case INPUT_EPSILON_ARC_FILTER: { Queue *queue = QueueConstructor >::Construct( fst, &weights); fst::ShortestDistanceOptions > sdopts( queue, InputEpsilonArcFilter(), opts.source, opts.delta); ShortestDistance(fst, &weights, sdopts); delete queue; break; } case OUTPUT_EPSILON_ARC_FILTER: { Queue *queue = QueueConstructor >::Construct( fst, &weights); fst::ShortestDistanceOptions > sdopts( queue, OutputEpsilonArcFilter(), opts.source, opts.delta); ShortestDistance(fst, &weights, sdopts); delete queue; break; } } // Copy the weights back args->arg2->resize(weights.size()); for (unsigned i = 0; i < weights.size(); ++i) { (*args->arg2)[i] = WeightClass(weights[i]); } } template void ShortestDistance(ShortestDistanceArgs1 *args) { const ShortestDistanceOptions &opts = args->arg3; typedef typename Arc::StateId StateId; typedef typename Arc::Weight Weight; // Must consider (opts.queue_type x opts.filter_type) options switch (opts.queue_type) { default: FSTERROR() << "Unknown queue type." << opts.queue_type; case AUTO_QUEUE: ShortestDistanceHelper >(args); return; case FIFO_QUEUE: ShortestDistanceHelper >(args); return; case LIFO_QUEUE: ShortestDistanceHelper >(args); return; case SHORTEST_FIRST_QUEUE: ShortestDistanceHelper >(args); return; case STATE_ORDER_QUEUE: ShortestDistanceHelper >(args); return; case TOP_ORDER_QUEUE: ShortestDistanceHelper >(args); return; } } // 2 typedef args::Package*, bool, double> ShortestDistanceArgs2; template void ShortestDistance(ShortestDistanceArgs2 *args) { const Fst &fst = *(args->arg1.GetFst()); vector distance; ShortestDistance(fst, &distance, args->arg3, args->arg4); // convert the typed weights back into weightclass vector *retval = args->arg2; retval->resize(distance.size()); for (unsigned i = 0; i < distance.size(); ++i) { (*retval)[i] = WeightClass(distance[i]); } } // 3 typedef args::WithReturnValue ShortestDistanceArgs3; template void ShortestDistance(ShortestDistanceArgs3 *args) { const Fst &fst = *(args->args.GetFst()); args->retval = WeightClass(ShortestDistance(fst)); } // 1 void ShortestDistance(const FstClass &fst, vector *distance, const ShortestDistanceOptions &opts); // 2 void ShortestDistance(const FstClass &ifst, vector *distance, bool reverse = false, double delta = fst::kDelta); #ifndef SWIG // 3 WeightClass ShortestDistance(const FstClass &ifst); #endif } // namespace script } // namespace fst #endif // FST_SCRIPT_SHORTEST_DISTANCE_H_