1 // See www.openfst.org for extensive documentation on this weighted
2 // finite-state transducer library.
4 // Class to draw a binary FST by producing a text file in dot format, a helper
5 // class to fstdraw.cc.
7 #ifndef FST_SCRIPT_DRAW_IMPL_H_
8 #define FST_SCRIPT_DRAW_IMPL_H_
16 #include <fst/script/fst-class.h>
20 // Print a binary FST in GraphViz textual format (helper class for fstdraw.cc).
21 // WARNING: Stand-alone use not recommend.
25 using Label = typename Arc::Label;
26 using StateId = typename Arc::StateId;
27 using Weight = typename Arc::Weight;
29 FstDrawer(const Fst<Arc> &fst, const SymbolTable *isyms,
30 const SymbolTable *osyms, const SymbolTable *ssyms, bool accep,
31 const string &title, float width, float height, bool portrait,
32 bool vertical, float ranksep, float nodesep, int fontsize,
33 int precision, const string &float_format, bool show_weight_one)
38 accep_(accep && fst.Properties(kAcceptor, true)),
48 precision_(precision),
49 float_format_(float_format),
50 show_weight_one_(show_weight_one) {}
52 // Draw Fst to an output buffer (or stdout if buf = 0)
53 void Draw(std::ostream *strm, const string &dest) {
55 ostrm_->precision(precision_);
56 if (float_format_ == "e")
57 ostrm_->setf(std::ios_base::scientific, std::ios_base::floatfield);
58 if (float_format_ == "f")
59 ostrm_->setf(std::ios_base::fixed, std::ios_base::floatfield);
60 // O.w. defaults to "g" per standard lib.
62 StateId start = fst_.Start();
63 if (start == kNoStateId) return;
64 PrintString("digraph FST {\n");
66 PrintString("rankdir = BT;\n");
68 PrintString("rankdir = LR;\n");
70 PrintString("size = \"");
75 if (!dest_.empty()) PrintString("label = \"" + title_ + "\";\n");
76 PrintString("center = 1;\n");
78 PrintString("orientation = Portrait;\n");
80 PrintString("orientation = Landscape;\n");
82 PrintString("ranksep = \"");
85 PrintString("nodesep = \"");
88 // Initial state first.
90 for (StateIterator<Fst<Arc>> siter(fst_); !siter.Done(); siter.Next()) {
91 const auto s = siter.Value();
92 if (s != start) DrawState(s);
98 // Maximum line length in text file.
99 static const int kLineLen = 8096;
101 void PrintString(const string &str) const { *ostrm_ << str; }
103 // Escapes backslash and double quote if these occur in the string. Dot will
104 // not deal gracefully with these if they are not escaped.
105 inline void EscapeChars(const string &str, string *ns) const {
106 const char *c = str.c_str();
108 if (*c == '\\' || *c == '"') ns->push_back('\\');
114 void PrintId(StateId id, const SymbolTable *syms, const char *name) const {
116 auto symbol = syms->Find(id);
118 FSTERROR() << "FstDrawer: Integer " << id
119 << " is not mapped to any textual symbol"
120 << ", symbol table = " << syms->Name()
121 << ", destination = " << dest_;
125 EscapeChars(symbol, &nsymbol);
126 PrintString(nsymbol);
128 PrintString(std::to_string(id));
132 void PrintStateId(StateId s) const { PrintId(s, ssyms_, "state ID"); }
134 void PrintILabel(Label label) const {
135 PrintId(label, isyms_, "arc input label");
138 void PrintOLabel(Label label) const {
139 PrintId(label, osyms_, "arc output label");
143 void Print(T t) const { *ostrm_ << t; }
145 void DrawState(StateId s) const {
147 PrintString(" [label = \"");
149 const auto weight = fst_.Final(s);
150 if (weight != Weight::Zero()) {
151 if (show_weight_one_ || (weight != Weight::One())) {
155 PrintString("\", shape = doublecircle,");
157 PrintString("\", shape = circle,");
159 if (s == fst_.Start()) {
160 PrintString(" style = bold,");
162 PrintString(" style = solid,");
164 PrintString(" fontsize = ");
167 for (ArcIterator<Fst<Arc>> aiter(fst_, s); !aiter.Done(); aiter.Next()) {
168 const auto &arc = aiter.Value();
172 Print(arc.nextstate);
173 PrintString(" [label = \"");
174 PrintILabel(arc.ilabel);
177 PrintOLabel(arc.olabel);
179 if (show_weight_one_ || (arc.weight != Weight::One())) {
183 PrintString("\", fontsize = ");
189 const Fst<Arc> &fst_;
190 const SymbolTable *isyms_; // ilabel symbol table.
191 const SymbolTable *osyms_; // olabel symbol table.
192 const SymbolTable *ssyms_; // slabel symbol table.
193 bool accep_; // Print as acceptor when possible.
194 std::ostream *ostrm_; // Drawn FST destination.
195 string dest_; // Drawn FST destination name.
206 string float_format_;
207 bool show_weight_one_;
209 FstDrawer(const FstDrawer &) = delete;
210 FstDrawer &operator=(const FstDrawer &) = delete;
215 #endif // FST_SCRIPT_DRAW_IMPL_H_