FST  openfst-1.7.2
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 
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 
17 DECLARE_double(delta);
18 DECLARE_int32(nshortest);
19 DECLARE_int64(nstate);
20 DECLARE_string(queue_type);
21 DECLARE_bool(unique);
22 DECLARE_string(weight);
23 
24 int fstshortestpath_main(int argc, char **argv) {
25  namespace s = fst::script;
29 
30  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 string in_name =
42  (argc > 1 && (strcmp(argv[1], "-") != 0)) ? argv[1] : "";
43  const string out_name = argc > 2 ? 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:124
DECLARE_int64(nstate)
QueueType
Definition: queue.h:56
DECLARE_bool(unique)
#define LOG(type)
Definition: log.h:48
void FailedNewHandler()
Definition: compat.cc:25
#define SET_FLAGS(usage, argc, argv, rmflags)
Definition: flags.h:214
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)
int fstshortestpath_main(int argc, char **argv)
bool GetQueueType(const string &str, QueueType *queue_type)
Definition: getters.cc:101
DECLARE_int32(nshortest)