FST  openfst-1.7.1
OpenFst Library
fstprint-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 // Prints out binary FSTs in simple text format used by AT&T.
5 
6 #include <cstring>
7 
8 #include <fstream>
9 #include <memory>
10 #include <ostream>
11 #include <string>
12 
13 #include <fst/flags.h>
14 #include <fst/log.h>
15 #include <fst/script/print.h>
16 
17 DECLARE_bool(acceptor);
18 DECLARE_string(isymbols);
19 DECLARE_string(osymbols);
20 DECLARE_string(ssymbols);
21 DECLARE_bool(numeric);
22 DECLARE_string(save_isymbols);
23 DECLARE_string(save_osymbols);
24 DECLARE_bool(show_weight_one);
25 DECLARE_bool(allow_negative_labels);
26 DECLARE_string(missing_symbol);
27 
28 int fstprint_main(int argc, char **argv) {
29  namespace s = fst::script;
31  using fst::SymbolTable;
33 
34  string usage = "Prints out binary FSTs in simple text format.\n\n Usage: ";
35  usage += argv[0];
36  usage += " [binary.fst [text.fst]]\n";
37 
38  std::set_new_handler(FailedNewHandler);
39  SET_FLAGS(usage.c_str(), &argc, &argv, true);
40  if (argc > 3) {
41  ShowUsage();
42  return 1;
43  }
44 
45  const string in_name = (argc > 1 && strcmp(argv[1], "-") != 0) ? argv[1] : "";
46  const string out_name = argc > 2 ? argv[2] : "";
47 
48  std::unique_ptr<FstClass> fst(FstClass::Read(in_name));
49  if (!fst) return 1;
50 
51  string dest = "standard output";
52  std::ofstream fstrm;
53  if (argc == 3) {
54  fstrm.open(argv[2]);
55  if (!fstrm) {
56  LOG(ERROR) << argv[0] << ": Open failed, file = " << argv[2];
57  return 1;
58  }
59  dest = argv[2];
60  }
61  std::ostream &ostrm = fstrm.is_open() ? fstrm : std::cout;
62  ostrm.precision(9);
63 
64  const SymbolTableTextOptions opts(FLAGS_allow_negative_labels);
65 
66  std::unique_ptr<const SymbolTable> isyms;
67  if (!FLAGS_isymbols.empty() && !FLAGS_numeric) {
68  isyms.reset(SymbolTable::ReadText(FLAGS_isymbols, opts));
69  if (!isyms) return 1;
70  }
71 
72  std::unique_ptr<const SymbolTable> osyms;
73  if (!FLAGS_osymbols.empty() && !FLAGS_numeric) {
74  osyms.reset(SymbolTable::ReadText(FLAGS_osymbols, opts));
75  if (!osyms) return 1;
76  }
77 
78  std::unique_ptr<const SymbolTable> ssyms;
79  if (!FLAGS_ssymbols.empty() && !FLAGS_numeric) {
80  ssyms.reset(SymbolTable::ReadText(FLAGS_ssymbols));
81  if (!ssyms) return 1;
82  }
83 
84  if (!isyms && !FLAGS_numeric && fst->InputSymbols()) {
85  isyms.reset(fst->InputSymbols()->Copy());
86  }
87 
88  if (!osyms && !FLAGS_numeric && fst->OutputSymbols()) {
89  osyms.reset(fst->OutputSymbols()->Copy());
90  }
91 
92  s::PrintFst(*fst, ostrm, dest, isyms.get(), osyms.get(), ssyms.get(),
93  FLAGS_acceptor, FLAGS_show_weight_one, FLAGS_missing_symbol);
94 
95  if (isyms && !FLAGS_save_isymbols.empty()) {
96  if (!isyms->WriteText(FLAGS_save_isymbols)) return 1;
97  }
98 
99  if (osyms && !FLAGS_save_osymbols.empty()) {
100  if (!osyms->WriteText(FLAGS_save_osymbols)) return 1;
101  }
102 
103  return 0;
104 }
void ShowUsage(bool long_usage=true)
Definition: flags.cc:124
#define LOG(type)
Definition: log.h:48
DECLARE_string(isymbols)
void FailedNewHandler()
Definition: compat.cc:25
#define SET_FLAGS(usage, argc, argv, rmflags)
Definition: flags.h:214
void PrintFst(FstPrinterArgs *args)
Definition: print.h:52
int fstprint_main(int argc, char **argv)
DECLARE_bool(acceptor)