• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1  // fstarcsort.cc
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  // Copyright 2005-2010 Google, Inc.
16  // Author: riley@google.com (Michael Riley)
17  // Modified: jpr@google.com (Jake Ratkiewicz) to use FstClass
18  //
19  // \file
20  // Sorts arcs of an FST.
21  //
22  
23  #include <string>
24  
25  #include <fst/compat.h>
26  #include <fst/script/arcsort.h>
27  
28  DEFINE_string(sort_type, "ilabel",
29                "Comparison method, one of: \"ilabel\", \"olabel\"");
30  
main(int argc,char ** argv)31  int main(int argc, char **argv) {
32    using fst::script::FstClass;
33    using fst::script::MutableFstClass;
34    using fst::script::ArcSort;
35  
36    string usage = "Sorts arcs of an FST.\n\n  Usage: ";
37    usage += argv[0];
38    usage += " [in.fst [out.fst]]\n";
39  
40    std::set_new_handler(FailedNewHandler);
41    SET_FLAGS(usage.c_str(), &argc, &argv, true);
42  
43    if (argc > 3) {
44      ShowUsage();
45      return 1;
46    }
47  
48    string in_name = (argc > 1 && (strcmp(argv[1], "-") != 0)) ? argv[1] : "";
49    string out_name = argc > 2 ? argv[2] : "";
50  
51    MutableFstClass *fst = MutableFstClass::Read(in_name, true);
52    if (!fst) return 1;
53  
54    if (FLAGS_sort_type == "ilabel") {
55      ArcSort(fst, fst::script::ILABEL_COMPARE);
56    } else if (FLAGS_sort_type == "olabel") {
57      ArcSort(fst, fst::script::OLABEL_COMPARE);
58    } else {
59      LOG(ERROR) << argv[0] << ": Unknown sort type \""
60                 << FLAGS_sort_type << "\"\n";
61      return 1;
62    }
63  
64    fst->Write(out_name);
65  
66    return 0;
67  }
68