FST  openfst-1.8.3
OpenFst Library
fstrmepsilon-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 // Removes epsilons from 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>
29 #include <fst/script/rmepsilon.h>
31 
32 DECLARE_bool(connect);
33 DECLARE_double(delta);
34 DECLARE_int64(nstate);
35 DECLARE_string(queue_type);
36 DECLARE_string(weight);
37 
38 int fstrmepsilon_main(int argc, char **argv) {
39  namespace s = fst::script;
40  using fst::QueueType;
43 
44  std::string usage = "Removes epsilons from an FST.\n\n Usage: ";
45  usage += argv[0];
46  usage += " [in.fst [out.fst]]\n";
47 
48  SET_FLAGS(usage.c_str(), &argc, &argv, true);
49  if (argc > 3) {
50  ShowUsage();
51  return 1;
52  }
53 
54  const std::string in_name =
55  (argc > 1 && strcmp(argv[1], "-") != 0) ? argv[1] : "";
56  const std::string out_name =
57  (argc > 2 && strcmp(argv[2], "-") != 0) ? argv[2] : "";
58 
59  std::unique_ptr<MutableFstClass> fst(MutableFstClass::Read(in_name, true));
60  if (!fst) return 1;
61 
62  const auto weight_threshold =
63  FST_FLAGS_weight.empty()
64  ? WeightClass::Zero(fst->WeightType())
65  : WeightClass(fst->WeightType(), FST_FLAGS_weight);
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  const s::RmEpsilonOptions opts(queue_type, FST_FLAGS_connect,
75  weight_threshold, FST_FLAGS_nstate,
76  FST_FLAGS_delta);
77 
78  s::RmEpsilon(fst.get(), opts);
79 
80  return !fst->Write(out_name);
81 }
void ShowUsage(bool long_usage=true)
Definition: flags.cc:138
void RmEpsilon(MutableFst< Arc > *fst, std::vector< typename Arc::Weight > *distance, const RmEpsilonOptions< Arc, Queue > &opts)
Definition: rmepsilon.h:217
QueueType
Definition: queue.h:76
#define LOG(type)
Definition: log.h:53
int fstrmepsilon_main(int argc, char **argv)
DECLARE_bool(connect)
#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)
DECLARE_int64(nstate)