FST  openfst-1.8.3
OpenFst Library
fstdraw-main.cc
Go to the documentation of this file.
1 // Copyright 2005-2024 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 // Draws a binary FSTs in the Graphviz dot text format.
19 
20 #include <cstring>
21 #include <iostream>
22 #include <memory>
23 #include <ostream>
24 #include <string>
25 
26 #include <fst/flags.h>
27 #include <fst/log.h>
28 #include <fstream>
29 #include <fst/symbol-table.h>
30 #include <fst/script/draw.h>
31 #include <fst/script/fst-class.h>
32 
33 DECLARE_bool(acceptor);
34 DECLARE_string(isymbols);
35 DECLARE_string(osymbols);
36 DECLARE_string(ssymbols);
37 DECLARE_bool(numeric);
38 DECLARE_int32(precision);
39 DECLARE_string(float_format);
40 DECLARE_bool(show_weight_one);
41 DECLARE_string(title);
42 DECLARE_bool(portrait);
43 DECLARE_bool(vertical);
44 DECLARE_int32(fontsize);
45 DECLARE_double(height);
46 DECLARE_double(width);
47 DECLARE_double(nodesep);
48 DECLARE_double(ranksep);
49 
50 int fstdraw_main(int argc, char **argv) {
51  namespace s = fst::script;
52  using fst::SymbolTable;
54 
55  std::string usage = "Prints out binary FSTs in dot text format.\n\n Usage: ";
56  usage += argv[0];
57  usage += " [binary.fst [text.dot]]\n";
58 
59  SET_FLAGS(usage.c_str(), &argc, &argv, true);
60  if (argc > 3) {
61  ShowUsage();
62  return 1;
63  }
64 
65  const std::string in_name =
66  argc > 1 && strcmp(argv[1], "-") != 0 ? argv[1] : "";
67 
68  std::unique_ptr<FstClass> fst(FstClass::Read(in_name));
69  if (!fst) return 1;
70 
71  const std::string out_name =
72  argc > 2 && strcmp(argv[2], "-") != 0 ? argv[2] : "";
73  std::ofstream fstrm;
74  if (!out_name.empty()) {
75  fstrm.open(out_name);
76  if (!fstrm) {
77  LOG(ERROR) << argv[0] << ": Open failed, file = " << out_name;
78  return 1;
79  }
80  }
81  std::ostream &ostrm = fstrm.is_open() ? fstrm : std::cout;
82 
83  std::unique_ptr<const SymbolTable> isyms;
84  if (!FST_FLAGS_isymbols.empty() && !FST_FLAGS_numeric) {
85  isyms.reset(
86  SymbolTable::ReadText(FST_FLAGS_isymbols,
87  FST_FLAGS_fst_field_separator));
88  if (!isyms) return 1;
89  }
90 
91  std::unique_ptr<const SymbolTable> osyms;
92  if (!FST_FLAGS_osymbols.empty() && !FST_FLAGS_numeric) {
93  osyms.reset(
94  SymbolTable::ReadText(FST_FLAGS_osymbols,
95  FST_FLAGS_fst_field_separator));
96  if (!osyms) return 1;
97  }
98 
99  std::unique_ptr<const SymbolTable> ssyms;
100  if (!FST_FLAGS_ssymbols.empty() && !FST_FLAGS_numeric) {
101  ssyms.reset(
102  SymbolTable::ReadText(FST_FLAGS_osymbols,
103  FST_FLAGS_fst_field_separator));
104  if (!ssyms) return 1;
105  }
106 
107  if (!isyms && !FST_FLAGS_numeric && fst->InputSymbols()) {
108  isyms.reset(fst->InputSymbols()->Copy());
109  }
110 
111  if (!osyms && !FST_FLAGS_numeric && fst->OutputSymbols()) {
112  osyms.reset(fst->OutputSymbols()->Copy());
113  }
114 
115  // "dest" is only used for the name of the file in error messages.
116  const std::string dest = out_name.empty() ? "stdout" : out_name;
117  s::Draw(*fst, isyms.get(), osyms.get(), ssyms.get(),
118  FST_FLAGS_acceptor, FST_FLAGS_title,
119  FST_FLAGS_width, FST_FLAGS_height,
120  FST_FLAGS_portrait, FST_FLAGS_vertical,
121  FST_FLAGS_ranksep, FST_FLAGS_nodesep,
122  FST_FLAGS_fontsize, FST_FLAGS_precision,
123  FST_FLAGS_float_format,
124  FST_FLAGS_show_weight_one, ostrm, dest);
125 
126  return 0;
127 }
void ShowUsage(bool long_usage=true)
Definition: flags.cc:138
DECLARE_string(isymbols)
DECLARE_int32(precision)
#define LOG(type)
Definition: log.h:53
int fstdraw_main(int argc, char **argv)
Definition: fstdraw-main.cc:50
#define SET_FLAGS(usage, argc, argv, rmflags)
Definition: flags.h:226
void Draw(FstDrawArgs *args)
Definition: draw.h:57
DECLARE_double(height)
DECLARE_bool(acceptor)