Number2txt.pl
8.84 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
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
#!/usr/bin/perl -w
#------------------------------------------
# Author : Emmanuel FERREIRA
# Contact: emmanuel.ferreira0194@gmail.com
# Date : 14/02/11
# Brief : in order to convert a raw text containing numbers
# in fulltext (without numbers)
#------------------------------------------
use strict;
use warnings;
use Getopt::Long;
use Pod::Usage;
#-------------------------------------------
# MAIN
# N.B : in this current file mu stands for
# measuring unit
#-------------------------------------------
# options variables
my $help = 0;
my $muRulesFile;
my $number2txtRulesFile;
my $processNbClustering; #65.000 => 65000
my $oovMu;
# usefull variables
my %singularMuSpellingMap;
my %pluralMuSpellingMap;
my %femMu;
my %oovMuMap;
#number to text map
my %num2txtMap;
#rules map
my %rulesMap;
#valid digit separator map
my %sepMap;
#regex in order to catch valid digit separator
my $sepRegex ="";
#config
my $rulesSep = "\t";
my $unitSep = " ";
my $numberSep = "";
my $spotfem ="<e>";
# Parse options and print usage if there is a syntax
# error, or if usage was explicitly requested
GetOptions('help|?' => \$help,
'clustNb' => \$processNbClustering,
'muRules=s' => \$muRulesFile,
'oovMu=s' => \$oovMu,
'rule=s' => \$number2txtRulesFile,
'sep=s' =>\$rulesSep,
'usep=s' =>\$unitSep,
'numbSep=s' =>\$numberSep);
pod2usage(1) if($help);
pod2usage({-msg => "BAD USAGE - mandatory option : rule (--rule <path_to_rule_file>), in order to process the number convertion\n", -exitval => 1, -verbose => 0, -output => \*STDERR}) if(!$number2txtRulesFile);
#Load rules in programming structures
#MU RULES FILE => SINGULAR_MU_MAP & PLURAL_MU_MAP
if($muRulesFile){
my $file;
open($file, $muRulesFile) or die("Cannot open: $muRulesFile");
while(<$file>){
chomp($_);
#non-comment lines
if($_ && $_ !~ /^\s*$/ && $_ !~ /^#/ ){
my @splitLine = split(/$rulesSep/, $_);
#check column number (must be 4 (-1))
if($#splitLine != 3){
#waring bad format
die("Bad format in $muRulesFile : $_");
}
else{
#take into account variant spelling of the unit
my @sameUnitTab = split($unitSep, $splitLine[0]);
#for each spelling store the singular and plural form
for(my $i=0; $i <= $#sameUnitTab; $i++){
my $unit = $sameUnitTab[$i];
$singularMuSpellingMap{$unit}=$splitLine[1];
$pluralMuSpellingMap{$unit}=$splitLine[2];
#store if it is a female form
if($splitLine[3] eq "f"){
$femMu{$unit}=1;
}
}
}
}
}
close($file);
}
initializeMaps($number2txtRulesFile);
#perform the clean task preprocess
while(<STDIN>){
chomp($_);
if($_){
if($processNbClustering){
$_ =~ s/(\b\d{1,3})\s*(([\.\s](\d{3}))+)(\s*(\D|$))/clust($1,$2).$5/eg;
}
if($muRulesFile){
$_ =~ s/(\b\d+([\.,]\d+)?)\s?([^\s\d\.,]+)/goodFormat($1, $3, '')/eg;
}
#text convertion of all the valid digit separator
$_ =~ s/(\d+)($sepRegex)(\d+)/sep2txt($1,$2,$3)/ge;
#find and format 0 sequels
$_ =~ s/(\b)(0+)([1-9]\d*)?(\b)/preformatZeroSequel($1,$2,$3)/ge;
#number convertion
$_ =~ s/(\b)(\d+)(\b)/numberConvertion($1,$2,$3)/ge;
#replace the spot fem if needed
$_ =~ s/([^e]) $spotfem/$1e/g;
#remove them otherwise
$_ =~ s/$spotfem//g;
#remove the mutiple space
$_ =~ s/ +/ /g;
#display
print "$_\n";
}
}
#build the report (if needed)
#if($oovMu){
# my $file;
# open($file, ">$oovMu") or die("Cannot open : $oovMu");
# while( my($k, $v) = each(%oovMuMap)){
# print $file "$k $v\n";
# }
# close($file);
#}
#-------------------------------------------
# SUBROUTINES
#-------------------------------------------
#
# \brief clust process 16.000 => 16000 and 15 000 => 15000
#
sub clust{
my ($a, $b) = @_;
$b =~ s/\s+//g;
$b =~ s/\.//g;
return $a.$b;
}
#
# \breif format a number sequence applying the muRules
#
sub goodFormat{
my ($nb_prec, $current, $nb_succ) = @_;
$nb_succ = '' if(!$nb_succ);
return $nb_prec.writeMu($current, $nb_prec, $nb_succ).$nb_succ;
}
#
# \brief apply the muRules
#
sub writeMu{
my ($current, $nb_prec, $nb_succ) = @_;
my $modif=" ";
#if the key exist
if($singularMuSpellingMap{$current}){
$nb_prec =~ s/,/./g;
$nb_prec =~ s/([^\d\.]*)([\d\.]+$)/$2/g;
if($nb_prec =~ /1$/ && $nb_prec !~ /11$/ && exists $femMu{$current}){
$modif=" $spotfem ";
}
if($nb_prec < -1 || $nb_prec > 1){
return $modif.$pluralMuSpellingMap{$current}." ";
}
else{
return $modif.$singularMuSpellingMap{$current}.$modif;
}
}
else{
#count occurence oov
$oovMuMap{$current}++;
if(!$nb_prec && $nb_prec != 0){
return $current;
} else{
return $modif.$current.$modif;
}
}
}
#
# \brief initialize the different maps use in this script
# rulesMap : map containing the rules
# sepMap : map containing the number valid separator
# number2charMap : map containing the usefull number to text convertion
#
sub initializeMaps
{
my ($ruleFile) = @_;
#open the rule file
open(RULE_FILE, $ruleFile) or die("Cannot open the rule file : $ruleFile");
#reading
while(<RULE_FILE>)
{
chomp($_);
my @columns = split($rulesSep);
# for all the rule containing in the rules file (line starting by rule)
if($columns[0] =~ /rule/){
$rulesMap{$columns[1]} = $columns[2];
}
# for all the separator (line starting by chars_sep)
elsif($columns[0] =~ /chars_sep/){
$sepMap{$columns[1]} = $columns[2];
#construction simultanee de la regex permettant de
#les retrouver
if($sepRegex eq "")
{
$sepRegex .= $columns[1];
}
else
{
$sepRegex .= "|".$columns[1];
}
}
#for all the number value
elsif($columns[0] =~ /exep/){
$num2txtMap{$columns[1]} = $columns[2];
}
else{
#nothing
}
}
close(RULE_FILE);
}
#
# \brief from valide digit separator to text
#
sub sep2txt
{
my ($leftNum, $sep, $rigthNum) = @_;
if($sepMap{$sep}){
$sep = " $sepMap{$sep} ";
}
return "${leftNum}${sep}${rigthNum}";
}
#
# \brief preformat a 0 sequel
#
sub preformatZeroSequel
{
my ($left, $zeroSequel, $rigth) = @_;
$left = "" if(!$left);
$rigth = "" if(!$rigth);
my $sequelTransf = "";
for(my $i = 0; $i < length($zeroSequel); $i++){
$sequelTransf.= "0 ";
}
return "${left}${sequelTransf}${rigth}";
}
#
# \brief number convertion
#
sub numberConvertion
{
my ($boundLeft, $number, $boundRigth) = @_;
$number = processNumberTransf($number);
return "${boundLeft}${number}${boundRigth}";
}
#
# \brief process a number convertion
#
sub processNumberTransf{
my ($number) = @_;
#remove all the useless 0
$number =~ s/^(0+)([1-9])/$2/g;
my $numberTxt = $number;
#if the number correspond to a map key exec the change
if($num2txtMap{$number}){
$numberTxt = $num2txtMap{$number};
}
#if the number isn't a 0 sequel
elsif($number !~ /^0+$/){
my $foundMatch = 0;
# try to find a matching rules
while((my $ruleMatch, my $transf)=each(%rulesMap)){
# test if the current rule match
if($number =~ /^\b$ruleMatch\b$/){
$numberTxt =~ s/$ruleMatch/processNumberTransf($1)/ge;
if($transf){
$numberTxt .= " $transf";
}
return $numberTxt;
}
}
$numberTxt = splitNumber($number);
}
return $numberTxt;
}
#
# \brief process to a number splitting
# ex: 1253 ==> 1000 + 253
#
sub splitNumber
{
my ($number) = @_;
print $number;
my ($subStr1, $subStr2, $subStrConverted1, $subStrConverted2);
my $splittedNbTxt = "";
# find where split
my $splitLength = foundSplitNumberLength($number);
$subStr1 = substr($number, 0, $splitLength-1)."0" x length(substr($number, $splitLength-1));
$subStr2 = substr($number, $splitLength-1);
print $subStr1."\n";
$subStrConverted1 = processNumberTransf($subStr1);
if($subStr2 !~ /^0+$/){
$subStrConverted2 = processNumberTransf($subStr2);
}
else{
$subStrConverted2 = "";
}
$splittedNbTxt = " $subStrConverted1 $numberSep $subStrConverted2 ";
return $splittedNbTxt;
}
#
# \brief find the length of a split
#
sub foundSplitNumberLength
{
my ($number) = @_;
my $splitLength=length($number);
my $nbLength = length($number);
if($nbLength%3){
if($nbLength < 3){
return $nbLength%3; #2(3)
}
else{
$splitLength = $nbLength%3 + 1; #11(054)
}
}
else{
if($nbLength > 3){
return 4; #100(004)
}
else{
return 2; #1(02)
}
}
return $splitLength;
}
__END__
=head1 NAME
PreprocessCorpus.pl - preprocess a raw corpus
=head1 SYNOPSIS
PreprocessCorpus.pl [options]
Options:
-help|? brief help message
-rule (i) file containing rules to convert number to text (mandatory)
Format :
#tag #regex #correct
Ex :
exep 1 un
rule \d)00 cents
chars_sep , virgule
-muRules (i) file containing measuring unit rules
Format :
#symbol #singular #plural
Ex:
h heure heures
-clustNb perform a clustering task on number
Ex: 65.000 => 65000
-oovMu (o) ouput of the measuring unit oov report (default : no report produced)
-sep (i) change se sperator a the rule file (default \t)
-numberSep (i) introduce a char sequence between two text number units (ex: with "et" : 12 => dix et deux)