FST  openfst-1.7.9
OpenFst Library
fstshortestpath-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 path(s) in an FST.
5 
6 #include <cstring>
7 #include <memory>
8 #include <string>
9 #include <vector>
10 
11 #include <fst/flags.h>
12 #include <fst/log.h>
13 #include <fst/script/getters.h>
15 
16 DECLARE_double(delta);
17 DECLARE_int32(nshortest);
18 DECLARE_int64(nstate);
19 DECLARE_string(queue_type);
20 DECLARE_bool(unique);
21 DECLARE_string(weight);
22 
23 int fstshortestpath_main(int argc, char **argv) {
24  namespace s = fst::script;
25  using fst::QueueType;
29 
30  std::string usage = "Finds shortest path(s) in an FST.\n\n Usage: ";
31  usage += argv[0];
32  usage += " [in.fst [out.fst]]\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  const std::string in_name =
42  (argc > 1 && strcmp(argv[1], "-") != 0) ? argv[1] : "";
43  const std::string out_name =
44  (argc > 2 && strcmp(argv[2], "-") != 0) ? argv[2] : "";
45 
46  std::unique_ptr<FstClass> ifst(FstClass::Read(in_name));
47  if (!ifst) return 1;
48 
49  const auto weight_threshold =
50  FLAGS_weight.empty() ? WeightClass::Zero(ifst->WeightType())
51  : WeightClass(ifst->WeightType(), FLAGS_weight);
52 
53  VectorFstClass ofst(ifst->ArcType());
54 
55  QueueType queue_type;
56  if (!s::GetQueueType(FLAGS_queue_type, &queue_type)) {
57  LOG(ERROR) << "Unknown or unsupported queue type: " << FLAGS_queue_type;
58  return 1;
59  }
60 
61  const s::ShortestPathOptions opts(queue_type, FLAGS_nshortest, FLAGS_unique,
62  FLAGS_delta, weight_threshold,
63  FLAGS_nstate);
64 
65  s::ShortestPath(*ifst, &ofst, opts);
66 
67  return !ofst.Write(out_name);
68 }
void ShowUsage(bool long_usage=true)
Definition: flags.cc:123
DECLARE_int64(nstate)
QueueType
Definition: queue.h:57
DECLARE_bool(unique)
#define LOG(type)
Definition: log.h:46
void FailedNewHandler()
Definition: compat.cc:22
#define SET_FLAGS(usage, argc, argv, rmflags)
Definition: flags.h:216
DECLARE_string(queue_type)
DECLARE_double(delta)
void ShortestPath(const Fst< Arc > &ifst, const std::vector< std::pair< typename Arc::Label, typename Arc::Label >> &parens, MutableFst< Arc > *ofst, const PdtShortestPathOptions< Arc, Queue > &opts)
bool GetQueueType(const std::string &str, QueueType *queue_type)
Definition: getters.cc:115
int fstshortestpath_main(int argc, char **argv)
DECLARE_int32(nshortest)