FST  openfst-1.8.1
OpenFst Library
pdtshortestpath-main.cc
Go to the documentation of this file.
1 // Copyright 2005-2020 Google LLC
2 //
3 // Licensed under the Apache License, Version 2.0 (the 'License');
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an 'AS IS' BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 //
15 // See www.openfst.org for extensive documentation on this weighted
16 // finite-state transducer library.
17 //
18 // Returns the shortest path in a (bounded-stack) PDT.
19 
20 #include <cstring>
21 #include <memory>
22 #include <string>
23 #include <utility>
24 #include <vector>
25 
26 #include <fst/flags.h>
27 #include <fst/types.h>
28 #include <fst/log.h>
30 #include <fst/util.h>
31 
32 DECLARE_bool(keep_parentheses);
33 DECLARE_string(queue_type);
34 DECLARE_bool(path_gc);
35 DECLARE_string(pdt_parentheses);
36 
37 int pdtshortestpath_main(int argc, char **argv) {
38  namespace s = fst::script;
39  using fst::QueueType;
40  using fst::ReadLabelPairs;
43 
44  std::string usage = "Shortest path in a (bounded-stack) PDT.\n\n Usage: ";
45  usage += argv[0];
46  usage += " in.pdt [out.fst]\n";
47 
48  std::set_new_handler(FailedNewHandler);
49  SET_FLAGS(usage.c_str(), &argc, &argv, true);
50  if (argc > 3) {
51  ShowUsage();
52  return 1;
53  }
54 
55  const std::string in_name =
56  (argc > 1 && (strcmp(argv[1], "-") != 0)) ? argv[1] : "";
57  const std::string out_name =
58  (argc > 2 && (strcmp(argv[2], "-") != 0)) ? argv[2] : "";
59 
60  std::unique_ptr<FstClass> ifst(FstClass::Read(in_name));
61  if (!ifst) return 1;
62 
63  if (FST_FLAGS_pdt_parentheses.empty()) {
64  LOG(ERROR) << argv[0] << ": No PDT parenthesis label pairs provided";
65  return 1;
66  }
67 
68  std::vector<std::pair<int64, int64>> parens;
69  if (!ReadLabelPairs(FST_FLAGS_pdt_parentheses, &parens, false))
70  return 1;
71 
72  VectorFstClass ofst(ifst->ArcType());
73 
74  QueueType qt;
75  if (FST_FLAGS_queue_type == "fifo") {
76  qt = fst::FIFO_QUEUE;
77  } else if (FST_FLAGS_queue_type == "lifo") {
78  qt = fst::LIFO_QUEUE;
79  } else if (FST_FLAGS_queue_type == "state") {
81  } else {
82  LOG(ERROR) << "Unknown queue type: " << FST_FLAGS_queue_type;
83  return 1;
84  }
85 
86  const s::PdtShortestPathOptions opts(
87  qt, FST_FLAGS_keep_parentheses, FST_FLAGS_path_gc);
88 
89  s::PdtShortestPath(*ifst, parens, &ofst, opts);
90 
91  return !ofst.Write(out_name);
92 }
void ShowUsage(bool long_usage=true)
Definition: flags.cc:123
void PdtShortestPath(const FstClass &ifst, const std::vector< std::pair< int64, int64 >> &parens, MutableFstClass *ofst, const PdtShortestPathOptions &opts)
Definition: pdtscript.cc:101
QueueType
Definition: queue.h:71
#define LOG(type)
Definition: log.h:46
void FailedNewHandler()
Definition: compat.cc:22
#define SET_FLAGS(usage, argc, argv, rmflags)
Definition: flags.h:222
bool ReadLabelPairs(const std::string &source, std::vector< std::pair< Label, Label >> *pairs, bool allow_negative=false)
Definition: util.h:372
int pdtshortestpath_main(int argc, char **argv)
DECLARE_string(queue_type)
DECLARE_bool(keep_parentheses)