304
EST_write_status EST_WFST::save(const EST_String &filename)
304
EST_write_status EST_WFST::save_binary(FILE *fd)
308
int num_transitions, type, in, out, next_state;
311
for (i=0; i<p_num_states; i++)
313
num_transitions = p_states[i]->num_transitions();
314
fwrite(&num_transitions,4,1,fd);
315
if (p_states[i]->type() == wfst_final)
317
else if (p_states[i]->type() == wfst_nonfinal)
318
type = WFST_NONFINAL;
319
else if (p_states[i]->type() == wfst_licence)
323
fwrite(&type,4,1,fd);
324
for (j=p_states[i]->transitions.head(); j != 0; j=next(j))
326
in = p_states[i]->transitions(j)->in_symbol();
327
out = p_states[i]->transitions(j)->out_symbol();
328
next_state = p_states[i]->transitions(j)->state();
329
weight = p_states[i]->transitions(j)->weight();
341
fwrite(&next_state,4,1,fd);
342
fwrite(&weight,4,1,fd);
349
EST_write_status EST_WFST::save(const EST_String &filename,
350
const EST_String type)
327
373
p_out_symbols.print_to_string(TRUE)+")",
329
375
fprintf(ofd,"NumStates %d\n",p_num_states);
376
fprintf(ofd, "ByteOrder %s\n", ((EST_NATIVE_BO == bo_big) ? "10" : "01"));
330
377
fprintf(ofd,"EST_Header_End\n");
332
for (i=0; i < p_num_states; i++)
379
if (type == "binary")
334
EST_WFST_State *s=p_states[i];
335
fprintf(ofd,"((%d ",s->name());
339
fprintf(ofd,"final ");
342
fprintf(ofd,"nonfinal ");
345
fprintf(ofd,"licence ");
348
fprintf(ofd,"error ");
350
fprintf(ofd,"%d)\n",s->num_transitions());
351
for (j=s->transitions.head(); j != 0; j=next(j))
353
EST_String in = p_in_symbols.name(s->transitions(j)->in_symbol());
354
EST_String out=p_out_symbols.name(s->transitions(j)->out_symbol());
355
if (in.matches(needquotes))
356
fprintf(ofd," (%s ",(const char *)quote_string(in,"\"","\\",1));
358
fprintf(ofd," (%s ",(const char *)in);
359
if (out.matches(needquotes))
360
fprintf(ofd," %s ",(const char *)quote_string(out,"\"","\\",1));
362
fprintf(ofd," %s ",(const char *)out);
363
fprintf(ofd,"%d %g)\n",
364
s->transitions(j)->state(),
365
s->transitions(j)->weight());
383
for (i=0; i < p_num_states; i++)
385
EST_WFST_State *s=p_states[i];
386
fprintf(ofd,"((%d ",s->name());
390
fprintf(ofd,"final ");
393
fprintf(ofd,"nonfinal ");
396
fprintf(ofd,"licence ");
399
fprintf(ofd,"error ");
401
fprintf(ofd,"%d)\n",s->num_transitions());
402
for (j=s->transitions.head(); j != 0; j=next(j))
404
EST_String in = p_in_symbols.name(s->transitions(j)->in_symbol());
405
EST_String out=p_out_symbols.name(s->transitions(j)->out_symbol());
406
if (in.matches(needquotes))
407
fprintf(ofd," (%s ",(const char *)quote_string(in,"\"","\\",1));
409
fprintf(ofd," (%s ",(const char *)in);
410
if (out.matches(needquotes))
411
fprintf(ofd," %s ",(const char *)quote_string(out,"\"","\\",1));
413
fprintf(ofd," %s ",(const char *)out);
414
fprintf(ofd,"%d %g)\n",
415
s->transitions(j)->state(),
416
s->transitions(j)->weight());
369
421
if (ofd != stdout)
427
static float get_float(FILE *fd,int swap)
431
if (swap) swapfloat(&f);
435
static int get_int(FILE *fd,int swap)
445
EST_read_status EST_WFST::load_binary(FILE *fd,
452
int num_trans, state_type;
453
int in_sym, out_sym, next_state;
458
for (i=0; i < num_states; i++)
460
num_trans = get_int(fd,swap);
461
state_type = get_int(fd,swap);
463
if (state_type == WFST_FINAL)
464
s = add_state(wfst_final);
465
else if (state_type == WFST_NONFINAL)
466
s = add_state(wfst_nonfinal);
467
else if (state_type == WFST_LICENCE)
468
s = add_state(wfst_licence);
469
else if (state_type == WFST_ERROR)
470
s = add_state(wfst_error);
473
cerr << "WFST load: unknown state type \"" <<
474
state_type << "\"" << endl;
475
r = read_format_error;
481
cerr << "WFST load: internal error: unexpected state misalignment"
483
r = read_format_error;
487
for (j=0; j < num_trans; j++)
489
in_sym = get_int(fd,swap);
496
out_sym = get_int(fd,swap);
497
next_state = get_int(fd,swap);
498
trans_cost = get_float(fd,swap);
500
p_states[i]->add_transition(trans_cost,next_state,in_sym,out_sym);
375
508
EST_read_status EST_WFST::load(const EST_String &filename)
377
510
// Load a WFST from a file
411
545
init(inalpha,outalpha);
413
547
int num_states = hinfo.ival("NumStates");
415
for (i=0; i < num_states; i++)
417
LISP sd = lreadf(fd);
418
if (i != get_c_int(car(car(sd))))
420
cerr << "WFST load: expected description of state " << i <<
421
" but found \"" << siod_sprint(sd) << "\"" << endl;
422
return read_format_error;
424
if (streq("final",get_c_string(car(cdr(car(sd))))))
425
s = add_state(wfst_final);
426
else if (streq("nonfinal",get_c_string(car(cdr(car(sd))))))
427
s = add_state(wfst_nonfinal);
428
else if (streq("licence",get_c_string(car(cdr(car(sd))))))
429
s = add_state(wfst_licence);
552
if (!hinfo.present("ByteOrder"))
553
swap = FALSE; // ascii or not there for some reason
554
else if (((hinfo.val("ByteOrder") == "01") ? bo_little : bo_big)
432
cerr << "WFST load: unknown state type \"" <<
433
siod_sprint(car(cdr(car(sd)))) << "\"" << endl;
434
return read_format_error;
439
cerr << "WFST load: internal error: unexpected state misalignment"
441
return read_format_error;
443
if (load_transitions_from_lisp(s,cdr(sd)) != format_ok)
444
return read_format_error;
559
r = load_binary(fd,hinfo,num_states,swap);
563
for (i=0; i < num_states; i++)
565
LISP sd = lreadf(fd);
566
if (i != get_c_int(car(car(sd))))
568
cerr << "WFST load: expected description of state " << i <<
569
" but found \"" << siod_sprint(sd) << "\"" << endl;
570
r = read_format_error;
573
if (streq("final",get_c_string(car(cdr(car(sd))))))
574
s = add_state(wfst_final);
575
else if (streq("nonfinal",get_c_string(car(cdr(car(sd))))))
576
s = add_state(wfst_nonfinal);
577
else if (streq("licence",get_c_string(car(cdr(car(sd))))))
578
s = add_state(wfst_licence);
581
cerr << "WFST load: unknown state type \"" <<
582
siod_sprint(car(cdr(car(sd)))) << "\"" << endl;
583
r = read_format_error;
589
cerr << "WFST load: internal error: unexpected state misalignment"
591
r = read_format_error;
594
if (load_transitions_from_lisp(s,cdr(sd)) != format_ok)
596
r = read_format_error;
452
607
EST_read_status EST_WFST::load_transitions_from_lisp(int s, LISP trans)