Blame view
tools/sctk-2.4.10/src/asclite/core/ctm_inputparser.cpp
9.1 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 |
/* * ASCLITE * Author: Jerome Ajot, Jon Fiscus, Nicolas Radde, Chris Laprun * * This software was developed at the National Institute of Standards and Technology by * employees of the Federal Government in the course of their official duties. Pursuant * to title 17 Section 105 of the United States Code this software is not subject to * copyright protection and is in the public domain. ASCLITE is an experimental system. * NIST assumes no responsibility whatsoever for its use by other parties, and makes no * guarantees, expressed or implied, about its quality, reliability, or any other * characteristic. We would appreciate acknowledgement if the software is used. * * THIS SOFTWARE IS PROVIDED "AS IS." With regard to this software, NIST MAKES NO EXPRESS * OR IMPLIED WARRANTY AS TO ANY MATTER WHATSOEVER, INCLUDING MERCHANTABILITY, * OR FITNESS FOR A PARTICULAR PURPOSE. */ /** * Implementation of a CTM parser for asclite. */ #include "ctm_inputparser.h" // class's header file Logger* CTMInputParser::logger = Logger::getLogger(); /* * Load the named file into a Speech element. * For a CTM file no Segment are available so * one Segment is created for each token. * Also no spkr info are available so it's * leaved empty. * * @todo Finish this :P */ SpeechSet* CTMInputParser::loadFile(const string& name) { string line; ifstream file; file.open(name.c_str(), ifstream::in); if (! file.is_open()) { LOG_FATAL(logger, "Error opening CTM file " + name); exit (E_LOAD); } SpeechSet* res = new SpeechSet(name); Speech* speech = new Speech(res); char f_file[BUFFER_SIZE]; char channel[BUFFER_SIZE]; char start[BUFFER_SIZE]; char dur[BUFFER_SIZE]; char conf[BUFFER_SIZE]; int start_ms, dur_ms; char text[BUFFER_SIZE]; Token* prec_tok = NULL; // initialization to NULL to avoid warning compilation bool isFirstToken = true; unsigned long int line_nb = 1; bool use_confidence = true; // Alt temp variables Token* prec_alt_tok = NULL; // initialization to NULL to avoid warning compilation Segment* alt_seg = NULL; // initialization to NULL to avoid warning compilation Segment* prec_alt_seg = NULL; // initialization to NULL to avoid warning compilation bool isAltBegin = false; bool isInsideAlt = false; bool isAlt = false; bool isAltEnd = false; while (getline(file,line,' ')) { if(line.find_first_of(";;") != 0) // Not a comment { int nbArgParsed = 0; nbArgParsed = sscanf(line.c_str(), "%s %s %s %s %s %s", (char*) &f_file, (char*) &channel, (char*) &start, (char*) &dur, (char*) &text, (char*) &conf); if (nbArgParsed < 5) { char temp_start[BUFFER_SIZE]; char temp_dur[BUFFER_SIZE]; nbArgParsed = sscanf(line.c_str(), "%s %s %s %s %s %s", (char*) &f_file, (char*) &channel, (char*) &temp_start, (char*) &temp_dur, (char*) &text, (char*) &conf); if (nbArgParsed < 5) { char buffer[BUFFER_SIZE]; sprintf(buffer, "Error parsing the line %li in file %s", line_nb, name.c_str()); LOG_ERR(logger, buffer); } } start_ms = ParseString(string(start)); dur_ms = ParseString(string(dur)); //start_ms = (int)(atof(start)*1000.0); //dur_ms = (int)(atof(dur)*1000.0); if (nbArgParsed == 5) { use_confidence = false; } else if (nbArgParsed == 6) { use_confidence = true; } //cout << "load line: " << line << endl; //cout << " file: " << f_file << endl; //cout << " chan: " << channel << endl; //cout << " beg : " << start << endl; //cout << " dur : " << dur << endl; //cout << " text: " << text << endl; if (string(text).compare("<ALT_BEGIN>") == 0) { isAltBegin = true; isInsideAlt = true; } else if (string(text).compare("<ALT_END>") == 0) { isAltEnd = true; isInsideAlt = false; } else if (string(text).compare("<ALT>") == 0) { isAlt = true; } else { if (isAltBegin) // After a ALT_BEGIN tag { Segment* new_seg = Segment::CreateWithDuration(start_ms, dur_ms, speech); new_seg->SetSource(string(f_file)); new_seg->SetChannel(string(channel)); Token* tok = BuildToken(start_ms, dur_ms, string(text), new_seg); if (!isFirstToken) { if (isAltEnd) { alt_seg->AddLastToken(prec_alt_tok); for (size_t i=0 ; i < alt_seg->GetNumberOfLastToken() ; ++i) { alt_seg->GetLastToken(i)->AddNextToken(tok); tok->AddPrecToken(alt_seg->GetLastToken(i)); } isAltEnd = false; } else { prec_tok->AddNextToken(tok); tok->AddPrecToken(prec_tok); } } new_seg->AddFirstToken(tok); isFirstToken = false; prec_alt_tok = tok; if (use_confidence) tok->SetConfidence(atof(conf)); speech->AddSegment(new_seg); prec_alt_seg = alt_seg; alt_seg = new_seg; isAltBegin = false; } else if (isAlt) // After a ALT tag { Token* tok = BuildToken(start_ms, dur_ms, string(text), alt_seg); if (start_ms+dur_ms > alt_seg->GetEndTime()) { alt_seg->SetEndTime(start_ms+dur_ms); } alt_seg->AddFirstToken(tok); if (prec_alt_seg == NULL) { if (prec_tok != NULL) { prec_tok->AddNextToken(tok); tok->AddPrecToken(prec_tok); } } else { for (size_t i=0 ; i < prec_alt_seg->GetNumberOfLastToken() ; ++i) { prec_alt_seg->GetLastToken(i)->AddNextToken(tok); tok->AddPrecToken(prec_alt_seg->GetLastToken(i)); } } alt_seg->AddLastToken(prec_alt_tok); prec_alt_tok = tok; if (use_confidence) tok->SetConfidence(atof(conf)); isAlt = false; } else if (isInsideAlt && !isAltBegin && !isAlt) // Inside a ALT tag but not in first pos { Token* tok = BuildToken(start_ms, dur_ms, string(text), alt_seg); if (start_ms+dur_ms > alt_seg->GetEndTime()) { alt_seg->SetEndTime(start_ms+dur_ms); } prec_alt_tok->AddNextToken(tok); tok->AddPrecToken(prec_alt_tok); prec_alt_tok = tok; if (use_confidence) tok->SetConfidence(atof(conf)); } else if (isAltEnd) // After a ALT_END tag { Segment* seg = Segment::CreateWithDuration(start_ms, dur_ms, speech); seg->SetSource(string(f_file)); seg->SetChannel(string(channel)); Token* tok = BuildToken(start_ms, dur_ms, string(text), alt_seg); seg->AddFirstToken(tok); seg->AddLastToken(tok); alt_seg->AddLastToken(prec_alt_tok); for (size_t i=0 ; i < alt_seg->GetNumberOfLastToken() ; ++i) { alt_seg->GetLastToken(i)->AddNextToken(tok); tok->AddPrecToken(alt_seg->GetLastToken(i)); } prec_tok = tok; if (use_confidence) tok->SetConfidence(atof(conf)); speech->AddSegment(seg); prec_alt_tok = NULL; alt_seg = NULL; prec_alt_seg = NULL; isAltEnd = false; isInsideAlt = false; } else // Outside an ALT_BEGIN, ALT_END tag. { Segment* seg = Segment::CreateWithDuration(start_ms, dur_ms, speech); seg->SetSource(string(f_file)); seg->SetChannel(string(channel)); Token* tok = BuildToken(start_ms, dur_ms, string(text), seg); seg->AddFirstToken(tok); seg->AddLastToken(tok); if (!isFirstToken) { prec_tok->AddNextToken(tok); tok->AddPrecToken(prec_tok); } isFirstToken = false; prec_tok = tok; if (use_confidence) tok->SetConfidence(atof(conf)); speech->AddSegment(seg); } } } ++line_nb; } LOG_INFO(logger, "loading of file '" + name + "' done"); file.close(); if(speech->NbOfSegments() == 0) { LOG_FATAL(logger, "CTM file '" + name + "' contains no data!"); exit(E_MISSINFO); } res->AddSpeech(speech); return res; } |