43 #include "EST_types.h"
44 #include "ling_class/EST_Relation.h"
45 #include "EST_string_aux.h"
46 #include "EST_cutils.h"
47 #include "EST_TList.h"
48 #include "EST_Option.h"
49 #include "relation_io.h"
51 #define DEF_SAMPLE_RATE 16000
52 #define HTK_UNITS_PER_SECOND 10000000
54 static EST_Regex RXleadingwhitespace(
"^[ \t\n\r][ \t\n\r]*.*$");
68 key = ts.
get().string();
74 if (val.
matches(RXleadingwhitespace))
75 val = val.
after(RXwhite);
79 if (ts.
peek() ==
"")
return format_ok;
86 si->
set(
"end",(
float)atof(ts.
get().string()));
89 for (name =
""; (!ts.
eoln()) && (ts.
peek() !=
";"); )
93 name += t.whitespace();
98 if (ts.
peek().string() ==
";")
101 si->features().
load(ts);
107 EST_write_status save_esps_label(
const EST_String &filename,
115 outf =
new ofstream(filename);
119 cerr <<
"save_esps_label: can't open label output file \"" <<
120 filename <<
"\"" << endl;
124 EST_write_status st=save_esps_label(outf, s, evaluate_ff);
132 EST_write_status save_esps_label(ostream *outf,
138 *outf <<
"separator ;\n";
140 *outf <<
"nfields 1\n";
143 for (p.
begin(s.
f); p; ++p)
144 *outf << p->k <<
" " << p->v << endl;
153 for (ptr = s.
head(); ptr != 0; ptr = ptr->next())
157 outf->setf(ios::fixed, ios::floatfield);
160 if (s.
f(
"timing_style",
"0") ==
"event")
161 *outf << ptr->
F(
"time",0);
163 *outf << ptr->
F(
"end",0);
165 *outf <<
" 26 \t" << ptr->
S(
"name",
"0");
168 f2 = ptr->features();
200 if ((ts.
peek().
col() == 0) && (ts.
peek() ==
"END"))
202 if (ts.
peek() ==
"END")
210 key = ts.
get().string();
211 val = ts.
get().string();
214 sr = 1000.0 / atof(val);
219 cerr <<
"Error: couldn't find header in label file "
224 if (read_label_portion(ts, s, isr) == misc_read_error)
226 cerr <<
"error: in label file " << ts.
filename() <<
" at line " <<
228 return misc_read_error;
243 item->
set(
"end",0.0);
249 static float convert_long_num_string_to_time(
const char *s,
int sample)
256 return atof(s)/sample;
262 (strchr(
" \n\r\t",s[i]) != NULL) && (s[i] !=
'\0');
266 (s[i] !=
'\0') && (s[i] >=
'0') && (s[i] <=
'9');
271 a += (d/(double)sample);
286 str = ts.
get().string();
292 hstart = convert_long_num_string_to_time(str,sample);
293 str = ts.
get().string();
294 hend = convert_long_num_string_to_time(str,sample);
296 item->
set(
"end",hend);
297 item->
set(
"name",ts.
get().string());
311 sample = DEF_SAMPLE_RATE;
317 if (read_label_portion(ts, s, sample) == misc_read_error)
319 cerr <<
"error: in label file " << ts.
filename() <<
" at line " <<
321 return misc_read_error;
326 EST_write_status save_htk_label(
const EST_String &filename,
333 outf =
new ofstream(filename);
337 cerr <<
"save_htk_label: can't open label output file \"" <<
338 filename <<
"\"" << endl;
342 EST_write_status s = save_htk_label(outf, a);
351 EST_write_status save_htk_label(ostream *outf,
360 for (ptr = a.
head(); ptr != 0; ptr = ptr->next())
363 cout.setf(ios::left,ios::adjustfield);
364 *outf << (int)(start * HTK_UNITS_PER_SECOND);
366 end = ptr->
F(
"end",0.0);
367 *outf << (int)(end * HTK_UNITS_PER_SECOND);
368 *outf <<
" " << ptr->name() << endl;
376 EST_write_status save_label_spn(
const EST_String &filename,
379 EST_Stream_Item *ptr;
385 outf =
new ofstream(filename);
389 cerr <<
"save_label_spn: can't open label output file \""
390 << filename <<
"\"" << endl;
396 outf->setf(ios::left, ios::adjustfield);
398 *outf << ptr->name();
399 outf->setf(ios::fixed, ios::floatfield);
401 *outf << (ptr->dur() * 1000.0) <<
"\t (0,140)" << endl;
403 for (; next(ptr) != 0; ptr = ptr->next())
406 outf->setf(ios::left, ios::adjustfield);
408 *outf << ptr->name();
409 outf->setf(ios::fixed, ios::floatfield);
411 *outf << (ptr->dur() * 1000.0) << endl;
416 *outf << ptr->name();
417 outf->setf(ios::fixed, ios::floatfield);
419 *outf << (ptr->dur() * 1000.0) <<
"\t (99,80)" << endl;
427 EST_write_status save_label_names(
const EST_String &filename,
431 EST_Stream_Item *ptr;
437 outf =
new ofstream(filename);
441 cerr <<
"save_label_name: can't open label output file \""
442 << filename <<
"\"" << endl;
443 return misc_write_error;
446 for (ptr = a.
head(); next(ptr) != 0; ptr = ptr->next())
448 *outf << ptr->name();
449 if ((features !=
"") && (features !=
"OneLine"))
455 *outf << ptr->name() << endl;
463 EST_write_status save_RelationList(
const EST_String &filename,
476 outf =
new ofstream(filename);
480 cerr <<
"save_StreamList: can't open MLF output file \""
481 << filename <<
"\"\n";
485 *outf <<
"#!MLF!#\n";
489 for (p = plist.head(); p != 0; p = p->next())
491 outname = path ? plist(p).name() : basename(plist(p).name());
492 *outf <<
"\"*/" << outname<<
"\"\n";
493 for (ptr = plist(p).head(); ptr != 0; ptr = ptr->next())
498 cout.setf(ios::left,ios::adjustfield);
499 *outf << (int)(start * HTK_UNITS_PER_SECOND);
501 end = ptr->
F(
"end",0.0);
502 *outf << (int)(end * HTK_UNITS_PER_SECOND) <<
" ";
505 *outf << ptr->
S(
"name",
"0") << endl;
515 EST_write_status save_WordList(
const EST_String &filename,
526 outf =
new ofstream(filename);
530 cerr <<
"save:WordList: can't open WordList output file \""
531 << filename <<
"\"\n";
535 for (p = plist.head(); p != 0; p = p->next())
537 for (ptr = plist(p).head(); ptr->next() != 0; ptr = ptr->next())
539 *outf << ptr->name();
546 *outf << ptr->name() << endl;
554 EST_write_status save_ind_RelationList(
const EST_String &filename,
564 for (p = plist.head(); p != 0; p = p->next())
566 outname = path ? plist(p).name() : basename(plist(p).name());
567 if (plist(p).save(outname,
false) != write_ok)
568 return misc_write_error;
574 EST_read_status load_RelationList(
const EST_String &filename,
580 if (((filename ==
"-") ? ts.
open(cin) : ts.
open(filename)) != 0)
582 cerr <<
"Can't open label input file " << filename << endl;
583 return misc_read_error;
589 if (ts.
get().string() !=
"#!MLF!#")
591 cerr <<
"Not MLF file\n";
599 fns = ts.
get().string();
602 s.f.set(
"name", fns);
605 if (read_label_portion(ts, plist.
last(), 10000000) == misc_read_error)
607 cerr <<
"error: in reading MLF file\n";
608 cerr <<
"section for file " << fns <<
609 " at line " << ts.
linenum() <<
" is badly formatted\n";
611 return misc_read_error;
624 for (i = 0, p = s.
head(); p; p = p->next(), ++i)
625 p->
set(
"end",(length *
float(i)/
float(s.
length())));
633 if (al.
val(
"-itype", 0) ==
"mlf")
635 if (load_RelationList(files.
first(), plist) != format_ok)
639 for (p = files.head(); p; p = p->next())
646 if (plist(plp).load(files(p), al.
val(
"-itype")) != format_ok)
649 else if (plist(plp).load(files(p)) != format_ok)
651 if ((al.
val(
"-itype", 0) ==
"words") && (al.
present(
"-length")))
652 pad_ends(s, al.
fval(
"-length"));