Blame view
src/util/kaldi-table.cc
10.7 KB
8dcb6dfcb first commit |
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 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 |
// util/kaldi-table.cc // Copyright 2009-2011 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/kaldi-table.h" #include "util/text-utils.h" namespace kaldi { bool ReadScriptFile(const std::string &rxfilename, bool warn, std::vector<std::pair<std::string, std::string> > *script_out) { bool is_binary; Input input; if (!input.Open(rxfilename, &is_binary)) { if (warn) KALDI_WARN << "Error opening script file: " << PrintableRxfilename(rxfilename); return false; } if (is_binary) { if (warn) KALDI_WARN << "Error: script file appears to be binary: " << PrintableRxfilename(rxfilename); return false; } bool ans = ReadScriptFile(input.Stream(), warn, script_out); if (warn && !ans) KALDI_WARN << "[script file was: " << PrintableRxfilename(rxfilename) << "]"; return ans; } bool ReadScriptFile(std::istream &is, bool warn, std::vector<std::pair<std::string, std::string> > *script_out) { KALDI_ASSERT(script_out != NULL); std::string line; int line_number = 0; while (getline(is, line)) { line_number++; const char *c = line.c_str(); if (*c == '\0') { if (warn) KALDI_WARN << "Empty " << line_number << "'th line in script file"; return false; // Empty line so invalid scp file format.. } std::string key, rest; SplitStringOnFirstSpace(line, &key, &rest); if (key.empty() || rest.empty()) { if (warn) KALDI_WARN << "Invalid " << line_number << "'th line in script file" <<":\"" << line << '"'; return false; } script_out->resize(script_out->size()+1); script_out->back().first = key; script_out->back().second = rest; } return true; } bool WriteScriptFile(std::ostream &os, const std::vector<std::pair<std::string, std::string> > &script) { if (!os.good()) { KALDI_WARN << "WriteScriptFile: attempting to write to invalid stream."; return false; } std::vector<std::pair<std::string, std::string> >::const_iterator iter; for (iter = script.begin(); iter != script.end(); ++iter) { if (!IsToken(iter->first)) { KALDI_WARN << "WriteScriptFile: using invalid token \"" << iter->first << '"'; return false; } if (iter->second.find(' ') != std::string::npos || (iter->second.length() != 0 && (isspace(iter->second[0]) || isspace(iter->second[iter->second.length()-1])))) { // second part contains newline or leading or trailing space. KALDI_WARN << "WriteScriptFile: attempting to write invalid line \"" << iter->second << '"'; return false; } os << iter->first << ' ' << iter->second << ' '; } if (!os.good()) { KALDI_WARN << "WriteScriptFile: stream in error state."; return false; } return true; } bool WriteScriptFile(const std::string &wxfilename, const std::vector<std::pair<std::string, std::string> > &script) { Output output; if (!output.Open(wxfilename, false, false)) { // false, false means not // binary, no binary-mode header. KALDI_ERR << "Error opening output stream for script file: " << PrintableWxfilename(wxfilename); return false; } if (!WriteScriptFile(output.Stream(), script)) { KALDI_ERR << "Error writing script file to stream " << PrintableWxfilename(wxfilename); return false; } return true; } WspecifierType ClassifyWspecifier(const std::string &wspecifier, std::string *archive_wxfilename, std::string *script_wxfilename, WspecifierOptions *opts) { // Examples: // ark,t:wxfilename -> kArchiveWspecifier // ark,b:wxfilename -> kArchiveWspecifier // scp,t:rxfilename -> kScriptWspecifier // scp,t:rxfilename -> kScriptWspecifier // ark,scp,t:filename, wxfilename -> kBothWspecifier // ark,scp:filename, wxfilename -> kBothWspecifier // Note we can include the flush option (f) or no-flush (nf) // anywhere: e.g. // ark,scp,f:filename, wxfilename -> kBothWspecifier // or: // scp,t,nf:rxfilename -> kScriptWspecifier if (archive_wxfilename) archive_wxfilename->clear(); if (script_wxfilename) script_wxfilename->clear(); size_t pos = wspecifier.find(':'); if (pos == std::string::npos) return kNoWspecifier; if (isspace(*(wspecifier.rbegin()))) return kNoWspecifier; // Trailing space // disallowed. std::string before_colon(wspecifier, 0, pos), after_colon(wspecifier, pos+1); std::vector<std::string> split_first_part; // Split part before ':' on ', '. SplitStringToVector(before_colon, ", ", false, &split_first_part); // false== // don't omit empty strings between commas. WspecifierType ws = kNoWspecifier; if (opts != NULL) *opts = WspecifierOptions(); // Make sure all the defaults are as in the // default constructor of the options class. for (size_t i = 0; i < split_first_part.size(); i++) { const std::string &str = split_first_part[i]; // e.g. "b", "t", "f", "ark", // "scp". const char *c = str.c_str(); if (!strcmp(c, "b")) { if (opts) opts->binary = true; } else if (!strcmp(c, "f")) { if (opts) opts->flush = true; } else if (!strcmp(c, "nf")) { if (opts) opts->flush = false; } else if (!strcmp(c, "t")) { if (opts) opts->binary = false; } else if (!strcmp(c, "p")) { if (opts) opts->permissive = true; } else if (!strcmp(c, "ark")) { if (ws == kNoWspecifier) ws = kArchiveWspecifier; else return kNoWspecifier; // We do not allow "scp, ark", only "ark, // scp". } else if (!strcmp(c, "scp")) { if (ws == kNoWspecifier) ws = kScriptWspecifier; else if (ws == kArchiveWspecifier) ws = kBothWspecifier; else return kNoWspecifier; // repeated "scp" option: invalid. } else { return kNoWspecifier; // Could not interpret this option. } } switch (ws) { case kArchiveWspecifier: if (archive_wxfilename) *archive_wxfilename = after_colon; break; case kScriptWspecifier: if (script_wxfilename) *script_wxfilename = after_colon; break; case kBothWspecifier: pos = after_colon.find(','); // first comma. if (pos == std::string::npos) return kNoWspecifier; if (archive_wxfilename) *archive_wxfilename = std::string(after_colon, 0, pos); if (script_wxfilename) *script_wxfilename = std::string(after_colon, pos+1); break; case kNoWspecifier: default: break; } return ws; } RspecifierType ClassifyRspecifier(const std::string &rspecifier, std::string *rxfilename, RspecifierOptions *opts) { // Examples // ark:rxfilename -> kArchiveRspecifier // scp:rxfilename -> kScriptRspecifier // // We also allow the meaningless prefixes b, and t, // plus the options o (once), no (not-once), // s (sorted) and ns (not-sorted), p (permissive) // and np (not-permissive). // so the following would be valid: // // f, o, b, np, ark:rxfilename -> kArchiveRspecifier // // Examples: // // b, ark:rxfilename -> kArchiveRspecifier // t, ark:rxfilename -> kArchiveRspecifier // b, scp:rxfilename -> kScriptRspecifier // t, no, s, scp:rxfilename -> kScriptRspecifier // t, ns, scp:rxfilename -> kScriptRspecifier // Improperly formed Rspecifiers will be classified as kNoRspecifier. if (rxfilename) rxfilename->clear(); if (opts != NULL) *opts = RspecifierOptions(); // Make sure all the defaults are as in the // default constructor of the options class. size_t pos = rspecifier.find(':'); if (pos == std::string::npos) return kNoRspecifier; if (isspace(*(rspecifier.rbegin()))) return kNoRspecifier; // Trailing space // disallowed. std::string before_colon(rspecifier, 0, pos), after_colon(rspecifier, pos+1); std::vector<std::string> split_first_part; // Split part before ':' on ', '. SplitStringToVector(before_colon, ", ", false, &split_first_part); // false== // don't omit empty strings between commas. RspecifierType rs = kNoRspecifier; for (size_t i = 0; i < split_first_part.size(); i++) { const std::string &str = split_first_part[i]; // e.g. "b", "t", "f", "ark", // "scp". const char *c = str.c_str(); if (!strcmp(c, "b")); // Ignore this option. It's so we can use the same // specifiers for rspecifiers and wspecifiers. else if (!strcmp(c, "t")); // Ignore this option too. else if (!strcmp(c, "o")) { if (opts) opts->once = true; } else if (!strcmp(c, "no")) { if (opts) opts->once = false; } else if (!strcmp(c, "p")) { if (opts) opts->permissive = true; } else if (!strcmp(c, "np")) { if (opts) opts->permissive = false; } else if (!strcmp(c, "s")) { if (opts) opts->sorted = true; } else if (!strcmp(c, "ns")) { if (opts) opts->sorted = false; } else if (!strcmp(c, "cs")) { if (opts) opts->called_sorted = true; } else if (!strcmp(c, "ncs")) { if (opts) opts->called_sorted = false; } else if (!strcmp(c, "bg")) { if (opts) opts->background = true; } else if (!strcmp(c, "ark")) { if (rs == kNoRspecifier) rs = kArchiveRspecifier; else return kNoRspecifier; // Repeated or combined ark and scp options // invalid. } else if (!strcmp(c, "scp")) { if (rs == kNoRspecifier) rs = kScriptRspecifier; else return kNoRspecifier; // Repeated or combined ark and scp options // invalid. } else { return kNoRspecifier; // Could not interpret this option. } } if ((rs == kArchiveRspecifier || rs == kScriptRspecifier) && rxfilename != NULL) *rxfilename = after_colon; return rs; } } // end namespace kaldi |