FST  openfst-1.8.3
OpenFst Library
fstconnect-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 useless (inaccessible or non-coaccessible) states and arcs from an
19 // FST.
20 
21 #include <cstring>
22 #include <memory>
23 #include <string>
24 
25 #include <fst/flags.h>
26 #include <fst/script/connect.h>
27 #include <fst/script/fst-class.h>
28 
29 int fstconnect_main(int argc, char **argv) {
30  namespace s = fst::script;
33 
34  std::string usage =
35  "Removes useless states and arcs from an FST.\n\n Usage: ";
36  usage += argv[0];
37  usage += " [in.fst [out.fst]]\n";
38 
39  SET_FLAGS(usage.c_str(), &argc, &argv, true);
40  if (argc > 3) {
41  ShowUsage();
42  return 1;
43  }
44 
45  const std::string in_name =
46  (argc > 1 && strcmp(argv[1], "-") != 0) ? argv[1] : "";
47  const std::string out_name =
48  (argc > 2 && strcmp(argv[2], "-") != 0) ? argv[2] : "";
49 
50  std::unique_ptr<MutableFstClass> fst(MutableFstClass::Read(in_name, true));
51  if (!fst) return 1;
52 
53  s::Connect(fst.get());
54 
55  return !fst->Write(out_name);
56 }
void ShowUsage(bool long_usage=true)
Definition: flags.cc:138
void Connect(MutableFst< Arc > *fst)
Definition: connect.h:47
#define SET_FLAGS(usage, argc, argv, rmflags)
Definition: flags.h:226
int fstconnect_main(int argc, char **argv)