Blame view

Scripts/utils/validate_dict_dir.pl 7.09 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
  #!/usr/bin/perl
  
  # Apache 2.0.
  # Guoguo Chen (guoguo@jhu.edu)
  # Daniel Povey (dpovey@gmail.com)
  #
  # Validation script for data/local/dict
  
  
  if(@ARGV != 1) {
    die "Usage: validate_dict_dir.pl dict_directory
  ";
  }
  
  $dict = shift @ARGV;
  
  $exit = 0;
  # Checking silence_phones.txt -------------------------------
  print "Checking $dict/silence_phones.txt ...
  ";
  if(-z "$dict/silence_phones.txt") {print "--> ERROR: $dict/silence_phones.txt is empty or not exists
  "; exit 1;}
  if(!open(S, "<$dict/silence_phones.txt")) {print "--> ERROR: fail to open $dict/silence_phones.txt
  "; exit 1;}
  $idx = 1;
  %silence = ();
  $success = 1;
  print "--> reading $dict/silence_phones.txt
  ";
  while(<S>) {
    chomp;
    my @col = split(" ", $_);
    foreach(0 .. @col-1) {
      my $p = $col[$_];
      if($silence{$p}) {$exit = 1; print "--> ERROR: phone \"$p\" duplicates in $dict/silence_phones.txt (line $idx)
  "; $success = 0;}
      else {$silence{$p} = 1;}
      if ($p =~ m/_$/ || $p =~ m/#/ || $p =~ m/_[BESI]$/){
        $exit = 1;
        print "--> ERROR: phone \"$p\" has disallowed written form";
        $success = 0;
      }
    }
    $idx ++;
  }
  close(S);
  $success == 0 || print "--> $dict/silence_phones.txt is OK
  ";
  print "
  ";
  
  # Checking optional_silence.txt -------------------------------
  print "Checking $dict/optional_silence.txt ...
  ";
  if(-z "$dict/optional_silence.txt") {print "--> ERROR: $dict/optional_silence.txt is empty or not exists
  "; exit 1;}
  if(!open(OS, "<$dict/optional_silence.txt")) {print "--> ERROR: fail to open $dict/optional_silence.txt
  "; exit 1;}
  $idx = 1;
  $success = 1;
  print "--> reading $dict/optional_silence.txt
  ";
  while(<OS>) {
    chomp;
    my @col = split(" ", $_);
    if ($idx > 1 or @col > 1) {
      $exit = 1; print "--> ERROR: only 1 phone expected in $dict/optional_silence.txt
  "; $success = 0;
    } elsif (!$silence{$col[0]}) {
      $exit = 1; print "--> ERROR: phone $col[0] not found in $dict/silence_phones.txt
  "; $success = 0;
    }
    $idx ++;
  }
  close(OS);
  $success == 0 || print "--> $dict/optional_silence.txt is OK
  ";
  print "
  ";
  
  # Checking nonsilence_phones.txt -------------------------------
  print "Checking $dict/nonsilence_phones.txt ...
  ";
  if(-z "$dict/nonsilence_phones.txt") {print "--> ERROR: $dict/nonsilence_phones.txt is empty or not exists
  "; exit 1;}
  if(!open(NS, "<$dict/nonsilence_phones.txt")) {print "--> ERROR: fail to open $dict/nonsilence_phones.txt
  "; exit 1;}
  $idx = 1;
  %nonsilence = ();
  $success = 1;
  print "--> reading $dict/nonsilence_phones.txt
  ";
  while(<NS>) {
    chomp;
    my @col = split(" ", $_);
    foreach(0 .. @col-1) {
      my $p = $col[$_];
      if($nonsilence{$p}) {$exit = 1; print "--> ERROR: phone \"$p\" duplicates in $dict/nonsilence_phones.txt (line $idx)
  "; $success = 0;}
      else {$nonsilence{$p} = 1;}
      if ($p =~ m/_$/ || $p =~ m/#/ || $p =~ m/_[BESI]$/){
        $exit = 1;
        print "--> ERROR: phone \"$p\" has disallowed written form";
        $success = 0;
      }
    }
    $idx ++;
  }
  close(NS);
  $success == 0 || print "--> $dict/silence_phones.txt is OK
  ";
  print "
  ";
  
  # Checking disjoint -------------------------------
  sub intersect {
    my ($a, $b) = @_;
    @itset = ();
    %itset = ();
    foreach(keys %$a) {
      if(exists $b->{$_} and !$itset{$_}) {
        push(@itset, $_);
        $itset{$_} = 1;
      }
    }
    return @itset;
  }
  
  print "Checking disjoint: silence_phones.txt, nonsilence_phones.txt
  ";
  @itset = intersect(\%silence, \%nonsilence);
  if(@itset == 0) {print "--> disjoint property is OK.
  ";}
  else {$exit = 1; print "--> ERROR: silence_phones.txt and nonsilence_phones.txt has overlap: "; foreach(@itset) {print "$_ ";} print "
  ";}
  print "
  ";
  
  
  sub check_lexicon {
    my ($lexfn, $pron_probs) = @_;
    print "Checking $lexfn
  ";
    if(-z "$lexfn") {$exit = 1; print "--> ERROR: $lexfn is empty or not exists
  ";}
    if(!open(L, "<$lexfn")) {$exit = 1; print "--> ERROR: fail to open $lexfn
  ";}
    $idx = 1;
    $success = 1;
    print "--> reading $lexfn
  ";
    while (<L>) {
      chomp;
      my @col = split(" ", $_);
      $word = shift @col;
      if (!defined $word) {
        $exit = 1; print "--> ERROR: empty lexicon line in $lexfn
  "; 
        $success = 0;
      }
      if ($pron_probs) {
        $prob = shift @col;
        if (!($prob > 0.0 && $prob <= 1.0)) { 
          $exit = 1; print "--> ERROR: bad pron-prob in lexicon-line '$_', in $lexfn
  ";
          $success = 0;
        }
      }
      foreach (0 .. @col-1) {
        if (!$silence{@col[$_]} and !$nonsilence{@col[$_]}) {
          $exit = 1; print "--> ERROR: phone \"@col[$_]\" is not in {, non}silence.txt (line $idx)
  "; 
          $success = 0;
        }
      }
      $idx ++;
    }
    close(L);
    $success == 0 || print "--> $lexfn is OK
  ";
    print "
  ";
  }
  
  if (-f "$dict/lexicon.txt") { check_lexicon("$dict/lexicon.txt", 0); }
  if (-f "$dict/lexiconp.txt") { check_lexicon("$dict/lexiconp.txt", 1); }
  if (!(-f "$dict/lexicon.txt" || -f "$dict/lexiconp.txt")) {
    print "--> ERROR: neither lexicon.txt or lexiconp.txt exist in directory $dir
  ";
    $exit = 1;
  }
  # If both lexicon.txt and lexiconp.txt exist, we check that they correspond to
  # each other.  If not, it could be that the user overwrote one and we need to
  # regenerate the other, but we don't know which is which.
  if ( (-f "$dict/lexicon.txt") && (-f "$dict/lexiconp.txt")) {
    print "Checking that lexicon.txt and lexiconp.txt match
  ";
    if (!open(L, "<$dict/lexicon.txt") || !open(P, "<$dict/lexiconp.txt")) {
      die "Error opening lexicon.txt and/or lexiconp.txt"; # already checked, so would be code error.
    }
    while(<L>) {
      @A = split;
      $x = <P>;
      if (!defined $x) {
        print "--> ERROR: lexicon.txt and lexiconp.txt have different numbers of lines (mismatch); delete one.
  ";
        $exit = 1;
        last;
      }
      @B = split(" ", $x);
      $w = shift @B;
      $p = shift @B;
      unshift @B, $w;
      # now @A and @B should be the same.
      if ($#A != $#B) {
        print "--> ERROR: lexicon.txt and lexiconp.txt have mismatched lines '$_' versus '$x'; delete one.
  ";
        $exit = 1;
        last;
      }
      for ($n = 0; $n < @A; $n++) {
        if ($A[$n] ne $B[$n]) {
          print "--> ERROR: lexicon.txt and lexiconp.txt have mismatched lines '$_' versus '$x'; delete one.
  ";
          $exit = 1;
          last;
        }
      }
    }
    $x = <P>;
    if (defined $x && $exit == 0) {
      print "--> ERROR: lexicon.txt and lexiconp.txt have different numbers of lines (mismatch); delete one.
  ";
      $exit = 1;
    }
  }
  
  # Checking extra_questions.txt -------------------------------
  print "Checking $dict/extra_questions.txt ...
  ";
  if(-s "$dict/extra_questions.txt") {
    if(!open(EX, "<$dict/extra_questions.txt")) {$exit = 1; print "--> ERROR: fail to open $dict/extra_questions.txt
  ";}
    $idx = 1;
    $success = 1;
    print "--> reading $dict/extra_questions.txt
  ";
    while(<EX>) {
      chomp;
      my @col = split(" ", $_);
      foreach(0 .. @col-1) {
        if(!$silence{@col[$_]} and !$nonsilence{@col[$_]}) {
          $exit = 1; print "--> ERROR: phone \"@col[$_]\" is not in {, non}silence.txt (line $idx, block ", $_+1, ")
  "; 
          $success = 0;
        }
      }
      $idx ++;
    } 
    close(EX);
    $success == 0 || print "--> $dict/extra_questions.txt is OK
  ";
  } else {print "--> $dict/extra_phones.txt is empty
  ";}
  
  if($exit == 1) { print " [Error detected ]
  "; exit 1;}