build-tree-questions.h
Go to the documentation of this file.
1 // tree/build-tree-questions.h
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 #ifndef KALDI_TREE_BUILD_TREE_QUESTIONS_H_
21 #define KALDI_TREE_BUILD_TREE_QUESTIONS_H_
22 
23 #include "util/stl-utils.h"
24 #include "tree/context-dep.h"
25 
26 namespace kaldi {
27 
28 
32 typedef std::vector<std::pair<EventType, Clusterable*> > BuildTreeStatsType;
33 
37 
39 
44 
50 struct QuestionsForKey { // Configuration class associated with a particular key
51  // (of type EventKeyType). It also contains the questions themselves.
52  std::vector<std::vector<EventValueType> > initial_questions;
53  RefineClustersOptions refine_opts; // if refine_opts.max_iter == 0,
54  // we just pick from the initial questions.
55 
56  QuestionsForKey(int32 num_iters = 5): refine_opts(num_iters, 2) {
57  // refine_cfg with 5 iters and top-n = 2 (this is no restriction because
58  // RefineClusters called with 2 clusters; would get set to that anyway as
59  // it's the only possible value for 2 clusters). User has to add questions.
60  // This config won't work as-is, as it has no questions.
61  }
62 
63  void Check() const {
64  for (size_t i = 0;i < initial_questions.size();i++) KALDI_ASSERT(IsSorted(initial_questions[i]));
65  }
66 
67  void Write(std::ostream &os, bool binary) const;
68  void Read(std::istream &is, bool binary);
69 
70  // copy and assign allowed.
71 };
72 
77 class Questions { // careful, this is a class.
78  public:
80  std::map<EventKeyType, size_t>::const_iterator iter;
81  if ( (iter = key_idx_.find(key)) == key_idx_.end()) {
82  KALDI_ERR << "Questions: no options for key "<< key;
83  }
84  size_t idx = iter->second;
85  KALDI_ASSERT(idx < key_options_.size());
86  key_options_[idx]->Check();
87  return *(key_options_[idx]);
88  }
89  void SetQuestionsOf(EventKeyType key, const QuestionsForKey &options_of_key) {
90  options_of_key.Check();
91  if (key_idx_.count(key) == 0) {
92  key_idx_[key] = key_options_.size();
93  key_options_.push_back(new QuestionsForKey());
94  *(key_options_.back()) = options_of_key;
95  } else {
96  size_t idx = key_idx_[key];
97  KALDI_ASSERT(idx < key_options_.size());
98  *(key_options_[idx]) = options_of_key;
99  }
100  }
101  void GetKeysWithQuestions(std::vector<EventKeyType> *keys_out) const {
102  KALDI_ASSERT(keys_out != NULL);
103  CopyMapKeysToVector(key_idx_, keys_out);
104  }
106  return (key_idx_.count(key) != 0);
107  }
108  ~Questions() { kaldi::DeletePointers(&key_options_); }
109 
110 
113  Questions() { }
114 
115 
121  void InitRand(const BuildTreeStatsType &stats, int32 num_quest, int32 num_iters_refine, AllKeysType all_keys_type);
122 
123  void Write(std::ostream &os, bool binary) const;
124  void Read(std::istream &is, bool binary);
125  private:
126  std::vector<QuestionsForKey*> key_options_;
127  std::map<EventKeyType, size_t> key_idx_;
129 };
130 
132 
133 }// end namespace kaldi
134 
135 #endif // KALDI_TREE_BUILD_TREE_QUESTIONS_H_
void GetKeysWithQuestions(std::vector< EventKeyType > *keys_out) const
This code computes Goodness of Pronunciation (GOP) and extracts phone-level pronunciation feature for...
Definition: chain.dox:20
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 CopyMapKeysToVector(const std::map< A, B > &m, std::vector< A > *v)
Copies the keys in a map to a vector.
Definition: stl-utils.h:126
This class defines, for each EventKeyType, a set of initial questions that it tries and also a number...
std::vector< QuestionsForKey * > key_options_
void SetQuestionsOf(EventKeyType key, const QuestionsForKey &options_of_key)
const QuestionsForKey & GetQuestionsOf(EventKeyType key) const
kaldi::int32 int32
std::map< EventKeyType, size_t > key_idx_
#define KALDI_DISALLOW_COPY_AND_ASSIGN(type)
Definition: kaldi-utils.h:121
static void InitRand(VectorBase< Real > *v)
AllKeysType
Typedef used when we get "all keys" from a set of stats– used in specifying which kinds of questions...
QuestionsForKey(int32 num_iters=5)
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
#define KALDI_ERR
Definition: kaldi-error.h:147
RefineClustersOptions refine_opts
void Write(std::ostream &os, bool binary) const
void Read(std::istream &is, bool binary)
bool HasQuestionsForKey(EventKeyType key) const
bool IsSorted(const std::vector< T > &vec)
Returns true if the vector is sorted.
Definition: stl-utils.h:47
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185
Questions()
Initializer with arguments.
std::vector< std::pair< EventType, Clusterable * > > BuildTreeStatsType