1f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson// fstdraw.cc
2f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson
3f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson// Licensed under the Apache License, Version 2.0 (the "License");
4f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson// you may not use this file except in compliance with the License.
5f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson// You may obtain a copy of the License at
6f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson//
7f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson//     http://www.apache.org/licenses/LICENSE-2.0
8f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson//
9f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson// Unless required by applicable law or agreed to in writing, software
10f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson// distributed under the License is distributed on an "AS IS" BASIS,
11f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson// See the License for the specific language governing permissions and
13f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson// limitations under the License.
14f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson//
15f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson// Copyright 2005-2010 Google, Inc.
16f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson// Author: allauzen@google.com (Cyril Allauzen)
17f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson// Modified: jpr@google.com (Jake Ratkiewicz) to use FstClass
18f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson//
19f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson// \file
20f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson// Draws a binary FSTs in the Graphviz dot text format
21f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson
22f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson#include <fst/script/draw.h>
23f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson
24f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian HodsonDEFINE_bool(acceptor, false, "Input in acceptor format");
25f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian HodsonDEFINE_string(isymbols, "", "Input label symbol table");
26f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian HodsonDEFINE_string(osymbols, "", "Output label symbol table");
27f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian HodsonDEFINE_string(ssymbols, "", "State label symbol table");
28f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian HodsonDEFINE_bool(numeric, false, "Print numeric labels");
29f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian HodsonDEFINE_int32(precision, 5, "Set precision (number of char/float)");
30f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian HodsonDEFINE_bool(show_weight_one, false,
31f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson            "Print/draw arc weights and final weights equal to Weight::One()");
32f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian HodsonDEFINE_string(title, "", "Set figure title");
33f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian HodsonDEFINE_bool(portrait, false, "Portrait mode (def: landscape)");
34f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian HodsonDEFINE_bool(vertical, false, "Draw bottom-to-top instead of left-to-right");
35f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian HodsonDEFINE_int32(fontsize, 14, "Set fontsize");
36f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian HodsonDEFINE_double(height, 11, "Set height");
37f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian HodsonDEFINE_double(width, 8.5, "Set width");
38f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian HodsonDEFINE_double(nodesep, 0.25,
39f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson              "Set minimum separation between nodes (see dot documentation)");
40f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian HodsonDEFINE_double(ranksep, 0.40,
41f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson              "Set minimum separation between ranks (see dot documentation)");
42f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian HodsonDEFINE_bool(allow_negative_labels, false,
43f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson            "Allow negative labels (not recommended; may cause conflicts)");
44f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson
45f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodsonint main(int argc, char **argv) {
46f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson  namespace s = fst::script;
47f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson  using fst::ostream;
48f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson  using fst::SymbolTable;
49f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson
50f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson  string usage = "Prints out binary FSTs in dot text format.\n\n  Usage: ";
51f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson  usage += argv[0];
52dfd8b8327b93660601d016cdc6f29f433b45a8d8Alexander Gutkin  usage += " [binary.fst [text.dot]]\n";
53f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson
54f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson  std::set_new_handler(FailedNewHandler);
55dfd8b8327b93660601d016cdc6f29f433b45a8d8Alexander Gutkin  SET_FLAGS(usage.c_str(), &argc, &argv, true);
56f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson  if (argc > 3) {
57f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson    ShowUsage();
58f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson    return 1;
59f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson  }
60f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson
61f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson  string in_name = (argc > 1 && strcmp(argv[1], "-") != 0) ? argv[1] : "";
62f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson
63f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson  s::FstClass *fst = s::FstClass::Read(in_name);
64f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson  if (!fst) return 1;
65f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson
66dfd8b8327b93660601d016cdc6f29f433b45a8d8Alexander Gutkin  ostream *ostrm = &cout;
67f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson  string dest = "stdout";
68f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson  if (argc == 3) {
69f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson    dest = argv[2];
70f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson    ostrm = new fst::ofstream(argv[2]);
71f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson    if (!*ostrm) {
72f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson      LOG(ERROR) << argv[0] << ": Open failed, file = " << argv[2];
73f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson      return 1;
74f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson    }
75f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson  }
76f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson  ostrm->precision(FLAGS_precision);
77f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson
78f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson  const SymbolTable *isyms = 0, *osyms = 0, *ssyms = 0;
79f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson
80dfd8b8327b93660601d016cdc6f29f433b45a8d8Alexander Gutkin  fst::SymbolTableTextOptions opts;
81dfd8b8327b93660601d016cdc6f29f433b45a8d8Alexander Gutkin  opts.allow_negative = FLAGS_allow_negative_labels;
82dfd8b8327b93660601d016cdc6f29f433b45a8d8Alexander Gutkin
83f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson  if (!FLAGS_isymbols.empty() && !FLAGS_numeric) {
84dfd8b8327b93660601d016cdc6f29f433b45a8d8Alexander Gutkin    isyms = SymbolTable::ReadText(FLAGS_isymbols, opts);
85f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson    if (!isyms) exit(1);
86f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson  }
87f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson
88f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson  if (!FLAGS_osymbols.empty() && !FLAGS_numeric) {
89dfd8b8327b93660601d016cdc6f29f433b45a8d8Alexander Gutkin    osyms = SymbolTable::ReadText(FLAGS_osymbols, opts);
90f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson    if (!osyms) exit(1);
91f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson  }
92f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson
93f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson  if (!FLAGS_ssymbols.empty() && !FLAGS_numeric) {
94f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson    ssyms = SymbolTable::ReadText(FLAGS_ssymbols);
95f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson    if (!ssyms) exit(1);
96f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson  }
97f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson
98f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson  if (!isyms && !FLAGS_numeric)
99f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson    isyms = fst->InputSymbols();
100f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson  if (!osyms && !FLAGS_numeric)
101f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson    osyms = fst->OutputSymbols();
102f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson
103f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson  s::DrawFst(*fst, isyms, osyms, ssyms,  FLAGS_acceptor,
104f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson             FLAGS_title, FLAGS_width, FLAGS_height,
105f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson             FLAGS_portrait, FLAGS_vertical,
106f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson             FLAGS_ranksep, FLAGS_nodesep,
107f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson             FLAGS_fontsize, FLAGS_precision,
108f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson             FLAGS_show_weight_one, ostrm, dest);
109f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson
110dfd8b8327b93660601d016cdc6f29f433b45a8d8Alexander Gutkin  if (ostrm != &cout)
111f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson    delete ostrm;
112f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson  return 0;
113f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson}
114