TableWriterScriptImpl< Holder > Class Template Reference

#include <kaldi-table-inl.h>

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

Public Types

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

Public Member Functions

 TableWriterScriptImpl ()
 
virtual bool Open (const std::string &wspecifier)
 
virtual bool IsOpen () const
 
virtual bool Close ()
 
virtual bool Write (const std::string &key, const T &value)
 
virtual void Flush ()
 
virtual ~TableWriterScriptImpl ()
 
- Public Member Functions inherited from TableWriterImplBase< Holder >
virtual ~TableWriterImplBase ()
 
 TableWriterImplBase ()
 

Private Types

enum  { kUninitialized, kReadScript, kNotReadScript }
 

Private Member Functions

bool LookupFilename (const std::string &key, std::string *wxfilename)
 

Private Attributes

WspecifierOptions opts_
 
std::string wspecifier_
 
std::string script_rxfilename_
 
std::vector< std::pair< std::string, std::string > > script_
 
size_t last_found_
 
enum kaldi::TableWriterScriptImpl:: { ... }  state_
 

Detailed Description

template<class Holder>
class kaldi::TableWriterScriptImpl< Holder >

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

Member Typedef Documentation

◆ T

typedef Holder::T T

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

Member Enumeration Documentation

◆ anonymous enum

anonymous enum
private
Enumerator
kUninitialized 
kReadScript 
kNotReadScript 

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

Constructor & Destructor Documentation

◆ TableWriterScriptImpl()

◆ ~TableWriterScriptImpl()

virtual ~TableWriterScriptImpl ( )
inlinevirtual

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

1233  {
1234  // Nothing to do in destructor.
1235  }

Member Function Documentation

◆ Close()

virtual bool Close ( )
inlinevirtual

Implements TableWriterImplBase< Holder >.

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

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

1180  {
1181  if (!IsOpen())
1182  KALDI_ERR << "Close() called on TableWriter that was not open.";
1184  last_found_ = 0;
1185  script_.clear();
1186  return true;
1187  }
enum kaldi::TableWriterScriptImpl::@2 state_
std::vector< std::pair< std::string, std::string > > script_
#define KALDI_ERR
Definition: kaldi-error.h:147
virtual bool IsOpen() const

◆ Flush()

virtual void Flush ( )
inlinevirtual

Implements TableWriterImplBase< Holder >.

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

1230 { }

◆ IsOpen()

virtual bool IsOpen ( ) const
inlinevirtual

Implements TableWriterImplBase< Holder >.

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

1178 { return (state_ == kReadScript); }
enum kaldi::TableWriterScriptImpl::@2 state_

◆ LookupFilename()

bool LookupFilename ( const std::string &  key,
std::string *  wxfilename 
)
inlineprivate

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

1240  {
1241  // First, an optimization: if we're going consecutively, this will
1242  // make the lookup very fast.
1243  last_found_++;
1244  if (last_found_ < script_.size() && script_[last_found_].first == key) {
1245  *wxfilename = script_[last_found_].second;
1246  return true;
1247  }
1248  std::pair<std::string, std::string> pr(key, ""); // Important that ""
1249  // compares less than or equal to any string, so lower_bound points to the
1250  // element that has the same key.
1251  typedef typename std::vector<std::pair<std::string, std::string> >
1252  ::const_iterator IterType;
1253  IterType iter = std::lower_bound(script_.begin(), script_.end(), pr);
1254  if (iter != script_.end() && iter->first == key) {
1255  last_found_ = iter - script_.begin();
1256  *wxfilename = iter->second;
1257  return true;
1258  } else {
1259  return false;
1260  }
1261  }
std::vector< std::pair< std::string, std::string > > script_

◆ Open()

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

Implements TableWriterImplBase< Holder >.

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

References kaldi::ClassifyWspecifier(), rnnlm::i, KALDI_ASSERT, KALDI_ERR, KALDI_WARN, kaldi::kScriptWspecifier, kaldi::PrintableRxfilename(), and kaldi::ReadScriptFile().

1142  {
1143  switch (state_) {
1144  case kReadScript:
1145  KALDI_ERR << " Opening already open TableWriter: call Close first.";
1146  case kUninitialized: case kNotReadScript:
1147  break;
1148  }
1149  wspecifier_ = wspecifier;
1150  WspecifierType ws = ClassifyWspecifier(wspecifier,
1151  NULL,
1153  &opts_);
1154  KALDI_ASSERT(ws == kScriptWspecifier); // or wrongly called.
1155  KALDI_ASSERT(script_.empty()); // no way it could be nonempty at this point.
1156 
1158  true, // print any warnings
1159  &script_)) { // error reading script file or invalid
1160  // format
1162  return false; // no need to print further warnings. user gets the error.
1163  }
1164  std::sort(script_.begin(), script_.end());
1165  for (size_t i = 0; i+1 < script_.size(); i++) {
1166  if (script_[i].first.compare(script_[i+1].first) >= 0) {
1167  // script[i] not < script[i+1] in lexical order...
1168  KALDI_WARN << "Script file " << PrintableRxfilename(script_rxfilename_)
1169  << " contains duplicate key " << script_[i].first;
1171  return false;
1172  }
1173  }
1174  state_ = kReadScript;
1175  return true;
1176  }
enum kaldi::TableWriterScriptImpl::@2 state_
std::vector< std::pair< std::string, std::string > > script_
#define KALDI_ERR
Definition: kaldi-error.h:147
#define KALDI_WARN
Definition: kaldi-error.h:150
WspecifierType
Definition: kaldi-table.h:106
WspecifierType ClassifyWspecifier(const std::string &wspecifier, std::string *archive_wxfilename, std::string *script_wxfilename, WspecifierOptions *opts)
Definition: kaldi-table.cc:135
bool ReadScriptFile(const std::string &rxfilename, bool warn, std::vector< std::pair< std::string, std::string > > *script_out)
Definition: kaldi-table.cc:26
#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

◆ Write()

virtual bool Write ( const std::string &  key,
const T value 
)
inlinevirtual

Implements TableWriterImplBase< Holder >.

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

References Output::Close(), SequentialTableReader< Holder >::IsOpen(), kaldi::IsToken(), KALDI_ERR, KALDI_WARN, Output::Open(), kaldi::PrintableRxfilename(), kaldi::PrintableWxfilename(), and Output::Stream().

1191  {
1192  if (!IsOpen())
1193  KALDI_ERR << "Write called on invalid stream";
1194 
1195  if (!IsToken(key)) // e.g. empty string or has spaces...
1196  KALDI_ERR << "Using invalid key " << key;
1197 
1198  std::string wxfilename;
1199  if (!LookupFilename(key, &wxfilename)) {
1200  if (opts_.permissive) {
1201  return true; // In permissive mode, it's as if we're writing to
1202  // /dev/null for missing keys.
1203  } else {
1204  KALDI_WARN << "Script file "
1206  << " has no entry for key " <<key;
1207  return false;
1208  }
1209  }
1210  Output output;
1211  if (!output.Open(wxfilename, opts_.binary, false)) {
1212  // Open in the text/binary mode (on Windows) given by member var. "binary"
1213  // (obtained from wspecifier), but do not put the binary-mode header (it
1214  // will be written, if needed, by the Holder::Write function.)
1215  KALDI_WARN << "Failed to open stream: "
1216  << PrintableWxfilename(wxfilename);
1217  return false;
1218  }
1219  if (!Holder::Write(output.Stream(), opts_.binary, value)
1220  || !output.Close()) {
1221  KALDI_WARN << "Failed to write data to "
1222  << PrintableWxfilename(wxfilename);
1223 
1224  return false;
1225  }
1226  return true;
1227  }
bool LookupFilename(const std::string &key, std::string *wxfilename)
bool IsToken(const std::string &token)
Returns true if "token" is nonempty, and all characters are printable and whitespace-free.
Definition: text-utils.cc:105
#define KALDI_ERR
Definition: kaldi-error.h:147
#define KALDI_WARN
Definition: kaldi-error.h:150
virtual bool IsOpen() const
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
std::string PrintableWxfilename(const std::string &wxfilename)
PrintableWxfilename turns the wxfilename into a more human-readable form for error reporting...
Definition: kaldi-io.cc:73

Member Data Documentation

◆ last_found_

size_t last_found_
private

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

◆ opts_

WspecifierOptions opts_
private

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

◆ script_

std::vector<std::pair<std::string, std::string> > script_
private

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

◆ script_rxfilename_

std::string script_rxfilename_
private

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

◆ state_

enum { ... } state_

◆ wspecifier_

std::string wspecifier_
private

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


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