FST  openfst-1.7.2
OpenFst Library
fstcompile-main.cc
Go to the documentation of this file.
1 // See www.openfst.org for extensive documentation on this weighted
2 // finite-state transducer library.
3 //
4 // Creates binary FSTs from simple text format used by AT&T.
5 
6 #include <cstring>
7 
8 #include <fstream>
9 #include <istream>
10 #include <memory>
11 #include <string>
12 
13 #include <fst/flags.h>
14 #include <fst/log.h>
15 #include <fst/script/compile.h>
16 
17 DECLARE_bool(acceptor);
18 DECLARE_string(arc_type);
19 DECLARE_string(fst_type);
20 DECLARE_string(isymbols);
21 DECLARE_string(osymbols);
22 DECLARE_string(ssymbols);
23 DECLARE_bool(keep_isymbols);
24 DECLARE_bool(keep_osymbols);
25 DECLARE_bool(keep_state_numbering);
26 DECLARE_bool(allow_negative_labels);
27 
28 int fstcompile_main(int argc, char **argv) {
29  namespace s = fst::script;
30  using fst::SymbolTable;
32 
33  string usage = "Creates binary FSTs from simple text format.\n\n Usage: ";
34  usage += argv[0];
35  usage += " [text.fst [binary.fst]]\n";
36 
37  std::set_new_handler(FailedNewHandler);
38  SET_FLAGS(usage.c_str(), &argc, &argv, true);
39  if (argc > 3) {
40  ShowUsage();
41  return 1;
42  }
43 
44  string source = "standard input";
45  std::ifstream fstrm;
46  if (argc > 1 && strcmp(argv[1], "-") != 0) {
47  fstrm.open(argv[1]);
48  if (!fstrm) {
49  LOG(ERROR) << argv[0] << ": Open failed, file = " << argv[1];
50  return 1;
51  }
52  source = argv[1];
53  }
54  std::istream &istrm = fstrm.is_open() ? fstrm : std::cin;
55 
56  const SymbolTableTextOptions opts(FLAGS_allow_negative_labels);
57 
58  std::unique_ptr<const SymbolTable> isyms;
59  if (!FLAGS_isymbols.empty()) {
60  isyms.reset(SymbolTable::ReadText(FLAGS_isymbols, opts));
61  if (!isyms) return 1;
62  }
63 
64  std::unique_ptr<const SymbolTable> osyms;
65  if (!FLAGS_osymbols.empty()) {
66  osyms.reset(SymbolTable::ReadText(FLAGS_osymbols, opts));
67  if (!osyms) return 1;
68  }
69 
70  std::unique_ptr<const SymbolTable> ssyms;
71  if (!FLAGS_ssymbols.empty()) {
72  ssyms.reset(SymbolTable::ReadText(FLAGS_ssymbols));
73  if (!ssyms) return 1;
74  }
75 
76  const string dest = argc > 2 ? argv[2] : "";
77 
78  s::CompileFst(istrm, source, dest, FLAGS_fst_type, FLAGS_arc_type,
79  isyms.get(), osyms.get(), ssyms.get(), FLAGS_acceptor,
80  FLAGS_keep_isymbols, FLAGS_keep_osymbols,
81  FLAGS_keep_state_numbering, FLAGS_allow_negative_labels);
82 
83  return 0;
84 }
void ShowUsage(bool long_usage=true)
Definition: flags.cc:124
int fstcompile_main(int argc, char **argv)
DECLARE_bool(acceptor)
#define LOG(type)
Definition: log.h:48
void CompileFst(std::istream &istrm, const string &source, const string &dest, const string &fst_type, const 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:14
void FailedNewHandler()
Definition: compat.cc:25
#define SET_FLAGS(usage, argc, argv, rmflags)
Definition: flags.h:214
DECLARE_string(arc_type)