FST  openfst-1.7.3
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;
28 
29  std::string usage = "Finds shortest path(s) in an FST.\n\n Usage: ";
30  usage += argv[0];
31  usage += " [in.fst [out.fst]]\n";
32 
33  std::set_new_handler(FailedNewHandler);
34  SET_FLAGS(usage.c_str(), &argc, &argv, true);
35  if (argc > 3) {
36  ShowUsage();
37  return 1;
38  }
39 
40  const std::string in_name =
41  (argc > 1 && strcmp(argv[1], "-") != 0) ? argv[1] : "";
42  const std::string out_name =
43  (argc > 2 && strcmp(argv[2], "-") != 0) ? argv[2] : "";
44 
45  std::unique_ptr<FstClass> ifst(FstClass::Read(in_name));
46  if (!ifst) return 1;
47 
48  const auto weight_threshold =
49  FLAGS_weight.empty() ? WeightClass::Zero(ifst->WeightType())
50  : WeightClass(ifst->WeightType(), FLAGS_weight);
51 
52  VectorFstClass ofst(ifst->ArcType());
53 
54  fst::QueueType queue_type;
55  if (!s::GetQueueType(FLAGS_queue_type, &queue_type)) {
56  LOG(ERROR) << "Unknown or unsupported queue type: " << FLAGS_queue_type;
57  return 1;
58  }
59 
60  const s::ShortestPathOptions opts(queue_type, FLAGS_nshortest,
61  FLAGS_unique, FLAGS_delta,
62  weight_threshold, FLAGS_nstate);
63 
64  s::ShortestPath(*ifst, &ofst, opts);
65 
66  return !ofst.Write(out_name);
67 }
void ShowUsage(bool long_usage=true)
Definition: flags.cc:121
DECLARE_int64(nstate)
QueueType
Definition: queue.h:56
DECLARE_bool(unique)
#define LOG(type)
Definition: log.h:46
void FailedNewHandler()
Definition: compat.cc:20
#define SET_FLAGS(usage, argc, argv, rmflags)
Definition: flags.h:209
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:101
int fstshortestpath_main(int argc, char **argv)
DECLARE_int32(nshortest)