validate_dict_dir.pl
7.09 KB
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
#!/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\n";
}
$dict = shift @ARGV;
$exit = 0;
# Checking silence_phones.txt -------------------------------
print "Checking $dict/silence_phones.txt ...\n";
if(-z "$dict/silence_phones.txt") {print "--> ERROR: $dict/silence_phones.txt is empty or not exists\n"; exit 1;}
if(!open(S, "<$dict/silence_phones.txt")) {print "--> ERROR: fail to open $dict/silence_phones.txt\n"; exit 1;}
$idx = 1;
%silence = ();
$success = 1;
print "--> reading $dict/silence_phones.txt\n";
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)\n"; $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\n";
print "\n";
# Checking optional_silence.txt -------------------------------
print "Checking $dict/optional_silence.txt ...\n";
if(-z "$dict/optional_silence.txt") {print "--> ERROR: $dict/optional_silence.txt is empty or not exists\n"; exit 1;}
if(!open(OS, "<$dict/optional_silence.txt")) {print "--> ERROR: fail to open $dict/optional_silence.txt\n"; exit 1;}
$idx = 1;
$success = 1;
print "--> reading $dict/optional_silence.txt\n";
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\n"; $success = 0;
} elsif (!$silence{$col[0]}) {
$exit = 1; print "--> ERROR: phone $col[0] not found in $dict/silence_phones.txt\n"; $success = 0;
}
$idx ++;
}
close(OS);
$success == 0 || print "--> $dict/optional_silence.txt is OK\n";
print "\n";
# Checking nonsilence_phones.txt -------------------------------
print "Checking $dict/nonsilence_phones.txt ...\n";
if(-z "$dict/nonsilence_phones.txt") {print "--> ERROR: $dict/nonsilence_phones.txt is empty or not exists\n"; exit 1;}
if(!open(NS, "<$dict/nonsilence_phones.txt")) {print "--> ERROR: fail to open $dict/nonsilence_phones.txt\n"; exit 1;}
$idx = 1;
%nonsilence = ();
$success = 1;
print "--> reading $dict/nonsilence_phones.txt\n";
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)\n"; $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\n";
print "\n";
# 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\n";
@itset = intersect(\%silence, \%nonsilence);
if(@itset == 0) {print "--> disjoint property is OK.\n";}
else {$exit = 1; print "--> ERROR: silence_phones.txt and nonsilence_phones.txt has overlap: "; foreach(@itset) {print "$_ ";} print "\n";}
print "\n";
sub check_lexicon {
my ($lexfn, $pron_probs) = @_;
print "Checking $lexfn\n";
if(-z "$lexfn") {$exit = 1; print "--> ERROR: $lexfn is empty or not exists\n";}
if(!open(L, "<$lexfn")) {$exit = 1; print "--> ERROR: fail to open $lexfn\n";}
$idx = 1;
$success = 1;
print "--> reading $lexfn\n";
while (<L>) {
chomp;
my @col = split(" ", $_);
$word = shift @col;
if (!defined $word) {
$exit = 1; print "--> ERROR: empty lexicon line in $lexfn\n";
$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\n";
$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)\n";
$success = 0;
}
}
$idx ++;
}
close(L);
$success == 0 || print "--> $lexfn is OK\n";
print "\n";
}
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\n";
$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\n";
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.\n";
$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.\n";
$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.\n";
$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.\n";
$exit = 1;
}
}
# Checking extra_questions.txt -------------------------------
print "Checking $dict/extra_questions.txt ...\n";
if(-s "$dict/extra_questions.txt") {
if(!open(EX, "<$dict/extra_questions.txt")) {$exit = 1; print "--> ERROR: fail to open $dict/extra_questions.txt\n";}
$idx = 1;
$success = 1;
print "--> reading $dict/extra_questions.txt\n";
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, ")\n";
$success = 0;
}
}
$idx ++;
}
close(EX);
$success == 0 || print "--> $dict/extra_questions.txt is OK\n";
} else {print "--> $dict/extra_phones.txt is empty\n";}
if($exit == 1) { print " [Error detected ]\n"; exit 1;}