FST  openfst-1.8.3
OpenFst Library
fstminimize-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 // Minimizes a deterministic 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/script/fst-class.h>
27 #include <fst/script/minimize.h>
28 
29 DECLARE_double(delta);
30 DECLARE_bool(allow_nondet);
31 
32 int fstminimize_main(int argc, char **argv) {
33  namespace s = fst::script;
36 
37  std::string usage = "Minimizes a deterministic FST.\n\n Usage: ";
38  usage += argv[0];
39  usage += " [in.fst [out1.fst [out2.fst]]]\n";
40 
41  SET_FLAGS(usage.c_str(), &argc, &argv, true);
42  if (argc > 4) {
43  ShowUsage();
44  return 1;
45  }
46 
47  const std::string in_name =
48  (argc > 1 && strcmp(argv[1], "-") != 0) ? argv[1] : "";
49  const std::string out1_name =
50  (argc > 2 && strcmp(argv[2], "-") != 0) ? argv[2] : "";
51  const std::string out2_name =
52  (argc > 3 && strcmp(argv[3], "-") != 0) ? argv[3] : "";
53 
54  if (out1_name.empty() && out2_name.empty() && argc > 3) {
55  LOG(ERROR) << argv[0] << ": Both outputs can't be standard output.";
56  return 1;
57  }
58 
59  std::unique_ptr<MutableFstClass> fst1(MutableFstClass::Read(in_name, true));
60  if (!fst1) return 1;
61 
62  if (argc > 3) {
63  std::unique_ptr<MutableFstClass> fst2(new VectorFstClass(fst1->ArcType()));
64  s::Minimize(fst1.get(), fst2.get(), FST_FLAGS_delta,
65  FST_FLAGS_allow_nondet);
66  if (!fst2->Write(out2_name)) return 1;
67  } else {
68  s::Minimize(fst1.get(), nullptr, FST_FLAGS_delta,
69  FST_FLAGS_allow_nondet);
70  }
71 
72  return !fst1->Write(out1_name);
73 }
void ShowUsage(bool long_usage=true)
Definition: flags.cc:138
DECLARE_bool(allow_nondet)
#define LOG(type)
Definition: log.h:53
#define SET_FLAGS(usage, argc, argv, rmflags)
Definition: flags.h:226
DECLARE_double(delta)
void Minimize(MutableFst< Arc > *fst, MutableFst< Arc > *sfst=nullptr, float delta=kShortestDelta, bool allow_nondet=false)
Definition: minimize.h:512
int fstminimize_main(int argc, char **argv)