FST  openfst-1.7.2
OpenFst Library
pdtshortestpath.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 // Returns the shortest path in a (bounded-stack) PDT.
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 
16 #include <fst/util.h>
17 
18 DEFINE_bool(keep_parentheses, false, "Keep PDT parentheses in result?");
19 DEFINE_string(queue_type, "fifo",
20  "Queue type: one of: "
21  "\"fifo\", \"lifo\", \"state\"");
22 DEFINE_bool(path_gc, true, "Garbage collect shortest path data?");
23 DEFINE_string(pdt_parentheses, "", "PDT parenthesis label pairs");
24 
25 int main(int argc, char **argv) {
26  namespace s = fst::script;
29  using fst::QueueType;
30  using fst::ReadLabelPairs;
31 
32  string usage = "Shortest path in a (bounded-stack) PDT.\n\n Usage: ";
33  usage += argv[0];
34  usage += " in.pdt [out.fst]\n";
35 
36  std::set_new_handler(FailedNewHandler);
37  SET_FLAGS(usage.c_str(), &argc, &argv, true);
38  if (argc > 3) {
39  ShowUsage();
40  return 1;
41  }
42 
43  const string in_name =
44  (argc > 1 && (strcmp(argv[1], "-") != 0)) ? argv[1] : "";
45  const string out_name = argc > 2 ? argv[2] : "";
46 
47  std::unique_ptr<FstClass> ifst(FstClass::Read(in_name));
48  if (!ifst) return 1;
49 
50  if (FLAGS_pdt_parentheses.empty()) {
51  LOG(ERROR) << argv[0] << ": No PDT parenthesis label pairs provided";
52  return 1;
53  }
54 
55  std::vector<s::LabelPair> parens;
56  if (!ReadLabelPairs(FLAGS_pdt_parentheses, &parens, false)) return 1;
57 
58  VectorFstClass ofst(ifst->ArcType());
59 
60  QueueType qt;
61  if (FLAGS_queue_type == "fifo") {
62  qt = fst::FIFO_QUEUE;
63  } else if (FLAGS_queue_type == "lifo") {
64  qt = fst::LIFO_QUEUE;
65  } else if (FLAGS_queue_type == "state") {
67  } else {
68  LOG(ERROR) << "Unknown queue type: " << FLAGS_queue_type;
69  return 1;
70  }
71 
72  const s::PdtShortestPathOptions opts(qt, FLAGS_keep_parentheses,
73  FLAGS_path_gc);
74 
75  s::PdtShortestPath(*ifst, parens, &ofst, opts);
76 
77  ofst.Write(out_name);
78 
79  return 0;
80 }
void ShowUsage(bool long_usage=true)
Definition: flags.cc:124
QueueType
Definition: queue.h:56
DEFINE_string(queue_type,"fifo","Queue type: one of: ""\"fifo\", \"lifo\", \"state\"")
bool ReadLabelPairs(const string &filename, std::vector< std::pair< Label, Label >> *pairs, bool allow_negative=false)
Definition: util.h:331
#define LOG(type)
Definition: log.h:48
int main(int argc, char **argv)
void FailedNewHandler()
Definition: compat.cc:25
#define SET_FLAGS(usage, argc, argv, rmflags)
Definition: flags.h:214
DEFINE_bool(keep_parentheses, false,"Keep PDT parentheses in result?")
void PdtShortestPath(const FstClass &ifst, const std::vector< LabelPair > &parens, MutableFstClass *ofst, const PdtShortestPathOptions &opts)
Definition: pdtscript.cc:74