Blame view
tools/scripts/FindNormRules.pl
4.18 KB
e6be5137b reinitialized pro... |
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 |
#!/usr/bin/perl -w #------------------------------------------ # Author : Emmanuel FERREIRA # Contact: emmanuel.ferreira0194@gmail.com # Date : 08/02/12 # Brief : # Analyze a list of coverage reports and process some text transformation # in order to check usual words spelling mistakes # create both a NOEMALIZATION_RULES_FILE containing transformation table in order # to correct guessed mistakes found by this process and a OOV_LST_FILE # containing the guessed real OOV words #------------------------------------------ use strict; use Getopt::Long; use Pod::Usage; use Switch; #------------------------------------------- # MAIN #------------------------------------------- # options variables my $help = 0; my $vocab; my $penalty = 5; my $oovReport; # usefull variables my %vocabMap; my $sep="\t"; # Parse options and print usage if there is a syntax # error, or if usage as explicitly requested GetOptions('help|?' => \$help, 'penalty=s' => \$penalty); pod2usage(1) if($help); pod2usage({-msg =>" ERROR : you must specify an oov report and a lexicon ", -exitval => 1, -verbose => 0, -output => \*STDERR}) if($#ARGV != 1); $oovReport = $ARGV[0]; $vocab = $ARGV[1]; #******************************* # MAIN #******************************* #-------------------------- # Load the lexicon in a map #------------------------- if($vocab){ my $file; open($file, $vocab) or die("Cannot open : $vocab"); while(<$file>){ chomp($_); my $tmp = doTransformation($_); if(!$vocabMap{$tmp}){ $vocabMap{$tmp}=(); } push(@{$vocabMap{$tmp}}, $_); } close($file); } #----------------------- # Load the OOV report and display the result formatted as follows : # * normalization rules : r <OCC> <WORD>#<CORREC>#<SCORE> <ID> <EXP> # * oov : r <OCC> <WORD>#<CORREC>#<SCORE> <ID> <EXP> #----------------------- my $file; open($file, "$oovReport"); while(<$file>) { chomp($_); my @splittedLine = split(/$sep/, $_); my $word = $splittedLine[1]; my $id = $splittedLine[0]; my $occ = $splittedLine[2]; my $exp = $splittedLine[3]; my $transf = doTransformation($splittedLine[1]); if($vocabMap{$transf}){ my @bestWord = findBestTransf($word, @{$vocabMap{$transf}}); # on n'afficage pas de score inf a 0 my $score = $bestWord[1]; if($score < 0){ $score = 0; } print "r$sep${occ}$sep${word}#$bestWord[0]#$bestWord[1]$sep${id}$sep${exp} "; } else{ print "o$sep${occ}$sep${word}$sep${id}$sep${exp} "; } } close($file); #------------------------------------------- # SUBROUTINES #------------------------------------------- sub doTransformation { my ($word) = @_; $word = lc($word); $word =~ s/\d//g; $word =~ s/'//g; return $word; } sub findBestTransf { my ($word, @vocabTab) = @_; my $Best; my $BestScore; my $i; my $score; for( $i = 0; $i <= $#vocabTab; $i++){ if(!$Best){ $Best = $vocabTab[$i]; $BestScore = calculateScore($word, $Best); } else{ $score = calculateScore($word, $vocabTab[$i]); if($score < $BestScore){ $Best = $vocabTab[$i]; $BestScore = $score; } } } return ($Best, $BestScore); } sub calculateScore { my ($word, $cmpword) = @_; my $score = 0; if(length($word) > length($cmpword)){ $score += $penalty + levenshtein($word, $cmpword); } if ($cmpword =~ /^[A-Z]/ && $cmpword !~ /^[A-Z]+$/ && $word =~ /^[A-Z]/){ $score -= 1; } # if($cmpword =~ /^[A-Z]+$/ && $word !~ /^[A-Z]+$/){ # $score += $penalty; # } $score += levenshtein($word, $cmpword); return $score; } #--------------------------------------------------- # LEVENSHTEIN DISTANCE ALGORITHM # Initial version : Jorge Mas Trullenque #--------------------------------------------------- sub levenshtein($$){ my @A=split //, lc shift; my @B=split //, lc shift; my @W=(0..@B); my ($i, $j, $cur, $next); for $i (0..$#A){ $cur=$i+1; for $j (0..$#B){ $next=min( $W[$j+1]+1, $cur+1, ($A[$i] ne $B[$j])+$W[$j] ); $W[$j]=$cur; $cur=$next; } $W[@B]=$next; } return $next; } sub min($$$){ if ($_[0] < $_[2]){ pop @_; } else { shift @_; } return $_[0] < $_[1]? $_[0]:$_[1]; } __END__ =head1 NAME FindNormRules.pl - from oov produce invoc correction proposition =head1 SYNOPSIS FindNormRules.pl [options] <report_oov> <lexicon> |