FST  openfst-1.8.3
OpenFst Library
pdtshortestpath-main.cc
Go to the documentation of this file.
1 // Copyright 2005-2024 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 <cstdint>
21 #include <cstring>
22 #include <memory>
23 #include <string>
24 #include <utility>
25 #include <vector>
26 
27 #include <fst/flags.h>
28 #include <fst/log.h>
30 #include <fst/queue.h>
31 #include <fst/util.h>
32 #include <fst/script/arg-packs.h>
33 #include <fst/script/fst-class.h>
34 
35 DECLARE_bool(keep_parentheses);
36 DECLARE_string(queue_type);
37 DECLARE_bool(path_gc);
38 DECLARE_string(pdt_parentheses);
39 
40 int pdtshortestpath_main(int argc, char **argv) {
41  namespace s = fst::script;
42  using fst::QueueType;
43  using fst::ReadLabelPairs;
46 
47  std::string usage = "Shortest path in a (bounded-stack) PDT.\n\n Usage: ";
48  usage += argv[0];
49  usage += " in.pdt [out.fst]\n";
50 
51  SET_FLAGS(usage.c_str(), &argc, &argv, true);
52  if (argc > 3) {
53  ShowUsage();
54  return 1;
55  }
56 
57  const std::string in_name =
58  (argc > 1 && (strcmp(argv[1], "-") != 0)) ? argv[1] : "";
59  const std::string out_name =
60  (argc > 2 && (strcmp(argv[2], "-") != 0)) ? argv[2] : "";
61 
62  std::unique_ptr<FstClass> ifst(FstClass::Read(in_name));
63  if (!ifst) return 1;
64 
65  if (FST_FLAGS_pdt_parentheses.empty()) {
66  LOG(ERROR) << argv[0] << ": No PDT parenthesis label pairs provided";
67  return 1;
68  }
69 
70  std::vector<std::pair<int64_t, int64_t>> parens;
71  if (!ReadLabelPairs(FST_FLAGS_pdt_parentheses, &parens)) return 1;
72 
73  VectorFstClass ofst(ifst->ArcType());
74 
75  QueueType qt;
76  if (FST_FLAGS_queue_type == "fifo") {
77  qt = fst::FIFO_QUEUE;
78  } else if (FST_FLAGS_queue_type == "lifo") {
79  qt = fst::LIFO_QUEUE;
80  } else if (FST_FLAGS_queue_type == "state") {
82  } else {
83  LOG(ERROR) << "Unknown queue type: " << FST_FLAGS_queue_type;
84  return 1;
85  }
86 
87  const s::PdtShortestPathOptions opts(
88  qt, FST_FLAGS_keep_parentheses, FST_FLAGS_path_gc);
89 
90  s::ShortestPath(*ifst, parens, &ofst, opts);
91 
92  return !ofst.Write(out_name);
93 }
void ShowUsage(bool long_usage=true)
Definition: flags.cc:138
QueueType
Definition: queue.h:76
#define LOG(type)
Definition: log.h:53
void ShortestPath(const FstClass &ifst, const std::vector< std::pair< int64_t, int64_t >> &parens, MutableFstClass *ofst, const PdtShortestPathOptions &opts)
Definition: pdtscript.cc:109
#define SET_FLAGS(usage, argc, argv, rmflags)
Definition: flags.h:226
int pdtshortestpath_main(int argc, char **argv)
DECLARE_string(queue_type)
DECLARE_bool(keep_parentheses)
bool ReadLabelPairs(std::string_view source, std::vector< std::pair< Label, Label >> *pairs)
Definition: util.h:422