Repository navigation
Expand file tree
/
Copy pathtext-utils.cc
More file actions
255 lines (219 loc) · 7.78 KB
/
Copy pathtext-utils.cc
File metadata and controls
255 lines (219 loc) · 7.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
// util/text-utils.cc
// Copyright 2009-2011 Saarland University; Microsoft Corporation
// See ../../COPYING for clarification regarding multiple authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// THIS CODE IS PROVIDED *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED
// WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
// MERCHANTABLITY OR NON-INFRINGEMENT.
// See the Apache 2 License for the specific language governing permissions and
// limitations under the License.
#include "util/text-utils.h"
#include <limits>
#include "base/kaldi-common.h"
namespace kaldi {
template<class F>
bool SplitStringToFloats(const std::string &full,
const char *delim,
bool omit_empty_strings, // typically false
std::vector<F> *out) {
KALDI_ASSERT(out != NULL);
if (*(full.c_str()) == '\0') {
out->clear();
return true;
}
std::vector<std::string> split;
SplitStringToVector(full, delim, omit_empty_strings, &split);
out->resize(split.size());
for (size_t i = 0; i < split.size(); i++) {
F f = 0;
if (!ConvertStringToReal(split[i], &f))
return false;
(*out)[i] = f;
}
return true;
}
// Instantiate the template above for float and double.
template
bool SplitStringToFloats(const std::string &full,
const char *delim,
bool omit_empty_strings,
std::vector<float> *out);
template
bool SplitStringToFloats(const std::string &full,
const char *delim,
bool omit_empty_strings,
std::vector<double> *out);
void SplitStringToVector(const std::string &full, const char *delim,
bool omit_empty_strings,
std::vector<std::string> *out) {
size_t start = 0, found = 0, end = full.size();
out->clear();
while (found != std::string::npos) {
found = full.find_first_of(delim, start);
// start != end condition is for when the delimiter is at the end
if (!omit_empty_strings || (found != start && start != end))
out->push_back(full.substr(start, found - start));
start = found + 1;
}
}
void JoinVectorToString(const std::vector<std::string> &vec_in,
const char *delim, bool omit_empty_strings,
std::string *str_out) {
std::string tmp_str;
for (size_t i = 0; i < vec_in.size(); i++) {
if (!omit_empty_strings || !vec_in[i].empty()) {
tmp_str.append(vec_in[i]);
if (i < vec_in.size() - 1)
if (!omit_empty_strings || !vec_in[i+1].empty())
tmp_str.append(delim);
}
}
str_out->swap(tmp_str);
}
void Trim(std::string *str) {
const char *white_chars = " \t\n\r\f\v";
std::string::size_type pos = str->find_last_not_of(white_chars);
if (pos != std::string::npos) {
str->erase(pos + 1);
pos = str->find_first_not_of(white_chars);
if (pos != std::string::npos) str->erase(0, pos);
} else {
str->erase(str->begin(), str->end());
}
}
bool IsToken(const std::string &token) {
size_t l = token.length();
if (l == 0) return false;
for (size_t i = 0; i < l; i++) {
unsigned char c = token[i];
if ((!isprint(c) || isspace(c)) && (isascii(c) || c == (unsigned char)255))
return false;
// The "&& (isascii(c) || c == 255)" was added so that we won't reject
// non-ASCII characters such as French characters with accents [except for
// 255 which is "nbsp", a form of space].
}
return true;
}
void SplitStringOnFirstSpace(const std::string &str,
std::string *first,
std::string *rest) {
const char *white_chars = " \t\n\r\f\v";
typedef std::string::size_type I;
const I npos = std::string::npos;
I first_nonwhite = str.find_first_not_of(white_chars);
if (first_nonwhite == npos) {
first->clear();
rest->clear();
return;
}
// next_white is first whitespace after first nonwhitespace.
I next_white = str.find_first_of(white_chars, first_nonwhite);
if (next_white == npos) { // no more whitespace...
*first = std::string(str, first_nonwhite);
rest->clear();
return;
}
I next_nonwhite = str.find_first_not_of(white_chars, next_white);
if (next_nonwhite == npos) {
*first = std::string(str, first_nonwhite, next_white-first_nonwhite);
rest->clear();
return;
}
I last_nonwhite = str.find_last_not_of(white_chars);
KALDI_ASSERT(last_nonwhite != npos); // or coding error.
*first = std::string(str, first_nonwhite, next_white-first_nonwhite);
*rest = std::string(str, next_nonwhite, last_nonwhite+1-next_nonwhite);
}
bool IsLine(const std::string &line) {
if (line.find('\n') != std::string::npos) return false;
if (line.empty()) return true;
if (isspace(*(line.begin()))) return false;
if (isspace(*(line.rbegin()))) return false;
std::string::const_iterator iter = line.begin(), end = line.end();
for (; iter!= end; iter++)
if (!isprint(*iter)) return false;
return true;
}
inline bool starts_with(const std::string &in, const std::string &prefix) {
return in.substr(0, prefix.size()) == prefix;
}
inline bool stricmp(const std::string &in, const std::string &prefix) {
int ret = KALDI_STRCASECMP(in.c_str(), prefix.c_str());
return ret == 0;
}
inline bool is_nan_text(const std::string &in, const std::string &prefix) {
if (in.size() < prefix.size())
return false;
if (stricmp(in, prefix))
return true;
for (int i = 0; i < prefix.size(); ++i)
if (tolower(in[i]) != tolower(prefix[i]))
return false;
for (int i = prefix.size(); i < in.size(); ++i)
if (!isalpha(in[i]) && (in[i] != '_'))
return false;
return true;
}
template<typename T>
bool convert_special_number(const std::string &str, T *out) {
if (stricmp(str, "infinity") || stricmp(str, "inf") ||
starts_with(str, "1.#INF")) {
*out = std::numeric_limits<T>::infinity();
return true;
} else if (stricmp(str, "-infinity") || stricmp(str, "-inf") ||
starts_with(str, "-1.#INF")) {
*out = -std::numeric_limits<T>::infinity();
return true;
} else if (is_nan_text(str, "nan") || starts_with(str, "1.#QNAN")) {
*out = std::numeric_limits<T>::quiet_NaN();
return true;
} else if (is_nan_text(str, "-nan") || starts_with(str, "-1.#QNAN")) {
*out = -std::numeric_limits<T>::quiet_NaN();
return true;
}
return false;
}
bool ConvertStringToReal(const std::string &str,
double *out) {
const char *this_str = str.c_str();
char *end = NULL;
errno = 0;
#if defined(_MSC_VER)
// TODO: check if the new MSVC already supports it
// depending on claims of the C++11 support, it should have
if (convert_special_number(str, out))
return true;
#endif // defined(_MSC_VER)
double d = KALDI_STRTOD(this_str, &end);
if (end != this_str)
while (isspace(*end)) end++;
if (end == this_str || *end != '\0' || errno != 0)
return false;
*out = d;
return true;
}
bool ConvertStringToReal(const std::string &str,
float *out) {
const char *this_str = str.c_str();
char *end = NULL;
errno = 0;
#ifdef _MSC_VER
// TODO: check if the new MSVC already supports it
// depending on claims of the C++11 support, it should have
if (convert_special_number(str, out))
return true;
#endif // _MSC_VER
float f = KALDI_STRTOF(this_str, &end);
if (end != this_str)
while (isspace(*end)) end++;
if (end == this_str || *end != '\0' || errno != 0)
return false;
*out = f;
return true;
}
} // end namespace kaldi