Blame view

LIA_kaldiUtils/normalize_fr.pl 3.68 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
  #!/usr/bin/perl -w
  
  use FileHandle;
  
  use strict;
  use utf8;
  binmode STDIN, ":utf8";
  binmode STDOUT, ":utf8";
  binmode STDERR, ":utf8";
  
  sub numtolitteral {
    my @num = (); # split input string into an array of digits
    my $sol = undef;
    my $len = undef;
  
    my $cent = undef;
    my $mille = undef;
    my $million = undef;
    
    my $str = shift;
    $str =~ s/^0*//;
    @num = reverse split(//, $str);
    $len = scalar @num;
  
    return "zéro" if not $len;
  
    $cent = join("", reverse splice @num, 0, 3);
    $mille = join("", reverse splice @num, 0, 3);
    $million = join("", reverse splice @num, 0, 3);
  
    if ($len <= 3) {
      $sol = dtoa("zéro", $cent, 1);
    }
    elsif ($len >= 4 and $len <= 6)  {
      $sol = "";
      $sol = dtoa("", $mille, 0) unless $len == 4 and $mille == 1;
      $sol = join(" ", $sol, "mille", dtoa("", $cent, 1));
    }
    else {
      $sol = join(" ", dtoa("", $million, 1), "million");
      $sol = $sol . "s" if $million > 1;
      my $tmp = "";
      $tmp = dtoa("", $mille, 0) unless $mille == 1;
      $sol = join(" ", $sol, $tmp, "mille") unless $mille == 0;
      $sol = join(" ", $sol, dtoa("", $cent, 1));
    }
    $sol =~ s/\s+/ /g;
    return $sol;
  }
  
  # --------------------- #
  # ---- sub dtoa() ----- #
  # --------------------- #
  sub dtoa() {
    my $zero = shift;
    my @tab = split(//, shift);
    my $final = shift;
    my $sol = "";
  
    my @digit = qw(zéro un deux trois quatre cinq six sept huit neuf);
    my @digit1x = qw/dix onze douze treize quatorze quinze seize dix-sept dix-huit dix-neuf/;
    my @digitx0 = ("", "dix", "vingt", "trente", "quarante", "cinquante", "soixante", "soixante-dix", "quatre-vingt", "quatre-vingt-dix");
  
    # remove heading zeros...
    shift @tab while $tab[0] == 0 and $#tab > 0;
    my $len = scalar @tab;
  
    if ($len == 1) { # [0-9]
      $sol = $digit[$tab[0]];
      $sol = $zero if $tab[0] == 0;
    }
    else {
      $sol = "";
      if ($len == 3) {
        my $c = shift @tab;
        $sol = $digit[$c] unless $c == 1;
        $sol = join(" ", $sol, "cent");
        $sol = $sol . "s" if $c > 1 and $tab[0] == 0 and $tab[1] == 0 and $final;
      }
  
      # convert 2 digits @tab to string
      my $d2 = "";
      if ($tab[0] == 0) {
        $d2 = $digit[$tab[1]] if $tab[1];
      }
      elsif ($tab[0] == 1) {
        $d2 = $digit1x[$tab[1]];
      }
      else {
        if ($tab[1] == 0) {
  	$d2 = $digitx0[$tab[0]];
        }
        else {
  	if ($tab[0] == 7 or $tab[0] == 9) {
  	  $d2 = join("-", $digitx0[$tab[0] - 1], $digit1x[$tab[1]]);
  	  $d2 = join("-", $digitx0[$tab[0]-1], "et", $digit1x[$tab[1]]) if $tab[0] == 7 and $tab[1] == 1;
  	}
  	else {
  	  $d2 = $digitx0[$tab[0]];
  	  $d2 = $d2 . "-et" if $tab[1] == 1 and $tab[0] != 8;
  	  $d2 = join("-", $d2, $digit[$tab[1]]);
  	}
        }
      }
  
      $sol = join(" ", $sol, $d2);
    }
  
    return $sol;
  }
  
  
  while(<>){
    s/\(([^)]*)\)/"<<<X".unpack("H*", $1)."X>>>"/ge;
  
    s/<pers=[^>]*>/ /g;
    s/<\/pers>/ /g;
  
    # remove tildes, points, double quotes, commas, underscores, double-points, non-diacritic circumflex,
    # stars, slashes, backslashes, interrogation points, exclamation points, semicolons
    s/[\~\.\"\,\_\:\^\*\\\/\?\!\;\#\¿\¡]+//g;
  
    # &blah -> (%hesitation)
    s/\B\&\S+/(%hesitation)/g;
  
    # Drop lines with '&' remaining
    s/^.*\B\&\B.*$/{}/;
  
    # %% -> (%hesitation)
    s/\%\%/(%hesitation)/g;
  
    s/([^(])%/$1 pourcent/g;
  
    s/([0-9])-([0-9])/$1 $2/g;
  
    s/'/' /g;
    s/aujourd' hui/aujourd'hui/g;
    s/Aujourd' hui/Aujourd'hui/g;
    s/\b([0-9]+)\b/numtolitteral($1)/ge;
  
    s/-/ /g;
    s/(<<<X([0-9a-f]+)X>>>)/"(".pack("H*", $2).")"/ge;
    s/<<<XX>>>/()/g;
  
    s/\(c'est\) à/à/g;
    s/\(c'est-\)à/à/g;
    s/\(c'est-à-\)dire/dire/g;
  #  s/ \(endroit\)/ /g;
  #  s/ \(est\)/ /g;
  #  s/ \(où\)/ /g;
  #  s/ \(que\)/  /g;
    s/deux\(011\)/deux/g;
    s/c\('est\)/c'/g;
    s/(\s)\([^\s]+\)(\s)/$1$2/g;
  
    print;
    STDOUT->flush;
  }