fstshortestdistance.cc revision f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2
1// fstshortestdistance.cc 2 3// Licensed under the Apache License, Version 2.0 (the "License"); 4// you may not use this file except in compliance with the License. 5// You may obtain a copy of the License at 6// 7// http://www.apache.org/licenses/LICENSE-2.0 8// 9// Unless required by applicable law or agreed to in writing, software 10// distributed under the License is distributed on an "AS IS" BASIS, 11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12// See the License for the specific language governing permissions and 13// limitations under the License. 14// 15// Copyright 2005-2010 Google, Inc. 16// Author: allauzen@google.com (Cyril Allauzen) 17// Modified: jpr@google.com (Jake Ratkiewicz) to use FstClass 18// 19// \file 20// Find shortest distances in an FST. 21 22#include <string> 23#include <vector> 24using std::vector; 25 26#include <fst/script/shortest-distance.h> 27#include <fst/script/text-io.h> 28 29DEFINE_bool(reverse, false, "Perform in the reverse direction"); 30DEFINE_double(delta, fst::kDelta, "Comparison/quantization delta"); 31DEFINE_int64(nstate, fst::kNoStateId, "State number threhold"); 32DEFINE_string(queue_type, "auto", "Queue type: one of: \"auto\", " 33 "\"fifo\", \"lifo\", \"shortest\", \"state\", \"top\""); 34 35int main(int argc, char **argv) { 36 namespace s = fst::script; 37 using fst::script::FstClass; 38 39 string usage = "Finds shortest distance(s) in an FST.\n\n Usage: "; 40 usage += argv[0]; 41 usage += " [in.fst [distance.txt]]\n"; 42 43 std::set_new_handler(FailedNewHandler); 44 SetFlags(usage.c_str(), &argc, &argv, true); 45 if (argc > 3) { 46 ShowUsage(); 47 return 1; 48 } 49 50 string in_fname = (argc > 1 && (strcmp(argv[1], "-") != 0)) ? argv[1] : ""; 51 string out_fname = argc > 2 ? argv[2] : ""; 52 53 FstClass *ifst = FstClass::Read(in_fname); 54 if (!ifst) return 1; 55 56 vector<s::WeightClass> distance; 57 58 fst::QueueType qt; 59 60 if (FLAGS_queue_type == "auto") { 61 qt = fst::AUTO_QUEUE; 62 } else if (FLAGS_queue_type == "fifo") { 63 qt = fst::FIFO_QUEUE; 64 } else if (FLAGS_queue_type == "lifo") { 65 qt = fst::LIFO_QUEUE; 66 } else if (FLAGS_queue_type == "shortest") { 67 qt = fst::SHORTEST_FIRST_QUEUE; 68 } else if (FLAGS_queue_type == "state") { 69 qt = fst::STATE_ORDER_QUEUE; 70 } else if (FLAGS_queue_type == "top") { 71 qt = fst::TOP_ORDER_QUEUE; 72 } else { 73 LOG(ERROR) << "Unknown or unsupported queue type: " << FLAGS_queue_type; 74 return 1; 75 } 76 77 if (FLAGS_reverse && qt != fst::AUTO_QUEUE) { 78 LOG(ERROR) << "Specifying a non-default queue with reverse not supported."; 79 return 1; 80 } 81 82 if (FLAGS_reverse) { 83 s::ShortestDistance(*ifst, &distance, FLAGS_reverse, FLAGS_delta); 84 } else { 85 s::ShortestDistanceOptions opts(qt, s::ANY_ARC_FILTER, 86 FLAGS_nstate, FLAGS_delta); 87 s::ShortestDistance(*ifst, &distance, opts); 88 } 89 90 s::WritePotentials(out_fname, distance); 91 92 return 0; 93} 94