Blame view

egs/fisher_callhome_spanish/s5/local/spron.pl 6.78 KB
8dcb6dfcb   Yannick Estève   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
  #!/usr/bin/env perl
  
  # Oct 21, 2015 : Gaurav Kumar (Johns Hopkins University)
  # GNU General Public License, v3.0
  #
  # This script was modified under GPL and is being distributed with 
  # Kaldi. It requires the preference and rule files
  # (under LDC copyright) from LDC96L16. The main changes were
  # - Outdated usage of perl conventions updated @_ => $_ or @A
  # - This script no longer needs the preference and rule files to 
  #   be in the same directory as this script.
  # - Accepts tokens from <STDIN> instead of <>
  
  # --- Retained previous version information ----------------------------
  # spron.pl Version 0.1 Jan. 11 1995 
  # Written by Zhibiao Wu, LDC, wzb@unagi.cis.upenn.edu
  # This program needs the basic_rules file to run. The rules must be sorted 
  # in alphabetical order. The most specific rules should precede the more 
  # general ones. The conventions used in the basic rules are the same as 
  # regular expressions used in Perl.
  
  # Revised history: Feb. 10 1995
  
  # The file "preferences" (assumed to be in your current directory)
  # gives an "oracle" of correct pronunciations that override the
  # machine-generated ones.
  
  # slightly changed 97/09/05 robertm:
  #  - look for basic_rules and preferences in $PWD instead of ~wzb/...
  #  - use next to shortcut loop instead of if/else
  #  - added a bit of documentation, without really trying to decipher this thing
  # -----------------------------------------------------------------------
  
  use utf8;
  binmode(STDIN, ":utf8");
  binmode(STDOUT, ":utf8");
  
  $vfile = "";
  $preference_file = "";
  $rules_file = "";
  $print_input = 0;
  if ($#ARGV < 1) {
    # Print Usage
    print "Usage : local/spron.pl pref-file rules-file <v-file> <print-input>
  ";
    exit 1;
  } else {
    $preference_file = $ARGV[0];
    $rules_file = $ARGV[1];
    if ($#ARGV > 1) {
      $vfile = $ARGV[2];
    }
    if ($#ARGV > 2) {
      $print_input = 1;
    }
  }
  
  $rule_num = 0;
  $previous = "";
  if ($vfile ne "") {
    open(VF, $vfile) || die "Can't find file $vfile!
  ";
    while (<VF>) {
      chop;
      @A = split(//);
      if (($A[0] ne '#') && ($_ ne "")) {
        if (/(\S+)\s*->\s*(\S*)\s*:\s*(\S*)\s*__\s*(\S*)\s*(#?)/) {
          $head[$rule_num] = $1;
          $end[$rule_num] = $2;
          $pre[$rule_num] = $3;
          if ($4 =~ /#/) {
            $nex[$rule_num] = "";
            $some[$rule_num] = $4;
          } else {
            $nex[$rule_num] = $4;
            $some[$rule_num] = $5;
          }
          if ($previous ne substr($head[$rule_num],0,1)) {
            $first{$head[$rule_num]} = $rule_num;
            $last{$previous} = $rule_num - 1;
          }
          $previous = substr($head[$rule_num++],0,1);
        } else {
          print "Rule format error: Cannot parse $_
  ";
          exit(1);
        }
      }
    }
    $last{$previous} = $rule_num - 1;
  
    close(VF);
  }
  
  open(PF, $preference_file) || die "Can't read `preferences' file";
  binmode(PF, ":iso88591");
  while (<PF>) {
    chop;
    if ($_ ne "") {
      @A = split;
      $pron{$A[0]} = $A[1];
      $stre{$A[0]} = $A[2];
    }
  }
  
  $previous = "";
  $brule_num = 0;
  open(BF, $rules_file) || die "Can't read `basic_rules' file";
  binmode(BF, ":iso88591");
  while (<BF>) {
    chop;
    @A = split(//);
    if (($A[0] ne '#') && ($_ ne "")) {
      if (/(\S+)\s*->\s*(\S*)\s*:\s*(\S*)\s*__\s*(\S*)\s*(#?)/) {
        $bhead[$brule_num] = $1;
        $bend[$brule_num] = $2;
        $bpre[$brule_num] = $3;
        if ($4 =~ /#/) {
          $bnex[$brule_num] = "";
          $bsome[$brule_num] = $4;
        } else {
          $bnex[$brule_num] = $4;
          $bsome[$brule_num] = $5;
        }
        if ($previous ne substr($bhead[$brule_num],0,1)) {
          $bfirst{substr($bhead[$brule_num],0,1)} = $brule_num;
          $blast{$previous} = $brule_num - 1;
        }
        $previous = substr($bhead[$brule_num++],0,1);
      } else {
        print "Rule format error in file basic_rules: Cannot parse $_
  ";
        exit(1);
      }
    }
  }
  $blast{$previous} = $brule_num - 1;
  close(BF);
  
  if ($brule_num == 0) {
    print "No basic rules, Program exit!
  ";
    exit(1);
  }
  
  while(<STDIN>){
    next if ((/^#/) || (/^\s*$/) );
    chop;
    if ($print_input) {
      print $_, "\t";
    }
    if ($pron{$_}) {
      # print answer from preferences and skip to next word
      print "$pron{$_}\t$stre{$_}
  ";
      next;
    }
    $original = $_;
    tr/A-ZÁÉÍÓÚÏÜÑ/a-záéíóúïüñ/;
    $orig = "#" . $_ . "#";
  
    @l = ();
  
    push(@l,split("",$orig));
  
    @pron = &transfer(1);
  
    foreach (@pron) {
      $a = $_;
      y/aeiouáéíóú//cd;
      if ($_ eq "") {
        print "#No stressable vowel in $original
  ";
      } else {
        s/[aeiou]/0/go;
        s/[áéíóú]/1/go;
        if (!/1/) {
          if(length() == 1){
            s/\b./1/o;
          } elsif($l[$#l - 1] =~ /[aeiouns]/o){
            s/00\b/10/o;
          } else {
            s/0\b/1/o;
          }
        }
  
        $a =~ s/á/a/g;
        $a =~ s/é/e/g;
        $a =~ s/í/i/g;
        $a =~ s/ó/o/g;
        $a =~ s/ú/u/g;
  
        print "$a\t$_
  ";
      }
    }
  }
  
  sub transfer{
    local($_) = @_;
    local(@p) = ();
    local($s) = 0;
    local($over) = 0;
    local($i,$j,$k) = (0,0,0);
  
    if ($_ >= length($orig) - 1) {
      push(@p, "");
      return(@p);
    } else {
  
      if ($vfile ne "") {
        for ($i=   $first{substr($orig, $_, 1)}; 
          $i <= $last{substr($orig, $_, 1)} ; $i++) {
          if (&matchv($_,$i)) {
            $s = $_ + length($head[$i]);
            foreach $w (&transfer($s)) {
              push(@p, $end[$i] . $w);
              if ($some[$i] ne "") {
                $over = 0;
              } else {
                $over = 1;
              }
            }
          }
        }
      }
  
      if ($over == 0 ) {
        $i = $bfirst{substr($orig, $_, 1)}; 
        while (($i <= $blast{substr($orig, $_, 1)}) && ($over == 0)) {
          if (&matchb($_,$i)) {
            $over = 1;
            $s = $_ + length($bhead[$i]);
            foreach $w (&transfer($s)) {
              push(@p, $bend[$i] . $w);
            }
          }
          $i++;
        }
        if ($over == 0) {
          $s = $_ + 1;
          foreach $w (&transfer($s)) {
            push(@p, substr($orig,$_,1) . $w);
          }
        } 
      }
  
      return(@p);
    }
  }
  
  sub matchv {
    $h = $head[$_[1]];
    $p = $pre[$_[1]];
    $n = $nex[$_[1]];
  
    return(&match($_[0],$h,$p,$n));
  
  }
  
  sub matchb {
    $h = $bhead[$_[1]];
    $p = $bpre[$_[1]];
    $n = $bnex[$_[1]];
  
    return(&match($_[0],$h,$p,$n));
  
  }
  
  sub match {
  
    if (substr($orig, $_[0], length($_[1])) eq $_[1]) {
      return ( &match_n($_[0] + length($_[1]) - 1, $_[3]) && 
        &match_p($_[0], $_[2])); 
    } else {
      return (0);
    }
  }
  
  sub match_p {
    local($a) = $_[0];
    local($b) = $_[1];
    local($_);
  
    if ($b eq "" ) {
      return (1);
    } else {
      $_ = substr($orig, 0, $a) . "!";  
      if (/($b)!/) {
        return(1);
      } else {
        return(0);
      }
    }
  }
  
  sub match_n {
    local($a) = $_[0];
    local($b) = $_[1];
    local($_);
  
    if ($b eq "" ) {
      return (1);
    } else {
      $_ = "!" . substr($orig, $a + 1, length($orig) - $a - 1);  
      if (/!($b)/) {
        return(1);
      } else {
        return(0);
      }
    }
  }