FST  openfst-1.8.3
OpenFst Library
fstshortestpath-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 // Find shortest path(s) in an FST.
19 
20 #include <cstring>
21 #include <memory>
22 #include <string>
23 
24 #include <fst/flags.h>
25 #include <fst/log.h>
26 #include <fst/queue.h>
27 #include <fst/script/fst-class.h>
28 #include <fst/script/getters.h>
31 
32 DECLARE_double(delta);
33 DECLARE_int32(nshortest);
34 DECLARE_int64(nstate);
35 DECLARE_string(queue_type);
36 DECLARE_bool(unique);
37 DECLARE_string(weight);
38 
39 int fstshortestpath_main(int argc, char **argv) {
40  namespace s = fst::script;
41  using fst::QueueType;
45 
46  std::string usage = "Finds shortest path(s) in an FST.\n\n Usage: ";
47  usage += argv[0];
48  usage += " [in.fst [out.fst]]\n";
49 
50  SET_FLAGS(usage.c_str(), &argc, &argv, true);
51  if (argc > 3) {
52  ShowUsage();
53  return 1;
54  }
55 
56  const std::string in_name =
57  (argc > 1 && strcmp(argv[1], "-") != 0) ? argv[1] : "";
58  const std::string out_name =
59  (argc > 2 && strcmp(argv[2], "-") != 0) ? argv[2] : "";
60 
61  std::unique_ptr<FstClass> ifst(FstClass::Read(in_name));
62  if (!ifst) return 1;
63 
64  const auto weight_threshold =
65  FST_FLAGS_weight.empty()
66  ? WeightClass::Zero(ifst->WeightType())
67  : WeightClass(ifst->WeightType(), FST_FLAGS_weight);
68 
69  VectorFstClass ofst(ifst->ArcType());
70 
71  QueueType queue_type;
72  if (!s::GetQueueType(FST_FLAGS_queue_type, &queue_type)) {
73  LOG(ERROR) << "Unknown or unsupported queue type: "
74  << FST_FLAGS_queue_type;
75  return 1;
76  }
77 
78  const s::ShortestPathOptions opts(
79  queue_type, FST_FLAGS_nshortest, FST_FLAGS_unique,
80  FST_FLAGS_delta, weight_threshold,
81  FST_FLAGS_nstate);
82 
83  s::ShortestPath(*ifst, &ofst, opts);
84 
85  return !ofst.Write(out_name);
86 }
void ShowUsage(bool long_usage=true)
Definition: flags.cc:138
DECLARE_int64(nstate)
QueueType
Definition: queue.h:76
DECLARE_bool(unique)
#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
DECLARE_string(queue_type)
bool GetQueueType(std::string_view str, QueueType *queue_type)
Definition: getters.cc:184
DECLARE_double(delta)
int fstshortestpath_main(int argc, char **argv)
DECLARE_int32(nshortest)