context-dep.h
Go to the documentation of this file.
1 // tree/context-dep.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_CONTEXT_DEP_H_
21 #define KALDI_TREE_CONTEXT_DEP_H_
22 
23 #include "util/stl-utils.h"
24 #include "itf/context-dep-itf.h"
25 #include "tree/event-map.h"
26 #include "matrix/matrix-lib.h"
27 #include "tree/cluster-utils.h"
28 
29 /*
30  This header provides the declarations for the class ContextDependency, which inherits
31  from the interface class "ContextDependencyInterface" in itf/context-dep-itf.h.
32  This is basically a wrapper around an EventMap. The EventMap
33  (tree/event-map.h) declares most of the internals of the class, and the building routines are
34  in build-tree.h which uses build-tree-utils.h, which uses cluster-utils.h . */
35 
36 
37 namespace kaldi {
38 
39 static const EventKeyType kPdfClass = -1; // The "name" to which we assign the
40 // pdf-class (generally corresponds to position in the HMM, zero-based);
41 // must not be used for any other event. I.e. the value corresponding to
42 // this key is the pdf-class (see hmm-topology.h for explanation of what this is).
43 
44 
45 /* ContextDependency is quite a generic decision tree.
46 
47  It does not actually do very much-- all the magic is in the EventMap object.
48  All this class does is to encode the phone context as a sequence of events, and
49  pass this to the EventMap object to turn into what it will interpret as a
50  vector of pdfs.
51 
52  Different versions of the ContextDependency class that are written in the future may
53  have slightly different interfaces and pass more stuff in as events, to the
54  EventMap object.
55 
56  In order to separate the process of training decision trees from the process
57  of actually using them, we do not put any training code into the ContextDependency class.
58  */
60  public:
61  virtual int32 ContextWidth() const { return N_; }
62  virtual int32 CentralPosition() const { return P_; }
63 
64 
68  virtual bool Compute(const std::vector<int32> &phoneseq,
69  int32 pdf_class, int32 *pdf_id) const;
70 
71  virtual int32 NumPdfs() const {
72  // this routine could be simplified to return to_pdf_->MaxResult()+1. we're a
73  // bit more paranoid than that.
74  if (!to_pdf_) return 0;
75  EventAnswerType max_result = to_pdf_->MaxResult();
76  if (max_result < 0 ) return 0;
77  else return (int32) max_result+1;
78  }
79  virtual ContextDependencyInterface *Copy() const {
80  return new ContextDependency(N_, P_, to_pdf_->Copy());
81  }
82 
84  void Read (std::istream &is, bool binary);
85 
86  // Constructor with no arguments; will normally be called
87  // prior to Read()
88  ContextDependency(): N_(0), P_(0), to_pdf_(NULL) { }
89 
90  // Constructor takes ownership of pointers.
92  EventMap *to_pdf):
93  N_(N), P_(P), to_pdf_(to_pdf) { }
94  void Write (std::ostream &os, bool binary) const;
95 
97 
98  const EventMap &ToPdfMap() const { return *to_pdf_; }
99 
106  virtual void GetPdfInfo(
107  const std::vector<int32> &phones, // list of phones
108  const std::vector<int32> &num_pdf_classes, // indexed by phone,
109  std::vector<std::vector<std::pair<int32, int32> > > *pdf_info)
110  const;
111 
131  virtual void GetPdfInfo(
132  const std::vector<int32> &phones,
133  const std::vector<std::vector<std::pair<int32, int32> > > &pdf_class_pairs,
134  std::vector<std::vector<std::vector<std::pair<int32, int32> > > > *pdf_info)
135  const;
136 
137  private:
138  int32 N_; //
140  EventMap *to_pdf_; // owned here.
141 
142  // 'context' is the context-window of phones, of
143  // length N, with -1 for those positions where phones
144  // that are currently unknown, treated as wildcards; at least
145  // the central phone [position P] must be a real phone, i.e.
146  // not -1.
147  // This function inserts any allowed pairs (forward_pdf, self_loop_pdf)
148  // to the set "pairs".
149  void EnumeratePairs(
150  const std::vector<int32> &phones,
151  int32 self_loop_pdf_class, int32 forward_pdf_class,
152  const std::vector<int32> &context,
153  unordered_set<std::pair<int32,int32>, PairHasher<int32> > *pairs)
154  const;
155 
157 };
158 
167 ContextDependency *GenRandContextDependency(const std::vector<int32> &phones,
168  bool ensure_all_covered,
169  std::vector<int32> *num_pdf_classes);
170 
173 ContextDependency *GenRandContextDependencyLarge(const std::vector<int32> &phones,
174  int N, int P,
175  bool ensure_all_covered,
176  std::vector<int32> *num_pdf_classes);
177 
178 // MonophoneContextDependency() returns a new ContextDependency object that
179 // corresponds to a monophone system.
180 // The map phone2num_pdf_classes maps from the phone id to the number of
181 // pdf-classes we have for that phone (e.g. 3, so the pdf-classes would be
182 // 0, 1, 2).
183 
185 MonophoneContextDependency(const std::vector<int32> &phones,
186  const std::vector<int32> &phone2num_pdf_classes);
187 
188 // MonophoneContextDependencyShared is as MonophoneContextDependency but lets
189 // you define classes of phones which share pdfs (e.g. different stress-markers of a single
190 // phone.) Each element of phone_classes is a set of phones that are in that class.
192 MonophoneContextDependencyShared(const std::vector<std::vector<int32> > &phone_classes,
193  const std::vector<int32> &phone2num_pdf_classes);
194 
195 
196 // Important note:
197 // Statistics for training decision trees will be of type:
198 // std::vector<std::pair<EventType, Clusterable*> >
199 // We don't make this a typedef as it doesn't add clarity.
200 // they will be sorted and unique on the EventType member, which
201 // itself is sorted and unique on the name (see event-map.h).
202 
203 // See build-tree.h for functions relating to actually building the decision trees.
204 
205 
206 
207 
208 } // namespace Kaldi
209 
210 
211 #endif
This code computes Goodness of Pronunciation (GOP) and extracts phone-level pronunciation feature for...
Definition: chain.dox:20
virtual int32 ContextWidth() const
ContextWidth() returns the value N (e.g.
Definition: context-dep.h:61
virtual void GetPdfInfo(const std::vector< int32 > &phones, const std::vector< int32 > &num_pdf_classes, std::vector< std::vector< std::pair< int32, int32 > > > *pdf_info) const
GetPdfInfo returns a vector indexed by pdf-id, saying for each pdf which pairs of (phone...
Definition: context-dep.cc:287
const EventMap & ToPdfMap() const
Definition: context-dep.h:98
virtual ContextDependencyInterface * Copy() const
Returns pointer to new object which is copy of current one.
Definition: context-dep.h:79
ContextDependency * GenRandContextDependencyLarge(const std::vector< int32 > &phone_ids, int N, int P, bool ensure_all_covered, std::vector< int32 > *hmm_lengths)
GenRandContextDependencyLarge is like GenRandContextDependency but generates a larger tree with speci...
Definition: context-dep.cc:97
virtual EventAnswerType MaxResult() const
Definition: event-map.h:142
virtual bool Compute(const std::vector< int32 > &phoneseq, int32 pdf_class, int32 *pdf_id) const
returns success or failure; outputs pdf to pdf_id For positions that were outside the sequence (due t...
Definition: context-dep.cc:26
ContextDependency * MonophoneContextDependency(const std::vector< int32 > &phones, const std::vector< int32 > &phone2num_pdf_classes)
Definition: context-dep.cc:331
ContextDependency * MonophoneContextDependencyShared(const std::vector< std::vector< int32 > > &phone_sets, const std::vector< int32 > &phone2num_pdf_classes)
Definition: context-dep.cc:343
kaldi::int32 int32
void EnumeratePairs(const std::vector< int32 > &phones, int32 self_loop_pdf_class, int32 forward_pdf_class, const std::vector< int32 > &context, unordered_set< std::pair< int32, int32 >, PairHasher< int32 > > *pairs) const
Definition: context-dep.cc:181
static const EventKeyType kPdfClass
Definition: context-dep.h:39
virtual int32 NumPdfs() const
NumPdfs() returns the number of acoustic pdfs (they are numbered 0.. NumPdfs()-1).
Definition: context-dep.h:71
void Write(std::ostream &os, bool binary) const
Definition: context-dep.cc:145
int32 EventKeyType
Things of type EventKeyType can take any value.
Definition: event-map.h:45
virtual int32 CentralPosition() const
Central position P of the phone context, in 0-based numbering, e.g.
Definition: context-dep.h:62
virtual EventMap * Copy(const std::vector< EventMap *> &new_leaves) const =0
KALDI_DISALLOW_COPY_AND_ASSIGN(ContextDependency)
context-dep-itf.h provides a link between the tree-building code in ../tree/, and the FST code in ...
A class that is capable of representing a generic mapping from EventType (which is a vector of (key...
Definition: event-map.h:86
void Read(std::istream &is, bool binary)
Read context-dependency object from disk; throws on error.
Definition: context-dep.cc:155
ContextDependency * GenRandContextDependency(const std::vector< int32 > &phone_ids, bool ensure_all_covered, std::vector< int32 > *hmm_lengths)
GenRandContextDependency is mainly of use for debugging.
Definition: context-dep.cc:46
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
ContextDependency(int32 N, int32 P, EventMap *to_pdf)
Definition: context-dep.h:91
A hashing function-object for pairs of ints.
Definition: stl-utils.h:235