All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
draw-tree.cc File Reference
Include dependency graph for draw-tree.cc:

Go to the source code of this file.

Functions

void MakeEvent (std::string &qry, fst::SymbolTable *phone_syms, kaldi::EventType **query)
 
int main (int argc, char **argv)
 

Function Documentation

int main ( int  argc,
char **  argv 
)

Definition at line 61 of file draw-tree.cc.

References ParseOptions::GetArg(), KALDI_ERR, MakeEvent(), ParseOptions::NumArgs(), ParseOptions::PrintUsage(), ParseOptions::Read(), ParseOptions::Register(), TreeRenderer::Render(), and Input::Stream().

61  {
62  using namespace kaldi;
63  try {
64  std::string qry;
65  bool use_tooltips = false;
66  bool gen_html = false;
67 
68  const char *usage =
69  "Outputs a decision tree description in GraphViz format\n"
70  "Usage: draw-tree [options] <phone-symbols> <tree>\n"
71  "e.g.: draw-tree phones.txt tree | dot -Gsize=8,10.5 -Tps | ps2pdf - tree.pdf\n";
72 
73  ParseOptions po(usage);
74  po.Register("query", &qry,
75  "a query to trace through the tree"
76  "(format: pdf-class/ctx-phone1/.../ctx-phoneN)");
77  po.Register("use-tooltips", &use_tooltips, "use tooltips instead of labels");
78  po.Register("gen-html", &gen_html, "generates HTML boilerplate(useful with SVG)");
79  po.Read(argc, argv);
80  if (gen_html) {
81  std::cout << "<html>\n<head><title>Decision Tree</tree></head>\n"
82  << "<body><object data=\"tree.svg\" type=\"image/svg+xml\"/>"
83  << "</body>\n</html>\n";
84  return 0;
85  }
86  if (po.NumArgs() != 2) {
87  po.PrintUsage();
88  return -1;
89  }
90  std::string phnfile = po.GetArg(1);
91  std::string treefile = po.GetArg(2);
92 
93  fst::SymbolTable *phones_symtab = NULL;
94  {
95  std::ifstream is(phnfile.c_str());
96  phones_symtab = ::fst::SymbolTable::ReadText(is, phnfile);
97  if (!phones_symtab)
98  KALDI_ERR << "Could not read phones symbol table file "<< phnfile;
99  }
100 
101  EventType *query = NULL;
102  if (!qry.empty())
103  MakeEvent(qry, phones_symtab, &query);
104 
105  TreeRenderer *renderer = NULL;
106  {
107  bool binary;
108  Input ki(treefile, &binary);
109  renderer = new TreeRenderer(ki.Stream(), binary, std::cout,
110  *phones_symtab, use_tooltips);
111  renderer->Render(query);
112  }
113 
114  delete renderer;
115  delete query;
116  } catch (const std::exception &e) {
117  std::cerr << e.what();
118  return -1;
119  }
120 }
Relabels neural network egs with the read pdf-id alignments.
Definition: chain.dox:20
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:22
#define KALDI_ERR
Definition: kaldi-error.h:127
void Render(const EventType *query)
void MakeEvent ( std::string &  qry,
fst::SymbolTable *  phone_syms,
kaldi::EventType **  query 
)

Definition at line 22 of file draw-tree.cc.

References KALDI_ERR, and kaldi::kPdfClass.

Referenced by main().

24 {
25  using namespace kaldi;
26 
27  EventType *query_event = new EventType();
28  size_t found, old_found = 0;
29  EventKeyType key = kPdfClass; // this code in fact relies on kPdfClass = -1
30  while ((found = qry.find('/', old_found)) != std::string::npos) {
31  std::string valstr = qry.substr(old_found, found - old_found);
32  EventValueType value;
33  if (key == kPdfClass) {
34  value = static_cast<EventValueType>(atoi(valstr.c_str()));
35  if (value < 0) { // not valid pdf-class
36  KALDI_ERR << "Bad query: invalid pdf-class ("
37  << valstr << ')' << std::endl << std::endl;
38  }
39  }
40  else {
41  value = static_cast<EventValueType>(phone_syms->Find(valstr.c_str()));
42  if (value == fst::SymbolTable::kNoSymbol) {
43  KALDI_ERR << "Bad query: invalid symbol ("
44  << valstr << ')' << std::endl << std::endl;
45  }
46  }
47  query_event->push_back(std::make_pair(key++, value));
48  old_found = found + 1;
49  }
50  std::string valstr = qry.substr(old_found);
51  EventValueType value = static_cast<EventValueType>(phone_syms->Find(valstr.c_str()));
52  if (value == fst::SymbolTable::kNoSymbol) {
53  KALDI_ERR << "Bad query: invalid symbol ("
54  << valstr << ')' << std::endl << std::endl;
55  }
56  query_event->push_back(std::make_pair(key, value));
57 
58  *query = query_event;
59 }
Relabels neural network egs with the read pdf-id alignments.
Definition: chain.dox:20
static const EventKeyType kPdfClass
Definition: context-dep.h:39
std::vector< std::pair< EventKeyType, EventValueType > > EventType
Definition: event-map.h:58
int32 EventKeyType
Things of type EventKeyType can take any value.
Definition: event-map.h:45
#define KALDI_ERR
Definition: kaldi-error.h:127
int32 EventValueType
Given current code, things of type EventValueType should generally be nonnegative and in a reasonably...
Definition: event-map.h:51