FST  openfst-1.8.2
OpenFst Library
fstshortestdistance-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 // 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>
28 #include <fst/script/getters.h>
30 #include <fst/script/text-io.h>
31 
32 DECLARE_bool(reverse);
33 DECLARE_double(delta);
34 DECLARE_int64(nstate);
35 DECLARE_string(queue_type);
36 
37 int fstshortestdistance_main(int argc, char **argv) {
38  namespace s = fst::script;
39  using fst::AUTO_QUEUE;
40  using fst::QueueType;
43 
44  std::string usage = "Finds shortest distance(s) in an FST.\n\n Usage: ";
45  usage += argv[0];
46  usage += " [in.fst [distance.txt]]\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  std::vector<WeightClass> distance;
64 
65  QueueType queue_type;
66  if (!s::GetQueueType(FST_FLAGS_queue_type, &queue_type)) {
67  LOG(ERROR) << argv[0] << ": Unknown or unsupported queue type: "
68  << FST_FLAGS_queue_type;
69  return 1;
70  }
71 
72  if (FST_FLAGS_reverse && queue_type != AUTO_QUEUE) {
73  LOG(ERROR) << argv[0] << ": Can't use non-default queue with reverse";
74  return 1;
75  }
76 
77  if (FST_FLAGS_reverse) {
78  s::ShortestDistance(*ifst, &distance, FST_FLAGS_reverse,
79  FST_FLAGS_delta);
80  } else {
81  const s::ShortestDistanceOptions opts(queue_type, s::ArcFilterType::ANY,
82  FST_FLAGS_nstate,
83  FST_FLAGS_delta);
84  s::ShortestDistance(*ifst, &distance, opts);
85  }
86 
87  return !s::WritePotentials(out_name, distance);
88 }
void ShowUsage(bool long_usage=true)
Definition: flags.cc:132
bool WritePotentials(const std::string &source, const std::vector< WeightClass > &potentials)
Definition: text-io.cc:68
QueueType
Definition: queue.h:70
DECLARE_double(delta)
DECLARE_string(queue_type)
#define LOG(type)
Definition: log.h:49
void FailedNewHandler()
Definition: compat.cc:26
#define SET_FLAGS(usage, argc, argv, rmflags)
Definition: flags.h:224
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:169
DECLARE_bool(reverse)
DECLARE_int64(nstate)