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 = inext(ptr))
157 outf->setf(ios::scientific, 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 str = ts.
get().string();
293 hend = convert_long_num_string_to_time(str,sample);
295 item->
set(
"end",hend);
296 item->
set(
"name",ts.
get().string());
310 sample = DEF_SAMPLE_RATE;
316 if (read_label_portion(ts, s, sample) == misc_read_error)
318 cerr <<
"error: in label file " << ts.
filename() <<
" at line " <<
320 return misc_read_error;
325 EST_write_status save_htk_label(
const EST_String &filename,
332 outf =
new ofstream(filename);
336 cerr <<
"save_htk_label: can't open label output file \"" <<
337 filename <<
"\"" << endl;
341 EST_write_status s = save_htk_label(outf, a);
350 EST_write_status save_htk_label(ostream *outf,
359 for (ptr = a.
head(); ptr != 0; ptr = inext(ptr))
362 cout.setf(ios::left,ios::adjustfield);
363 *outf << (int)(start * HTK_UNITS_PER_SECOND);
365 end = ptr->
F(
"end",0.0);
366 *outf << (int)(end * HTK_UNITS_PER_SECOND);
367 *outf <<
" " << ptr->name() << endl;
375 EST_write_status save_label_spn(
const EST_String &filename,
378 EST_Stream_Item *ptr;
384 outf =
new ofstream(filename);
388 cerr <<
"save_label_spn: can't open label output file \""
389 << filename <<
"\"" << endl;
395 outf->setf(ios::left, ios::adjustfield);
397 *outf << ptr->name();
398 outf->setf(ios::scientific, ios::floatfield);
400 *outf << (ptr->dur() * 1000.0) <<
"\t (0,140)" << endl;
402 for (; inext(ptr) != 0; ptr = inext(ptr))
405 outf->setf(ios::left, ios::adjustfield);
407 *outf << ptr->name();
408 outf->setf(ios::scientific, ios::floatfield);
410 *outf << (ptr->dur() * 1000.0) << endl;
415 *outf << ptr->name();
416 outf->setf(ios::scientific, ios::floatfield);
418 *outf << (ptr->dur() * 1000.0) <<
"\t (99,80)" << endl;
426 EST_write_status save_label_names(
const EST_String &filename,
430 EST_Stream_Item *ptr;
436 outf =
new ofstream(filename);
440 cerr <<
"save_label_name: can't open label output file \""
441 << filename <<
"\"" << endl;
442 return misc_write_error;
445 for (ptr = a.
head(); inext(ptr) != 0; ptr = inext(ptr))
447 *outf << ptr->name();
448 if ((features !=
"") && (features !=
"OneLine"))
454 *outf << ptr->name() << endl;
462 EST_write_status save_RelationList(
const EST_String &filename,
475 outf =
new ofstream(filename);
479 cerr <<
"save_StreamList: can't open MLF output file \""
480 << filename <<
"\"\n";
484 *outf <<
"#!MLF!#\n";
488 for (p = plist.head(); p != 0; p = p->next())
490 outname = path ? plist(p).name() : basename(plist(p).name());
491 *outf <<
"\"*/" << outname<<
"\"\n";
492 for (ptr = plist(p).head(); ptr != 0; ptr = inext(ptr))
497 cout.setf(ios::left,ios::adjustfield);
498 *outf << (int)(start * HTK_UNITS_PER_SECOND);
500 end = ptr->
F(
"end",0.0);
501 *outf << (int)(end * HTK_UNITS_PER_SECOND) <<
" ";
504 *outf << ptr->
S(
"name",
"0") << endl;
514 EST_write_status save_WordList(
const EST_String &filename,
525 outf =
new ofstream(filename);
529 cerr <<
"save:WordList: can't open WordList output file \""
530 << filename <<
"\"\n";
534 for (p = plist.head(); p != 0; p = p->next())
536 for (ptr = plist(p).head(); inext(ptr) != 0; ptr = inext(ptr))
538 *outf << ptr->name();
545 *outf << ptr->name() << endl;
553 EST_write_status save_ind_RelationList(
const EST_String &filename,
563 for (p = plist.head(); p != 0; p = p->next())
565 outname = path ? plist(p).name() : basename(plist(p).name());
566 if (plist(p).save(outname,
false) != write_ok)
567 return misc_write_error;
573 EST_read_status load_RelationList(
const EST_String &filename,
579 if (((filename ==
"-") ? ts.
open(cin) : ts.
open(filename)) != 0)
581 cerr <<
"Can't open label input file " << filename << endl;
582 return misc_read_error;
588 if (ts.
get().string() !=
"#!MLF!#")
590 cerr <<
"Not MLF file\n";
598 fns = ts.
get().string();
601 s.f.set(
"name", fns);
604 if (read_label_portion(ts, plist.
last(), 10000000) == misc_read_error)
606 cerr <<
"error: in reading MLF file\n";
607 cerr <<
"section for file " << fns <<
608 " at line " << ts.
linenum() <<
" is badly formatted\n";
610 return misc_read_error;
623 for (i = 0, p = s.
head(); p; p = inext(p), ++i)
624 p->
set(
"end",(length *
float(i)/
float(s.
length())));
632 if (al.
val(
"-itype", 0) ==
"mlf")
634 if (load_RelationList(files.
first(), plist) != format_ok)
638 for (p = files.head(); p; p = p->next())
645 if (plist(plp).load(files(p), al.
val(
"-itype")) != format_ok)
648 else if (plist(plp).load(files(p)) != format_ok)
650 if ((al.
val(
"-itype", 0) ==
"words") && (al.
present(
"-length")))
651 pad_ends(s, al.
fval(
"-length"));