draw-tree.cc
Go to the documentation of this file.
1 // bin/draw-tree.cc
2 
3 // Copyright 2012 Vassil Panayotov
4 
5 // See ../../COPYING for clarification regarding multiple authors
6 //
7 // Licensed under the Apache License, Version 2.0 (the "License");
8 // you may not use this file except in compliance with the License.
9 // You may obtain a copy of the License at
10 //
11 // http://www.apache.org/licenses/LICENSE-2.0
12 //
13 // THIS CODE IS PROVIDED *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14 // KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED
15 // WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
16 // MERCHANTABLITY OR NON-INFRINGEMENT.
17 // See the Apache 2 License for the specific language governing permissions and
18 // limitations under the License.
19 
20 #include "tree/tree-renderer.h"
21 #include "tree/context-dep.h"
22 
23 void MakeEvent(std::string &qry, fst::SymbolTable *phone_syms,
24  kaldi::EventType **query)
25 {
26  using namespace kaldi;
27 
28  EventType *query_event = new EventType();
29  size_t found, old_found = 0;
30  EventKeyType key = kPdfClass; // this code in fact relies on kPdfClass = -1
31  while ((found = qry.find('/', old_found)) != std::string::npos) {
32  std::string valstr = qry.substr(old_found, found - old_found);
33  EventValueType value;
34  if (key == kPdfClass) {
35  value = static_cast<EventValueType>(atoi(valstr.c_str()));
36  if (value < 0) { // not valid pdf-class
37  KALDI_ERR << "Bad query: invalid pdf-class (" << valstr << ')';
38  }
39  }
40  else {
41  value = static_cast<EventValueType>(phone_syms->Find(valstr.c_str()));
42  if (value == -1) { // fst::kNoSymbol
43  KALDI_ERR << "Bad query: invalid symbol (" << valstr << ')';
44  }
45  }
46  query_event->push_back(std::make_pair(key++, value));
47  old_found = found + 1;
48  }
49  std::string valstr = qry.substr(old_found);
50  EventValueType value =
51  static_cast<EventValueType>(phone_syms->Find(valstr.c_str()));
52  if (value == -1) { // fst::kNoSymbol
53  KALDI_ERR << "Bad query: invalid symbol (" << valstr << ')';
54  }
55  query_event->push_back(std::make_pair(key, value));
56 
57  *query = query_event;
58 }
59 
60 int main(int argc, char **argv) {
61  using namespace kaldi;
62  try {
63  std::string qry;
64  bool use_tooltips = false;
65  bool gen_html = false;
66 
67  const char *usage =
68  "Outputs a decision tree description in GraphViz format\n"
69  "Usage: draw-tree [options] <phone-symbols> <tree>\n"
70  "e.g.: draw-tree phones.txt tree | dot -Gsize=8,10.5 -Tps | ps2pdf - tree.pdf\n";
71 
72  ParseOptions po(usage);
73  po.Register("query", &qry,
74  "a query to trace through the tree"
75  "(format: pdf-class/ctx-phone1/.../ctx-phoneN)");
76  po.Register("use-tooltips", &use_tooltips, "use tooltips instead of labels");
77  po.Register("gen-html", &gen_html, "generates HTML boilerplate(useful with SVG)");
78  po.Read(argc, argv);
79  if (gen_html) {
80  std::cout << "<html>\n<head><title>Decision Tree</tree></head>\n"
81  << "<body><object data=\"tree.svg\" type=\"image/svg+xml\"/>"
82  << "</body>\n</html>\n";
83  return 0;
84  }
85  if (po.NumArgs() != 2) {
86  po.PrintUsage();
87  return -1;
88  }
89  std::string phnfile = po.GetArg(1);
90  std::string treefile = po.GetArg(2);
91 
92  fst::SymbolTable *phones_symtab = NULL;
93  {
94  std::ifstream is(phnfile.c_str());
95  phones_symtab = ::fst::SymbolTable::ReadText(is, phnfile);
96  if (!phones_symtab)
97  KALDI_ERR << "Could not read phones symbol table file "<< phnfile;
98  }
99 
100  EventType *query = NULL;
101  if (!qry.empty())
102  MakeEvent(qry, phones_symtab, &query);
103 
104  TreeRenderer *renderer = NULL;
105  {
106  bool binary;
107  Input ki(treefile, &binary);
108  renderer = new TreeRenderer(ki.Stream(), binary, std::cout,
109  *phones_symtab, use_tooltips);
110  renderer->Render(query);
111  }
112 
113  delete renderer;
114  delete query;
115  } catch (const std::exception &e) {
116  std::cerr << e.what();
117  return -1;
118  }
119 }
This code computes Goodness of Pronunciation (GOP) and extracts phone-level pronunciation feature for...
Definition: chain.dox:20
void PrintUsage(bool print_command_line=false)
Prints the usage documentation [provided in the constructor].
void Register(const std::string &name, bool *ptr, const std::string &doc)
static const EventKeyType kPdfClass
Definition: context-dep.h:39
int main(int argc, char **argv)
Definition: draw-tree.cc:60
std::istream & Stream()
Definition: kaldi-io.cc:826
std::vector< std::pair< EventKeyType, EventValueType > > EventType
Definition: event-map.h:58
The class ParseOptions is for parsing command-line options; see Parsing command-line options for more...
Definition: parse-options.h:36
void MakeEvent(std::string &qry, fst::SymbolTable *phone_syms, kaldi::EventType **query)
Definition: draw-tree.cc:23
int32 EventKeyType
Things of type EventKeyType can take any value.
Definition: event-map.h:45
int Read(int argc, const char *const *argv)
Parses the command line options and fills the ParseOptions-registered variables.
#define KALDI_ERR
Definition: kaldi-error.h:147
std::string GetArg(int param) const
Returns one of the positional parameters; 1-based indexing for argc/argv compatibility.
int NumArgs() const
Number of positional parameters (c.f. argc-1).
int32 EventValueType
Given current code, things of type EventValueType should generally be nonnegative and in a reasonably...
Definition: event-map.h:51
void Render(const EventType *query)