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

◆ main()

int main ( int  argc,
char **  argv 
)

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

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

60  {
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
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
#define KALDI_ERR
Definition: kaldi-error.h:147
void Render(const EventType *query)

◆ MakeEvent()

void MakeEvent ( std::string &  qry,
fst::SymbolTable *  phone_syms,
kaldi::EventType **  query 
)

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

References KALDI_ERR, and kaldi::kPdfClass.

Referenced by main().

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 }
This code computes Goodness of Pronunciation (GOP) and extracts phone-level pronunciation feature for...
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:147
int32 EventValueType
Given current code, things of type EventValueType should generally be nonnegative and in a reasonably...
Definition: event-map.h:51