rttm_inputparser.cpp
14.7 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
/*
* 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 RTTM parser for asclite.
*/
#include "rttm_inputparser.h" // class's header file
Logger* RTTMInputParser::logger = Logger::getLogger();
/**
* Load the named file into a Speech element.
* For a RTTM file no Segment are available so one Segment is created for each token.
* Also no spkr info are available so it's left empty.
*/
SpeechSet* RTTMInputParser::loadFile(const string& name)
{
if(isOneTokenPerSegment())
{
LOG_DEBUG(logger, "Load in Lexeme mode file " + name);
return ExpandAlternationSpeechSet(loadFileLexeme(name));
}
else
{
LOG_DEBUG(logger, "Load in Speaker mode file " + name);
return ExpandAlternationSpeechSet(loadFileSpeaker(name));
}
}
SpeechSet* RTTMInputParser::loadFileLexeme(const string& name)
{
string line;
ifstream file;
long int lineNum = -1;
long int elemNum = -1;
file.open(name.c_str(), ifstream::in);
if (! file.is_open())
{
LOG_FATAL(logger, "Error opening RTTM file " + name);
exit(E_LOAD);
}
SpeechSet* vec = new SpeechSet(name);
map<string, Speech*> res;
bool use_confidence = true;
while (getline(file,line,'\n'))
{
++lineNum;
if(line.substr(0, 6) == "LEXEME")
// if Type is 'LEXEME' we take it, unless we drop the line
{
char l_type[BUFFER_SIZE];
char l_file[BUFFER_SIZE];
char l_channel[BUFFER_SIZE];
char l_word[BUFFER_SIZE];
char l_stype[BUFFER_SIZE];
char l_spkr[BUFFER_SIZE];
char l_conf[BUFFER_SIZE];
char l_start[BUFFER_SIZE];
char l_duration[BUFFER_SIZE];
char l_slat[BUFFER_SIZE];
int nbArgParsed = 0;
nbArgParsed = sscanf(line.c_str(), "%s %s %s %s %s %s %s %s %s %s", (char*) &l_type,
(char*) &l_file,
(char*) &l_channel,
(char*) &l_start,
(char*) &l_duration,
(char*) &l_word,
(char*) &l_stype,
(char*) &l_spkr,
(char*) &l_conf,
(char*) &l_slat );
if( ! ( (nbArgParsed == 9) || (nbArgParsed == 10) ) )
{
char buffer[BUFFER_SIZE];
sprintf(buffer, "Error parsing the line %li in file %s", lineNum, name.c_str());
LOG_ERR(logger, buffer);
}
string lower_stype = string(l_stype);
for(size_t i=0;i<lower_stype.length(); ++i)
{
lower_stype[i] = tolower(lower_stype[i]);
}
if( lower_stype == string("lex") ||
lower_stype == string("fp") ||
lower_stype == string("frag") ||
lower_stype == string("un-lex") ||
lower_stype == string("for-lex") ||
lower_stype == string("alpha") ||
lower_stype == string("acronym") ||
lower_stype == string("interjection") ||
lower_stype == string("propernoun") )
// stype is not 'lex', so drop the line
{
Speech* speech = res[l_spkr];
if (!speech)
{
res[l_spkr] = new Speech(vec);
speech = res[l_spkr];
}
int start = ParseString(string(l_start));
int duration = ParseString(string(l_duration));
float confscr = 0;
if(string(l_conf).compare("<NA>") == 0)
use_confidence = false;
else
confscr = atof(l_conf);
string str_word = string(l_word);
if (str_word.find('"', 0) != string::npos)
{
char buffer_err[BUFFER_SIZE];
sprintf(buffer_err, "Error parsing the line %li in file %s, forbidden character detected", lineNum, name.c_str());
LOG_FATAL(logger, buffer_err);
exit(E_LOAD);
}
bool _OptionallyDeletable = false;
if( lower_stype == string("fp") ||
lower_stype == string("frag") ||
lower_stype == string("un-lex") ||
lower_stype == string("for-lex") )
{
//str_word = string("(" + str_word + ")");
_OptionallyDeletable = true;
}
Segment* seg = ParseWordsEx(string(l_file), string(l_channel), string(l_spkr), start, start+duration, speech, str_word, use_confidence, confscr, _OptionallyDeletable);
++elemNum;
seg->SetSourceLineNum(lineNum);
seg->SetSourceElementNum(elemNum);
size_t nbSeg = speech->NbOfSegments();
ostringstream osstr;
osstr << "(" << l_spkr << "-";
osstr << setw(3) << nouppercase << setfill('0') << nbSeg << ")";
seg->SetId(osstr.str());
if (nbSeg != 0)
{
Attach(speech->GetSegment(nbSeg - 1), seg);
}
speech->AddSegment(seg);
}
}
}
file.close();
LOG_INFO(logger, "loading of file '" + name + "' done");
map<string, Speech*>::iterator i = res.begin();
map<string, Speech*>::iterator ei = res.end();
bool emptyFile = true;
while (i != ei)
{
vec->AddSpeech(i->second);
emptyFile = false;
++i;
}
if(emptyFile)
{
LOG_FATAL(logger, "RTTM file '" + name + "' contains no data!");
exit(E_MISSINFO);
}
return vec;
}
SpeechSet* RTTMInputParser::loadFileSpeaker(const string& name)
{
string line;
ifstream file;
long int lineNum = -1;
//long int elemNum = -1;
file.open(name.c_str(), ifstream::in);
if (! file.is_open())
{
LOG_FATAL(logger, "Error opening RTTM file " + name);
exit(E_LOAD);
}
SpeechSet* vec = new SpeechSet(name);
map<string, Speech*> res;
map<string, Segment*> mapCurrentSegmentbySpkr;
map<Segment*, Token*> mapPrevTokenbySegment;
mapCurrentSegmentbySpkr.clear();
mapPrevTokenbySegment.clear();
while (getline(file,line,'\n'))
{
++lineNum;
if (line.substr(0, 2) == ";;")
{
//comment so skip (for now)
}
else if(line.substr(0, 7) == "SPEAKER")
{
//New Segment
char l_type[BUFFER_SIZE];
char l_file[BUFFER_SIZE];
char l_channel[BUFFER_SIZE];
char l_word[BUFFER_SIZE];
char l_stype[BUFFER_SIZE];
char l_spkr[BUFFER_SIZE];
char l_conf[BUFFER_SIZE];
char l_start[BUFFER_SIZE];
char l_duration[BUFFER_SIZE];
int nbArgParsed = 0;
nbArgParsed = sscanf(line.c_str(), "%s %s %s %s %s %s %s %s %s", (char*) &l_type,
(char*) &l_file,
(char*) &l_channel,
(char*) &l_start,
(char*) &l_duration,
(char*) &l_word,
(char*) &l_stype,
(char*) &l_spkr,
(char*) &l_conf );
if(nbArgParsed != 9)
{
char buffer[BUFFER_SIZE];
sprintf(buffer, "Error parsing the line %li in file %s", lineNum, name.c_str());
LOG_ERR(logger, buffer);
}
Speech* speech = res[l_spkr];
if (!speech)
{
res[l_spkr] = new Speech(vec);
speech = res[l_spkr];
}
Segment* currentSeg = Segment::CreateWithDuration(ParseString(string(l_start)), ParseString(string(l_duration)), speech);
currentSeg->SetSource(string(l_file));
currentSeg->SetChannel(string(l_channel));
currentSeg->SetSpeakerId(string(l_spkr));
currentSeg->SetSourceLineNum(lineNum);
currentSeg->SetSourceElementNum(lineNum);
size_t nbSeg = speech->NbOfSegments();
ostringstream osstr;
osstr << "(" << l_spkr << "-";
osstr << setw(3) << nouppercase << setfill('0') << nbSeg << ")";
currentSeg->SetId(osstr.str());
speech->AddSegment(currentSeg);
mapCurrentSegmentbySpkr[string(l_spkr)] = currentSeg;
mapPrevTokenbySegment[currentSeg] = NULL;
}
else if(line.substr(0, 6) == "LEXEME")
// if Type is 'LEXEME' we take it, unless we drop the line
{
char l_type[BUFFER_SIZE];
char l_file[BUFFER_SIZE];
char l_channel[BUFFER_SIZE];
char l_word[BUFFER_SIZE];
char l_stype[BUFFER_SIZE];
char l_spkr[BUFFER_SIZE];
char l_conf[BUFFER_SIZE];
char l_start[BUFFER_SIZE];
char l_duration[BUFFER_SIZE];
int nbArgParsed = 0;
nbArgParsed = sscanf(line.c_str(), "%s %s %s %s %s %s %s %s %s", (char*) &l_type,
(char*) &l_file,
(char*) &l_channel,
(char*) &l_start,
(char*) &l_duration,
(char*) &l_word,
(char*) &l_stype,
(char*) &l_spkr,
(char*) &l_conf );
if(nbArgParsed != 9)
{
char buffer[BUFFER_SIZE];
sprintf(buffer, "Error parsing the line %li in file %s", lineNum, name.c_str());
LOG_ERR(logger, buffer);
}
string lower_stype = string(l_stype);
for(size_t i=0;i<lower_stype.length(); ++i)
{
lower_stype[i] = tolower(lower_stype[i]);
}
if( lower_stype == string("lex") ||
lower_stype == string("fp") ||
lower_stype == string("frag") ||
lower_stype == string("un-lex") ||
lower_stype == string("for-lex") ||
lower_stype == string("alpha") ||
lower_stype == string("acronym") ||
lower_stype == string("interjection") ||
lower_stype == string("propernoun") )
// stype is not 'lex', so drop the line
{
if( mapCurrentSegmentbySpkr.find(string(l_spkr)) == mapCurrentSegmentbySpkr.end() )
{
//Not found
char buffer[BUFFER_SIZE];
sprintf(buffer, "No Segment found for speaker %s at line %li", l_spkr, lineNum);
LOG_ERR(logger, buffer);
}
else
{
Segment* seg = mapCurrentSegmentbySpkr[string(l_spkr)];
Token* tkn = mapPrevTokenbySegment[seg];
string str_word = string(l_word);
if( lower_stype == string("fp") ||
lower_stype == string("frag") ||
lower_stype == string("un-lex") ||
lower_stype == string("for-lex") )
{
str_word = string("(" + str_word + ")");
}
Token* tok = BuildToken(ParseString(string(l_start)), ParseString(string(l_duration)), str_word, seg);
if(string(l_conf).compare("<NA>") != 0)
tok->SetConfidence(atof(l_conf));
if(tkn == NULL)
{
seg->AddFirstToken(tok);
}
else
{
tkn->AddNextToken(tok);
tok->AddPrecToken(tkn);
}
mapPrevTokenbySegment[seg] = tok;
}
}
}
}
map<Segment*, Token*>::iterator j = mapPrevTokenbySegment.begin();
map<Segment*, Token*>::iterator ej = mapPrevTokenbySegment.end();
while(j != ej)
{
Segment* segmt = j->first;
Token* tken = j->second;
if(tken != NULL)
segmt->AddLastToken(tken);
++j;
}
map<string, Speech*>::iterator ssp = res.begin();
map<string, Speech*>::iterator essp = res.end();
while(ssp != essp)
{
string _spkr = ssp->first;
Speech* _speech = ssp->second;
size_t nbrseg = _speech->NbOfSegments();
if(nbrseg > 1)
{
Segment* prevSeg = NULL;
Segment* currSeg = NULL;
for(size_t ii=0; ii<nbrseg; ++ii)
{
Segment* segalpha = _speech->GetSegment(ii);
if( (segalpha->GetNumberOfFirstToken() != 0) && (segalpha->GetNumberOfLastToken() != 0) )
currSeg = segalpha;
if(prevSeg && currSeg)
Attach(prevSeg, currSeg);
if(currSeg)
{
prevSeg = currSeg;
currSeg = NULL;
}
}
}
++ssp;
}
mapCurrentSegmentbySpkr.clear();
mapPrevTokenbySegment.clear();
file.close();
LOG_INFO(logger, "loading of file '" + name + "' done");
map<string, Speech*>::iterator i = res.begin();
map<string, Speech*>::iterator ei = res.end();
while (i != ei)
{
vec->AddSpeech(i->second);
++i;
}
return vec;
}
void RTTMInputParser::Attach(Segment* seg1, Segment* seg2)
{
for (size_t i=0; i < seg1->GetNumberOfLastToken(); ++i)
{
for (size_t j=0; j < seg2->GetNumberOfFirstToken(); ++j)
{
seg1->GetLastToken(i)->AddNextToken(seg2->GetFirstToken(j));
seg2->GetFirstToken(j)->AddPrecToken(seg1->GetLastToken(i));
}
}
}