• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // fstcompile.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 // Creates binary FSTs from simple text format used by AT&T
21 // (see http://www.research.att.com/projects/mohri/fsm/doc4/fsm.5.html).
22 
23 #include <fst/script/compile.h>
24 
25 DEFINE_bool(acceptor, false, "Input in acceptor format");
26 DEFINE_string(arc_type, "standard", "Output arc type");
27 DEFINE_string(fst_type, "vector", "Output FST type");
28 DEFINE_string(isymbols, "", "Input label symbol table");
29 DEFINE_string(osymbols, "", "Output label symbol table");
30 DEFINE_string(ssymbols, "", "State label symbol table");
31 DEFINE_bool(keep_isymbols, false, "Store input label symbol table with FST");
32 DEFINE_bool(keep_osymbols, false, "Store output label symbol table with FST");
33 DEFINE_bool(keep_state_numbering, false, "Do not renumber input states");
34 DEFINE_bool(allow_negative_labels, false,
35             "Allow negative labels (not recommended; may cause conflicts)");
36 
main(int argc,char ** argv)37 int main(int argc, char **argv) {
38   namespace s = fst::script;
39   using fst::istream;
40   using fst::ifstream;
41   using fst::SymbolTable;
42 
43   string usage = "Creates binary FSTs from simple text format.\n\n  Usage: ";
44   usage += argv[0];
45   usage += " [text.fst [binary.fst]]\n";
46 
47   std::set_new_handler(FailedNewHandler);
48   SET_FLAGS(usage.c_str(), &argc, &argv, true);
49   if (argc > 3) {
50     ShowUsage();
51     return 1;
52   }
53 
54   const char *source = "standard input";
55   istream *istrm = &cin;
56   if (argc > 1 && strcmp(argv[1], "-") != 0) {
57     source = argv[1];
58     istrm = new fst::ifstream(argv[1]);
59     if (!*istrm) {
60       LOG(ERROR) << argv[0] << ": Open failed, file = " << argv[1];
61       return 1;
62     }
63   }
64   const SymbolTable *isyms = 0, *osyms = 0, *ssyms = 0;
65 
66   fst::SymbolTableTextOptions opts;
67   opts.allow_negative = FLAGS_allow_negative_labels;
68 
69   if (!FLAGS_isymbols.empty()) {
70     isyms = SymbolTable::ReadText(FLAGS_isymbols, opts);
71     if (!isyms) exit(1);
72   }
73 
74   if (!FLAGS_osymbols.empty()) {
75     osyms = SymbolTable::ReadText(FLAGS_osymbols, opts);
76     if (!osyms) exit(1);
77   }
78 
79   if (!FLAGS_ssymbols.empty()) {
80     ssyms = SymbolTable::ReadText(FLAGS_ssymbols);
81     if (!ssyms) exit(1);
82   }
83 
84   string dest = argc > 2 ? argv[2] : "";
85 
86   s::CompileFst(*istrm, source, dest, FLAGS_fst_type, FLAGS_arc_type,
87                 isyms, osyms, ssyms,
88                 FLAGS_acceptor, FLAGS_keep_isymbols, FLAGS_keep_osymbols,
89                 FLAGS_keep_state_numbering, FLAGS_allow_negative_labels);
90 
91   if (istrm != &cin)
92     delete istrm;
93 
94   return 0;
95 }
96