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