1 // fstdraw.cc
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 // Copyright 2005-2010 Google, Inc.
16 // Author: allauzen@google.com (Cyril Allauzen)
17 // Modified: jpr@google.com (Jake Ratkiewicz) to use FstClass
18 //
19 // \file
20 // Draws a binary FSTs in the Graphviz dot text format
21
22 #include <fst/script/draw.h>
23
24 DEFINE_bool(acceptor, false, "Input in acceptor format");
25 DEFINE_string(isymbols, "", "Input label symbol table");
26 DEFINE_string(osymbols, "", "Output label symbol table");
27 DEFINE_string(ssymbols, "", "State label symbol table");
28 DEFINE_bool(numeric, false, "Print numeric labels");
29 DEFINE_string(save_isymbols, "", "Save input symbol table to file");
30 DEFINE_string(save_osymbols, "", "Save output symbol table to file");
31 DEFINE_int32(precision, 5, "Set precision (number of char/float)");
32 DEFINE_bool(show_weight_one, false,
33 "Print/draw arc weights and final weights equal to Weight::One()");
34 DEFINE_string(title, "", "Set figure title");
35 DEFINE_bool(portrait, false, "Portrait mode (def: landscape)");
36 DEFINE_bool(vertical, false, "Draw bottom-to-top instead of left-to-right");
37 DEFINE_int32(fontsize, 14, "Set fontsize");
38 DEFINE_double(height, 11, "Set height");
39 DEFINE_double(width, 8.5, "Set width");
40 DEFINE_double(nodesep, 0.25,
41 "Set minimum separation between nodes (see dot documentation)");
42 DEFINE_double(ranksep, 0.40,
43 "Set minimum separation between ranks (see dot documentation)");
44 DEFINE_bool(allow_negative_labels, false,
45 "Allow negative labels (not recommended; may cause conflicts)");
46
main(int argc,char ** argv)47 int main(int argc, char **argv) {
48 namespace s = fst::script;
49 using fst::ostream;
50 using fst::SymbolTable;
51
52 string usage = "Prints out binary FSTs in dot text format.\n\n Usage: ";
53 usage += argv[0];
54 usage += " [binary.fst [text.fst]]\n";
55
56 std::set_new_handler(FailedNewHandler);
57 SetFlags(usage.c_str(), &argc, &argv, true);
58 if (argc > 3) {
59 ShowUsage();
60 return 1;
61 }
62
63 string in_name = (argc > 1 && strcmp(argv[1], "-") != 0) ? argv[1] : "";
64
65 s::FstClass *fst = s::FstClass::Read(in_name);
66 if (!fst) return 1;
67
68 ostream *ostrm = &std::cout;
69 string dest = "stdout";
70 if (argc == 3) {
71 dest = argv[2];
72 ostrm = new fst::ofstream(argv[2]);
73 if (!*ostrm) {
74 LOG(ERROR) << argv[0] << ": Open failed, file = " << argv[2];
75 return 1;
76 }
77 }
78 ostrm->precision(FLAGS_precision);
79
80 const SymbolTable *isyms = 0, *osyms = 0, *ssyms = 0;
81
82 if (!FLAGS_isymbols.empty() && !FLAGS_numeric) {
83 isyms = SymbolTable::ReadText(FLAGS_isymbols, FLAGS_allow_negative_labels);
84 if (!isyms) exit(1);
85 }
86
87 if (!FLAGS_osymbols.empty() && !FLAGS_numeric) {
88 osyms = SymbolTable::ReadText(FLAGS_osymbols, FLAGS_allow_negative_labels);
89 if (!osyms) exit(1);
90 }
91
92 if (!FLAGS_ssymbols.empty() && !FLAGS_numeric) {
93 ssyms = SymbolTable::ReadText(FLAGS_ssymbols);
94 if (!ssyms) exit(1);
95 }
96
97 if (!isyms && !FLAGS_numeric)
98 isyms = fst->InputSymbols();
99 if (!osyms && !FLAGS_numeric)
100 osyms = fst->OutputSymbols();
101
102 s::DrawFst(*fst, isyms, osyms, ssyms, FLAGS_acceptor,
103 FLAGS_title, FLAGS_width, FLAGS_height,
104 FLAGS_portrait, FLAGS_vertical,
105 FLAGS_ranksep, FLAGS_nodesep,
106 FLAGS_fontsize, FLAGS_precision,
107 FLAGS_show_weight_one, ostrm, dest);
108
109 if (isyms && !FLAGS_save_isymbols.empty())
110 isyms->WriteText(FLAGS_save_isymbols);
111
112 if (osyms && !FLAGS_save_osymbols.empty())
113 osyms->WriteText(FLAGS_save_osymbols);
114
115 if (ostrm != &std::cout)
116 delete ostrm;
117 return 0;
118 }
119