22 #ifndef KALDI_UTIL_KALDI_HOLDER_INL_H_ 23 #define KALDI_UTIL_KALDI_HOLDER_INL_H_ 51 static bool Write(std::ostream &os,
bool binary,
const T &t) {
56 }
catch(
const std::exception &e) {
57 KALDI_WARN <<
"Exception caught writing Table object. " << e.what();
70 bool Read(std::istream &is) {
77 KALDI_WARN <<
"Reading Table object, failed reading binary header\n";
81 t_->Read(is, is_binary);
83 }
catch(
const std::exception &e) {
84 KALDI_WARN <<
"Exception caught reading Table object. " << e.what();
97 if (!
t_)
KALDI_ERR <<
"KaldiObjectHolder::Value() called wrongly.";
107 const std::string &range) {
133 static bool Write(std::ostream &os,
bool binary,
const T &t) {
137 if (!binary) os <<
'\n';
140 }
catch(
const std::exception &e) {
141 KALDI_WARN <<
"Exception caught writing Table object. " << e.what();
152 KALDI_WARN <<
"Reading Table object [integer type], failed reading binary" 161 while (isspace((c = is.peek())) && c !=
static_cast<int>(
'\n')) {
164 if (is.peek() ==
'\n') {
165 KALDI_WARN <<
"Found newline but expected basic type.";
177 while (isspace((c = is.peek())) && c !=
static_cast<int>(
'\n')) {
180 if (is.peek() !=
'\n') {
181 KALDI_WARN <<
"BasicHolder::Read, expected newline, got " 182 <<
CharToString(is.peek()) <<
", position " << is.tellg();
188 }
catch(
const std::exception &e) {
189 KALDI_WARN <<
"Exception caught reading Table object. " << e.what();
207 KALDI_ERR <<
"ExtractRange is not defined for this type of holder.";
226 typedef std::vector<BasicType>
T;
230 static bool Write(std::ostream &os,
bool binary,
const T &t) {
234 KALDI_ASSERT(static_cast<size_t>(static_cast<int32>(t.size())) ==
240 for (
typename std::vector<BasicType>::const_iterator iter = t.begin();
241 iter != t.end(); ++iter)
245 for (
typename std::vector<BasicType>::const_iterator iter = t.begin();
246 iter != t.end(); ++iter)
253 }
catch(
const std::exception &e) {
254 KALDI_WARN <<
"Exception caught writing Table object (BasicVector). " 267 KALDI_WARN <<
"Reading Table object [integer type], failed reading binary" 276 KALDI_WARN <<
"BasicVectorHolder::Read, error reading line " <<
277 (is.eof() ?
"[eof]" :
"");
280 std::istringstream line_is(line);
284 if (line_is.eof())
break;
290 }
catch(
const std::exception &e) {
291 KALDI_WARN <<
"BasicVectorHolder::Read, could not interpret line: " 292 <<
"'" << line <<
"'" <<
"\n" << e.what();
296 size_t filepos = is.tellg();
301 for (
typename std::vector<BasicType>::iterator iter =
t_.begin();
308 KALDI_WARN <<
"BasicVectorHolder::Read, read error or unexpected data" 309 " at archive entry beginning at file position " << filepos;
326 const std::string &range) {
327 KALDI_ERR <<
"ExtractRange is not defined for this type of holder.";
345 typedef std::vector<std::vector<BasicType> >
T;
349 static bool Write(std::ostream &os,
bool binary,
const T &t) {
353 KALDI_ASSERT(static_cast<size_t>(static_cast<int32>(t.size())) ==
359 for (
typename std::vector<std::vector<BasicType> >::const_iterator
361 iter != t.end(); ++iter) {
362 KALDI_ASSERT(static_cast<size_t>(static_cast<int32>(iter->size()))
365 for (
typename std::vector<BasicType>::const_iterator
366 iter2 = iter->begin();
367 iter2 != iter->end(); ++iter2) {
377 for (
typename std::vector<std::vector<BasicType> >::const_iterator
381 for (
typename std::vector<BasicType>::const_iterator
382 iter2 = iter->begin();
383 iter2 != iter->end(); ++iter2)
390 }
catch(
const std::exception &e) {
391 KALDI_WARN <<
"Exception caught writing Table object. " << e.what();
403 KALDI_WARN <<
"Failed reading binary header\n";
409 std::vector<BasicType> v;
415 }
else if (static_cast<char>(i) ==
'\n') {
417 KALDI_WARN <<
"No semicolon before newline (wrong format)";
423 }
else if (std::isspace(i)) {
425 }
else if (static_cast<char>(i) ==
';') {
435 }
catch(
const std::exception &e) {
436 KALDI_WARN <<
"BasicVectorVectorHolder::Read, read error. " << e.what();
440 size_t filepos = is.tellg();
445 for (
typename std::vector<std::vector<BasicType> >::iterator
452 for (
typename std::vector<BasicType>::iterator iter2 = iter->begin();
453 iter2 != iter->end();
459 KALDI_WARN <<
"Read error or unexpected data at archive entry beginning" 460 " at file position " << filepos;
477 const std::string &range) {
478 KALDI_ERR <<
"ExtractRange is not defined for this type of holder.";
496 typedef std::vector<std::pair<BasicType, BasicType> >
T;
500 static bool Write(std::ostream &os,
bool binary,
const T &t) {
504 KALDI_ASSERT(static_cast<size_t>(static_cast<int32>(t.size())) ==
510 for (
typename T::const_iterator iter = t.begin();
511 iter != t.end(); ++iter) {
519 for (
typename T::const_iterator iter = t.begin();
530 }
catch(
const std::exception &e) {
531 KALDI_WARN <<
"Exception caught writing Table object. " << e.what();
543 KALDI_WARN <<
"Reading Table object [integer type], failed reading binary" 550 std::vector<BasicType> v;
556 }
else if (static_cast<char>(i) ==
'\n') {
557 if (
t_.empty() && v.empty()) {
560 }
else if (v.size() == 2) {
561 t_.push_back(std::make_pair(v[0], v[1]));
565 KALDI_WARN <<
"Unexpected newline, reading vector<pair<?> >; got " 566 << v.size() <<
" elements, expected 2.";
569 }
else if (std::isspace(i)) {
571 }
else if (static_cast<char>(i) ==
';') {
573 KALDI_WARN <<
"Wrong input format, reading vector<pair<?> >; got " 574 << v.size() <<
" elements, expected 2.";
577 t_.push_back(std::make_pair(v[0], v[1]));
586 }
catch(
const std::exception &e) {
587 KALDI_WARN <<
"BasicPairVectorHolder::Read, read error. " << e.what();
591 size_t filepos = is.tellg();
596 for (
typename T::iterator iter =
t_.begin();
604 KALDI_WARN <<
"BasicVectorHolder::Read, read error or unexpected data" 605 " at archive entry beginning at file position " << filepos;
622 const std::string &range) {
623 KALDI_ERR <<
"ExtractRange is not defined for this type of holder.";
641 typedef std::string
T;
645 static bool Write(std::ostream &os,
bool,
const T &t) {
656 if (is.fail())
return false;
658 while (isspace(c = is.peek()) && c!=
'\n') is.get();
659 if (is.peek() !=
'\n') {
660 KALDI_WARN <<
"TokenHolder::Read, expected newline, got char " 662 <<
", at stream pos " << is.tellg();
683 const std::string &range) {
684 KALDI_ERR <<
"ExtractRange is not defined for this type of holder.";
697 typedef std::vector<std::string>
T;
701 static bool Write(std::ostream &os,
bool,
const T &t) {
702 for (std::vector<std::string>::const_iterator iter = t.begin();
725 KALDI_WARN <<
"BasicVectorHolder::Read, error reading line " << (is.eof()
729 const char *white_chars =
" \t\n\r\f\v";
746 const std::string &range) {
747 KALDI_ERR <<
"ExtractRange is not defined for this type of holder.";
763 static bool Write(std::ostream &os,
bool binary,
const T &t) {
765 KALDI_ERR <<
"Non-binary HTK-format write not supported.";
766 bool ans =
WriteHtk(os, t.first, t.second);
768 KALDI_WARN <<
"Error detected writing HTK-format matrix.";
778 KALDI_WARN <<
"Error detected reading HTK-format matrix.";
790 t_.first.Swap(&(other->
t_.first));
795 const std::string &range) {
796 KALDI_ERR <<
"ExtractRange is not defined for this type of holder.";
822 void Clear() { feats_.Resize(0, 0); }
825 static bool Write(std::ostream &os,
bool binary,
const T &m) {
827 KALDI_WARN <<
"SphinxMatrixHolder can't write Sphinx features in text ";
835 os.write(reinterpret_cast<char*> (&size),
sizeof(size));
838 std::vector<float32> tmp(m.
NumCols());
844 os.write(reinterpret_cast<char*>(&(tmp[0])),
854 is.read(reinterpret_cast<char*> (&nmfcc),
sizeof(nmfcc));
858 int32 nfvec = nmfcc / kFeatDim;
859 if ((nmfcc % kFeatDim) != 0) {
860 KALDI_WARN <<
"Sphinx feature count is inconsistent with vector length ";
864 feats_.Resize(nfvec, kFeatDim);
867 is.read(reinterpret_cast<char*> (feats_.RowData(
i)),
870 KALDI_WARN <<
"Unexpected error/EOF while reading Sphinx features ";
879 is.read(reinterpret_cast<char*> (tmp),
sizeof(tmp));
881 KALDI_WARN <<
"Unexpected error/EOF while reading Sphinx features ";
901 feats_.Swap(&(other->
feats_));
905 const std::string &range) {
906 KALDI_ERR <<
"ExtractRange is not defined for this type of holder.";
922 #endif // KALDI_UTIL_KALDI_HOLDER_INL_H_
This code computes Goodness of Pronunciation (GOP) and extracts phone-level pronunciation feature for...
bool InitKaldiInputStream(std::istream &is, bool *binary)
Initialize an opened stream for reading by detecting the binary header and.
KaldiObjectHolder works for Kaldi objects that have the "standard" Read and Write functions...
bool ExtractRange(const BasicVectorHolder< BasicType > &other, const std::string &range)
bool ExtractRange(BasicVectorVectorHolder< BasicType > &other, const std::string &range)
static bool Write(std::ostream &os, bool, const T &t)
BasicVectorVectorHolder()
bool Read(std::istream &is)
void Swap(TokenVectorHolder *other)
MatrixIndexT NumCols() const
Returns number of columns (or zero for empty matrix).
bool ExtractRange(const HtkMatrixHolder &other, const std::string &range)
void ReadBasicType(std::istream &is, bool binary, T *t)
ReadBasicType is the name of the read function for bool, integer types, and floating-point types...
std::vector< BasicType > T
bool WriteHtk(std::ostream &os, const MatrixBase< Real > &M, HtkHeader htk_hdr)
A Holder for a vector of basic types, e.g.
bool ExtractRange(const BasicHolder< T > &other, const std::string &range)
static bool Write(std::ostream &os, bool binary, const T &t)
~BasicVectorVectorHolder()
std::vector< std::pair< BasicType, BasicType > > T
void Swap(BasicHolder< T > *other)
bool ExtractRange(const BasicPairVectorHolder< BasicType > &other, const std::string &range)
bool Read(std::istream &is)
bool ExtractRange(const TokenHolder &other, const std::string &range)
void swap(basic_filebuf< CharT, Traits > &x, basic_filebuf< CharT, Traits > &y)
bool Read(std::istream &is)
void Swap(BasicPairVectorHolder< BasicType > *other)
A class for reading/writing Sphinx format matrices.
bool IsToken(const std::string &token)
Returns true if "token" is nonempty, and all characters are printable and whitespace-free.
void Swap(BasicVectorHolder< BasicType > *other)
static bool Write(std::ostream &os, bool binary, const T &t)
bool Read(std::istream &is)
static bool IsReadInBinary()
static bool IsReadInBinary()
static bool Write(std::ostream &os, bool binary, const T &m)
bool ExtractObjectRange(const GeneralMatrix &input, const std::string &range, GeneralMatrix *output)
GeneralMatrix is always of type BaseFloat.
BasicHolder is valid for float, double, bool, and integer types.
bool Read(std::istream &is)
std::pair< Matrix< BaseFloat >, HtkHeader > T
static bool Write(std::ostream &os, bool binary, const T &t)
KALDI_DISALLOW_COPY_AND_ASSIGN(KaldiObjectHolder)
static bool Write(std::ostream &os, bool binary, const T &t)
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.
static bool IsReadInBinary()
bool Read(std::istream &is)
int MachineIsLittleEndian()
std::vector< std::string > T
void Swap(SphinxMatrixHolder *other)
BasicVectorVectorHolder is a Holder for a vector of vector of a basic type, e.g.
static bool IsReadInBinary()
static bool Write(std::ostream &os, bool binary, const T &t)
void Swap(BasicVectorVectorHolder< BasicType > *other)
std::vector< std::vector< BasicType > > T
bool ExtractRange(const KaldiObjectHolder< T > &other, const std::string &range)
std::string CharToString(const char &c)
bool Read(std::istream &is)
#define KALDI_ASSERT(cond)
static bool Write(std::ostream &os, bool, const T &t)
MatrixIndexT NumRows() const
Returns number of rows (or zero for empty matrix).
BasicPairVectorHolder is a Holder for a vector of pairs of a basic type, e.g.
static bool IsReadInBinary()
bool ExtractRange(const TokenVectorHolder &other, const std::string &range)
void WriteBasicType(std::ostream &os, bool binary, T t)
WriteBasicType is the name of the write function for bool, integer types, and floating-point types...
static bool IsReadInBinary()
void InitKaldiOutputStream(std::ostream &os, bool binary)
InitKaldiOutputStream initializes an opened stream for writing by writing an optional binary header a...
static bool IsReadInBinary()
bool Read(std::istream &is)
void Swap(KaldiObjectHolder< T > *other)
static bool IsReadInBinary()
bool ExtractRange(const SphinxMatrixHolder &other, const std::string &range)
static bool Write(std::ostream &os, bool binary, const T &t)
bool Read(std::istream &is)
bool ReadHtk(std::istream &is, Matrix< Real > *M_ptr, HtkHeader *header_ptr)
Extension of the HTK header.
static bool IsReadInBinary()
void Swap(HtkMatrixHolder *other)
void Swap(TokenHolder *other)