~ubuntu-branches/ubuntu/jaunty/speech-tools/jaunty

« back to all changes in this revision

Viewing changes to grammar/wfst/EST_WFST.cc

  • Committer: Bazaar Package Importer
  • Author(s): Matthias Urlichs
  • Date: 2004-07-16 09:25:39 UTC
  • mfrom: (2.1.1 warty)
  • Revision ID: james.westby@ubuntu.com-20040716092539-5p1tzif55b8j924e
Tags: 1:1.2.3-8
Added alaw processing code.

Show diffs side-by-side

added added

removed removed

Lines of Context:
42
42
/*                                                                       */
43
43
/*=======================================================================*/
44
44
 
45
 
#include <iostream.h>
 
45
#include <iostream>
46
46
#include "EST_Pathname.h"
47
47
#include "EST_cutils.h"
48
48
#include "EST_Token.h"
301
301
    }
302
302
}
303
303
 
304
 
EST_write_status EST_WFST::save(const EST_String &filename)
 
304
EST_write_status EST_WFST::save_binary(FILE *fd)
 
305
{
 
306
    int i;
 
307
    EST_Litem *j;
 
308
    int num_transitions, type, in, out, next_state;
 
309
    float weight;
 
310
    
 
311
    for (i=0; i<p_num_states; i++)
 
312
    {
 
313
        num_transitions = p_states[i]->num_transitions();
 
314
        fwrite(&num_transitions,4,1,fd);
 
315
        if (p_states[i]->type() == wfst_final)
 
316
            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)
 
320
            type = WFST_LICENCE;
 
321
        else
 
322
            type = WFST_ERROR;
 
323
        fwrite(&type,4,1,fd);
 
324
        for (j=p_states[i]->transitions.head(); j != 0; j=next(j))
 
325
        {
 
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();
 
330
 
 
331
            if (in == out)
 
332
            {
 
333
                in *= -1;
 
334
                fwrite(&in,4,1,fd);
 
335
            }
 
336
            else
 
337
            {
 
338
                fwrite(&in,4,1,fd);
 
339
                fwrite(&out,4,1,fd);
 
340
            }
 
341
            fwrite(&next_state,4,1,fd);
 
342
            fwrite(&weight,4,1,fd);
 
343
        }
 
344
    }
 
345
 
 
346
    return write_ok;
 
347
}
 
348
 
 
349
EST_write_status EST_WFST::save(const EST_String &filename,
 
350
                                const EST_String type)
305
351
{
306
352
    FILE *ofd;
307
353
    int i;
317
363
    }
318
364
    
319
365
    fprintf(ofd,"EST_File fst\n");
320
 
    fprintf(ofd,"DataType ascii\n");
 
366
    fprintf(ofd,"DataType %s\n",(const char *)type);
321
367
    fprintf(ofd,"in %s\n",
322
368
      (const char *)quote_string(EST_String("(")+
323
369
                                 p_in_symbols.print_to_string(TRUE)+")",
327
373
                                 p_out_symbols.print_to_string(TRUE)+")",
328
374
                                 "\"","\\",1));
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");
331
378
 
332
 
    for (i=0; i < p_num_states; i++)
 
379
    if (type == "binary")
 
380
        save_binary(ofd);
 
381
    else
333
382
    {
334
 
        EST_WFST_State *s=p_states[i];
335
 
        fprintf(ofd,"((%d ",s->name());
336
 
        switch(s->type())
337
 
        {
338
 
          case wfst_final: 
339
 
            fprintf(ofd,"final ");
340
 
            break;
341
 
          case wfst_nonfinal: 
342
 
            fprintf(ofd,"nonfinal ");
343
 
            break;
344
 
          case wfst_licence: 
345
 
            fprintf(ofd,"licence ");
346
 
            break;
347
 
          default: 
348
 
            fprintf(ofd,"error ");
349
 
        }
350
 
        fprintf(ofd,"%d)\n",s->num_transitions());
351
 
        for (j=s->transitions.head(); j != 0; j=next(j))
352
 
        {
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));
357
 
            else
358
 
                fprintf(ofd,"  (%s ",(const char *)in);
359
 
            if (out.matches(needquotes))
360
 
                fprintf(ofd," %s ",(const char *)quote_string(out,"\"","\\",1));
361
 
            else
362
 
                fprintf(ofd," %s ",(const char *)out);
363
 
            fprintf(ofd,"%d %g)\n",
364
 
                    s->transitions(j)->state(),
365
 
                    s->transitions(j)->weight());
366
 
        }
367
 
        fprintf(ofd,")\n");
 
383
        for (i=0; i < p_num_states; i++)
 
384
        {
 
385
            EST_WFST_State *s=p_states[i];
 
386
            fprintf(ofd,"((%d ",s->name());
 
387
            switch(s->type())
 
388
            {
 
389
            case wfst_final: 
 
390
                fprintf(ofd,"final ");
 
391
                break;
 
392
            case wfst_nonfinal: 
 
393
                fprintf(ofd,"nonfinal ");
 
394
                break;
 
395
            case wfst_licence: 
 
396
                fprintf(ofd,"licence ");
 
397
                break;
 
398
            default: 
 
399
                fprintf(ofd,"error ");
 
400
            }
 
401
            fprintf(ofd,"%d)\n",s->num_transitions());
 
402
            for (j=s->transitions.head(); j != 0; j=next(j))
 
403
            {
 
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));
 
408
                else
 
409
                    fprintf(ofd,"  (%s ",(const char *)in);
 
410
                if (out.matches(needquotes))
 
411
                    fprintf(ofd," %s ",(const char *)quote_string(out,"\"","\\",1));
 
412
                else
 
413
                    fprintf(ofd," %s ",(const char *)out);
 
414
                fprintf(ofd,"%d %g)\n",
 
415
                        s->transitions(j)->state(),
 
416
                        s->transitions(j)->weight());
 
417
            }
 
418
            fprintf(ofd,")\n");
 
419
        }
368
420
    }
369
421
    if (ofd != stdout)
370
422
        fclose(ofd);
372
424
    return write_ok;
373
425
}
374
426
 
 
427
static float get_float(FILE *fd,int swap)
 
428
{
 
429
    float f;
 
430
    fread(&f,4,1,fd);
 
431
    if (swap) swapfloat(&f);
 
432
    return f;
 
433
}
 
434
 
 
435
static int get_int(FILE *fd,int swap)
 
436
{
 
437
    int i;
 
438
    fread(&i,4,1,fd);
 
439
    if (swap) 
 
440
        return SWAPINT(i);
 
441
    else
 
442
        return i;
 
443
}
 
444
 
 
445
EST_read_status EST_WFST::load_binary(FILE *fd,
 
446
                                      EST_Option &hinfo,
 
447
                                      int num_states,
 
448
                                      int swap)
 
449
{
 
450
    EST_read_status r;
 
451
    int i,j, s;
 
452
    int num_trans, state_type;
 
453
    int in_sym, out_sym, next_state;
 
454
    float trans_cost;
 
455
 
 
456
    r = format_ok;
 
457
 
 
458
    for (i=0; i < num_states; i++)
 
459
    {
 
460
        num_trans = get_int(fd,swap);
 
461
        state_type = get_int(fd,swap);
 
462
        
 
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);
 
471
        else
 
472
        {
 
473
            cerr << "WFST load: unknown state type \"" << 
 
474
                state_type << "\"" << endl;
 
475
            r = read_format_error;
 
476
            break;
 
477
        }
 
478
 
 
479
        if (s != i)
 
480
        {
 
481
            cerr << "WFST load: internal error: unexpected state misalignment"
 
482
                 << endl;
 
483
            r = read_format_error;
 
484
            break;
 
485
        }
 
486
 
 
487
        for (j=0; j < num_trans; j++)
 
488
        {
 
489
            in_sym = get_int(fd,swap);
 
490
            if (in_sym < 0)
 
491
            {
 
492
                in_sym *= -1;
 
493
                out_sym = in_sym;
 
494
            }
 
495
            else
 
496
                out_sym = get_int(fd,swap);
 
497
            next_state = get_int(fd,swap);
 
498
            trans_cost = get_float(fd,swap);
 
499
 
 
500
            p_states[i]->add_transition(trans_cost,next_state,in_sym,out_sym);
 
501
        }
 
502
    }
 
503
 
 
504
    return r;
 
505
}
 
506
 
 
507
 
375
508
EST_read_status EST_WFST::load(const EST_String &filename)
376
509
{
377
510
    // Load a WFST from a file
382
515
    EST_EstFileType t;
383
516
    EST_read_status r;
384
517
    int i,s;
 
518
    int swap;
385
519
 
386
520
    if ((fd=fopen(filename,"r")) == NULL)
387
521
    {
411
545
    init(inalpha,outalpha);
412
546
 
413
547
    int num_states = hinfo.ival("NumStates");
414
 
    
415
 
    for (i=0; i < num_states; i++)
 
548
    r = format_ok;
 
549
 
 
550
    if (!ascii)
416
551
    {
417
 
        LISP sd = lreadf(fd);
418
 
        if (i != get_c_int(car(car(sd))))
419
 
        {
420
 
            cerr << "WFST load: expected description of state " << i <<
421
 
                " but found \"" << siod_sprint(sd) << "\"" << endl;
422
 
            return read_format_error;
423
 
        }
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) 
 
555
                 != EST_NATIVE_BO)
 
556
            swap = TRUE;
430
557
        else
431
 
        {
432
 
            cerr << "WFST load: unknown state type \"" << 
433
 
                siod_sprint(car(cdr(car(sd)))) << "\"" << endl;
434
 
            return read_format_error;
435
 
        }
436
 
 
437
 
        if (s != i)
438
 
        {
439
 
            cerr << "WFST load: internal error: unexpected state misalignment"
440
 
                << endl;
441
 
            return read_format_error;
442
 
        }
443
 
        if (load_transitions_from_lisp(s,cdr(sd)) != format_ok)
444
 
            return read_format_error;
 
558
            swap = FALSE;
 
559
        r = load_binary(fd,hinfo,num_states,swap);
 
560
    }
 
561
    else
 
562
    {
 
563
        for (i=0; i < num_states; i++)
 
564
        {
 
565
            LISP sd = lreadf(fd);
 
566
            if (i != get_c_int(car(car(sd))))
 
567
            {
 
568
                cerr << "WFST load: expected description of state " << i <<
 
569
                    " but found \"" << siod_sprint(sd) << "\"" << endl;
 
570
                r = read_format_error;
 
571
                break;
 
572
            }
 
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);
 
579
            else
 
580
            {
 
581
                cerr << "WFST load: unknown state type \"" << 
 
582
                    siod_sprint(car(cdr(car(sd)))) << "\"" << endl;
 
583
                r = read_format_error;
 
584
                break;
 
585
            }
 
586
            
 
587
            if (s != i)
 
588
            {
 
589
                cerr << "WFST load: internal error: unexpected state misalignment"
 
590
                     << endl;
 
591
                r = read_format_error;
 
592
                break;
 
593
            }
 
594
            if (load_transitions_from_lisp(s,cdr(sd)) != format_ok)
 
595
            {
 
596
                r = read_format_error;
 
597
                break;
 
598
            }
 
599
        }
445
600
    }
446
601
 
447
602
    fclose(fd);
448
603
    
449
 
    return format_ok;
 
604
    return r;
450
605
}
451
606
 
452
607
EST_read_status EST_WFST::load_transitions_from_lisp(int s, LISP trans)