FST  openfst-1.8.0
OpenFst Library
fstcompile-main.cc
Go to the documentation of this file.
1 // Copyright 2005-2020 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 // Creates binary FSTs from simple text format used by AT&T.
19 
20 #include <cstring>
21 #include <istream>
22 #include <memory>
23 #include <string>
24 
25 #include <fst/flags.h>
26 #include <fst/log.h>
27 #include <fstream>
28 #include <fst/script/compile.h>
29 
30 DECLARE_bool(acceptor);
31 DECLARE_string(arc_type);
32 DECLARE_string(fst_type);
33 DECLARE_string(isymbols);
34 DECLARE_string(osymbols);
35 DECLARE_string(ssymbols);
36 DECLARE_bool(keep_isymbols);
37 DECLARE_bool(keep_osymbols);
38 DECLARE_bool(keep_state_numbering);
39 DECLARE_bool(allow_negative_labels);
40 
41 int fstcompile_main(int argc, char **argv) {
42  namespace s = fst::script;
43  using fst::SymbolTable;
45 
46  std::string usage =
47  "Creates binary FSTs from simple text format.\n\n Usage: ";
48  usage += argv[0];
49  usage += " [text.fst [binary.fst]]\n";
50 
51  std::set_new_handler(FailedNewHandler);
52  SET_FLAGS(usage.c_str(), &argc, &argv, true);
53  if (argc > 3) {
54  ShowUsage();
55  return 1;
56  }
57 
58  std::string source = "standard input";
59  std::ifstream fstrm;
60  if (argc > 1 && strcmp(argv[1], "-") != 0) {
61  fstrm.open(argv[1]);
62  if (!fstrm) {
63  LOG(ERROR) << argv[0] << ": Open failed, file = " << argv[1];
64  return 1;
65  }
66  source = argv[1];
67  }
68  std::istream &istrm = fstrm.is_open() ? fstrm : std::cin;
69 
70  const SymbolTableTextOptions opts(FLAGS_allow_negative_labels);
71 
72  std::unique_ptr<const SymbolTable> isyms;
73  if (!FLAGS_isymbols.empty()) {
74  isyms.reset(SymbolTable::ReadText(FLAGS_isymbols, opts));
75  if (!isyms) return 1;
76  }
77 
78  std::unique_ptr<const SymbolTable> osyms;
79  if (!FLAGS_osymbols.empty()) {
80  osyms.reset(SymbolTable::ReadText(FLAGS_osymbols, opts));
81  if (!osyms) return 1;
82  }
83 
84  std::unique_ptr<const SymbolTable> ssyms;
85  if (!FLAGS_ssymbols.empty()) {
86  ssyms.reset(SymbolTable::ReadText(FLAGS_ssymbols));
87  if (!ssyms) return 1;
88  }
89 
90  const std::string dest = argc > 2 && strcmp(argv[2], "-") != 0 ? argv[2] : "";
91 
93  istrm, source, dest, FLAGS_fst_type, FLAGS_arc_type, isyms.get(),
94  osyms.get(), ssyms.get(), FLAGS_acceptor,
95  FLAGS_keep_isymbols, FLAGS_keep_osymbols,
96  FLAGS_keep_state_numbering,
97  FLAGS_allow_negative_labels);
98 
99  return 0;
100 }
void ShowUsage(bool long_usage=true)
Definition: flags.cc:123
int fstcompile_main(int argc, char **argv)
DECLARE_bool(acceptor)
#define LOG(type)
Definition: log.h:46
void FailedNewHandler()
Definition: compat.cc:22
#define SET_FLAGS(usage, argc, argv, rmflags)
Definition: flags.h:216
void CompileFst(std::istream &istrm, const std::string &source, const std::string &dest, const std::string &fst_type, const std::string &arc_type, const SymbolTable *isyms, const SymbolTable *osyms, const SymbolTable *ssyms, bool accep, bool ikeep, bool okeep, bool nkeep, bool allow_negative_labels)
Definition: compile.cc:25
DECLARE_string(arc_type)