Blame view
tools/sctk-2.4.10/src/asclite/core/segment.cpp
10.4 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 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 |
/* * 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. */ /** * Internal representation of a segment. * A segment is a list of Token. */ #include "segment.h" // class's header file #include "speech.h" #include "speechset.h" Logger* Segment::logger = Logger::getLogger(); Segment* Segment::CreateWithDuration(const int& _startTime, const int& _duration, Speech* parent) { Segment* segment = new Segment(); segment->speech = parent; return (Segment*) segment->InitWithDuration(_startTime, _duration); } Segment* Segment::CreateWithEndTime(const int& _startTime, const int& _endTime, Speech* parent) { Segment* segment = new Segment(); segment->speech = parent; return (Segment*) segment->InitWithEndTime(_startTime, _endTime); } bool Segment::AreStartTimeAndEndTimeValid(const int& _startTime, const int& _endTime) { if(!TimedObject::AreStartTimeAndEndTimeValid(_startTime, _endTime)) return false; return _startTime <= GetLowestTokenStartTime() && _endTime >= GetHighestTokenEndTime(); } /** * Merge all Segments in one segments */ Segment* Segment::Merge(const vector<Segment*> & segments) { int start=INT_MAX, end=0; Token* t_start = NULL; Token* t_end = NULL; Segment* currentSegment = NULL; for (size_t i=0 ; i < segments.size() ; ++i) { currentSegment = segments[i]; if (currentSegment->GetStartTime() < start) { t_start = currentSegment->f_token; start = currentSegment->GetStartTime(); } if (currentSegment->GetEndTime() > end) { t_end = currentSegment->l_token; end = currentSegment->GetEndTime(); } } Segment* seg = Segment::CreateWithEndTime(start, end, currentSegment->GetParentSpeech()); seg->f_token = t_start; seg->l_token = t_end; return seg; } int Segment::GetLowestTokenStartTime() { int lowestStartTime = 0; for(size_t i = 0; i < f_token->GetNbOfNextTokens() ; ++i) { int sTime = f_token->GetNextToken(i)->GetStartTime(); if(sTime < lowestStartTime) lowestStartTime = sTime; } return lowestStartTime; } int Segment::GetHighestTokenEndTime() { int highestEndTime = 0; for(size_t i = 0; i < l_token->GetNbOfNextTokens() ; ++i) { int eTime = l_token->GetNextToken(i)->GetEndTime(); if(eTime < highestEndTime) highestEndTime = eTime; } return highestEndTime; } // class constructor Segment::Segment() { //f_token = new Token(); f_token = Token::CreateWithEndTime(0, 0, this); l_token = Token::CreateWithEndTime(0, 0, this); ignoreSegmentInScoring = false; } // class destructor Segment::~Segment() { vector<Token*> listAllTokens = ToTopologicalOrderedStruct(); for(size_t i=0; i<listAllTokens.size(); ++i) { Token* ptr_elt = listAllTokens[i]; if(ptr_elt) delete ptr_elt; } listAllTokens.clear(); delete f_token; delete l_token; } string Segment::ToString() { std::ostringstream oss; oss << "<Segment " << TimedObject::ToString() << "> \tspkrId='" << GetSpeakerId() << "' src='" << GetSource() << "' chnl='" << GetChannel() << "'" << endl; vector<Token*> tokens = ToTopologicalOrderedStruct(); Token* token; for(size_t i = 0; i < tokens.size(); ++i) { token = tokens[i]; if(token != NULL) oss << "\t" << token->ToString() << endl; } oss << "</Segment>"; return oss.str(); } string Segment::ToStringAsLine() { std::ostringstream oss; oss << "<Segment " << TimedObject::ToString() << " spkrId='" << GetSpeakerId() << "' src='" << GetSource() << "' chnl='" << GetChannel() << "'"; vector<Token*> tokens = ToTopologicalOrderedStruct(); Token* token; for(size_t i = 0; i < tokens.size(); ++i) { token = tokens[i]; if(token != NULL) oss << " " << token->ToString(); } return oss.str(); } // sets the value of token void Segment::AddFirstToken(Token* token) { if (token->GetStartTime() != -1 && (token->GetStartTime() < this->GetStartTime())) { char buffer[BUFFER_SIZE]; sprintf(buffer, "Segment: Tried to add a token with start time before begin of segment file ='%s' tkn='%s' src=%s chnl=%s st=%d et=%d seg: st=%d et=%d", GetParentSpeech()->GetParentSpeechSet()->GetSourceFileName().c_str(), token->GetText().c_str(), GetSource().c_str(), GetChannel().c_str(), token->GetStartTime(), token->GetEndTime(), GetStartTime(), GetEndTime()); LOG_FATAL(logger, buffer); exit(E_INVALID); } else { f_token->AddNextToken(token); } } // sets the value of token void Segment::AddLastToken(Token* token) { if (token->GetEndTime() != -1 && (token->GetEndTime() > this->GetEndTime())) { char buffer[BUFFER_SIZE]; sprintf(buffer, "Segment: Tried to add a token with start time before begin of segment file ='%s' tkn='%s' src=%s chnl=%s st=%d et=%d seg: st=%d et=%d", GetParentSpeech()->GetParentSpeechSet()->GetSourceFileName().c_str(), token->GetText().c_str(), GetSource().c_str(), GetChannel().c_str(), token->GetStartTime(), token->GetEndTime(), GetStartTime(), GetEndTime()); LOG_FATAL(logger, buffer); exit(E_INVALID); } else { l_token->AddNextToken(token); } } /** * Output a plan version of the segment. * Put them on a topological order. */ vector<Token*> Segment::ToTopologicalOrderedStruct() { vector<Token*> res; for(size_t i = 0 ; i < f_token->GetNbOfNextTokens() ; ++i) { Token* token = f_token->GetNextToken(i); res.push_back(token); ToTopologicalOrderedStruct(token, &res); } return res; } /** * Recurs methods to compute a topological order of a graph */ void Segment::ToTopologicalOrderedStruct(Token* start, vector<Token*>* doneNode) { if (!isLastToken(start)) { for(size_t i = 0 ; i < start->GetNbOfNextTokens() ; ++i) { Token* token = start->GetNextToken(i); if (find(doneNode->begin(), doneNode->end(), token) == doneNode->end()) { doneNode->push_back(token); ToTopologicalOrderedStruct(token, doneNode); } } } } /** * Return if the token is on the list of the First tokens. */ bool Segment::isFirstToken(Token* token) { for(size_t i = 0 ; i < f_token->GetNbOfNextTokens() ; ++i) if (f_token->GetNextToken(i) == token) return true; return false; } /** * Return if the token is on the list of the last tokens. */ bool Segment::isLastToken(Token* token) { for(size_t i = 0 ; i < l_token->GetNbOfNextTokens() ; ++i) if (l_token->GetNextToken(i) == token) return true; return false; } /** * Return the parent Speech */ Speech* Segment::GetParentSpeech() { return speech; } /** Determines if case is taken into account to align Tokens part of this Speech. */ bool Segment::PerformCaseSensitiveAlignment() { return speech->PerformCaseSensitiveAlignment(); } /** Determines if fragments are considered as correct when aligning Tokens part of this Speech. */ bool Segment::AreFragmentsCorrect() { return speech->AreFragmentsCorrect(); } /** Determines if optionally deletable Tokens need to be accounted for. */ bool Segment::UseOptionallyDeletable() { return speech->UseOptionallyDeletable(); } /** Replaces the token with a linked list of tokens. The initial token is NOT deleted **/ void Segment:: ReplaceTokenWith(Token *token, const vector<Token*> & startTokens, const vector<Token*> & endTokens) { // Store a list of prev tokens to avoid continual adding of tokens!!! vector<Token *>prevTokens; for (size_t p=0; p<token->GetNbOfPrecTokens(); ++p) prevTokens.push_back(token->GetPrecToken(p)); // Do the link for (size_t s = 0; s<startTokens.size(); ++s) for (size_t p=0; p<prevTokens.size(); ++p) prevTokens[p]->LinkTokens(startTokens[s]); // Link in the Starts if (isFirstToken(token)) for (size_t s = 0; s<startTokens.size(); ++s) AddFirstToken(startTokens[s]); // Store a list of next tokens to avoid continual adding of tokens!!! vector<Token *>nextTokens; for (size_t n=0; n<token->GetNbOfNextTokens(); ++n) nextTokens.push_back(token->GetNextToken(n)); // Do the link for (size_t e = 0; e<endTokens.size(); ++e) for (size_t n=0; n<nextTokens.size(); ++n) endTokens[e]->LinkTokens(nextTokens[n]); // Link in the End if (isLastToken(token)) for (size_t e = 0; e<endTokens.size(); ++e) AddLastToken(endTokens[e]); // Unlink the token --- With the assumption that something has replaced IT if (isFirstToken(token)) f_token->UnlinkNextToken(token); if(isLastToken(token)) l_token->UnlinkNextToken(token); while(token->GetNbOfPrecTokens() > 0) token->GetPrecToken(0)->UnlinkTokens(token); while(token->GetNbOfNextTokens() > 0) token->UnlinkTokens(token->GetNextToken(0)); // cleanup prevTokens.clear(); nextTokens.clear(); } int Segment::GetMinTokensTime() { int minTime = -1; vector<Token*> vectok = ToTopologicalOrderedStruct(); for(size_t veci=0; veci<vectok.size(); ++veci) if( (minTime == -1) || (vectok[veci]->GetStartTime() < minTime) ) minTime = vectok[veci]->GetStartTime(); vectok.clear(); return minTime; } int Segment::GetMaxTokensTime() { int maxTime = -1; vector<Token*> vectok = ToTopologicalOrderedStruct(); for(size_t veci=0; veci<vectok.size(); ++veci) if( (maxTime == -1) || (vectok[veci]->GetEndTime() > maxTime) ) maxTime = vectok[veci]->GetStartTime(); vectok.clear(); return maxTime; } void Segment::SetTokensOptionallyDeletable() { vector<Token*> vectok = ToTopologicalOrderedStruct(); for(size_t veci=0; veci<vectok.size(); ++veci) { vectok[veci]->BecomeOptionallyDeletable(); } vectok.clear(); } |