kaldi-holder.h
Go to the documentation of this file.
1 // util/kaldi-holder.h
2 
3 // Copyright 2009-2011 Microsoft Corporation
4 // 2016 Johns Hopkins University (author: Daniel Povey)
5 // 2016 Xiaohui Zhang
6 
7 // See ../../COPYING for clarification regarding multiple authors
8 //
9 // Licensed under the Apache License, Version 2.0 (the "License");
10 // you may not use this file except in compliance with the License.
11 // You may obtain a copy of the License at
12 //
13 // http://www.apache.org/licenses/LICENSE-2.0
14 //
15 // THIS CODE IS PROVIDED *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16 // KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED
17 // WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
18 // MERCHANTABLITY OR NON-INFRINGEMENT.
19 // See the Apache 2 License for the specific language governing permissions and
20 // limitations under the License.
21 
22 
23 #ifndef KALDI_UTIL_KALDI_HOLDER_H_
24 #define KALDI_UTIL_KALDI_HOLDER_H_
25 
26 #include <algorithm>
27 #include "util/kaldi-io.h"
28 #include "util/text-utils.h"
29 #include "matrix/kaldi-vector.h"
30 #include "matrix/sparse-matrix.h"
31 
32 namespace kaldi {
33 
34 
35 // The Table class uses a Holder class to wrap objects, and make them behave
36 // in a "normalized" way w.r.t. reading and writing, so the Table class can
37 // be template-ized without too much trouble. Look below this
38 // comment (search for GenericHolder) to see what it looks like.
39 //
40 // Requirements of the holder class:
41 //
42 // They can only contain objects that can be read/written without external
43 // information; other objects cannot be stored in this type of archive.
44 //
45 // In terms of what functions it should have, see GenericHolder below.
46 // It is just for documentation.
47 //
48 // (1) Requirements of the Read and Write functions
49 //
50 // The Read and Write functions should have the property that in a longer
51 // file, if the Read function is started from where the Write function started
52 // writing, it should go to where the Write function stopped writing, in either
53 // text or binary mode (but it's OK if it doesn't eat up trailing space).
54 //
55 // [Desirable property: when writing in text mode the output should contain
56 // exactly one newline, at the end of the output; this makes it easier to
57 // manipulate]
58 //
59 // [Desirable property for classes: the output should just be a binary-mode
60 // header (if in binary mode and it's a Kaldi object, or no header
61 // othewise), and then the output of Object.Write(). This means that when
62 // written to individual files with the scp: type of wspecifier, we can
63 // read the individual files in the "normal" Kaldi way by reading the
64 // binary header and then the object.]
65 //
66 //
67 // The Write function takes a 'binary' argument. In general, each object will
68 // have two formats: text and binary. However, it's permitted to throw() if
69 // asked to read in the text format if there is none. The file will be open, if
70 // the file system has binary/text modes, in the corresponding mode. However,
71 // the object should have a file-mode in which it can read either text or binary
72 // output. It announces this via the static IsReadInBinary() function. This
73 // will generally be the binary mode and it means that where necessary, in text
74 // formats, we must ignore \r characters.
75 //
76 // Memory requirements: if it allocates memory, the destructor should
77 // free that memory. Copying and assignment of Holder objects may be
78 // disallowed as the Table code never does this.
79 
80 
83 template<class SomeType> class GenericHolder {
84  public:
85  typedef SomeType T;
86 
89 
98  static bool Write(std::ostream &os, bool binary, const T &t);
99 
114  bool Read(std::istream &is);
115 
123  static bool IsReadInBinary() { return true; }
124 
128  T &Value() { return t_; } // if t is a pointer, would return *t_;
129 
132  void Clear() { }
133 
138  void Swap(GenericHolder<T> *other) { std::swap(t_, other->t_); }
139 
146  bool ExtractRange(const GenericHolder<T> &other, const std::string &range) {
147  KALDI_ERR << "ExtractRange is not defined for this type of holder.";
148  return false;
149  }
150 
153 
154  private:
156  T t_; // t_ may alternatively be of type T*.
157 };
158 
159 
160 // See kaldi-holder-inl.h for examples of some actual Holder
161 // classes and templates.
162 
163 
164 // The following two typedefs should probably be in their own file, but they're
165 // here until there are enough of them to warrant their own header.
166 
167 
170 
173 template<class KaldiType> class KaldiObjectHolder;
174 
179 template<class BasicType> class BasicHolder;
180 
181 
182 // A Holder for a vector of basic types, e.g.
183 // std::vector<int32>, std::vector<float>, and so on.
184 // Note: a basic type is defined as a type for which ReadBasicType
185 // and WriteBasicType are implemented, i.e. integer and floating
186 // types, and bool.
187 template<class BasicType> class BasicVectorHolder;
188 
189 
190 // A holder for vectors of vectors of basic types, e.g.
191 // std::vector<std::vector<int32> >, and so on.
192 // Note: a basic type is defined as a type for which ReadBasicType
193 // and WriteBasicType are implemented, i.e. integer and floating
194 // types, and bool.
195 template<class BasicType> class BasicVectorVectorHolder;
196 
197 // A holder for vectors of pairs of basic types, e.g.
198 // std::vector<std::pair<int32, int32> >, and so on.
199 // Note: a basic type is defined as a type for which ReadBasicType
200 // and WriteBasicType are implemented, i.e. integer and floating
201 // types, and bool. Text format is (e.g. for integers),
202 // "1 12 ; 43 61 ; 17 8 \n"
203 template<class BasicType> class BasicPairVectorHolder;
204 
209 class TokenHolder;
210 
213 class TokenVectorHolder;
214 
217 class HtkMatrixHolder;
218 
220 template<int kFeatDim = 13> class SphinxMatrixHolder;
221 
232 template <class T>
233 bool ExtractObjectRange(const T &input, const std::string &range, T *output) {
234  KALDI_ERR << "Ranges not supported for objects of this type.";
235  return false;
236 }
237 
242 template <class Real>
243 bool ExtractObjectRange(const Matrix<Real> &input, const std::string &range,
244  Matrix<Real> *output);
245 
247 template <class Real>
248 bool ExtractObjectRange(const Vector<Real> &input, const std::string &range,
249  Vector<Real> *output);
250 
252 bool ExtractObjectRange(const GeneralMatrix &input, const std::string &range,
253  GeneralMatrix *output);
254 
258 template <class Real>
259 bool ExtractObjectRange(const CompressedMatrix &input, const std::string &range,
260  Matrix<Real> *output);
261 
262 // In SequentialTableReaderScriptImpl and RandomAccessTableReaderScriptImpl, for
263 // cases where the scp contained 'range specifiers' (things in square brackets
264 // identifying parts of objects like matrices), use this function to separate
265 // the input string 'rxfilename_with_range' (e.g "1.ark:100[1:2,2:10]") into the data_rxfilename
266 // (e.g. "1.ark:100") and the optional range specifier which will be everything
267 // inside the square brackets. It returns true if everything seems OK, and
268 // false if for example the string contained more than one '['. This function
269 // should only be called if 'line' ends in ']', otherwise it is an error.
270 bool ExtractRangeSpecifier(const std::string &rxfilename_with_range,
271  std::string *data_rxfilename,
272  std::string *range);
273 
274 
276 
277 
278 } // end namespace kaldi
279 
280 #include "util/kaldi-holder-inl.h"
281 
282 #endif // KALDI_UTIL_KALDI_HOLDER_H_
This code computes Goodness of Pronunciation (GOP) and extracts phone-level pronunciation feature for...
Definition: chain.dox:20
This class is a wrapper that enables you to store a matrix in one of three forms: either as a Matrix<...
KaldiObjectHolder works for Kaldi objects that have the "standard" Read and Write functions...
static bool Write(std::ostream &os, bool binary, const T &t)
Write() writes this object of type T.
void Clear()
The Clear() function doesn&#39;t have to do anything.
Definition: kaldi-holder.h:132
A Holder for a vector of basic types, e.g.
void swap(basic_filebuf< CharT, Traits > &x, basic_filebuf< CharT, Traits > &y)
A class for storing matrices.
Definition: kaldi-matrix.h:823
A class for reading/writing Sphinx format matrices.
bool ExtractObjectRange(const GeneralMatrix &input, const std::string &range, GeneralMatrix *output)
GeneralMatrix is always of type BaseFloat.
Definition: kaldi-holder.cc:88
BasicHolder is valid for float, double, bool, and integer types.
bool Read(std::istream &is)
Reads into the holder.
GenericHolder()
Must have a constructor that takes no arguments.
Definition: kaldi-holder.h:88
#define KALDI_ERR
Definition: kaldi-error.h:147
BasicVectorVectorHolder is a Holder for a vector of vector of a basic type, e.g.
T & Value()
Returns the value of the object held here.
Definition: kaldi-holder.h:128
bool ExtractRange(const GenericHolder< T > &other, const std::string &range)
At the time of writing this will only do something meaningful KaldiObjectHolder holding matrix object...
Definition: kaldi-holder.h:146
A class representing a vector.
Definition: kaldi-vector.h:406
BasicPairVectorHolder is a Holder for a vector of pairs of a basic type, e.g.
bool ExtractRangeSpecifier(const std::string &rxfilename_with_range, std::string *data_rxfilename, std::string *range)
void Swap(GenericHolder< T > *other)
This swaps the objects held by *this and *other (preferably a shallow swap).
Definition: kaldi-holder.h:138
~GenericHolder()
If the object held pointers, the destructor would free them.
Definition: kaldi-holder.h:152
KALDI_DISALLOW_COPY_AND_ASSIGN(GenericHolder)
static bool IsReadInBinary()
IsReadInBinary() will return true if the object wants the file to be opened in binary for reading (if...
Definition: kaldi-holder.h:123
GenericHolder serves to document the requirements of the Holder interface; it&#39;s not intended to be us...
Definition: kaldi-holder.h:83