FST  openfst-1.7.2
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 
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/draw.h>
16 
17 DECLARE_bool(acceptor);
18 DECLARE_string(isymbols);
19 DECLARE_string(osymbols);
20 DECLARE_string(ssymbols);
21 DECLARE_bool(numeric);
22 DECLARE_int32(precision);
23 DECLARE_string(float_format);
24 DECLARE_bool(show_weight_one);
25 DECLARE_string(title);
26 DECLARE_bool(portrait);
27 DECLARE_bool(vertical);
28 DECLARE_int32(fontsize);
29 DECLARE_double(height);
30 DECLARE_double(width);
31 DECLARE_double(nodesep);
32 DECLARE_double(ranksep);
33 DECLARE_bool(allow_negative_labels);
34 
35 int fstdraw_main(int argc, char **argv) {
36  namespace s = fst::script;
38  using fst::SymbolTable;
40 
41  string usage = "Prints out binary FSTs in dot text format.\n\n Usage: ";
42  usage += argv[0];
43  usage += " [binary.fst [text.dot]]\n";
44 
45  std::set_new_handler(FailedNewHandler);
46  SET_FLAGS(usage.c_str(), &argc, &argv, true);
47  if (argc > 3) {
48  ShowUsage();
49  return 1;
50  }
51 
52  const string in_name = (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  string dest = "stdout";
58  std::ofstream fstrm;
59  if (argc == 3) {
60  fstrm.open(argv[2]);
61  if (!fstrm) {
62  LOG(ERROR) << argv[0] << ": Open failed, file = " << argv[2];
63  return 1;
64  }
65  dest = argv[2];
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  s::DrawFst(*fst, isyms.get(), osyms.get(), ssyms.get(), FLAGS_acceptor,
98  FLAGS_title, FLAGS_width, FLAGS_height, FLAGS_portrait,
99  FLAGS_vertical, FLAGS_ranksep, FLAGS_nodesep, FLAGS_fontsize,
100  FLAGS_precision, FLAGS_float_format, FLAGS_show_weight_one,
101  &ostrm, dest);
102 
103  return 0;
104 }
void ShowUsage(bool long_usage=true)
Definition: flags.cc:124
DECLARE_string(isymbols)
DECLARE_int32(precision)
#define LOG(type)
Definition: log.h:48
int fstdraw_main(int argc, char **argv)
Definition: fstdraw-main.cc:35
void FailedNewHandler()
Definition: compat.cc:25
#define SET_FLAGS(usage, argc, argv, rmflags)
Definition: flags.h:214
void DrawFst(FstDrawerArgs *args)
Definition: draw.h:66
DECLARE_double(height)
DECLARE_bool(acceptor)