FST  openfst-1.8.3
OpenFst Library
fstarcsort-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 // Sorts arcs of 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/script/arcsort.h>
27 #include <fst/script/fst-class.h>
28 #include <fst/script/getters.h>
29 
30 DECLARE_string(sort_type);
31 
32 int fstarcsort_main(int argc, char **argv) {
33  namespace s = fst::script;
35 
36  std::string usage = "Sorts arcs of an FST.\n\n Usage: ";
37  usage += argv[0];
38  usage += " [in.fst [out.fst]]\n";
39 
40  SET_FLAGS(usage.c_str(), &argc, &argv, true);
41 
42  if (argc > 3) {
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 out_name =
50  (argc > 2 && (strcmp(argv[2], "-") != 0)) ? argv[2] : "";
51 
52  std::unique_ptr<MutableFstClass> fst(MutableFstClass::Read(in_name, true));
53  if (!fst) return 1;
54 
55  s::ArcSortType sort_type;
56  if (!s::GetArcSortType(FST_FLAGS_sort_type, &sort_type)) {
57  LOG(ERROR) << argv[0] << ": Unknown or unsupported sort type: "
58  << FST_FLAGS_sort_type;
59  return 1;
60  }
61 
62  s::ArcSort(fst.get(), sort_type);
63 
64  return !fst->Write(out_name);
65 }
void ShowUsage(bool long_usage=true)
Definition: flags.cc:138
bool GetArcSortType(std::string_view str, ArcSortType *sort_type)
Definition: getters.cc:55
int fstarcsort_main(int argc, char **argv)
#define LOG(type)
Definition: log.h:53
#define SET_FLAGS(usage, argc, argv, rmflags)
Definition: flags.h:226
void ArcSort(MutableFst< Arc > *fst, Compare comp)
Definition: arcsort.h:109
DECLARE_string(sort_type)