build-tree-questions.cc
Go to the documentation of this file.
1 // tree/build-tree-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-math.h"
21 #include "util/stl-utils.h"
23 #include "tree/build-tree-utils.h"
24 
25 namespace kaldi {
26 
27 void QuestionsForKey::Write(std::ostream &os, bool binary) const {
28  WriteToken(os, binary, "<QuestionsForKey>");
29  int32 size = initial_questions.size();
30  WriteBasicType(os, binary, size);
31  for (int32 i = 0; i < size; i++)
33  refine_opts.Write(os, binary);
34  WriteToken(os, binary, "</QuestionsForKey>");
35 }
36 
37 void QuestionsForKey::Read(std::istream &is, bool binary) {
38  int32 size;
39  ExpectToken(is, binary, "<QuestionsForKey>");
40  ReadBasicType(is, binary, &size);
41  initial_questions.resize(size);
42  for (int32 i = 0; i < size; i++)
43  ReadIntegerVector(is, binary, &(initial_questions[i]));
44  refine_opts.Read(is, binary);
45  ExpectToken(is, binary, "</QuestionsForKey>");
46 }
47 
48 void Questions::Write(std::ostream &os, bool binary) const {
49  WriteToken(os, binary, "<Questions>");
50 
51  std::vector<EventKeyType> keys_with_options;
52  this->GetKeysWithQuestions(&keys_with_options);
53  for (size_t i = 0; i < keys_with_options.size(); i++) {
54  EventKeyType key = keys_with_options[i];
55  WriteToken(os, binary, "<Key>");
56  WriteBasicType(os, binary, key);
57  const QuestionsForKey &opts = GetQuestionsOf(key);
58  opts.Write(os, binary);
59  }
60  WriteToken(os, binary, "</Questions>");
61 }
62 
63 void Questions::Read(std::istream &is, bool binary) {
64  // First, clear anything present.
65  DeletePointers(&key_options_);
66  key_options_.clear();
67  key_idx_.clear();
68 
69  ExpectToken(is, binary, "<Questions>");
70 
71  std::vector<EventKeyType> keys_with_options;
72 
73  while (1) {
74  std::string token;
75  ReadToken(is, binary, &token);
76  if (token == "</Questions>") return;
77  else {
78  if (token != "<Key>")
79  KALDI_ERR << "Questions::Read, expecting <Key>, got "<<token;
80  EventKeyType key;
81  ReadBasicType(is, binary, &key);
82  QuestionsForKey opts;
83  opts.Read(is, binary);
84  SetQuestionsOf(key, opts);
85  }
86  }
87 }
88 
89 void Questions::InitRand(const BuildTreeStatsType &stats, int32 num_quest, int32 num_iters_refine,
90  AllKeysType all_keys_type) {
91  std::vector<EventKeyType> all_keys;
92  FindAllKeys(stats, all_keys_type, &all_keys); // get all keys.
93  if (all_keys_type == kAllKeysUnion) {
94  KALDI_WARN << "Questions::InitRand(), using union of all keys. This may work depending on how you are building the tree but may crash (depends if you have already ensured that stats currently on the same leaf all share the same set of keys.)";
95  }
96 
97  for (size_t i = 0;i < all_keys.size();i++) {
98  EventKeyType key = all_keys[i];
99  std::vector<EventValueType> all_values;
100  bool b = PossibleValues(key, stats, &all_values); // get possible values.
101  if (all_keys_type != kAllKeysUnion) KALDI_ASSERT(b);
102  KALDI_ASSERT(all_values.size() != 0); // since key exists in stats, must have some value defined.
103  QuestionsForKey q_for_key;
104  q_for_key.refine_opts.num_iters = num_iters_refine;
105  q_for_key.initial_questions.clear(); // Make sure empty.
106  if (all_values.size() == 1) { // can have no meaningful questions because only 1 possible value. use empty set of questions.
107  ; // Do nothing. No questions.
108  } else {
109  q_for_key.initial_questions.resize((size_t)num_quest);
110  for (size_t i = 0;i < (size_t)num_quest;i++) {
111  std::vector<EventValueType> &this_quest = q_for_key.initial_questions[i];
112  for (size_t j = 0;j < all_values.size()/2;j++)
113  this_quest.push_back(all_values[RandInt(0, all_values.size()-1)]);
114  SortAndUniq(&this_quest);
115  KALDI_ASSERT(!this_quest.empty());
116  }
117  SortAndUniq(&q_for_key.initial_questions); // Ensure unique questions.
118  }
119  q_for_key.Check();
120  SetQuestionsOf(key, q_for_key);
121  }
122 }
123 
124 
125 
126 } // end namespace kaldi
127 
This code computes Goodness of Pronunciation (GOP) and extracts phone-level pronunciation feature for...
Definition: chain.dox:20
void Write(std::ostream &os, bool binary) const
void DeletePointers(std::vector< A *> *v)
Deletes any non-NULL pointers in the vector v, and sets the corresponding entries of v to NULL...
Definition: stl-utils.h:184
void Read(std::istream &is, bool binary)
void ReadBasicType(std::istream &is, bool binary, T *t)
ReadBasicType is the name of the read function for bool, integer types, and floating-point types...
Definition: io-funcs-inl.h:55
void FindAllKeys(const BuildTreeStatsType &stats, AllKeysType keys_type, std::vector< EventKeyType > *keys_out)
FindAllKeys puts in *keys the (sorted, unique) list of all key identities in the stats.
kaldi::int32 int32
void ReadToken(std::istream &is, bool binary, std::string *str)
ReadToken gets the next token and puts it in str (exception on failure).
Definition: io-funcs.cc:154
void SortAndUniq(std::vector< T > *vec)
Sorts and uniq&#39;s (removes duplicates) from a vector.
Definition: stl-utils.h:39
bool PossibleValues(EventKeyType key, const BuildTreeStatsType &stats, std::vector< EventValueType > *ans)
Convenience function e.g.
AllKeysType
Typedef used when we get "all keys" from a set of stats– used in specifying which kinds of questions...
void ReadIntegerVector(std::istream &is, bool binary, std::vector< T > *v)
Function for reading STL vector of integer types.
Definition: io-funcs-inl.h:232
void ExpectToken(std::istream &is, bool binary, const char *token)
ExpectToken tries to read in the given token, and throws an exception on failure. ...
Definition: io-funcs.cc:191
void InitRand(const BuildTreeStatsType &stats, int32 num_quest, int32 num_iters_refine, AllKeysType all_keys_type)
InitRand attempts to generate "reasonable" random questions.
int32 EventKeyType
Things of type EventKeyType can take any value.
Definition: event-map.h:45
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
void Read(std::istream &is, bool binary)
#define KALDI_ERR
Definition: kaldi-error.h:147
RefineClustersOptions refine_opts
#define KALDI_WARN
Definition: kaldi-error.h:150
void Write(std::ostream &os, bool binary) const
void WriteToken(std::ostream &os, bool binary, const char *token)
The WriteToken functions are for writing nonempty sequences of non-space characters.
Definition: io-funcs.cc:134
void Read(std::istream &is, bool binary)
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185
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 WriteBasicType(std::ostream &os, bool binary, T t)
WriteBasicType is the name of the write function for bool, integer types, and floating-point types...
Definition: io-funcs-inl.h:34
std::vector< std::pair< EventType, Clusterable * > > BuildTreeStatsType
void Write(std::ostream &os, bool binary) const
int32 RandInt(int32 min_val, int32 max_val, struct RandomState *state)
Definition: kaldi-math.cc:95