| 1 | #!/usr/local/bin/perl -w | ||
| 2 | |||
| 3 | =head1 NAME | ||
| 4 | |||
| 5 | balance.pl - Create a balanced Senseval-2 data file that has the same | ||
| 6 | number of instances for each possible sense. | ||
| 7 | |||
| 8 | =head1 SYNOPSIS | ||
| 9 | |||
| 10 | balance.pl [OPTIONS] DATA N > Balanced-DATA | ||
| 11 | |||
| 12 | This is the original distribution of senses in a Senseval-2 data file: | ||
| 13 | |||
| 14 | frequency.pl begin-v.test.xml | ||
| 15 | |||
| 16 | Output => | ||
| 17 | |||
| 18 | <sense id="begin%2:30:00::" percent="64.31"/> | ||
| 19 | <sense id="begin%2:30:01::" percent="14.51"/> | ||
| 20 | <sense id="begin%2:42:04::" percent="21.18"/> | ||
| 21 | Total Instances = 255 | ||
| 22 | Total Distinct Senses=3 | ||
| 23 | Distribution={64.31,21.18,14.51} | ||
| 24 | % of Majority Sense = 64.31 | ||
| 25 | |||
| 26 | Here they are balanced with 20 instances per sense. | ||
| 27 | |||
| 28 | balance.pl begin.v-test.xml 20 > bal-output | ||
| 29 | |||
| 30 | frequency.pl bal-output | ||
| 31 | |||
| 32 | Output => | ||
| 33 | |||
| 34 | <sense id="begin%2:30:00::" percent="33.33"/> | ||
| 35 | <sense id="begin%2:30:01::" percent="33.33"/> | ||
| 36 | <sense id="begin%2:42:04::" percent="33.33"/> | ||
| 37 | Total Instances = 60 | ||
| 38 | Total Distinct Senses=3 | ||
| 39 | Distribution={33.33,33.33,33.33} | ||
| 40 | % of Majority Sense = 33.33 | ||
| 41 | |||
| 42 | Here they are balanced with 50 instances per sense. Please note that any | ||
| 43 | sense with less than 50 instances is removed from the data. | ||
| 44 | |||
| 45 | balance.pl begin.v-test.xml 50 > bal-output | ||
| 46 | |||
| 47 | frequency.pl bal-output | ||
| 48 | |||
| 49 | Output => | ||
| 50 | |||
| 51 | <sense id="begin%2:30:00::" percent="50.00"/> | ||
| 52 | <sense id="begin%2:42:04::" percent="50.00"/> | ||
| 53 | Total Instances = 100 | ||
| 54 | Total Distinct Senses=2 | ||
| 55 | Distribution={50.00,50.00} | ||
| 56 | % of Majority Sense = 50.00 | ||
| 57 | |||
| 58 | You can find begin-v.test.xml in samples/Data | ||
| 59 | |||
| 60 | Type C<balance.pl --help> for a quick summary of options | ||
| 61 | |||
| 62 | =head1 DESCRIPTION | ||
| 63 | |||
| 64 | This program will choose exactly the same number of instances for each | ||
| 65 | sense found in a given Senseval-2 file. Unless a value is specified, it | ||
| 66 | will choose the number of instances present in the least frequent sense. | ||
| 67 | Output is to STDOUT, so the original input data is unchanged. | ||
| 68 | |||
| 69 | =head1 INPUT | ||
| 70 | |||
| 71 | =head2 Required Arguments: | ||
| 72 | |||
| 73 | =head4 DATA | ||
| 74 | |||
| 75 | balance.pl accepts a Senseval-2 data file. | ||
| 76 | |||
| 77 | =head4 N | ||
| 78 | |||
| 79 | Specifies the number of instances to be selected from each sense. | ||
| 80 | |||
| 81 | =head2 Optional Arguments: | ||
| 82 | |||
| 83 | --count COUNT | ||
| 84 | |||
| 85 | Balances the COUNT file created by SenseTool's L<preprocess.pl> along | ||
| 86 | with the DATA file. COUNT file is balanced such that it stays consistent | ||
| 87 | with the new balanced DATA file and contains only those instances left | ||
| 88 | after balancing, in the same order as they appear in the output. | ||
| 89 | |||
| 90 | Balanced COUNT is written to file COUNT.balanced and every ith line in | ||
| 91 | COUNT.balanced is instance data within <context> and </context> tags for the | ||
| 92 | ith instance in the output of balance. | ||
| 93 | |||
| 94 | =head3 Other Options : | ||
| 95 | |||
| 96 | =head4 --help | ||
| 97 | |||
| 98 | Displays this message. | ||
| 99 | |||
| 100 | =head4 --version | ||
| 101 | |||
| 102 | Displays the version information. | ||
| 103 | |||
| 104 | =head1 OUTPUT | ||
| 105 | |||
| 106 | Output is a sense balanced Senseval-2 file and is displayed to stdout. Output | ||
| 107 | will show exactly N instances of each sense that has at least N | ||
| 108 | instances. All senses in the output Senseval-2 will have equal number of | ||
| 109 | instances meaning the senses will be equally distributed. | ||
| 110 | |||
| 111 | =head1 BUGS | ||
| 112 | |||
| 113 | The output of balance.pl will have un-balanced distribution of senses | ||
| 114 | when some of the instances have multiple sense tags in the given DATA | ||
| 115 | file. | ||
| 116 | |||
| 117 | =head1 AUTHORS | ||
| 118 | |||
| 119 | Ted Pedersen, University of Minnesota, Duluth | ||
| 120 | tpederse at d.umn.edu | ||
| 121 | |||
| 122 | Amruta Purandare, University of Pittsburgh | ||
| 123 | |||
| 124 | =head1 COPYRIGHT | ||
| 125 | |||
| 126 | Copyright (c) 2002-2008, Amruta Purandare and Ted Pedersen | ||
| 127 | |||
| 128 | This program is free software; you can redistribute it and/or modify it under | ||
| 129 | the terms of the GNU General Public License as published by the Free Software | ||
| 130 | Foundation; either version 2 of the License, or (at your option) any later | ||
| 131 | version. | ||
| 132 | |||
| 133 | This program is distributed in the hope that it will be useful, but WITHOUT | ||
| 134 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS | ||
| 135 | FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. | ||
| 136 | |||
| 137 | You should have received a copy of the GNU General Public License along with | ||
| 138 | this program; if not, write to | ||
| 139 | |||
| 140 | The Free Software Foundation, Inc., | ||
| 141 | 59 Temple Place - Suite 330, | ||
| 142 | Boston, MA 02111-1307, USA. | ||
| 143 | |||
| 144 | =cut | ||
| 145 | |||
| 146 | ############################################################################### | ||
| 147 | |||
| 148 | # ==================== | ||
| 149 | # THE CODE STARTS HERE | ||
| 150 | # ==================== | ||
| 151 | |||
| 152 | ############################################################################### | ||
| 153 | |||
| 154 | # ============================== | ||
| 155 | # COMMAND LINE OPTIONS AND USAGE | ||
| 156 | # ============================== | ||
| 157 | |||
| 158 | #command line options | ||
| 159 | use Getopt::Long; | ||
| 160 | GetOptions ("help","version","count=s"); | ||
| 161 | |||
| 162 | #show help message | ||
| 163 | if(defined $opt_help) | ||
| 164 | { | ||
| 165 | $opt_help=1; | ||
| 166 | &showhelp(); | ||
| 167 | exit; | ||
| 168 | } | ||
| 169 | |||
| 170 | #show version information | ||
| 171 | if(defined $opt_version) | ||
| 172 | { | ||
| 173 | $opt_version=1; | ||
| 174 | &showversion(); | ||
| 175 | exit; | ||
| 176 | } | ||
| 177 | |||
| 178 | #show minimal usage note | ||
| 179 | if($#ARGV<1) | ||
| 180 | { | ||
| 181 | &minimal(); | ||
| 182 | exit; | ||
| 183 | } | ||
| 184 | |||
| 185 | #if --count file is provided | ||
| 186 | if(defined $opt_count) | ||
| 187 | { | ||
| 188 | $countfile=$opt_count; | ||
| 189 | } | ||
| 190 | ############################################################################## | ||
| 191 | |||
| 192 | # ================================ | ||
| 193 | # INITIALIZATION AND INPUT | ||
| 194 | # ================================ | ||
| 195 | |||
| 196 | #$0 contains the program name along with | ||
| 197 | #the complete path. Extract just the program | ||
| 198 | #name and use in error messages | ||
| 199 | $0=~s/.*\/(.+)/$1/; | ||
| 200 | |||
| 201 | #getting the source file name | ||
| 202 | if(!defined $ARGV[0]) | ||
| 203 | { | ||
| 204 | print STDERR "ERROR($0): | ||
| 205 | Please specify Senseval-2 formatted Data file to be balanced.\n"; | ||
| 206 | exit; | ||
| 207 | } | ||
| 208 | $infile=$ARGV[0]; | ||
| 209 | if(!(-e $infile)) | ||
| 210 | { | ||
| 211 | print STDERR "ERROR($0): | ||
| 212 | Source file $infile doesn't exist.\n"; | ||
| 213 | exit; | ||
| 214 | } | ||
| 215 | open(IN,$infile) || die "ERROR($0): | ||
| 216 | Error(code=$!) in opening file $infile.\n"; | ||
| 217 | |||
| 218 | #getting no of instances to be selected from each sense | ||
| 219 | if(!defined $ARGV[1]) | ||
| 220 | { | ||
| 221 | print STDERR "ERROR($0): | ||
| 222 | Please specify the Number of instances to be selected from each sense.\n"; | ||
| 223 | exit; | ||
| 224 | } | ||
| 225 | $number=$ARGV[1]; | ||
| 226 | |||
| 227 | # -------------------------- | ||
| 228 | # if count file is provided | ||
| 229 | # -------------------------- | ||
| 230 | if(defined $countfile) | ||
| 231 | { | ||
| 232 | if(!-e $countfile) | ||
| 233 | { | ||
| 234 | print STDERR "ERROR($0): | ||
| 235 | Count file <$countfile> doesn't exist.\n"; | ||
| 236 | exit; | ||
| 237 | } | ||
| 238 | open(COUNT,$countfile) || die "Error($0): | ||
| 239 | Error(code=$!) in opening <$countfile> file.\n"; | ||
| 240 | |||
| 241 | #----------------------------- | ||
| 242 | # Creating out file for count | ||
| 243 | #----------------------------- | ||
| 244 | $count_outfile=$countfile.".balanced"; | ||
| 245 | $ans="N"; | ||
| 246 | if(-e $count_outfile) | ||
| 247 | { | ||
| 248 | print STDERR "Warning($0): | ||
| 249 | Balanced file <$count_outfile> for count file <$countfile> already exists, | ||
| 250 | overwrite (y/n)? "; | ||
| 251 | $ans=<STDIN>; | ||
| 252 | } | ||
| 253 | if(!-e $count_outfile || $ans=~/Y|y/) | ||
| 254 | { | ||
| 255 | open(COUNT_OUT,">$count_outfile") || die "Error($0): | ||
| 256 | Error(code=$!) in opening balanced count file <$count_outfile>.\n"; | ||
| 257 | } | ||
| 258 | else | ||
| 259 | { | ||
| 260 | undef $countfile; | ||
| 261 | } | ||
| 262 | } | ||
| 263 | ############################################################################## | ||
| 264 | |||
| 265 | # ==================== | ||
| 266 | # Actual Balancing | ||
| 267 | # ==================== | ||
| 268 | |||
| 269 | # if sense tagged, get senses from data file | ||
| 270 | while(<IN>) | ||
| 271 | { | ||
| 272 | push @text,$_; | ||
| 273 | if(/instance id=\"([^\"]+)\"/) | ||
| 274 | { | ||
| 275 | $instance=$1; | ||
| 276 | } | ||
| 277 | if(/sense\s*id=\"([^\"]+)\"/) | ||
| 278 | { | ||
| 279 | # storing instances per sense | ||
| 280 | push @{$instances{$1}},$instance; | ||
| 281 | } | ||
| 282 | } | ||
| 283 | |||
| 284 | # selecting N instances of each sense | ||
| 285 | foreach $sense (keys %instances) | ||
| 286 | { | ||
| 287 | # shuffle and select only the instances of that sense which have | ||
| 288 | # number of instances > specified number N | ||
| 289 | if($#{$instances{$sense}} >= ($number-1)) | ||
| 290 | { | ||
| 291 | #selecting randomly by shuffling and selecting top N | ||
| 292 | shuffle(\@{$instances{$sense}}); | ||
| 293 | foreach (0..$number-1) | ||
| 294 | { | ||
| 295 | # selected will contain all the instances that are | ||
| 296 | # to be displayed | ||
| 297 | $select_it=shift @{$instances{$sense}}; | ||
| 298 | $selected{$select_it}=1; | ||
| 299 | } | ||
| 300 | } | ||
| 301 | } | ||
| 302 | |||
| 303 | # now display only the selected instances from the text | ||
| 304 | $write=1; | ||
| 305 | $line_num=0; | ||
| 306 | $count_flag=0; | ||
| 307 | foreach (@text) | ||
| 308 | { | ||
| 309 | if(/<\/context>/) | ||
| 310 | { | ||
| 311 | $count_flag=0; | ||
| 312 | } | ||
| 313 | if($count_flag==1) | ||
| 314 | { | ||
| 315 | $line_num++; | ||
| 316 | } | ||
| 317 | if(/instance id=\"([^\"]+)\"/) | ||
| 318 | { | ||
| 319 | if(!defined $selected{$1}) | ||
| 320 | { | ||
| 321 | $write=0; | ||
| 322 | } | ||
| 323 | } | ||
| 324 | if($write==1) | ||
| 325 | { | ||
| 326 | print; | ||
| 327 | if(defined $opt_count && $count_flag==1) | ||
| 328 | { | ||
| 329 | push @count_lines,$line_num; | ||
| 330 | } | ||
| 331 | } | ||
| 332 | if(/<\/instance>/) | ||
| 333 | { | ||
| 334 | $write=1; | ||
| 335 | } | ||
| 336 | if(/<context>/) | ||
| 337 | { | ||
| 338 | $count_flag=1; | ||
| 339 | } | ||
| 340 | } | ||
| 341 | |||
| 342 | ############################################################################### | ||
| 343 | |||
| 344 | # --------------------- | ||
| 345 | # balancing count file | ||
| 346 | # --------------------- | ||
| 347 | |||
| 348 | # @count_lines contains line numbers of lines to be written from count file | ||
| 349 | if(defined $countfile) | ||
| 350 | { | ||
| 351 | $line_num=0; | ||
| 352 | $next_line=shift @count_lines; | ||
| 353 | while(<COUNT>) | ||
| 354 | { | ||
| 355 | $line_num++; | ||
| 356 | # write this line | ||
| 357 | if(defined $next_line && $line_num==$next_line) | ||
| 358 | { | ||
| 359 | print COUNT_OUT $_; | ||
| 360 | # get the next line number | ||
| 361 | if($#count_lines>=0) | ||
| 362 | { | ||
| 363 | $next_line=shift @count_lines; | ||
| 364 | } | ||
| 365 | else | ||
| 366 | { | ||
| 367 | last; | ||
| 368 | } | ||
| 369 | } | ||
| 370 | } | ||
| 371 | # catching inconsistency between given count and Data file | ||
| 372 | # all line nos in count_lines array must occur in count file | ||
| 373 | if($#count_lines>=0) | ||
| 374 | { | ||
| 375 | print STDERR "ERROR($0): | ||
| 376 | Data File <$infile> and Count file <$countfile> are inconsistent.\n"; | ||
| 377 | exit; | ||
| 378 | } | ||
| 379 | } | ||
| 380 | ############################################################################## | ||
| 381 | |||
| 382 | #------------------- | ||
| 383 | #shuffle subroutine | ||
| 384 | #------------------- | ||
| 385 | #this code is taken from the book Perl Cookbook Chapter 4 that describes | ||
| 386 | #randomizing array(Page 121-122) | ||
| 387 | |||
| 388 | #Reference : Perl Cookbook, Tom Christiansen & Nathan Torkington, O'Reilly | ||
| 389 | # publication, 1998, Chapter 4, section 4.17, Randomizing an Array | ||
| 390 | |||
| 391 | sub shuffle | ||
| 392 | { | ||
| 393 | my $array = shift; | ||
| 394 | my $i; | ||
| 395 | for ($i = @$array; --$i; ) | ||
| 396 | { | ||
| 397 | my $j = int rand ($i+1); | ||
| 398 | next if $i == $j; | ||
| 399 | @$array[$i,$j] = @$array[$j,$i]; | ||
| 400 | } | ||
| 401 | } | ||
| 402 | |||
| 403 | #show minimal usage message | ||
| 404 | sub minimal() | ||
| 405 | { | ||
| 406 | print "Usage: balance.pl [OPTIONS] DATA N"; | ||
| 407 | print "\nTYPE balance.pl --help for help\n"; | ||
| 408 | } | ||
| 409 | #show help | ||
| 410 | sub showhelp() | ||
| 411 | { | ||
| 412 | print "Usage: | ||
| 413 | balance.pl [OPTIONS] DATA N | ||
| 414 | Balances sense distribution in a given Senseval-2 formatted DATA file by | ||
| 415 | randomly selecting exactly N instances of each sense tag. | ||
| 416 | |||
| 417 | DATA | ||
| 418 | Specify a Senseval-2 file to be balanced for sense tags. | ||
| 419 | |||
| 420 | N | ||
| 421 | Specify the number of instances to be selected from sense found in | ||
| 422 | the SOURCE file."; | ||
| 423 | print "\nOPTIONS:"; | ||
| 424 | print " | ||
| 425 | --count COUNT_FILE | ||
| 426 | Specify the COUNT_FILE created by preprocess.pl program that shows | ||
| 427 | instance data within <context> tags corresponding to each instance | ||
| 428 | in a given DATA file. COUNT_FILE will also be balanced along with | ||
| 429 | the DATA file and updated COUNT_FILE will be written into | ||
| 430 | COUNT_FILE.balanced. | ||
| 431 | |||
| 432 | --help | ||
| 433 | Displays this message."; | ||
| 434 | print " | ||
| 435 | --version | ||
| 436 | Displays the version information.\n"; | ||
| 437 | } | ||
| 438 | #version information | ||
| 439 | sub showversion() | ||
| 440 | { | ||
| 441 | print '$Id: balance.pl,v 1.12 2008/03/31 16:19:57 tpederse Exp $'; | ||
| 442 | # print "balance.pl - Version 0.11\n"; | ||
| 443 | print "\nBalance sense distribution in a Senseval-2 file\n"; | ||
| 444 | # print "Copyright (c) 2002-2005, Amruta Purandare, Ted Pedersen.\n"; | ||
| 445 | # print "Date Of Last Update: 05/23/2003\n"; | ||
| 446 | } |