compile-questions.cc
Go to the documentation of this file.
1 // bin/compile-questions.cc
2 
3 // Copyright 2009-2011 Microsoft Corporation
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 "base/kaldi-common.h"
21 #include "util/common-utils.h"
22 #include "hmm/hmm-topology.h"
24 
25 
26 namespace kaldi {
27 int32 ProcessTopo(const HmmTopology &topo, const std::vector<std::vector<int32> > &questions) {
28  std::vector<int32> seen_phones; // ids of phones seen in questions.
29  for (size_t i = 0; i < questions.size(); i++)
30  for (size_t j= 0; j < questions[i].size(); j++) seen_phones.push_back(questions[i][j]);
31  SortAndUniq(&seen_phones);
32  // topo_phones is also sorted and uniq; a list of phones defined in the topology.
33  const std::vector<int32> &topo_phones = topo.GetPhones();
34  if (seen_phones != topo_phones) {
35  std::ostringstream ss_seen, ss_topo;
36  WriteIntegerVector(ss_seen, false, seen_phones);
37  WriteIntegerVector(ss_topo, false, topo_phones);
38  KALDI_WARN << "ProcessTopo: phones seen in questions differ from those in topology: "
39  << ss_seen.str() << " vs. " << ss_topo.str();
40  if (seen_phones.size() > topo_phones.size()) {
41  KALDI_ERR << "ProcessTopo: phones are asked about that are undefined in the topology.";
42  } // we accept the reverse (not asking about all phones), even though it's very bad.
43  }
44 
45  int32 max_num_pdf_classes = 0;
46  for (size_t i = 0; i < topo_phones.size(); i++) {
47  int32 p = topo_phones[i];
48  int32 num_pdf_classes = topo.NumPdfClasses(p);
49  max_num_pdf_classes = std::max(num_pdf_classes, max_num_pdf_classes);
50  }
51  KALDI_LOG << "Max # pdf classes is " << max_num_pdf_classes;
52  return max_num_pdf_classes;
53 }
54 
55 } // end namespace kaldi.
56 
57 int main(int argc, char *argv[]) {
58  using namespace kaldi;
59  try {
60  using namespace kaldi;
61  typedef kaldi::int32 int32;
62 
63  const char *usage =
64  "Compile questions\n"
65  "Usage: compile-questions [options] <topo> <questions-text-file> <questions-out>\n"
66  "e.g.: \n"
67  " compile-questions questions.txt questions.qst\n";
68  bool binary = true;
69  int32 P = 1, N = 3;
70  int32 num_iters_refine = 0;
71 
72 
73  ParseOptions po(usage);
74  po.Register("binary", &binary,
75  "Write output in binary mode");
76  po.Register("context-width", &N,
77  "Context window size [must match acc-tree-stats].");
78  po.Register("central-position", &P,
79  "Central position in phone context window [must match acc-tree-stats]");
80  po.Register("num-iters-refine", &num_iters_refine,
81  "Number of iters of refining questions at each node. >0 --> questions "
82  "not refined");
83 
84  po.Read(argc, argv);
85 
86  if (po.NumArgs() != 3) {
87  po.PrintUsage();
88  exit(1);
89  }
90 
91  std::string
92  topo_filename = po.GetArg(1),
93  questions_rxfilename = po.GetArg(2),
94  questions_out_filename = po.GetArg(3);
95 
96  HmmTopology topo; // just needed for checking, and to get the
97  // largest number of pdf-classes for any phone.
98  ReadKaldiObject(topo_filename, &topo);
99 
100  std::vector<std::vector<int32> > questions; // sets of phones.
101  if (!ReadIntegerVectorVectorSimple(questions_rxfilename, &questions))
102  KALDI_ERR << "Could not read questions from "
103  << PrintableRxfilename(questions_rxfilename);
104  for (size_t i = 0; i < questions.size(); i++) {
105  std::sort(questions[i].begin(), questions[i].end());
106  if (!IsSortedAndUniq(questions[i]))
107  KALDI_ERR << "Questions contain duplicate phones";
108  }
109  size_t nq = static_cast<int32>(questions.size());
110  SortAndUniq(&questions);
111  if (questions.size() != nq)
112  KALDI_WARN << (nq-questions.size())
113  << " duplicate questions present in " << questions_rxfilename;
114 
115  // ProcessTopo checks that all phones in the topo are
116  // represented in at least one questions (else warns), and
117  // returns the max # pdf classes in any given phone (normally
118  // 3).
119  int32 max_num_pdfclasses = ProcessTopo(topo, questions);
120 
121  Questions qo;
122 
123  QuestionsForKey phone_opts(num_iters_refine);
124  // the questions-options corresponding to keys 0, 1, .. N-1 which
125  // represent the phonetic context positions (including the central phone).
126  for (int32 n = 0; n < N; n++) {
127  KALDI_LOG << "Setting questions for phonetic-context position "<< n;
128  phone_opts.initial_questions = questions;
129  qo.SetQuestionsOf(n, phone_opts);
130  }
131 
132  QuestionsForKey pdfclass_opts(num_iters_refine);
133  std::vector<std::vector<int32> > pdfclass_questions(max_num_pdfclasses-1);
134  for (int32 i = 0; i < max_num_pdfclasses - 1; i++)
135  for (int32 j = 0; j <= i; j++)
136  pdfclass_questions[i].push_back(j);
137  // E.g. if max_num_pdfclasses == 3, pdfclass_questions is now [ [0], [0, 1] ].
138  pdfclass_opts.initial_questions = pdfclass_questions;
139  KALDI_LOG << "Setting questions for hmm-position [hmm-position ranges from 0 to "<< (max_num_pdfclasses-1) <<"]";
140  qo.SetQuestionsOf(kPdfClass, pdfclass_opts);
141 
142  WriteKaldiObject(qo, questions_out_filename, binary);
143  KALDI_LOG << "Wrote questions to "<<questions_out_filename;
144  } catch(const std::exception &e) {
145  std::cerr << e.what();
146  return -1;
147  }
148 }
This code computes Goodness of Pronunciation (GOP) and extracts phone-level pronunciation feature for...
Definition: chain.dox:20
This class defines, for each EventKeyType, a set of initial questions that it tries and also a number...
A class for storing topology information for phones.
Definition: hmm-topology.h:93
void PrintUsage(bool print_command_line=false)
Prints the usage documentation [provided in the constructor].
int32 ProcessTopo(const HmmTopology &topo, const std::vector< std::vector< int32 > > &questions)
void SetQuestionsOf(EventKeyType key, const QuestionsForKey &options_of_key)
kaldi::int32 int32
void SortAndUniq(std::vector< T > *vec)
Sorts and uniq&#39;s (removes duplicates) from a vector.
Definition: stl-utils.h:39
void Register(const std::string &name, bool *ptr, const std::string &doc)
int32 NumPdfClasses(int32 phone) const
Returns the number of pdf-classes for this phone; throws exception if phone not covered by this topol...
void ReadKaldiObject(const std::string &filename, Matrix< float > *m)
Definition: kaldi-io.cc:832
static const EventKeyType kPdfClass
Definition: context-dep.h:39
The class ParseOptions is for parsing command-line options; see Parsing command-line options for more...
Definition: parse-options.h:36
bool ReadIntegerVectorVectorSimple(const std::string &rxfilename, std::vector< std::vector< int32 > > *list)
struct rnnlm::@11::@12 n
QuestionsForKey is a class used to define the questions for a key, and also options that allow us to ...
std::vector< std::vector< EventValueType > > initial_questions
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.
#define KALDI_WARN
Definition: kaldi-error.h:150
const std::vector< int32 > & GetPhones() const
Returns a reference to a sorted, unique list of phones covered by the topology (these phones will be ...
Definition: hmm-topology.h:163
int NumArgs() const
Number of positional parameters (c.f. argc-1).
void WriteIntegerVector(std::ostream &os, bool binary, const std::vector< T > &v)
Function for writing STL vectors of integer types.
Definition: io-funcs-inl.h:198
void WriteKaldiObject(const C &c, const std::string &filename, bool binary)
Definition: kaldi-io.h:257
std::string PrintableRxfilename(const std::string &rxfilename)
PrintableRxfilename turns the rxfilename into a more human-readable form for error reporting...
Definition: kaldi-io.cc:61
int main(int argc, char *argv[])
bool IsSortedAndUniq(const std::vector< T > &vec)
Returns true if the vector is sorted and contains each element only once.
Definition: stl-utils.h:63
#define KALDI_LOG
Definition: kaldi-error.h:153