segmentsgroup.cpp
12.1 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
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
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
/*
* 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.
*/
/**
* This class represent a group of segments with the information
* about ref/hyp if this information exist.
*/
#include "segmentsgroup.h"
Logger* SegmentsGroup::logger = Logger::getLogger();
// class destructor
SegmentsGroup::~SegmentsGroup()
{
size_t i;
for(i=0; i<references.size(); ++i)
references[i].clear();
references.clear();
for(i=0; i<hypothesiss.size(); ++i)
hypothesiss[i].clear();
hypothesiss.clear();
}
/**
* Add a reference segment into the SegmentGroup.
*/
void SegmentsGroup::AddReference(Segment* reference)
{
vector<Segment*> temp;
temp.push_back(reference);
references.push_back(temp);
}
/*
* Add an Hypothesis into the SegmentGroup.
*/
void SegmentsGroup::AddHypothesis(Segment* hypothesis)
{
vector<Segment*> temp;
temp.push_back(hypothesis);
hypothesiss.push_back(temp);
}
/**
* Return true if the Group contains segment to ignore in scoring
*/
bool SegmentsGroup::isIgnoreInScoring()
{
for (size_t i=0 ; i < references.size() ; ++i)
{
for (size_t j=0 ; j < references[i].size() ; ++j)
{
if (references[i][j]->isSegmentExcludeFromScoring())
return true;
}
}
return false;
}
/**
* Return a possible Reference for the given Hyp token.
* The Segment returned is the first that match the token time
* This method should always return a Segment as an Reference should always contain a Segment at a given time.
*/
Segment* SegmentsGroup::GetRefSegmentByTime(Token* token)
{
for(size_t i=0; i < references.size(); ++i)
{
for(size_t j=0; j < references[i].size(); ++j)
{
if(references[i][j]->OverlapWith(token))
return references[i][j];
}
}
//Hey why are you here ?
LOG_FATAL(logger, "Warning: This token don't have a reference segment for himself:");
cerr << token->GetText() <<" (" << token->GetStartTime() << "," << token->GetEndTime() << ")"<< endl;
cerr << "from segment(" << token->GetParentSegment()->GetStartTime() << "," << token->GetParentSegment()->GetEndTime() << ") ";
cerr << token->GetParentSegment()->GetSource() << ", " << token->GetParentSegment()->GetChannel() << ", " << token->GetParentSegment()->GetSpeakerId() << endl;
exit(E_INVALID);
return NULL;
}
string SegmentsGroup::GetDifficultyString()
{
string res;
char buff1[100];
sprintf(buff1, "REF:%lu(", (ulint) references.size());
res.append(buff1);
for (size_t i=0 ; i < references.size() ; ++i)
{
size_t temp_size = 0;
for (size_t j=0 ; j < references[i].size() ; ++j)
{
temp_size += references[i][j]->ToTopologicalOrderedStruct().size();
}
sprintf(buff1,"%lu", (ulint) temp_size);
if (i != 0) res.append(",");
res.append(buff1);
}
sprintf(buff1, ") HYP:%lu(", (ulint) hypothesiss.size());
res.append(buff1);
for (size_t i=0 ; i < hypothesiss.size() ; ++i)
{
size_t temp_size = 0;
for (size_t j=0 ; j < hypothesiss[i].size() ; ++j)
{
temp_size += hypothesiss[i][j]->ToTopologicalOrderedStruct().size();
}
if (i != 0) res.append(",");
sprintf(buff1,"%lu", (ulint) temp_size);
res.append(buff1);
}
res.append(")");
return res;
}
ullint SegmentsGroup::GetDifficultyNumber()
{
ullint res = 1;
for (size_t i=0 ; i < references.size() ; ++i)
{
ullint temp_size = 0;
for (size_t j=0 ; j < references[i].size() ; ++j)
{
temp_size += (ullint) references[i][j]->ToTopologicalOrderedStruct().size();
}
res = res * (temp_size+1);
}
for (size_t i=0 ; i < hypothesiss.size() ; ++i)
{
ullint temp_size = 0;
for (size_t j=0 ; j < hypothesiss[i].size() ; ++j)
{
temp_size += (ullint) hypothesiss[i][j]->ToTopologicalOrderedStruct().size();
}
res = res * (temp_size+1);
}
return res;
}
/**
* Return the List of Token by topological order for the Reference no i
*/
vector<Token*> SegmentsGroup::ToTopologicalOrderedStructRef(const size_t& index)
{
vector<Token*> tokens;
tokens.push_back(NULL);
for (size_t i=0 ; i < references[index].size() ; ++i)
{
vector<Token*> temp = references[index][i]->ToTopologicalOrderedStruct();
for (size_t j=0; j<temp.size(); ++j)
tokens.push_back(temp[j]);
}
tokens.push_back(NULL);
return tokens;
}
/**
* Return the List of Token by topological order for the Hypothesis no i
*/
vector<Token*> SegmentsGroup::ToTopologicalOrderedStructHyp(const size_t& index)
{
vector<Token*> tokens;
tokens.push_back(NULL);
for (size_t i=0 ; i < hypothesiss[index].size() ; ++i)
{
vector<Token*> temp = hypothesiss[index][i]->ToTopologicalOrderedStruct();
for (size_t j=0; j<temp.size(); ++j)
tokens.push_back(temp[j]);
}
tokens.push_back(NULL);
return tokens;
}
/** Returns a string representation of this SegmentGroup. */
string SegmentsGroup::ToString()
{
ostringstream osstr;
osstr << "References:" << endl;
for (size_t i = 0 ; i < references.size() ; ++i)
{
for (size_t j = 0 ; j < references[i].size() ; ++j)
{
vector<Token*> temp = references[i][j]->ToTopologicalOrderedStruct();
osstr << " spkr=" << i << " j=" << j << " #tokens=" << temp.size() << " ";
osstr << "[";
for (size_t k=0 ; k < temp.size() ; ++k)
{
osstr << " " << temp[k]->ToString();
}
osstr << " ]" << endl;
}
}
osstr << "Hypotheses:" << endl;
for (size_t i = 0 ; i < hypothesiss.size() ; ++i)
{
for (size_t j = 0 ; j < hypothesiss[i].size() ; ++j)
{
vector<Token*> temp = hypothesiss[i][j]->ToTopologicalOrderedStruct();
osstr << " spkr=" << i << " j=" << j << " #tokens=" << temp.size() << " ";;
osstr << "[";
for (size_t k=0 ; k < temp.size() ; ++k)
{
osstr << " " << temp[k]->ToString();
}
osstr << " ]" << endl;
}
}
return osstr.str();
}
/** Log display Alignment */
void SegmentsGroup::LoggingAlignment(const bool& bgeneric, const string& type)
{
char buffer1[BUFFER_SIZE];
sprintf(buffer1, "%li", s_id);
string seggrpsid = string(buffer1);
//References
for (size_t i = 0 ; i < references.size() ; ++i)
{
for (size_t j = 0 ; j < references[i].size() ; ++j)
{
vector<Token*> listTkn = references[i][j]->ToTopologicalOrderedStruct();
if(!listTkn.empty())
{
vector<Token*>::iterator k=listTkn.begin();
vector<Token*>::iterator ek=listTkn.end();
string csvHyp = ",,,,,,,,,,";
if(bgeneric)
csvHyp = "";
string csvRef = ",,,,,,,,,,";
while(k != ek)
{
csvRef = (*k)->GetCSVInformation();
string File = (*k)->GetParentSegment()->GetSource();
string Channel = (*k)->GetParentSegment()->GetChannel();
LOG_ALIGN(logger, type + "," + seggrpsid + "," + File + "," + Channel + ",," + csvRef + "," + csvHyp);
++k;
}
}
else
{
string csvHyp = ",,,,,,,,,,";
if(bgeneric)
csvHyp = "";
string csvRef = ",,,,,,,,,,";
string segbt = "";
if(references[i][j]->GetStartTime() >= 0)
{
char buffer [BUFFER_SIZE];
sprintf(buffer, "%.3f", ((double)(references[i][j]->GetStartTime()))/1000.0);
segbt = string(buffer);
}
string seget = "";
if(references[i][j]->GetEndTime() >= 0)
{
char buffer [BUFFER_SIZE];
sprintf(buffer, "%.3f", ((double)(references[i][j]->GetEndTime()))/1000.0);
seget = string(buffer);
}
string spkr = references[i][j]->GetSpeakerId();
char buffer2[BUFFER_SIZE];
sprintf(buffer2, "%li", references[i][j]->GetsID());
string segsid = string(buffer2);
string File = references[i][j]->GetSource();
string Channel = references[i][j]->GetChannel();
csvRef = segsid + "," + segbt + "," + seget + "," + spkr + ",,,,,,,";
LOG_ALIGN(logger, type + "," + seggrpsid + "," + File + "," + Channel + ",," + csvRef + "," + csvHyp);
}
}
}
//Hypotheses
for (size_t i = 0 ; i < hypothesiss.size() ; ++i)
{
for (size_t j = 0 ; j < hypothesiss[i].size() ; ++j)
{
vector<Token*> listTkn = hypothesiss[i][j]->ToTopologicalOrderedStruct();
if(!listTkn.empty())
{
vector<Token*>::iterator k=listTkn.begin();
vector<Token*>::iterator ek=listTkn.end();
string csvHyp = ",,,,,,,,,,";
string csvRef = ",,,,,,,,,,";
while(k != ek)
{
csvHyp = (*k)->GetCSVInformation();
string File = (*k)->GetParentSegment()->GetSource();
string Channel = (*k)->GetParentSegment()->GetChannel();
LOG_ALIGN(logger, type + "," + seggrpsid + "," + File + "," + Channel + ",," + csvRef + "," + csvHyp);
++k;
}
}
else
{
string csvHyp = ",,,,,,,,,,";
string csvRef = ",,,,,,,,,,";
string segbt = "";
if(hypothesiss[i][j]->GetStartTime() >= 0)
{
char buffer [BUFFER_SIZE];
sprintf(buffer, "%.3f", ((double)(hypothesiss[i][j]->GetStartTime()))/1000.0);
segbt = string(buffer);
}
string seget = "";
if(hypothesiss[i][j]->GetEndTime() >= 0)
{
char buffer [BUFFER_SIZE];
sprintf(buffer, "%.3f", ((double)(hypothesiss[i][j]->GetEndTime()))/1000.0);
seget = string(buffer);
}
string spkr = hypothesiss[i][j]->GetSpeakerId();
char buffer2[BUFFER_SIZE];
sprintf(buffer2, "%li", hypothesiss[i][j]->GetsID());
string segsid = string(buffer2);
string File = hypothesiss[i][j]->GetSource();
string Channel = hypothesiss[i][j]->GetChannel();
csvHyp = segsid + "," + segbt + "," + seget + "," + spkr + ",,,,,,,";
LOG_ALIGN(logger, type + "," + seggrpsid + "," + File + "," + Channel + ",," + csvRef + "," + csvHyp);
}
}
}
}
int SegmentsGroup::GetMinTime()
{
int minBTSG = INT_MAX;
for(size_t i=0; i<GetNumberOfReferences(); ++i)
{
vector<Segment*> vecSeg = GetReference(i);
for(size_t j=0; j<vecSeg.size(); ++j)
{
if(vecSeg[j]->GetStartTime() < minBTSG)
minBTSG = vecSeg[j]->GetStartTime();
}
}
for(size_t i=0; i<GetNumberOfHypothesis(); ++i)
{
vector<Segment*> vecSeg = GetHypothesis(i);
for(size_t j=0; j<vecSeg.size(); ++j)
{
if(vecSeg[j]->GetStartTime() < minBTSG)
minBTSG = vecSeg[j]->GetStartTime();
}
}
return(minBTSG);
}
int SegmentsGroup::GetMaxTime()
{
int maxETSG = 0;
for(size_t i=0; i<GetNumberOfReferences(); ++i)
{
vector<Segment*> vecSeg = GetReference(i);
for(size_t j=0; j<vecSeg.size(); ++j)
{
if(vecSeg[j]->GetEndTime() > maxETSG)
maxETSG = vecSeg[j]->GetEndTime();
}
}
for(size_t i=0; i<GetNumberOfHypothesis(); ++i)
{
vector<Segment*> vecSeg = GetHypothesis(i);
for(size_t j=0; j<vecSeg.size(); ++j)
{
if(vecSeg[j]->GetEndTime() > maxETSG)
maxETSG = vecSeg[j]->GetEndTime();
}
}
return(maxETSG);
}
int SegmentsGroup::GetTotalDuration()
{
int minBTSG = GetMinTime();
int maxETSG = GetMaxTime();
int diff = maxETSG - minBTSG;
if(diff <= 0)
{
LOG_FATAL(logger, "Duration of the Segment group is negative or equal 0");
exit(E_INVALID);
}
return(diff);
}