32 bool omit_empty_strings,
33 std::vector<F> *out) {
35 if (*(full.c_str()) ==
'\0') {
39 std::vector<std::string> split;
41 out->resize(split.size());
42 for (
size_t i = 0;
i < split.size();
i++) {
55 bool omit_empty_strings,
56 std::vector<float> *out);
60 bool omit_empty_strings,
61 std::vector<double> *out);
64 bool omit_empty_strings,
65 std::vector<std::string> *out) {
66 size_t start = 0, found = 0, end = full.size();
68 while (found != std::string::npos) {
69 found = full.find_first_of(delim, start);
71 if (!omit_empty_strings || (found != start && start != end))
72 out->push_back(full.substr(start, found - start));
78 const char *delim,
bool omit_empty_strings,
79 std::string *str_out) {
81 for (
size_t i = 0;
i < vec_in.size();
i++) {
82 if (!omit_empty_strings || !vec_in[
i].empty()) {
83 tmp_str.append(vec_in[
i]);
84 if (i < vec_in.size() - 1)
85 if (!omit_empty_strings || !vec_in[i+1].empty())
86 tmp_str.append(delim);
89 str_out->swap(tmp_str);
92 void Trim(std::string *str) {
93 const char *white_chars =
" \t\n\r\f\v";
95 std::string::size_type pos = str->find_last_not_of(white_chars);
96 if (pos != std::string::npos) {
98 pos = str->find_first_not_of(white_chars);
99 if (pos != std::string::npos) str->erase(0, pos);
101 str->erase(str->begin(), str->end());
106 size_t l = token.length();
107 if (l == 0)
return false;
108 for (
size_t i = 0;
i < l;
i++) {
109 unsigned char c = token[
i];
110 if ((!isprint(c) || isspace(c)) && (isascii(c) || c == (
unsigned char)255))
123 const char *white_chars =
" \t\n\r\f\v";
124 typedef std::string::size_type I;
125 const I npos = std::string::npos;
126 I first_nonwhite = str.find_first_not_of(white_chars);
127 if (first_nonwhite == npos) {
133 I next_white = str.find_first_of(white_chars, first_nonwhite);
135 if (next_white == npos) {
136 *first = std::string(str, first_nonwhite);
140 I next_nonwhite = str.find_first_not_of(white_chars, next_white);
141 if (next_nonwhite == npos) {
142 *first = std::string(str, first_nonwhite, next_white-first_nonwhite);
147 I last_nonwhite = str.find_last_not_of(white_chars);
150 *first = std::string(str, first_nonwhite, next_white-first_nonwhite);
151 *rest = std::string(str, next_nonwhite, last_nonwhite+1-next_nonwhite);
155 if (line.find(
'\n') != std::string::npos)
return false;
156 if (line.empty())
return true;
157 if (isspace(*(line.begin())))
return false;
158 if (isspace(*(line.rbegin())))
return false;
159 std::string::const_iterator iter = line.begin(), end = line.end();
160 for (; iter != end; iter++)
161 if (!isprint(*iter))
return false;
171 if (!in_.good())
return *
this;
173 if (!in_.fail() && RemainderIsOnlySpaces())
return *
this;
174 return ParseOnFail(&x);
181 if (in_.tellg() != std::istream::pos_type(-1)) {
185 if (rem.find_first_not_of(
' ') != std::string::npos) {
202 if (!(in_ >> str) || !RemainderIsOnlySpaces()) {
203 in_.setstate(std::ios_base::failbit);
207 std::map<std::string, T> inf_nan_map;
209 inf_nan_map[
"INF"] = std::numeric_limits<T>::infinity();
210 inf_nan_map[
"+INF"] = std::numeric_limits<T>::infinity();
211 inf_nan_map[
"-INF"] = - std::numeric_limits<T>::infinity();
212 inf_nan_map[
"INFINITY"] = std::numeric_limits<T>::infinity();
213 inf_nan_map[
"+INFINITY"] = std::numeric_limits<T>::infinity();
214 inf_nan_map[
"-INFINITY"] = - std::numeric_limits<T>::infinity();
215 inf_nan_map[
"NAN"] = std::numeric_limits<T>::quiet_NaN();
216 inf_nan_map[
"+NAN"] = std::numeric_limits<T>::quiet_NaN();
217 inf_nan_map[
"-NAN"] = - std::numeric_limits<T>::quiet_NaN();
219 inf_nan_map[
"1.#INF"] = std::numeric_limits<T>::infinity();
220 inf_nan_map[
"-1.#INF"] = - std::numeric_limits<T>::infinity();
221 inf_nan_map[
"1.#QNAN"] = std::numeric_limits<T>::quiet_NaN();
222 inf_nan_map[
"-1.#QNAN"] = - std::numeric_limits<T>::quiet_NaN();
224 std::transform(str.begin(), str.end(), str.begin(), ::toupper);
226 if (inf_nan_map.find(str) != inf_nan_map.end()) {
227 *x = inf_nan_map[str];
229 in_.setstate(std::ios_base::failbit);
237 template <
typename T>
240 std::istringstream iss(str);
287 int32 decimal_places_tolerance,
288 int32 places_into_number) {
290 char ca = *a, cb = *b;
295 if (places_into_number >= 0) {
297 places_into_number++;
299 places_into_number = -1;
303 places_into_number = 0;
311 if (places_into_number >= decimal_places_tolerance &&
312 (isdigit(ca) || isdigit(cb))) {
315 if (isdigit(ca)) a++;
316 if (isdigit(cb)) b++;
319 }
else if (places_into_number >= 0 &&
320 ((ca ==
'0' && !isdigit(cb)) || (cb ==
'0' && !isdigit(ca)))) {
325 places_into_number++;
336 const std::string &b,
337 int32 decimal_places_tolerance) {
339 decimal_places_tolerance, -1);
346 if (line.size() == 0)
return false;
347 size_t pos = 0, size = line.size();
348 while (isspace(line[pos]) && pos < size) pos++;
351 size_t first_token_start_pos = pos;
353 while (!isspace(line[pos]) && pos < size) {
354 if (line[pos] ==
'=') {
358 pos = first_token_start_pos;
363 first_token_ = std::string(line, first_token_start_pos, pos - first_token_start_pos);
371 if (isspace(line[pos])) {
377 size_t next_equals_sign = line.find_first_of(
"=", pos);
378 if (next_equals_sign == pos || next_equals_sign == std::string::npos) {
383 std::string key(line, pos, next_equals_sign - pos);
388 if (line[next_equals_sign+1] ==
'\'' || line[next_equals_sign+1] ==
'"') {
389 char my_quote = line[next_equals_sign+1];
390 size_t next_quote = line.find_first_of(my_quote, next_equals_sign + 2);
391 if (next_quote == std::string::npos) {
392 KALDI_WARN <<
"No matching quote for " << my_quote <<
" in config line '" 396 std::string value(line, next_equals_sign + 2,
397 next_quote - next_equals_sign - 2);
398 data_.insert(std::make_pair(key, std::make_pair(value,
false)));
399 pos = next_quote + 1;
406 size_t next_next_equals_sign = line.find_first_of(
"=", next_equals_sign + 1),
407 terminating_space = size;
409 if (next_next_equals_sign != std::string::npos) {
410 size_t preceding_space = line.find_last_of(
" \t", next_next_equals_sign);
411 if (preceding_space != std::string::npos &&
412 preceding_space > next_equals_sign)
413 terminating_space = preceding_space;
415 while (isspace(line[terminating_space - 1]) && terminating_space > 0)
418 std::string value(line, next_equals_sign + 1,
419 terminating_space - (next_equals_sign + 1));
420 data_.insert(std::make_pair(key, std::make_pair(value,
false)));
421 pos = terminating_space;
429 std::map<std::string, std::pair<std::string, bool> >::iterator it =
data_.begin();
430 for (; it !=
data_.end(); ++it) {
431 if (it->first == key) {
432 *value = (it->second).first;
433 (it->second).second =
true;
442 std::map<std::string, std::pair<std::string, bool> >::iterator it =
data_.begin();
443 for (; it !=
data_.end(); ++it) {
444 if (it->first == key) {
447 (it->second).second =
true;
456 std::map<std::string, std::pair<std::string, bool> >::iterator it =
data_.begin();
457 for (; it !=
data_.end(); ++it) {
458 if (it->first == key) {
461 (it->second).second =
true;
471 std::map<std::string, std::pair<std::string, bool> >::iterator it =
data_.begin();
472 for (; it !=
data_.end(); ++it) {
473 if (it->first == key) {
478 (it->second).second =
true;
487 std::map<std::string, std::pair<std::string, bool> >::iterator it =
data_.begin();
488 for (; it !=
data_.end(); ++it) {
489 if (it->first == key) {
490 if ((it->second).first.size() == 0)
return false;
491 switch (((it->second).first)[0]) {
503 (it->second).second =
true;
511 std::map<std::string, std::pair<std::string, bool> >::const_iterator it =
data_.begin();
512 for (; it !=
data_.end(); ++it) {
513 if (!(it->second).second)
return true;
519 std::string unused_str;
520 std::map<std::string, std::pair<std::string, bool> >::const_iterator it =
data_.begin();
521 for (; it !=
data_.end(); ++it) {
522 if (!(it->second).second) {
523 if (unused_str ==
"")
524 unused_str = it->first +
"=" + (it->second).first;
526 unused_str +=
" " + it->first +
"=" + (it->second).first;
537 const std::string &token1,
538 const std::string &token2) {
542 if (temp == token1) {
545 if (temp != token2) {
546 KALDI_ERR <<
"Expecting token " << token1 <<
" or " << token2
547 <<
" but got " << temp;
554 if (name.size() == 0)
return false;
555 for (
size_t i = 0;
i < name.size();
i++) {
556 if (
i == 0 && !isalpha(name[
i]) && name[
i] !=
'_')
558 if (!isalnum(name[i]) && name[
i] !=
'_' && name[
i] !=
'-' && name[
i] !=
'.')
565 std::vector<std::string> *lines) {
568 while (std::getline(is, line)) {
569 if (line.size() == 0)
continue;
570 size_t start = line.find_first_not_of(
" \t");
571 size_t end = line.find_first_of(
'#');
572 if (start == std::string::npos || start == end)
continue;
573 end = line.find_last_not_of(
" \t", end - 1);
575 lines->push_back(line.substr(start, end - start + 1));
580 std::vector<ConfigLine> *config_lines) {
581 config_lines->resize(lines.size());
582 for (
size_t i = 0;
i < lines.size();
i++) {
583 bool ret = (*config_lines)[
i].ParseLine(lines[
i]);
585 KALDI_ERR <<
"Error parsing config line: " << lines[
i];
This code computes Goodness of Pronunciation (GOP) and extracts phone-level pronunciation feature for...
bool ConvertStringToInteger(const std::string &str, Int *out)
Converts a string into an integer via strtoll and returns false if there was any kind of problem (i...
bool ParseLine(const std::string &line)
bool SplitStringToFloats(const std::string &full, const char *delim, bool omit_empty_strings, std::vector< F > *out)
bool IsLine(const std::string &line)
Returns true if "line" is free of characters and unprintable characters, and does not contain leadi...
bool SplitStringToIntegers(const std::string &full, const char *delim, bool omit_empty_strings, std::vector< I > *out)
Split a string (e.g.
void JoinVectorToString(const std::vector< std::string > &vec_in, const char *delim, bool omit_empty_strings, std::string *str_out)
Joins the elements of a vector of strings into a single string using "delim" as the delimiter...
void ReadToken(std::istream &is, bool binary, std::string *str)
ReadToken gets the next token and puts it in str (exception on failure).
bool IsValidName(const std::string &name)
Returns true if 'name' would be a valid name for a component or node in a nnet3Nnet.
bool IsToken(const std::string &token)
Returns true if "token" is nonempty, and all characters are printable and whitespace-free.
void ExpectOneOrTwoTokens(std::istream &is, bool binary, const std::string &token1, const std::string &token2)
This function is like ExpectToken but for two tokens, and it will either accept token1 and then token...
std::string UnusedValues() const
returns e.g.
bool StringsApproxEqual(const std::string &a, const std::string &b, int32 decimal_places_tolerance)
This function returns true when two text strings are approximately equal, and false when they are not...
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...
void ExpectToken(std::istream &is, bool binary, const char *token)
ExpectToken tries to read in the given token, and throws an exception on failure. ...
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.
bool RemainderIsOnlySpaces()
NumberIstream(std::istream &i)
bool ConvertStringToReal(const std::string &str, T *out)
ConvertStringToReal converts a string into either float or double and returns false if there was any ...
void ReadConfigLines(std::istream &is, std::vector< std::string > *lines)
This function reads in a config file and *appends* its contents to a vector of lines; it is responsib...
void Trim(std::string *str)
Removes the beginning and trailing whitespaces from a string.
std::istream & operator>>(std::istream &is, Matrix< Real > &M)
bool StringsApproxEqualInternal(const char *a, const char *b, int32 decimal_places_tolerance, int32 places_into_number)
#define KALDI_ASSERT(cond)
bool HasUnusedValues() const
void ParseConfigLines(const std::vector< std::string > &lines, std::vector< ConfigLine > *config_lines)
This function converts config-lines from a simple sequence of strings as output by ReadConfigLines()...
bool GetValue(const std::string &key, std::string *value)
NumberIstream & ParseOnFail(T *x)
std::map< std::string, std::pair< std::string, bool > > data_