All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
extract-ctx.cc File Reference
#include <vector>
#include <sstream>
#include "base/kaldi-common.h"
#include "util/common-utils.h"
#include "matrix/kaldi-vector.h"
#include "tree/build-tree.h"
#include "tree/clusterable-classes.h"
#include "tree/context-dep.h"
#include "tree/build-tree-questions.h"
#include "fst/fstlib.h"
Include dependency graph for extract-ctx.cc:

Go to the source code of this file.

Functions

static std::string EventTypeToString (EventType &e, fst::SymbolTable *phones_symtab, bool addpos)
 
int main (int argc, char *argv[])
 

Function Documentation

static std::string EventTypeToString ( EventType e,
fst::SymbolTable *  phones_symtab,
bool  addpos 
)
static

Definition at line 38 of file extract-ctx.cc.

References rnnlm::i, and KALDI_WARN.

40  {
41  // make sure it's sorted so that the kPdfClass is the first element!
42  std::sort(e.begin(), e.end());
43 
44  // first plot the pdf-class
45  std::stringstream ss;
46  ss << e[0].second;
47  for (size_t i = 1; i < e.size(); ++i) {
48  ss << " ";
49  if (addpos)
50  ss << (i-1) << ":";
51 
52  if (phones_symtab == NULL)
53  ss << e[i].second;
54  else {
55  std::string phn =
56  phones_symtab->Find(static_cast<kaldi::int64>(e[i].second));
57  if (phn.empty()) {
58  // in case we can't resolve the symbol, plot the ID
59  KALDI_WARN << "No phone found for ID " << e[i].second;
60  ss << e[i].second;
61  } else
62  ss << phn;
63  }
64  }
65  return ss.str();
66 }
#define KALDI_WARN
Definition: kaldi-error.h:130
int main ( int  argc,
char *  argv[] 
)

Definition at line 68 of file extract-ctx.cc.

References kaldi::EventTypeToString(), ParseOptions::GetArg(), rnnlm::i, KALDI_ERR, KALDI_LOG, KALDI_WARN, kaldi::kPdfClass, ParseOptions::NumArgs(), ParseOptions::PrintUsage(), ParseOptions::Read(), kaldi::ReadBuildTreeStats(), kaldi::ReadKaldiObject(), ParseOptions::Register(), and ContextDependency::ToPdfMap().

68  {
69  try {
70  typedef kaldi::int32 int32;
71 
72  const char *usage =
73  "Given the tree stats and the resulting tree, output a mapping of phones\n"
74  "in context (and pdf-class) to the pdf-id. This can be used to link the\n"
75  "acoustic model parameters to their phonetic meaning. Outputs lines such as\n"
76  " \"<pdf-id> <pdf-class> <left> <center> <right>\" in case of tri-phones\n"
77  "e.g.: \n"
78  " extract-ctx treeacc tree\n"
79  " extract-ctx --mono 48 tree\n";
80 
81  ParseOptions po(usage);
82 
83  std::string fsymboltab;
84  bool addpos = false;
85  bool mono = false;
86  std::string silphones = "1,2,3";
87  int32 silpdfclasses = 5;
88  int32 nonsilpdfclasses = 3;
89 
90  po.Register("mono", &mono,
91  "Assume mono-phone tree; instead of tree stats, specify highest id");
92  po.Register("sil-phones", &silphones,
93  "[only for --mono] Comma separated list of silence phones");
94  po.Register("sil-pdf-classes", &silpdfclasses,
95  "[only for --mono] Number of pdf-classes for silence phones");
96  po.Register("non-sil-pdf-classes", &nonsilpdfclasses,
97  "[only for --mono] Number of pdf-classes for non-silence phones");
98  po.Register("symbol-table", &fsymboltab,
99  "Specify phone sybol table for readable output");
100  po.Register("add-position-indicators", &addpos,
101  "Add position indicators for phonemes");
102  po.Read(argc, argv);
103 
104  if (po.NumArgs() != 2) {
105  po.PrintUsage();
106  exit(1);
107  }
108 
109  // read symtab if available
110  fst::SymbolTable *phones_symtab = NULL;
111  if (fsymboltab.length() > 0) {
112  KALDI_LOG << "Reading symbol table from " << fsymboltab;
113  std::ifstream is(fsymboltab.c_str());
114  phones_symtab = ::fst::SymbolTable::ReadText(is, fsymboltab);
115  if (!phones_symtab)
116  KALDI_ERR << "Could not read phones symbol table file "<< fsymboltab;
117  }
118 
119  // read the tree, get all the leaves
120  ContextDependency ctx_dep;
121  ReadKaldiObject(po.GetArg(2), &ctx_dep);
122  const EventMap &map = ctx_dep.ToPdfMap();
123 
124  // here we have to do different things for mono and tri+ trees
125  if (mono) {
126  // A mono-phone tree is not actually a real tree. We test for EventTypes
127  // that have the central phone and the possible pdf-classes
128 
129  int32 maxs = atoi(po.GetArg(1).c_str());
130  if (phones_symtab != NULL) {
131  size_t ns = phones_symtab->NumSymbols();
132  if (maxs != (int32) (ns-1)) {
133  KALDI_WARN << "specified highest symbol (" << maxs
134  << ") not equal to size of symtab (" << (ns-1) << "), adjusting ";
135  maxs = (ns-1);
136  }
137  }
138 
139  // parse silphones
140  std::set<int32> silset;
141 
142  std::string::size_type i1 = 0, i2;
143  do {
144  i2 = silphones.find(',', i1);
145  silset.insert(atoi(silphones.substr(i1, i2 - i1).c_str()));
146  KALDI_LOG << "silphone: " << silphones.substr(i1, i2 - i1);
147  if (i2 == std::string::npos)
148  break;
149  i1 = i2 + 1;
150  } while (true);
151 
152 
153  // now query each phone (ignore <eps> which is 0)
154  for (int32 p = 1; p <= maxs; ++p) {
155  int32 mpdf = (silset.find(p) == silset.end() ?
156  nonsilpdfclasses :
157  silpdfclasses);
158 
159  for (int i = 0; i < mpdf; ++i) {
160  EventType et;
161  et.push_back(std::pair<EventKeyType, EventValueType>(kPdfClass, i));
162  et.push_back(std::pair<EventKeyType, EventValueType>(0, p));
163 
164  EventAnswerType ans;
165  if (map.Map(et, &ans)) {
166  std::cout << ans << " "
167  << EventTypeToString(et, phones_symtab, addpos)
168  << std::endl;
169  } else {
170  KALDI_WARN << "Unable to get pdf-id for stats "
171  << EventTypeToString(et, phones_symtab, addpos);
172  }
173 
174  }
175  }
176 
177  } else {
178  // for tri+ trees, read the tree stats; this gives us basically all
179  // phones-in-context that may be linked to an individual model
180  // (in practice, many of them will be shared, but we plot them anyways)
181 
182  // build-tree-questions.h:typedef std::vector<std::pair<EventType, Clusterable*> > BuildTreeStatsType
183  BuildTreeStatsType stats;
184  {
185  bool binary_in;
186  GaussClusterable gc; // dummy needed to provide type.
187  Input ki(po.GetArg(1), &binary_in);
188  ReadBuildTreeStats(ki.Stream(), binary_in, gc, &stats);
189  }
190  KALDI_LOG << "Number of separate statistics is " << stats.size();
191 
192  // typedef std::vector<std::pair<EventKeyType,EventValueType> > EventType
193 
194  // now, for each tree stats element, query the tree to get the pdf-id
195  for (size_t i = 0; i < stats.size(); ++i) {
196  EventAnswerType ans;
197  if (map.Map(stats[i].first, &ans)) {
198  std::cout << ans << " "
199  << EventTypeToString(stats[i].first, phones_symtab, addpos)
200  << std::endl;
201  } else {
202  KALDI_WARN << "Unable to get pdf-id for stats "
203  << EventTypeToString(stats[i].first, phones_symtab, addpos);
204  }
205  }
206  }
207 
208  return 0;
209  } catch(const std::exception &e) {
210  std::cerr << e.what();
211  return -1;
212  }
213 }
const EventMap & ToPdfMap() const
Definition: context-dep.h:98
static std::string EventTypeToString(EventType &e, fst::SymbolTable *phones_symtab, bool addpos)
Definition: extract-ctx.cc:38
void ReadKaldiObject(const std::string &filename, Matrix< float > *m)
Definition: kaldi-io.cc:818
void ReadBuildTreeStats(std::istream &is, bool binary, const Clusterable &example, BuildTreeStatsType *stats)
Reads BuildTreeStats object.
static const EventKeyType kPdfClass
Definition: context-dep.h:39
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
#define KALDI_ERR
Definition: kaldi-error.h:127
#define KALDI_WARN
Definition: kaldi-error.h:130
A class that is capable of representing a generic mapping from EventType (which is a vector of (key...
Definition: event-map.h:86
int32 EventAnswerType
As far as the event-map code itself is concerned, things of type EventAnswerType may take any value e...
Definition: event-map.h:56
std::vector< std::pair< EventType, Clusterable * > > BuildTreeStatsType
GaussClusterable wraps Gaussian statistics in a form accessible to generic clustering algorithms...
#define KALDI_LOG
Definition: kaldi-error.h:133