FST  openfst-1.8.3
OpenFst Library
fstshortestdistance-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 distances in an FST.
19 
20 #include <cstring>
21 #include <memory>
22 #include <string>
23 #include <vector>
24 
25 #include <fst/flags.h>
26 #include <fst/log.h>
27 #include <fst/queue.h>
29 #include <fst/script/fst-class.h>
30 #include <fst/script/getters.h>
32 #include <fst/script/text-io.h>
34 
35 DECLARE_bool(reverse);
36 DECLARE_double(delta);
37 DECLARE_int64(nstate);
38 DECLARE_string(queue_type);
39 
40 int fstshortestdistance_main(int argc, char **argv) {
41  namespace s = fst::script;
42  using fst::AUTO_QUEUE;
43  using fst::QueueType;
46 
47  std::string usage = "Finds shortest distance(s) in an FST.\n\n Usage: ";
48  usage += argv[0];
49  usage += " [in.fst [distance.txt]]\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  std::vector<WeightClass> distance;
66 
67  QueueType queue_type;
68  if (!s::GetQueueType(FST_FLAGS_queue_type, &queue_type)) {
69  LOG(ERROR) << argv[0] << ": Unknown or unsupported queue type: "
70  << FST_FLAGS_queue_type;
71  return 1;
72  }
73 
74  if (FST_FLAGS_reverse && queue_type != AUTO_QUEUE) {
75  LOG(ERROR) << argv[0] << ": Can't use non-default queue with reverse";
76  return 1;
77  }
78 
79  if (FST_FLAGS_reverse) {
80  s::ShortestDistance(*ifst, &distance, FST_FLAGS_reverse,
81  FST_FLAGS_delta);
82  } else {
83  const s::ShortestDistanceOptions opts(queue_type, s::ArcFilterType::ANY,
84  FST_FLAGS_nstate,
85  FST_FLAGS_delta);
86  s::ShortestDistance(*ifst, &distance, opts);
87  }
88 
89  return !s::WritePotentials(out_name, distance);
90 }
void ShowUsage(bool long_usage=true)
Definition: flags.cc:138
bool WritePotentials(const std::string &source, const std::vector< WeightClass > &potentials)
Definition: text-io.cc:70
QueueType
Definition: queue.h:76
DECLARE_double(delta)
DECLARE_string(queue_type)
#define LOG(type)
Definition: log.h:53
#define SET_FLAGS(usage, argc, argv, rmflags)
Definition: flags.h:226
int fstshortestdistance_main(int argc, char **argv)
void ShortestDistance(const Fst< Arc > &fst, std::vector< typename Arc::Weight > *distance, const ShortestDistanceOptions &opts)
bool GetQueueType(std::string_view str, QueueType *queue_type)
Definition: getters.cc:184
DECLARE_bool(reverse)
DECLARE_int64(nstate)