SequentialTableReaderScriptImpl< Holder > Class Template Reference

#include <kaldi-table-inl.h>

Inheritance diagram for SequentialTableReaderScriptImpl< Holder >:
Collaboration diagram for SequentialTableReaderScriptImpl< Holder >:

Public Types

typedef Holder::T T
 
- Public Types inherited from SequentialTableReaderImplBase< Holder >
typedef Holder::T T
 

Public Member Functions

 SequentialTableReaderScriptImpl ()
 
virtual bool Open (const std::string &rspecifier)
 
virtual bool IsOpen () const
 
virtual bool Done () const
 
virtual std::string Key ()
 
TValue ()
 
void FreeCurrent ()
 
void SwapHolder (Holder *other_holder)
 
void Next ()
 
virtual bool Close ()
 
virtual ~SequentialTableReaderScriptImpl ()
 
- Public Member Functions inherited from SequentialTableReaderImplBase< Holder >
 SequentialTableReaderImplBase ()
 
virtual ~SequentialTableReaderImplBase ()
 

Private Types

enum  StateType {
  kUninitialized, kFileStart, kEof, kError,
  kHaveScpLine, kHaveObject, kHaveRange
}
 

Private Member Functions

bool EnsureObjectLoaded ()
 
void SetErrorState ()
 
void NextScpLine ()
 

Private Attributes

std::string rspecifier_
 
RspecifierOptions opts_
 
std::string script_rxfilename_
 
Input script_input_
 
Input data_input_
 
Holder holder_
 
Holder range_holder_
 
std::string key_
 
std::string data_rxfilename_
 
std::string range_
 
enum kaldi::SequentialTableReaderScriptImpl::StateType state_
 

Detailed Description

template<class Holder>
class kaldi::SequentialTableReaderScriptImpl< Holder >

Definition at line 86 of file kaldi-table-inl.h.

Member Typedef Documentation

◆ T

typedef Holder::T T

Definition at line 89 of file kaldi-table-inl.h.

Member Enumeration Documentation

◆ StateType

enum StateType
private
Enumerator
kUninitialized 
kFileStart 
kEof 
kError 
kHaveScpLine 
kHaveObject 
kHaveRange 

Definition at line 449 of file kaldi-table-inl.h.

449  {
450  // Summary of the states this object can be in (state_).
451  //
452  // (*) Does holder_ contain the object corresponding to
453  // data_rxfilename_ ?
454  // (*) Does range_holder_ contain a range object?
455  // (*) is script_input_ open?
456  // (*) are key_, data_rxfilename_ and range_ [if applicable] set?
457  //
458  kUninitialized, // no no no no Uninitialized or closed object.
459  kFileStart, // no no yes no We just opened the .scp file (we'll never be in this
460  // state when a user-visible function is called.)
461  kEof, // no no no no We did Next() and found eof in script file.
462  kError, // no no no no Error reading or parsing script file.
463  kHaveScpLine, // no no yes yes Have a line of the script file but nothing else.
464  kHaveObject, // yes no yes yes holder_ contains an object but range_holder_ does not.
465  kHaveRange, // yes yes yes yes we have the range object in range_holder_ (implies
466  // range_ nonempty).
467  } state_;
enum kaldi::SequentialTableReaderScriptImpl::StateType state_

Constructor & Destructor Documentation

◆ SequentialTableReaderScriptImpl()

Definition at line 91 of file kaldi-table-inl.h.

◆ ~SequentialTableReaderScriptImpl()

virtual ~SequentialTableReaderScriptImpl ( )
inlinevirtual

Definition at line 277 of file kaldi-table-inl.h.

References SequentialTableReaderImplBase< Holder >::Close(), SequentialTableReaderImplBase< Holder >::IsOpen(), KALDI_ERR, and kaldi::PrintableRxfilename().

277  {
278  if (this->IsOpen() && !Close())
279  KALDI_ERR << "TableReader: reading script file failed: from scp "
281  }
#define KALDI_ERR
Definition: kaldi-error.h:147
std::string PrintableRxfilename(const std::string &rxfilename)
PrintableRxfilename turns the rxfilename into a more human-readable form for error reporting...
Definition: kaldi-io.cc:61

Member Function Documentation

◆ Close()

virtual bool Close ( )
inlinevirtual

Implements SequentialTableReaderImplBase< Holder >.

Definition at line 244 of file kaldi-table-inl.h.

References SequentialTableReaderImplBase< Holder >::IsOpen(), KALDI_ERR, and KALDI_WARN.

244  {
245  int32 status = 0;
246  if (script_input_.IsOpen())
247  status = script_input_.Close();
248  if (data_input_.IsOpen())
249  data_input_.Close();
250  range_holder_.Clear();
251  holder_.Clear();
252  if (!this->IsOpen())
253  KALDI_ERR << "Close() called on input that was not open.";
254  StateType old_state = state_;
256  if (old_state == kError || (old_state == kEof && status != 0)) {
257  if (opts_.permissive) {
258  KALDI_WARN << "Close() called on scp file with read error, ignoring the"
259  " error because permissive mode specified.";
260  return true;
261  } else {
262  return false; // User will do something with the error status.
263  }
264  } else {
265  return true;
266  }
267  // Possible states Return value
268  // kLoadSucceeded/kRangeSucceeded/kRangeFailed true
269  // kError (if opts_.permissive) true
270  // kError (if !opts_.permissive) false
271  // kEof (if script_input_.Close() && !opts.permissive) false
272  // kEof (if !script_input_.Close() || opts.permissive) true
273  // kUninitialized/kFileStart/kHaveScpLine true
274  // kUnitialized true
275  }
enum kaldi::SequentialTableReaderScriptImpl::StateType state_
bool IsOpen()
Definition: kaldi-io-inl.h:34
kaldi::int32 int32
#define KALDI_ERR
Definition: kaldi-error.h:147
#define KALDI_WARN
Definition: kaldi-error.h:150
int32 Close()
Definition: kaldi-io.cc:761

◆ Done()

virtual bool Done ( ) const
inlinevirtual

Implements SequentialTableReaderImplBase< Holder >.

Definition at line 140 of file kaldi-table-inl.h.

References KALDI_ERR.

140  {
141  switch (state_) {
142  case kHaveScpLine: case kHaveObject: case kHaveRange: return false;
143  case kEof: case kError: return true; // Error condition, like Eof, counts
144  // as Done(); the destructor/Close() will inform the user of the error.
145  default: KALDI_ERR << "Done() called on TableReader object at the wrong"
146  " time.";
147  return false;
148  }
149  }
enum kaldi::SequentialTableReaderScriptImpl::StateType state_
#define KALDI_ERR
Definition: kaldi-error.h:147

◆ EnsureObjectLoaded()

bool EnsureObjectLoaded ( )
inlineprivate

Definition at line 296 of file kaldi-table-inl.h.

References KALDI_ASSERT, KALDI_ERR, KALDI_WARN, and kaldi::PrintableRxfilename().

296  {
297  if (!(state_ == kHaveScpLine || state_ == kHaveObject ||
298  state_ == kHaveRange))
299  KALDI_ERR << "Invalid state (code error)";
300 
301  if (state_ == kHaveScpLine) { // need to load the object into holder_.
302  bool ans;
303  // note, NULL means it doesn't read the binary-mode header
304  if (Holder::IsReadInBinary()) {
305  ans = data_input_.Open(data_rxfilename_, NULL);
306  } else {
308  }
309  if (!ans) {
310  KALDI_WARN << "Failed to open file "
312  return false;
313  } else {
314  if (holder_.Read(data_input_.Stream())) {
316  } else { // holder_ will not contain data.
317  KALDI_WARN << "Failed to load object from "
319  return false;
320  }
321  }
322  }
323  // OK, at this point the state must be either
324  // kHaveObject or kHaveRange.
325  if (range_.empty()) {
326  // if range_ is the empty string, we should not be in the state
327  // kHaveRange.
329  return true;
330  }
331  // range_ is nonempty.
332  if (state_ == kHaveRange) {
333  // range was already extracted, so there nothing to do.
334  return true;
335  }
336  // OK, range_ is nonempty and state_ is kHaveObject. We attempt to extract
337  // the range object. Note: ExtractRange() will throw with KALDI_ERR if the
338  // object type doesn't support ranges.
339  if (!range_holder_.ExtractRange(holder_, range_)) {
340  KALDI_WARN << "Failed to load object from "
342  << "[" << range_ << "]";
343  return false;
344  } else {
345  state_ = kHaveRange;
346  return true;
347  }
348  }
enum kaldi::SequentialTableReaderScriptImpl::StateType state_
bool Open(const std::string &rxfilename, bool *contents_binary=NULL)
Definition: kaldi-io-inl.h:26
bool OpenTextMode(const std::string &rxfilename)
Definition: kaldi-io-inl.h:30
std::istream & Stream()
Definition: kaldi-io.cc:826
#define KALDI_ERR
Definition: kaldi-error.h:147
#define KALDI_WARN
Definition: kaldi-error.h:150
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185
std::string PrintableRxfilename(const std::string &rxfilename)
PrintableRxfilename turns the rxfilename into a more human-readable form for error reporting...
Definition: kaldi-io.cc:61

◆ FreeCurrent()

void FreeCurrent ( )
inlinevirtual

Implements SequentialTableReaderImplBase< Holder >.

Definition at line 179 of file kaldi-table-inl.h.

References KALDI_WARN.

179  {
180  if (state_ == kHaveObject) {
181  holder_.Clear();
183  } else if (state_ == kHaveRange) {
184  range_holder_.Clear();
186  } else {
187  KALDI_WARN << "FreeCurrent called at the wrong time.";
188  }
189  }
enum kaldi::SequentialTableReaderScriptImpl::StateType state_
#define KALDI_WARN
Definition: kaldi-error.h:150

◆ IsOpen()

virtual bool IsOpen ( ) const
inlinevirtual

Implements SequentialTableReaderImplBase< Holder >.

Definition at line 127 of file kaldi-table-inl.h.

References KALDI_ERR.

127  {
128  switch (state_) {
129  case kEof: case kHaveScpLine: case kHaveObject: case kHaveRange:
130  return true;
131  case kUninitialized: case kError:
132  return false;
133  default: KALDI_ERR << "IsOpen() called on invalid object.";
134  // note: kFileStart is not a valid state for the user to call a member
135  // function (we never return from a public function in this state).
136  return false;
137  }
138  }
enum kaldi::SequentialTableReaderScriptImpl::StateType state_
#define KALDI_ERR
Definition: kaldi-error.h:147

◆ Key()

virtual std::string Key ( )
inlinevirtual

Implements SequentialTableReaderImplBase< Holder >.

Definition at line 151 of file kaldi-table-inl.h.

References KALDI_ERR.

151  {
152  // Valid to call this whenever Done() returns false.
153  switch (state_) {
154  case kHaveScpLine: case kHaveObject: case kHaveRange: break;
155  default:
156  // coding error.
157  KALDI_ERR << "Key() called on TableReader object at the wrong time.";
158  }
159  return key_;
160  }
enum kaldi::SequentialTableReaderScriptImpl::StateType state_
#define KALDI_ERR
Definition: kaldi-error.h:147

◆ Next()

void Next ( )
inlinevirtual

Implements SequentialTableReaderImplBase< Holder >.

Definition at line 223 of file kaldi-table-inl.h.

References SequentialTableReaderImplBase< Holder >::Done().

223  {
224  while (1) {
225  NextScpLine();
226  if (Done()) return;
227  if (opts_.permissive) {
228  // Permissive mode means, when reading scp files, we treat keys whose
229  // scp entry cannot be read as nonexistent. This means trying to read.
230  if (EnsureObjectLoaded()) return; // Success.
231  // else try the next scp line.
232  } else {
233  return; // We go the next key; Value() will crash if we can't read the
234  // object on the scp line.
235  }
236  }
237  }

◆ NextScpLine()

void NextScpLine ( )
inlineprivate

Definition at line 361 of file kaldi-table-inl.h.

References kaldi::ExtractRangeSpecifier(), KALDI_ERR, KALDI_WARN, and kaldi::SplitStringOnFirstSpace().

361  {
362  switch (state_) { // Check and simplify the state.
363  case kHaveRange:
364  range_holder_.Clear();
366  break;
367  case kHaveScpLine: case kHaveObject: case kFileStart: break;
368  default:
369  // No other states are valid to call Next() from.
370  KALDI_ERR << "Reading script file: Next called wrongly.";
371  }
372  // at this point the state will be kHaveObject, kHaveScpLine, or kFileStart.
373  std::string line;
374  if (getline(script_input_.Stream(), line)) {
375  // After extracting "key" from "line", we put the rest
376  // of "line" into "rest", and then extract data_rxfilename_
377  // (e.g. 1.ark:100) and possibly the range_ specifer
378  // (e.g. [1:2,2:10]) from "rest".
379  std::string data_rxfilename, rest;
380  SplitStringOnFirstSpace(line, &key_, &rest);
381  if (!key_.empty() && !rest.empty()) {
382  // Got a valid line.
383  if (rest[rest.size()-1] == ']') {
384  if(!ExtractRangeSpecifier(rest, &data_rxfilename, &range_)) {
385  KALDI_WARN << "Reading rspecifier '" << rspecifier_
386  << ", cannot make sense of scp line "
387  << line;
388  SetErrorState();
389  return;
390  }
391  } else {
392  data_rxfilename = rest;
393  range_ = "";
394  }
395  bool filenames_equal = (data_rxfilename_ == data_rxfilename);
396  if (!filenames_equal)
397  data_rxfilename_ = data_rxfilename;
398  if (state_ == kHaveObject) {
399  if (!filenames_equal) {
400  holder_.Clear();
402  }
403  // else leave state_ at kHaveObject and leave the object in the
404  // holder.
405  } else {
407  }
408  } else {
409  KALDI_WARN << "We got an invalid line in the scp file. "
410  << "It should look like: some_key 1.ark:10, got: "
411  << line;
412  SetErrorState();
413  }
414  } else {
415  state_ = kEof; // there is nothing more in the scp file. Might as well
416  // close input streams as we don't need them.
418  if (data_input_.IsOpen())
419  data_input_.Close();
420  holder_.Clear(); // clear the holder if it was nonempty.
421  range_holder_.Clear(); // clear the range holder if it was nonempty.
422  }
423  }
enum kaldi::SequentialTableReaderScriptImpl::StateType state_
bool IsOpen()
Definition: kaldi-io-inl.h:34
std::istream & Stream()
Definition: kaldi-io.cc:826
void SplitStringOnFirstSpace(const std::string &str, std::string *first, std::string *rest)
Removes leading and trailing white space from the string, then splits on the first section of whitesp...
Definition: text-utils.cc:120
#define KALDI_ERR
Definition: kaldi-error.h:147
#define KALDI_WARN
Definition: kaldi-error.h:150
int32 Close()
Definition: kaldi-io.cc:761
bool ExtractRangeSpecifier(const std::string &rxfilename_with_range, std::string *data_rxfilename, std::string *range)

◆ Open()

virtual bool Open ( const std::string &  rspecifier)
inlinevirtual

Implements SequentialTableReaderImplBase< Holder >.

Definition at line 95 of file kaldi-table-inl.h.

References kaldi::ClassifyRspecifier(), SequentialTableReaderImplBase< Holder >::Close(), KALDI_ASSERT, KALDI_ERR, KALDI_WARN, kaldi::kScriptRspecifier, SequentialTableReaderImplBase< Holder >::Next(), and kaldi::PrintableRxfilename().

95  {
96  if (state_ != kUninitialized && state_ != kError)
97  if (!Close()) // call Close() yourself to suppress this exception.
98  KALDI_ERR << "Error closing previous input: "
99  << "rspecifier was " << rspecifier_;
100  bool binary;
101  rspecifier_ = rspecifier;
103  &opts_);
105  if (!script_input_.Open(script_rxfilename_, &binary)) { // Failure on Open
106  KALDI_WARN << "Failed to open script file "
109  return false;
110  } else { // Open succeeded.
111  if (binary) {
112  KALDI_WARN << "Script file should not be binary file.";
113  SetErrorState();
114  return false;
115  } else {
116  state_ = kFileStart;
117  Next();
118  if (state_ == kError)
119  return false;
120  // any other status, including kEof, is OK from the point of view of
121  // the 'open' function (empty scp file is not inherently an error).
122  return true;
123  }
124  }
125  }
enum kaldi::SequentialTableReaderScriptImpl::StateType state_
bool Open(const std::string &rxfilename, bool *contents_binary=NULL)
Definition: kaldi-io-inl.h:26
RspecifierType ClassifyRspecifier(const std::string &rspecifier, std::string *rxfilename, RspecifierOptions *opts)
Definition: kaldi-table.cc:225
RspecifierType
Definition: kaldi-table.h:219
#define KALDI_ERR
Definition: kaldi-error.h:147
#define KALDI_WARN
Definition: kaldi-error.h:150
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185
std::string PrintableRxfilename(const std::string &rxfilename)
PrintableRxfilename turns the rxfilename into a more human-readable form for error reporting...
Definition: kaldi-io.cc:61

◆ SetErrorState()

void SetErrorState ( )
inlineprivate

◆ SwapHolder()

void SwapHolder ( Holder *  other_holder)
inlinevirtual

Implements SequentialTableReaderImplBase< Holder >.

Definition at line 191 of file kaldi-table-inl.h.

References KALDI_ERR, and SequentialTableReaderImplBase< Holder >::Value().

191  {
192  // call Value() to ensure we have a value, and ignore its return value while
193  // suppressing compiler warnings by casting to void. It will cause the
194  // program to die with KALDI_ERR if we couldn't get a value.
195  (void) Value();
196  // At this point we know that we successfully loaded an object,
197  // and if there was a range specified, it's in range_holder_.
198  if (state_ == kHaveObject) {
199  holder_.Swap(other_holder);
201  } else if (state_ == kHaveRange) {
202  range_holder_.Swap(other_holder);
204  // This indicates that we still have the base object (but no range).
205  } else {
206  KALDI_ERR << "Code error";
207  }
208  // Note: after this call there may be some junk left in range_holder_ or
209  // holder_, but it won't matter. We avoid calling Clear() on them, as this
210  // function needs to be lightweight for the 'bg' feature to work well.
211  }
enum kaldi::SequentialTableReaderScriptImpl::StateType state_
#define KALDI_ERR
Definition: kaldi-error.h:147

◆ Value()

T& Value ( )
inlinevirtual

Implements SequentialTableReaderImplBase< Holder >.

Definition at line 162 of file kaldi-table-inl.h.

References KALDI_ASSERT, KALDI_ERR, and kaldi::PrintableRxfilename().

162  {
163  if (!EnsureObjectLoaded())
164  KALDI_ERR << "Failed to load object from "
166  << " (to suppress this error, add the permissive "
167  << "(p, ) option to the rspecifier.";
168  // Because EnsureObjectLoaded() returned with success, we know
169  // that if range_ is nonempty (i.e. a range was requested), the
170  // state will be kHaveRange.
171  if (state_ == kHaveRange) {
172  return range_holder_.Value();
173  } else {
175  return holder_.Value();
176  }
177  }
enum kaldi::SequentialTableReaderScriptImpl::StateType state_
#define KALDI_ERR
Definition: kaldi-error.h:147
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185
std::string PrintableRxfilename(const std::string &rxfilename)
PrintableRxfilename turns the rxfilename into a more human-readable form for error reporting...
Definition: kaldi-io.cc:61

Member Data Documentation

◆ data_input_

Input data_input_
private

Definition at line 430 of file kaldi-table-inl.h.

◆ data_rxfilename_

std::string data_rxfilename_
private

Definition at line 445 of file kaldi-table-inl.h.

◆ holder_

Holder holder_
private

Definition at line 435 of file kaldi-table-inl.h.

◆ key_

std::string key_
private

Definition at line 444 of file kaldi-table-inl.h.

◆ opts_

RspecifierOptions opts_
private

Definition at line 426 of file kaldi-table-inl.h.

◆ range_

std::string range_
private

Definition at line 446 of file kaldi-table-inl.h.

◆ range_holder_

Holder range_holder_
private

Definition at line 436 of file kaldi-table-inl.h.

◆ rspecifier_

std::string rspecifier_
private

Definition at line 425 of file kaldi-table-inl.h.

◆ script_input_

Input script_input_
private

Definition at line 429 of file kaldi-table-inl.h.

◆ script_rxfilename_

std::string script_rxfilename_
private

Definition at line 427 of file kaldi-table-inl.h.

◆ state_


The documentation for this class was generated from the following file: