Blame view

Scripts/utils/validate_lang.pl 19.2 KB
ec85f8892   bigot benjamin   first commit
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
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
  #!/usr/bin/perl
  
  # Guoguo Chen (guoguo@jhu.edu)
  #
  # Validation script for data/lang
  
  if(@ARGV != 1) {
    die "Usage: validate_lang.pl lang_directory
  ";
  }
  
  $lang = shift @ARGV;
  $exit = 0;
  # Checking phones.txt -------------------------------
  print "Checking $lang/phones.txt ...
  ";
  if(-z "$lang/phones.txt") {print "--> ERROR: $lang/phones.txt is empty or not exists
  "; exit 1;}
  if(!open(P, "<$lang/phones.txt")) {print "--> ERROR: fail to open $lang/phones.txt
  "; exit 1;}
  $idx = 1;
  %psymtab = ();
  while(<P>) {
    chomp;
    my @col = split(" ", $_);
    if(@col != 2) {print "--> ERROR: expect 2 columns in $lang/phones.txt (break at line $idx)
  "; exit 1;}
    my $phone = shift @col;
    my $id = shift @col;
    $psymtab{$phone} = $id;
    $idx ++;
  }
  close(P);
  %pint2sym = (); 
  foreach(keys %psymtab) {
    if($pint2sym{$psymtab{$_}}) {print "--> ERROR: ID \"$psymtab{$_}\" duplicates
  "; exit 1;} 
    else {$pint2sym{$psymtab{$_}} = $_;}
  }
  print "--> $lang/phones.txt is OK
  ";
  print "
  ";
  
  # Check word.txt -------------------------------
  print "Checking words.txt: #0 ...
  ";
  if(-z "$lang/words.txt") {print "--> ERROR: $lang/words.txt is empty or not exists
  "; exit 1;}
  if(!open(W, "<$lang/words.txt")) {print "--> ERROR: fail to open $lang/words.txt
  "; exit 1;}
  $idx = 1;
  %wsymtab = ();
  while(<W>) {
    chomp;
    my @col = split(" ", $_);
    if(@col != 2) {print "--> ERROR: expect 2 columns in $lang/words.txt (line $idx)
  "; exit 1;}
    $word = shift @col;
    $id = shift @col;
    $wsymtab{$word} = $id;
    $idx ++;
  }
  close(W);
  %wint2sym = (); 
  foreach(keys %wsymtab) {
    if($wint2sym{$wsymtab{$_}}) {print "--> ERROR: ID \"$wsymtab{$_}\" duplicates
  "; exit 1;} 
    else {$wint2sym{$wsymtab{$_}} = $_;}
  }
  if(exists $wsymtab{"#0"}) {
    print "--> $lang/words.txt has \"#0\"
  ";
    print "--> $lang/words.txt is OK
  ";
  } else {print "--> ERROR: $lang/words.txt doesn't have \"#0\"
  "; $exit = 1;}
  print "
  ";
  
  # Checking phones/* -------------------------------
  sub check_txt_int_csl {
    my ($cat, $symtab) = @_;
    print "Checking $cat.\{txt, int, csl\} ...
  ";
    if(-z "$cat.txt") {$exit = 1; return print "--> ERROR: $cat.txt is empty or not exists
  ";}
    if(-z "$cat.int") {$exit = 1; return print "--> ERROR: $cat.int is empty or not exists
  ";}
    if(-z "$cat.csl") {$exit = 1; return print "--> ERROR: $cat.csl is empty or not exists
  ";}
    if(!open(TXT, "<$cat.txt")) {$exit = 1; return print "--> ERROR: fail to open $cat.txt
  ";}
    if(!open(INT, "<$cat.int")) {$exit = 1; return print "--> ERROR: fail to open $cat.int
  ";}
    if(!open(CSL, "<$cat.csl")) {$exit = 1; return print "--> ERROR: fail to open $cat.csl
  ";}
  
    $idx1 = 1;
    while(<TXT>) {
      chomp;
      my @col = split(" ", $_);
      if(@col != 1) {$exit = 1; return print "--> ERROR: expect 1 column in $cat.txt (break at line $idx1)
  ";}
      $entry[$idx1] = shift @col;
      $idx1 ++;
    }
    close(TXT); $idx1 --;
    print "--> $idx1 entry/entries in $cat.txt
  ";
  
    $idx2 = 1;
    while(<INT>) {
      chomp;
      my @col = split(" ", $_);
      if(@col != 1) {$exit = 1; return print "--> ERROR: expect 1 column in $cat.int (break at line $idx2)
  ";}
      if($symtab->{$entry[$idx2]} ne shift @col) {$exit = 1; return print "--> ERROR: $cat.int doesn't correspond to $cat.txt (break at line $idx2)
  ";}
      $idx2 ++;
    }
    close(INT); $idx2 --;
    if($idx1 != $idx2) {$exit = 1; return print "--> ERROR: $cat.int doesn't correspond to $cat.txt (break at line ", $idx2+1, ")
  ";}
    print "--> $cat.int corresponds to $cat.txt
  ";
  
    $idx3 = 1;
    while(<CSL>) {
      chomp;
      my @col = split(":", $_);
      if(@col != $idx1) {$exit = 1; return print "--> ERROR: expect $idx1 block/blocks in $cat.csl (break at line $idx3)
  ";}
      foreach(1 .. $idx1) {
        if($symtab->{$entry[$_]} ne @col[$_-1]) {$exit = 1; return print "--> ERROR: $cat.csl doesn't correspond to $cat.txt (break at line $idx3, block $_)
  ";}
      }
      $idx3 ++;
    }
    close(CSL); $idx3 --;
    if($idx3 != 1) {$exit = 1; return print "--> ERROR: expect 1 row in $cat.csl (break at line ", $idx3+1, ")
  ";}
    print "--> $cat.csl corresponds to $cat.txt
  ";
  
    return print "--> $cat.\{txt, int, csl\} are OK
  ";
  }
  
  sub check_txt_int {
    my ($cat, $symtab) = @_;
    print "Checking $cat.\{txt, int\} ...
  ";
    if(-z "$cat.txt") {$exit = 1; return print "--> ERROR: $cat.txt is empty or not exists
  ";}
    if(-z "$cat.int") {$exit = 1; return print "--> ERROR: $cat.int is empty or not exists
  ";}
    if(!open(TXT, "<$cat.txt")) {$exit = 1; return print "--> ERROR: fail to open $cat.txt
  ";}
    if(!open(INT, "<$cat.int")) {$exit = 1; return print "--> ERROR: fail to open $cat.int
  ";}
  
    $idx1 = 1;
    while(<TXT>) {
      chomp;
      s/^(shared|not-shared) (split|not-split) //g;
      s/ nonword$//g;
      s/ begin$//g;
      s/ end$//g;
      s/ internal$//g;
      s/ singleton$//g;
      $entry[$idx1] = $_;
      $idx1 ++; 
    }
    close(TXT); $idx1 --;
    print "--> $idx1 entry/entries in $cat.txt
  ";
  
    $idx2 = 1;
    while(<INT>) {
      chomp;
      s/^(shared|not-shared) (split|not-split) //g;
      s/ nonword$//g;
      s/ begin$//g;
      s/ end$//g;
      s/ internal$//g;
      s/ singleton$//g;
      my @col = split(" ", $_);
      @set = split(" ", $entry[$idx2]);
      if(@set != @col) {$exit = 1; return print "--> ERROR: $cat.int doesn't correspond to $cat.txt (break at line $idx2)
  ";}
      foreach(0 .. @set-1) {
        if($symtab->{@set[$_]} ne @col[$_]) {$exit = 1; return print "--> ERROR: $cat.int doesn't correspond to $cat.txt (break at line $idx2, block " ,$_+1, ")
  ";}
      }
      $idx2 ++;
    }
    close(INT); $idx2 --;
    if($idx1 != $idx2) {$exit = 1; return print "--> ERROR: $cat.int doesn't correspond to $cat.txt (break at line ", $idx2+1, ")
  ";}
    print "--> $cat.int corresponds to $cat.txt
  ";
  
    return print "--> $cat.\{txt, int\} are OK
  ";
  }
  
  @list1 = ("context_indep", "disambig", "nonsilence", "silence", "optional_silence");
  @list2 = ("roots", "sets");
  foreach(@list1) {
    check_txt_int_csl("$lang/phones/$_", \%psymtab); print "
  ";
  }
  foreach(@list2) {
    check_txt_int("$lang/phones/$_", \%psymtab); print "
  ";
  }
  if((-s "$lang/phones/extra_questions.txt") || (-s "$lang/phones/extra_questions.int")) {
    check_txt_int("$lang/phones/extra_questions", \%psymtab); print "
  ";
  } else {
    print "Checking $lang/phones/extra_questions.\{txt, int\} ...
  ";
    if((-f "$lang/phones/extra_questions.txt") && (-f "$lang/phones/extra_questions.int")) {
      print "--> WARNING: the optional $lang/phones/extra_questions.\{txt, int\} are empty!
  
  ";
    } else {
      $exit = 1; print "--> ERROR: $lang/phones/extra_questions.\{txt, int\} do not exist (they may be empty, but should be present)
  
  ";
    }
  } 
  if(-e "$lang/phones/word_boundary.txt") {
    check_txt_int("$lang/phones/word_boundary", \%psymtab); print "
  ";
  }
  
  # Check disjoint and summation -------------------------------
  sub intersect {
    my ($a, $b) = @_;
    @itset = ();
    %itset = ();
    foreach(keys %$a) {
      if(exists $b->{$_} and !$itset{$_}) {
        push(@itset, $_);
        $itset{$_} = 1;
      }
    }
    return @itset;
  }
  
  sub check_disjoint {
    print "Checking disjoint: silence.txt, nosilenct.txt, disambig.txt ...
  ";
    if(!open(S, "<$lang/phones/silence.txt"))    {$exit = 1; return print "--> ERROR: fail to open $lang/phones/silence.txt
  ";}
    if(!open(N, "<$lang/phones/nonsilence.txt")) {$exit = 1; return print "--> ERROR: fail to open $lang/phones/nonsilence.txt
  ";}
    if(!open(D, "<$lang/phones/disambig.txt"))   {$exit = 1; return print "--> ERROR: fail to open $lang/phones/disambig.txt
  ";}
  
    $idx = 1;
    while(<S>) {
      chomp;
      my @col = split(" ", $_);
      $phone = shift @col;
      if($silence{$phone}) {$exit = 1; print "--> ERROR: phone \"$phone\" duplicates in $lang/phones/silence.txt (line $idx)
  ";}
      $silence{$phone} = 1;
      push(@silence, $phone);
      $idx ++;
    }
    close(S);
  
    $idx = 1; 
    while(<N>) {
      chomp;
      my @col = split(" ", $_);
      $phone = shift @col;
      if($nonsilence{$phone}) {$exit = 1; print "--> ERROR: phone \"$phone\" duplicates in $lang/phones/nonsilence.txt (line $idx)
  ";}
      $nonsilence{$phone} = 1;
      push(@nonsilence, $phone);
      $idx ++;
    }
    close(N);
  
    $idx = 1;
    while(<D>) {
      chomp;
      my @col = split(" ", $_);
      $phone = shift @col;
      if($disambig{$phone}) {$exit = 1; print "--> ERROR: phone \"$phone\" duplicates in $lang/phones/disambig.txt (line $idx)
  ";}
      $disambig{$phone} = 1;
      $idx ++;
    }
    close(D);
  
    my @itsect1 = intersect(\%silence, \%nonsilence);
    my @itsect2 = intersect(\%silence, \%disambig);
    my @itsect3 = intersect(\%disambig, \%nonsilence);
  
    $success = 1;
    if(@itsect1 != 0) {
      $success = 0;
      $exit = 1; print "--> ERROR: silence.txt and nonsilence.txt have intersection -- ";
      foreach(@itsect1) {
        print $_, " ";
      }
      print "
  ";
    } else {print "--> silence.txt and nonsilence.txt are disjoint
  ";}
  
    if(@itsect2 != 0) {
      $success = 0;
      $exit = 1; print "--> ERROR: silence.txt and disambig.txt have intersection -- ";
      foreach(@itsect2) {
        print $_, " ";
      }
      print "
  ";
    } else {print "--> silence.txt and disambig.txt are disjoint
  ";}
  
    if(@itsect3 != 0) {
      $success = 0;
      $exit = 1; print "--> ERROR: disambig.txt and nonsilence.txt have intersection -- ";
      foreach(@itsect1) {
        print $_, " ";
      }
      print "
  ";
    } else {print "--> disambig.txt and nonsilence.txt are disjoint
  ";}
  
    $success == 0 || print "--> disjoint property is OK
  ";
    return;
  }
  
  sub check_summation {
    print "Checking sumation: silence.txt, nonsilence.txt, disambig.txt ...
  ";
    if(scalar(keys %silence) == 0)      {$exit = 1; return print "--> ERROR: $lang/phones/silence.txt is empty or not exists
  ";}
    if(scalar(keys %nonsilence) == 0)   {$exit = 1; return print "--> ERROR: $lang/phones/nonsilence.txt is empty or not exists
  ";}
    if(scalar(keys %disambig) == 0)     {$exit = 1; return print "--> ERROR: $lang/phones/disambig.txt is empty or not exists
  ";}
  
    %sum = (%silence, %nonsilence, %disambig);
    $sum{"<eps>"} = 1;
  
    my @itset = intersect(\%sum, \%psymtab);
    my @key1 = keys %sum;
    my @key2 = keys %psymtab;
    my %itset = (); foreach(@itset) {$itset{$_} = 1;}
    if(@itset < @key1) {
      $exit = 1; print "--> ERROR: phones in silence.txt, nonsilence.txt, disambig.txt but not in phones.txt -- ";
      foreach(@key1) {
        if(!$itset{$_}) {print "$_ ";}
      }
      print "
  ";
    }
  
    if(@itset < @key2) {
      $exit = 1; print "--> ERROR: phones in phones.txt but not in silence.txt, nonsilence.txt, disambig.txt -- ";
      foreach(@key2) {
        if(!$itset{$_}) {print "$_ ";}
      }
      print "
  ";
    }
  
    if(@itset == @key1 and @itset == @key2) {
      print "--> summation property is OK
  ";
    }
    return;
  }
  
  %silence = ();
  @silence = ();
  %nonsilence = ();
  @nonsilence = ();
  %disambig = ();
  check_disjoint; print "
  ";
  check_summation; print "
  ";
  
  # Checking optional_silence.txt -------------------------------
  print "Checking optional_silence.txt ...
  ";
  $idx = 1;
  $success = 1;
  if(-z "$lang/phones/optional_silence.txt") {$exit = 1; $success = 0; print "--> ERROR: $lang/phones/optional_silence.txt is empty or not exists
  ";}
  if(!open(OS, "<$lang/phones/optional_silence.txt")) {$exit = 1; $success = 0; print "--> ERROR: fail to open $lang/phones/optional_silence.txt
  ";}
  print "--> reading $lang/phones/optional_silence.txt
  ";
  while(<OS>) {
    chomp;
    my @col = split(" ", $_);
    if ($idx > 1 or @col > 1) {
      $exit = 1; print "--> ERROR: only 1 phone expected in $lang/phones/optional_silence.txt
  "; $success = 0;
    } elsif (!$silence{$col[0]}) {
      $exit = 1; print "--> ERROR: phone $col[0] not found in $lang/phones/silence_phones.txt
  "; $success = 0;
    }
    $idx ++;
  }
  close(OS);
  $success == 0 || print "--> $lang/phones/optional_silence.txt is OK
  ";
  print "
  ";
  
  # Check disambiguation symbols -------------------------------
  print "Checking disambiguation symbols: #0 and #1
  ";
  if(scalar(keys %disambig) == 0) {$exit = 1; print "--> ERROR: $lang/phones/disambig.txt is empty or not exists
  ";}
  if(exists $disambig{"#0"} and exists $disambig{"#1"}) {
    print "--> $lang/phones/disambig.txt has \"#0\" and \"#1\"
  ";
    print "--> $lang/phones/disambig.txt is OK
  
  ";
  } else {
    $exit = 1; print "--> ERROR: $lang/phones/disambig.txt doesn't have \"#0\" or \"#1\"
  ";
  }
  
  
  # Check topo -------------------------------
  print "Checking topo ...
  ";
  if(-z "$lang/topo") {$exit = 1; print "--> ERROR: $lang/topo is empty or not exists
  ";}
  if(!open(T, "<$lang/topo")) {$exit = 1; print "--> ERROR: fail to open $lang/topo
  ";}
  $idx = 1;
  while(<T>) {
    chomp;
    next if(m/^<.*>[ ]*$/);
    if($idx == 1) {$nonsilence_seq = $_; $idx ++;}
    if($idx == 2) {$silence_seq = $_;}
  }
  close(T);
  if($silence_seq == 0 || $nonsilence_seq == 0) {$exit = 1; print "--> ERROR: $lang/topo doesn't have nonsilence section or silence section
  ";}
  @silence_seq = split(" ", $silence_seq);
  @nonsilence_seq = split(" ", $nonsilence_seq);
  $success1 = 1;
  if(@nonsilence_seq != @nonsilence) {$exit = 1; print "--> ERROR: $lang/topo's nonsilence section doesn't correspond to nonsilence.txt
  ";}
  else {
    foreach(0 .. scalar(@nonsilence)-1) {
      if($psymtab{@nonsilence[$_]} ne @nonsilence_seq[$_]) {
        $exit = 1; print "--> ERROR: $lang/topo's nonsilence section doesn't correspond to nonsilence.txt
  ";
        $success = 0;
      }
    }
  }
  $success1 != 1 || print "--> $lang/topo's nonsilence section is OK
  ";
  $success2 = 1;
  if(@silence_seq != @silence) {$exit = 1; print "--> ERROR: $lang/topo's silence section doesn't correspond to silence.txt
  ";}
  else {
    foreach(0 .. scalar(@silence)-1) {
      if($psymtab{@silence[$_]} ne @silence_seq[$_]) {
        $exit = 1; print "--> ERROR: $lang/topo's silence section doesn't correspond to silence.txt
  ";
        $success = 0;
      }
    }
  }
  $success2 != 1 || print "--> $lang/topo's silence section is OK
  ";
  $success1 != 1 or $success2 != 1 || print "--> $lang/topo is OK
  ";
  print "
  ";
  
  # Check word_boundary -------------------------------
  $nonword   = "";
  $begin     = "";
  $end       = "";
  $internal  = "";
  $singleton = "";
  if(-s "$lang/phones/word_boundary.txt") {
    print "Checking word_boundary.txt: silence.txt, nonsilence.txt, disambig.txt ...
  ";
    if(!open (W, "<$lang/phones/word_boundary.txt")) {$exit = 1; print "--> ERROR: fail to open $lang/phones/word_boundary.txt
  ";}
    $idx = 1;
    %wb = ();
    while(<W>) {
      chomp;
      my @col;
      if (m/^.*nonword$/  ) {s/ nonword//g;    @col = split(" ", $_); if (@col == 1) {$nonword   .= "$col[0] ";}}
      if (m/^.*begin$/    ) {s/ begin$//g;     @col = split(" ", $_); if (@col == 1) {$begin     .= "$col[0] ";}}
      if (m/^.*end$/      ) {s/ end$//g;       @col = split(" ", $_); if (@col == 1) {$end       .= "$col[0] ";}}
      if (m/^.*internal$/ ) {s/ internal$//g;  @col = split(" ", $_); if (@col == 1) {$internal  .= "$col[0] ";}}
      if (m/^.*singleton$/) {s/ singleton$//g; @col = split(" ", $_); if (@col == 1) {$singleton .= "$col[0] ";}}
      if(@col != 1) {$exit = 1; print "--> ERROR: expect 1 column in $lang/phones/word_boundary.txt (line $idx)
  ";}
      $wb{shift @col} = 1;
      $idx ++;
    }
    close(W);
  
    @itset = intersect(\%disambig, \%wb);
    $success1 = 1;
    if(@itset != 0) {
      $success1 = 0;
      $exit = 1; print "--> ERROR: $lang/phones/word_boundary.txt has disambiguation symbols -- ";
      foreach(@itset) {print "$_ ";}
      print "
  ";
    }
    $success1 == 0 || print "--> $lang/phones/word_boundary.txt doesn't include disambiguation symbols
  ";
  
    %sum = (%silence, %nonsilence);
    @itset = intersect(\%sum, \%wb);
    %itset = (); foreach(@itset) {$itset{$_} = 1;}
    $success2 = 1;
    if(@itset < scalar(keys %sum)) {
      $success2 = 0;
      $exit = 1; print "--> ERROR: phones in nonsilence.txt and silence.txt but not in word_boundary.txt -- ";
      foreach(keys %sum) {
        if(!$itset{$_}) {print "$_ ";}            
      }
      print "
  ";
    }
    if(@itset < scalar(keys %wb)) {
      $success2 = 0;
      $exit = 1; print "--> ERROR: phones in word_boundary.txt but not in nonsilence.txt or silence.txt -- ";
      foreach(keys %wb) {
        if(!$itset{$_}) {print "$_ ";}
      }
      print "
  ";
    }
    $success2 == 0 || print "--> $lang/phones/word_boundary.txt is the union of nonsilence.txt and silence.txt
  ";
    $success1 != 1 or $success2 != 1 || print "--> $lang/phones/word_boundary.txt is OK
  ";
    print "
  ";
  }
  
  if(-s "$lang/phones/word_boundary.int") {
    print "Checking word_boundary.int and disambig.int
  ";
    if(!open (W, "<$lang/phones/word_boundary.int")) {$exit = 1; print "--> ERROR: fail to open $lang/phones/word_boundary.int
  ";}
    while (<W>) {
      @A = split;
      if (@A != 2) { $exit = 1; print "--> ERROR: bad line $_ in $lang/phones/word_boundary.int
  "; }
      $wbtype{$A[0]} = $A[1];
    }
    close(W);
    if(!open (D, "<$lang/phones/disambig.int")) {$exit = 1; print "--> ERROR: fail to open $lang/phones/disambig.int
  ";}
    while (<D>) { 
      @A = split;
      if (@A != 1) { $exit = 1; print "--> ERROR: bad line $_ in $lang/phones/disambig.int
  "; }
      $is_disambig{$A[0]} = 1;
    }
  
    foreach $fst ("L.fst", "L_disambig.fst") {
      $wlen = int(rand(100)) + 1;
      print "--> generating a $wlen words sequence
  ";
      $wordseq = "";
      $sid = 0;
      foreach (1 .. $wlen) {
        $id = int(rand(scalar(%wint2sym)));
        while ($wint2sym{$id} =~ m/^#[0-9]*$/ or $id == 0) {
          $id = int(rand(scalar(%wint2sym)));
        }
        $wordseq = $wordseq . "$sid ". ($sid + 1) . " $id $id 0
  ";
        $sid ++;
      }
      $wordseq = $wordseq . "$sid 0";
      $phoneseq = `echo \"$wordseq" | fstcompile | fstcompose $lang/$fst - | fstproject | fstrandgen | fstrmepsilon | fsttopsort | fstprint | awk '{if(NF > 2) {print \$3}}';`;
      @phoneseq = split(" ", $phoneseq);
      $transition = { }; # empty assoc. array of allowed transitions between phone types.  1 means we count a word,
      # 0 means transition is allowed.  bos and eos are added as extra symbols here.
      foreach $x ("bos", "nonword", "end", "singleton") {
        $transition{$x, "nonword"} = 0;
        $transition{$x, "begin"} = 1;
        $transition{$x, "singleton"} = 1;
        $transition{$x, "eos"} = 0;
      }
      $transition{"begin", "end"} = 0;
      $transition{"begin", "internal"} = 0;
      $transition{"internal", "internal"} = 0;
      $transition{"internal", "end"} = 0;
  
      $cur_state = "bos";
      $num_words = 0;
      foreach $phone (split (" ", "$phoneseq eos")) {
        if (!($fst == "L_disambig.fst" && defined $is_disambig{$phone})) {
          if ($phone == "eos") {
            $state = "eos";
          } else {
            $state = $wbtype{$phone};
          }
          if (!defined $state) {
            $exit = 1; print "--> ERROR: phone $phone is not specified in $lang/phones/word_boundary.int
  ";
            last;
          } elsif (!defined $transition{$cur_state, $state}) {
            $exit = 1; print "--> ERROR: transition from state $cur_state to $state indicates error in word_boundary.int or L.fst
  ";
            last;
          } else {
            $num_words += $transition{$cur_state, $state};
            $cur_state = $state;
          }
        }
      }
      if (!$exit) {
        if ($num_words != $wlen) {
          $exit = 1; print "--> ERROR: number of reconstructed words $num_words does not match real number of words $wlen; indicates problem in $fst or word_boundary.int.  phoneseq = $phoneseq
  ";
        } else {
          print "--> resulting phone sequence from $fst corresponds to the word sequence
  ";
          print "--> $fst is OK
  ";
        }
      }
    }
    print "
  ";
  }
  
  # Check oov -------------------------------
  check_txt_int("$lang/oov", \%wsymtab); print "
  ";
  
  
  if ($exit == 1) { print "--> ERROR
  "; exit 1;}
  else { print "--> SUCCESS
  "; exit 0; }