All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
"Holder types"

Holder types are types that are used as template arguments to the Table types (see "Table types and related functions"), and which help the Table types to read and write the object of type SomeHolder::T; see Holders as helpers to Table classes for more information. More...

Classes

singleton  KaldiObjectHolder< KaldiType >
 KaldiObjectHolder works for Kaldi objects that have the "standard" Read and Write functions, and a copy constructor. More...
 
singleton  BasicHolder< BasicType >
 BasicHolder is valid for float, double, bool, and integer types. More...
 
singleton  BasicVectorHolder< BasicType >
 A Holder for a vector of basic types, e.g. More...
 
singleton  BasicVectorVectorHolder< BasicType >
 BasicVectorVectorHolder is a Holder for a vector of vector of a basic type, e.g. More...
 
singleton  BasicPairVectorHolder< BasicType >
 BasicPairVectorHolder is a Holder for a vector of pairs of a basic type, e.g. More...
 
class  TokenHolder
 
class  TokenVectorHolder
 
class  HtkMatrixHolder
 
singleton  SphinxMatrixHolder< kFeatDim >
 A class for reading/writing Sphinx format matrices. More...
 

Functions

template<class T >
bool ExtractObjectRange (const T &input, const std::string &range, T *output)
 This templated function exists so that we can write .scp files with 'object ranges' specified: the canonical example is a [first:last] range of rows of a matrix, or [first-row:last-row,first-column,last-column] of a matrix. More...
 
template<class Real >
bool ExtractObjectRange (const Matrix< Real > &input, const std::string &range, Matrix< Real > *output)
 The template is specialized with a version that actually does something, for types Matrix<float> and Matrix<double>. More...
 
template<class Real >
bool ExtractObjectRange (const Vector< Real > &input, const std::string &range, Vector< Real > *output)
 The template is specialized types Vector<float> and Vector<double>. More...
 
bool ExtractRangeSpecifier (const std::string &rxfilename_with_range, std::string *data_rxfilename, std::string *range)
 

Detailed Description

Holder types are types that are used as template arguments to the Table types (see "Table types and related functions"), and which help the Table types to read and write the object of type SomeHolder::T; see Holders as helpers to Table classes for more information.

Function Documentation

bool ExtractObjectRange ( const Matrix< Real > &  input,
const std::string &  range,
Matrix< Real > *  output 
)

The template is specialized with a version that actually does something, for types Matrix<float> and Matrix<double>.

We can later add versions of this template for other types, such as Vector, which can meaningfully have ranges extracted.

Definition at line 27 of file kaldi-holder.cc.

References MatrixBase< Real >::CopyFromMat(), KALDI_ERR, KALDI_WARN, kaldi::kUndefined, MatrixBase< Real >::NumCols(), MatrixBase< Real >::NumRows(), MatrixBase< Real >::Range(), Matrix< Real >::Resize(), kaldi::SplitStringToIntegers(), and kaldi::SplitStringToVector().

Referenced by KaldiObjectHolder< KaldiType >::ExtractRange(), and kaldi::ReadKaldiObject().

28  {
29  if (range.empty()) {
30  KALDI_ERR << "Empty range specifier.";
31  return false;
32  }
33  std::vector<std::string> splits;
34  SplitStringToVector(range, ",", false, &splits);
35  if (!((splits.size() == 1 && !splits[0].empty()) ||
36  (splits.size() == 2 && !splits[0].empty() && !splits[1].empty()))) {
37  KALDI_ERR << "Invalid range specifier for matrix: " << range;
38  return false;
39  }
40  std::vector<int32> row_range, col_range;
41  bool status = true;
42  if (splits[0] != ":")
43  status = SplitStringToIntegers(splits[0], ":", false, &row_range);
44  if (splits.size() == 2 && splits[1] != ":") {
45  status = status && SplitStringToIntegers(splits[1], ":", false, &col_range);
46  }
47  if (row_range.size() == 0) {
48  row_range.push_back(0);
49  row_range.push_back(input.NumRows() - 1);
50  }
51  if (col_range.size() == 0) {
52  col_range.push_back(0);
53  col_range.push_back(input.NumCols() - 1);
54  }
55 
56  // Length tolerance of 3 -- 2 to account for edge effects when
57  // frame-length is 25ms and frame-shift is 10ms, and 1 for rounding effects
58  // since segments are usually retained up to 2 decimal places.
59  int32 length_tolerance = 3;
60  if (!(status && row_range.size() == 2 && col_range.size() == 2 &&
61  row_range[0] >= 0 && row_range[0] <= row_range[1] &&
62  row_range[1] < input.NumRows() + length_tolerance &&
63  col_range[0] >=0 &&
64  col_range[0] <= col_range[1] && col_range[1] < input.NumCols())) {
65  KALDI_ERR << "Invalid range specifier: " << range
66  << " for matrix of size " << input.NumRows()
67  << "x" << input.NumCols();
68  return false;
69  }
70 
71  if (row_range[1] >= input.NumRows())
72  KALDI_WARN << "Row range " << row_range[0] << ":" << row_range[1]
73  << " goes beyond the number of rows of the "
74  << "matrix " << input.NumRows();
75  int32 row_size = std::min(row_range[1], input.NumRows() - 1)
76  - row_range[0] + 1,
77  col_size = col_range[1] - col_range[0] + 1;
78  output->Resize(row_size, col_size, kUndefined);
79  output->CopyFromMat(input.Range(row_range[0], row_size,
80  col_range[0], col_size));
81  return true;
82 }
bool SplitStringToIntegers(const std::string &full, const char *delim, bool omit_empty_strings, std::vector< I > *out)
Split a string (e.g.
Definition: text-utils.h:68
void SplitStringToVector(const std::string &full, const char *delim, bool omit_empty_strings, std::vector< std::string > *out)
Split a string using any of the single character delimiters.
Definition: text-utils.cc:63
#define KALDI_ERR
Definition: kaldi-error.h:127
#define KALDI_WARN
Definition: kaldi-error.h:130
bool ExtractObjectRange ( const Vector< Real > &  input,
const std::string &  range,
Vector< Real > *  output 
)

The template is specialized types Vector<float> and Vector<double>.

Definition at line 91 of file kaldi-holder.cc.

References VectorBase< Real >::CopyFromVec(), VectorBase< Real >::Dim(), KALDI_ERR, KALDI_WARN, kaldi::kUndefined, VectorBase< Real >::Range(), Vector< Real >::Resize(), kaldi::SplitStringToIntegers(), and kaldi::SplitStringToVector().

92  {
93  if (range.empty()) {
94  KALDI_ERR << "Empty range specifier.";
95  return false;
96  }
97  std::vector<std::string> splits;
98  SplitStringToVector(range, ",", false, &splits);
99  if (!((splits.size() == 1 && !splits[0].empty()))) {
100  KALDI_ERR << "Invalid range specifier for vector: " << range;
101  return false;
102  }
103  std::vector<int32> index_range;
104  bool status = true;
105  if (splits[0] != ":")
106  status = SplitStringToIntegers(splits[0], ":", false, &index_range);
107 
108  if (index_range.size() == 0) {
109  index_range.push_back(0);
110  index_range.push_back(input.Dim() - 1);
111  }
112 
113  // Length tolerance of 3 -- 2 to account for edge effects when
114  // frame-length is 25ms and frame-shift is 10ms, and 1 for rounding effects
115  // since segments are usually retained up to 2 decimal places.
116  int32 length_tolerance = 3;
117  if (!(status && index_range.size() == 2 &&
118  index_range[0] >= 0 && index_range[0] <= index_range[1] &&
119  index_range[1] < input.Dim() + length_tolerance)) {
120  KALDI_ERR << "Invalid range specifier: " << range
121  << " for vector of size " << input.Dim();
122  return false;
123  }
124 
125  if (index_range[1] >= input.Dim())
126  KALDI_WARN << "Range " << index_range[0] << ":" << index_range[1]
127  << " goes beyond the vector dimension " << input.Dim();
128  int32 size = std::min(index_range[1], input.Dim() - 1) - index_range[0] + 1;
129  output->Resize(size, kUndefined);
130  output->CopyFromVec(input.Range(index_range[0], size));
131  return true;
132 }
bool SplitStringToIntegers(const std::string &full, const char *delim, bool omit_empty_strings, std::vector< I > *out)
Split a string (e.g.
Definition: text-utils.h:68
void SplitStringToVector(const std::string &full, const char *delim, bool omit_empty_strings, std::vector< std::string > *out)
Split a string using any of the single character delimiters.
Definition: text-utils.cc:63
#define KALDI_ERR
Definition: kaldi-error.h:127
#define KALDI_WARN
Definition: kaldi-error.h:130
bool kaldi::ExtractObjectRange ( const T &  input,
const std::string &  range,
T *  output 
)

This templated function exists so that we can write .scp files with 'object ranges' specified: the canonical example is a [first:last] range of rows of a matrix, or [first-row:last-row,first-column,last-column] of a matrix.

We can also support [begin-time:end-time] of a wave file. The string 'range' is whatever is in the square brackets; it is parsed inside this function. This function returns true if the partial object was successfully extracted, and false if there was an error such as an invalid range. The generic version of this function just fails; we overload the template whenever we need it for a specific class.

Definition at line 232 of file kaldi-holder.h.

References KALDI_ERR.

232  {
233  KALDI_ERR << "Ranges not supported for objects of this type.";
234  return false;
235 }
#define KALDI_ERR
Definition: kaldi-error.h:127
bool ExtractRangeSpecifier ( const std::string &  rxfilename_with_range,
std::string *  data_rxfilename,
std::string *  range 
)

Definition at line 140 of file kaldi-holder.cc.

References KALDI_ERR, and kaldi::SplitStringToVector().

Referenced by RandomAccessTableReaderScriptImpl< Holder >::HasKeyInternal(), SequentialTableReaderScriptImpl< Holder >::NextScpLine(), and kaldi::ReadKaldiObject().

142  {
143  if (rxfilename_with_range.empty() ||
144  rxfilename_with_range[rxfilename_with_range.size()-1] != ']')
145  KALDI_ERR << "ExtractRangeRspecifier called wrongly.";
146  std::vector<std::string> splits;
147  SplitStringToVector(rxfilename_with_range, "[", false, &splits);
148  if (splits.size() == 2 && !splits[0].empty() && splits[1].size() > 1) {
149  *data_rxfilename = splits[0];
150  range->assign(splits[1], 0, splits[1].size()-1);
151  return true;
152  }
153  return false;
154 }
void SplitStringToVector(const std::string &full, const char *delim, bool omit_empty_strings, std::vector< std::string > *out)
Split a string using any of the single character delimiters.
Definition: text-utils.cc:63
#define KALDI_ERR
Definition: kaldi-error.h:127