FST  openfst-1.7.3
OpenFst Library
fstdraw-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 // Draws a binary FSTs in the Graphviz dot text format.
5 
6 #include <cstring>
7 #include <fstream>
8 #include <memory>
9 #include <ostream>
10 #include <string>
11 
12 #include <fst/flags.h>
13 #include <fst/log.h>
14 #include <fst/script/draw.h>
15 
16 DECLARE_bool(acceptor);
17 DECLARE_string(isymbols);
18 DECLARE_string(osymbols);
19 DECLARE_string(ssymbols);
20 DECLARE_bool(numeric);
21 DECLARE_int32(precision);
22 DECLARE_string(float_format);
23 DECLARE_bool(show_weight_one);
24 DECLARE_string(title);
25 DECLARE_bool(portrait);
26 DECLARE_bool(vertical);
27 DECLARE_int32(fontsize);
28 DECLARE_double(height);
29 DECLARE_double(width);
30 DECLARE_double(nodesep);
31 DECLARE_double(ranksep);
32 DECLARE_bool(allow_negative_labels);
33 
34 int fstdraw_main(int argc, char **argv) {
35  namespace s = fst::script;
37  using fst::SymbolTable;
39 
40  std::string usage = "Prints out binary FSTs in dot text format.\n\n Usage: ";
41  usage += argv[0];
42  usage += " [binary.fst [text.dot]]\n";
43 
44  std::set_new_handler(FailedNewHandler);
45  SET_FLAGS(usage.c_str(), &argc, &argv, true);
46  if (argc > 3) {
47  ShowUsage();
48  return 1;
49  }
50 
51  const std::string in_name =
52  argc > 1 && strcmp(argv[1], "-") != 0 ? argv[1] : "";
53 
54  std::unique_ptr<FstClass> fst(FstClass::Read(in_name));
55  if (!fst) return 1;
56 
57  const std::string out_name =
58  argc > 2 && strcmp(argv[2], "-") != 0 ? argv[2] : "";
59  std::ofstream fstrm;
60  if (!out_name.empty()) {
61  fstrm.open(out_name);
62  if (!fstrm) {
63  LOG(ERROR) << argv[0] << ": Open failed, file = " << out_name;
64  return 1;
65  }
66  }
67  std::ostream &ostrm = fstrm.is_open() ? fstrm : std::cout;
68 
69  const SymbolTableTextOptions opts(FLAGS_allow_negative_labels);
70 
71  std::unique_ptr<const SymbolTable> isyms;
72  if (!FLAGS_isymbols.empty() && !FLAGS_numeric) {
73  isyms.reset(SymbolTable::ReadText(FLAGS_isymbols, opts));
74  if (!isyms) return 1;
75  }
76 
77  std::unique_ptr<const SymbolTable> osyms;
78  if (!FLAGS_osymbols.empty() && !FLAGS_numeric) {
79  osyms.reset(SymbolTable::ReadText(FLAGS_osymbols, opts));
80  if (!osyms) return 1;
81  }
82 
83  std::unique_ptr<const SymbolTable> ssyms;
84  if (!FLAGS_ssymbols.empty() && !FLAGS_numeric) {
85  ssyms.reset(SymbolTable::ReadText(FLAGS_ssymbols));
86  if (!ssyms) return 1;
87  }
88 
89  if (!isyms && !FLAGS_numeric && fst->InputSymbols()) {
90  isyms.reset(fst->InputSymbols()->Copy());
91  }
92 
93  if (!osyms && !FLAGS_numeric && fst->OutputSymbols()) {
94  osyms.reset(fst->OutputSymbols()->Copy());
95  }
96 
97  // "dest" is only used for the name of the file in error messages.
98  const std::string dest = out_name.empty() ? "stdout" : out_name;
99  s::DrawFst(*fst, isyms.get(), osyms.get(), ssyms.get(), FLAGS_acceptor,
100  FLAGS_title, FLAGS_width, FLAGS_height, FLAGS_portrait,
101  FLAGS_vertical, FLAGS_ranksep, FLAGS_nodesep, FLAGS_fontsize,
102  FLAGS_precision, FLAGS_float_format, FLAGS_show_weight_one,
103  &ostrm, dest);
104 
105  return 0;
106 }
void ShowUsage(bool long_usage=true)
Definition: flags.cc:121
DECLARE_string(isymbols)
DECLARE_int32(precision)
#define LOG(type)
Definition: log.h:46
int fstdraw_main(int argc, char **argv)
Definition: fstdraw-main.cc:34
void FailedNewHandler()
Definition: compat.cc:20
#define SET_FLAGS(usage, argc, argv, rmflags)
Definition: flags.h:209
void DrawFst(FstDrawerArgs *args)
Definition: draw.h:67
DECLARE_double(height)
DECLARE_bool(acceptor)