FST  openfst-1.7.2
OpenFst Library
fstshortestdistance-main.cc
Go to the documentation of this file.
1 // See www.openfst.org for extensive documentation on this weighted
2 // finite-state transducer library.
3 //
4 // Find shortest distances in an FST.
5 
6 #include <cstring>
7 
8 #include <memory>
9 #include <string>
10 #include <vector>
11 
12 #include <fst/flags.h>
13 #include <fst/log.h>
14 #include <fst/script/getters.h>
16 #include <fst/script/text-io.h>
17 
18 DECLARE_bool(reverse);
19 DECLARE_double(delta);
20 DECLARE_int64(nstate);
21 DECLARE_string(queue_type);
22 
23 int fstshortestdistance_main(int argc, char **argv) {
24  namespace s = fst::script;
27  using fst::QueueType;
28  using fst::AUTO_QUEUE;
29 
30  string usage = "Finds shortest distance(s) in an FST.\n\n Usage: ";
31  usage += argv[0];
32  usage += " [in.fst [distance.txt]]\n";
33 
34  std::set_new_handler(FailedNewHandler);
35  SET_FLAGS(usage.c_str(), &argc, &argv, true);
36  if (argc > 3) {
37  ShowUsage();
38  return 1;
39  }
40 
41  string in_name = (argc > 1 && (strcmp(argv[1], "-") != 0)) ? argv[1] : "";
42  string out_name = argc > 2 ? argv[2] : "";
43 
44  std::unique_ptr<FstClass> ifst(FstClass::Read(in_name));
45  if (!ifst) return 1;
46 
47  std::vector<WeightClass> distance;
48 
49  QueueType queue_type;
50  if (!s::GetQueueType(FLAGS_queue_type, &queue_type)) {
51  LOG(ERROR) << argv[0]
52  << ": Unknown or unsupported queue type: " << FLAGS_queue_type;
53  return 1;
54  }
55 
56  if (FLAGS_reverse && queue_type != AUTO_QUEUE) {
57  LOG(ERROR) << argv[0] << ": Can't use non-default queue with reverse";
58  return 1;
59  }
60 
61  if (FLAGS_reverse) {
62  s::ShortestDistance(*ifst, &distance, FLAGS_reverse, FLAGS_delta);
63  } else {
64  const s::ShortestDistanceOptions opts(queue_type, s::ANY_ARC_FILTER,
65  FLAGS_nstate, FLAGS_delta);
66  s::ShortestDistance(*ifst, &distance, opts);
67  }
68 
69  return !s::WritePotentials(out_name, distance);
70 }
void ShowUsage(bool long_usage=true)
Definition: flags.cc:124
QueueType
Definition: queue.h:56
DECLARE_double(delta)
DECLARE_string(queue_type)
#define LOG(type)
Definition: log.h:48
bool WritePotentials(const string &filename, const std::vector< WeightClass > &potentials)
Definition: text-io.cc:52
void FailedNewHandler()
Definition: compat.cc:25
#define SET_FLAGS(usage, argc, argv, rmflags)
Definition: flags.h:214
int fstshortestdistance_main(int argc, char **argv)
void ShortestDistance(const Fst< Arc > &fst, std::vector< typename Arc::Weight > *distance, const ShortestDistanceOptions &opts)
DECLARE_bool(reverse)
DECLARE_int64(nstate)
bool GetQueueType(const string &str, QueueType *queue_type)
Definition: getters.cc:101