Blame view
tools/scripts/CoverageReportMaker.pl
5.09 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 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 |
#!/usr/bin/perl -w #------------------------------------------ # Author : Emmanuel FERREIRA # Contact: emmanuel.ferreira0194@gmail.com # Date : 14/02/11 # Brief : in order to postprocess a clean corpus #------------------------------------------ use strict; use warnings; use Getopt::Long; use Pod::Usage; use File::Path; #------------------------------------------- # MAIN #------------------------------------------- # options variables my $help = 0; my $lexicon; my $verbose; my $filename = "report"; # usefull variables my %lexiconMap; my %oovMap; my $lexMatch = 0; my $oovMatch = 0; my $zeroton = 0; my $dirname = "./tmp_report"; my $write; my $sep="\t"; # Parse options and print usage if there is a syntax # error, or if usage was explicitly requested GetOptions('help|?' => \$help, 'verbose' => \$verbose, 'lex=s' => \$lexicon, 'out=s' => \$dirname, 'name=s' => \$filename, 'writeAll' => \$write); pod2usage(1) if($help); pod2usage({-msg => "BAD USAGE - you must specify a lexicon in argument ", -exitval => 1, -verbose => 0, -output => \*STDERR}) if(!$ARGV[0]); $lexicon = $ARGV[0]; #******************************************** # MAIN #******************************************** print "CREATE DIR... " if($verbose); mkdir $dirname, 0755; print "LOAD LEXICON... " if($verbose); # -------------------------------- # Load the lexicon into a programming structure # -------------------------------- my $file; open($file, $lexicon) or die("Cannot open : $lexicon"); while(<$file>){ chomp($_); #save the key $lexiconMap{$_}=0; $zeroton++; } close($file); # ------------------------------------- # postprocess : remove <s> and </s> tags # and register all the words # ------------------------------------- print "WORDS ANALYZE... " if($verbose); while(<STDIN>){ my $line = $_; my $sentence = $line; $line =~ s/<s>|<\/s>//g; wordRegister("<s>", $sentence); wordRegister("</s>", $sentence); $line =~ s/ ([\w_']+) /wordRegister($1,$sentence)/ge; } # ------------------------------------ # produce two reports : # * in-vocabulary words with counts # * out-of-vocabulary words counts # ------------------------------------ print "PRODUCE REPORTS... " if($verbose); print "\t* LEXICON MATCH : $lexMatch " if($verbose); print "\t* ZEROTON MATCH : $zeroton " if($verbose); print "\t* OOV : $oovMatch " if($verbose); # save in-vocabulary words sorted by occurences and alphabetic order # NB: this file is formatted as follow : <WORD> <NB_OCC> my @tmpTab = (); while(my ($word, $occ) = each %lexiconMap){ my @tmp = ($word, $occ); push(@tmpTab, \@tmp); } @tmpTab = sort { ${$b}[1] <=> ${$a}[1] || ${$a}[0] cmp ${$b}[0] } @tmpTab; open($file, ">$dirname/$filename.invoc"); for(my $i = 0; $i <= $#tmpTab; $i++){ my $word = ${$tmpTab[$i]}[0]; my $occ = ${$tmpTab[$i]}[1]; print $file "${word}$sep${occ} "; } close($file); # save out-of-vocabulary words by occurences and alphabetic order # NB: this file is formatted as follow : <ID> <WORD> <NB_OCC> <EXAMPLE_OF_CONTEXT> open($file, ">$dirname/$filename.oov"); my @oovs = values %oovMap; @oovs = sort { ${$b}[0] <=> ${$a}[0] || ${$a}[3] cmp ${$b}[3] } @oovs; for(my $i = 0; $i <= $#oovs; $i++){ my @tabOOV = @{$oovs[$i]}; my $word = $tabOOV[3]; if($word !~ /<\/?s>/){ my $id = $tabOOV[1]; my $occ = $tabOOV[0]; my $exp = $tabOOV[2]; chomp $exp; $exp =~ s/ ($word) / <$1> /g; print $file "${id}$sep${word}$sep${occ}$sep\[${exp}\] "; } } close($file); #******************************************** # SUB-ROUTINES #******************************************** sub wordRegister { my ($word, $sentence) = @_; if(defined $lexiconMap{$word}){ if($lexiconMap{$word} == 0){ $zeroton--; $lexMatch++; } $lexiconMap{$word}++; } else{ if($oovMap{$word}){ ${$oovMap{$word}}[0]++; } else{ $oovMatch++; my @tab = (1, $oovMatch, $sentence, $word); $oovMap{$word}=\@tab; if($write){ createOOVFile($oovMatch, $word); } } if($write){ writeContext(${$oovMap{$word}}[1], $sentence); } } return " $word "; } sub createOOVFile { my ($idOOV, $oov) = @_; my $file; open($file, ">$dirname/report_$idOOV"); print $file "OOV=$oov "; close($file); } sub writeContext { my ($idOOV, $sentence) = @_; my $file; open($file, ">>$dirname/report_$idOOV"); print $file "$sentence "; close($file); } #------------------------------------------- # SUBROUTINES #------------------------------------------- __END__ =head1 NAME CoverageReportMaker.pl - produce a corpus coverage reports with a lexicon =head1 SYNOPSIS cat <corpus_cleaned> | CoverageReportMaker.pl [options] <lexicon> NOTICE: produce two reports : - <basename_report>.oov : containing the oov list sorted by occurrences number and alphabetic order - <basename_report>.invoc : containing the in-vocabulary list sort by occurrences and alphabetic order (also display Zeroton) Options: -help|? brief help message -verbose verbose -out output directory (default ./tmp_report) -name reports basename used (default report) -writeAll build a file in the output directory for each oov word listing its sentences (file name : report_<oov_id>) |