| 1 | #!/usr/local/bin/perl -w | ||
| 2 | |||
| 3 | =head1 NAME | ||
| 4 | |||
| 5 | filter.pl - Remove the instances of low frequency sense tags from a Senseval-2 data file | ||
| 6 | |||
| 7 | =head1 SYNOPSIS | ||
| 8 | |||
| 9 | filter.pl [OPTIONS] DATA FREQUENCY_OUTPUT | ||
| 10 | |||
| 11 | Determine the distribution of senses in the given Senseval-2 input file | ||
| 12 | |||
| 13 | frequency.pl begin.v-test.xml > freq-output | ||
| 14 | |||
| 15 | frequency.pl freq-output | ||
| 16 | |||
| 17 | Output => | ||
| 18 | |||
| 19 | <sense id="begin%2:30:00::" percent="64.31"/> | ||
| 20 | <sense id="begin%2:30:01::" percent="14.51"/> | ||
| 21 | <sense id="begin%2:42:04::" percent="21.18"/> | ||
| 22 | Total Instances = 255 | ||
| 23 | Total Distinct Senses=3 | ||
| 24 | Distribution={64.31,21.18,14.51} | ||
| 25 | % of Majority Sense = 64.31 | ||
| 26 | |||
| 27 | Filter any sense that occurs in less than 1% of the instances (there are | ||
| 28 | none in this data, so frequency output is unchanged) | ||
| 29 | |||
| 30 | filter.pl begin.v-test.xml freq-output >fil-output | ||
| 31 | |||
| 32 | frequency.pl fil-output | ||
| 33 | |||
| 34 | Output => | ||
| 35 | |||
| 36 | <sense id="begin%2:30:00::" percent="64.31"/> | ||
| 37 | <sense id="begin%2:30:01::" percent="14.51"/> | ||
| 38 | <sense id="begin%2:42:04::" percent="21.18"/> | ||
| 39 | Total Instances = 255 | ||
| 40 | Total Distinct Senses=3 | ||
| 41 | Distribution={64.31,21.18,14.51} | ||
| 42 | % of Majority Sense = 64.31 | ||
| 43 | |||
| 44 | Keep only the top 2 ranked (most frequent) senses | ||
| 45 | |||
| 46 | filter.pl --rank 2 begin.v-test.xml freq-output > fil-output | ||
| 47 | |||
| 48 | frequency.pl fil-output | ||
| 49 | |||
| 50 | Output => | ||
| 51 | |||
| 52 | <sense id="begin%2:30:00::" percent="75.23"/> | ||
| 53 | <sense id="begin%2:42:04::" percent="24.77"/> | ||
| 54 | Total Instances = 218 | ||
| 55 | Total Distinct Senses=2 | ||
| 56 | Distribution={75.23,24.77} | ||
| 57 | % of Majority Sense = 75.23 | ||
| 58 | |||
| 59 | Keep all senses that occur in at least 20% of the instances in the | ||
| 60 | original data | ||
| 61 | |||
| 62 | filter.pl --p 20 begin.v-test.xml freq-output > fil-output | ||
| 63 | |||
| 64 | frequency.pl fil-output | ||
| 65 | |||
| 66 | Output => | ||
| 67 | |||
| 68 | <sense id="begin%2:30:00::" percent="75.23"/> | ||
| 69 | <sense id="begin%2:42:04::" percent="24.77"/> | ||
| 70 | Total Instances = 218 | ||
| 71 | Total Distinct Senses=2 | ||
| 72 | Distribution={75.23,24.77} | ||
| 73 | % of Majority Sense = 75.23 | ||
| 74 | |||
| 75 | You can find L<begin.v-test.xml> in samples/Data | ||
| 76 | |||
| 77 | Type C<filter.pl --help> for a quick summary of available options. | ||
| 78 | |||
| 79 | =head1 DESCRIPTION | ||
| 80 | |||
| 81 | This program will remove low frequency sense tags from a Senseval-2 data | ||
| 82 | set by specifying a percentage or rank threshhold. By default it | ||
| 83 | removes any sense tag associated with less than 1% of the total | ||
| 84 | instances. Output is to STDOUT, so the original input data file is | ||
| 85 | unchanged. | ||
| 86 | |||
| 87 | =head1 INPUT | ||
| 88 | |||
| 89 | =head2 Required Arguments: | ||
| 90 | |||
| 91 | filter.pl requires two compulsory arguments - | ||
| 92 | |||
| 93 | =head4 DATA | ||
| 94 | |||
| 95 | Senseval-2 formatted data file that is to be filtered. | ||
| 96 | |||
| 97 | =head4 FREQUENCY_OUTPUT | ||
| 98 | |||
| 99 | This should be an output created by program frequency.pl of this | ||
| 100 | package that shows percentage frequency of each sense tag appearing in given | ||
| 101 | DATA. FREQUENCY_OUTPUT should be created by running frequency.pl on the same | ||
| 102 | DATA file that is input to filter. | ||
| 103 | |||
| 104 | This should show tags | ||
| 105 | |||
| 106 | <sense id="S" percent="P"/> | ||
| 107 | |||
| 108 | that specify percent of each sense tag S in the DATA file. | ||
| 109 | |||
| 110 | =head2 Optional Arguments: | ||
| 111 | |||
| 112 | =head3 Filter Options: | ||
| 113 | |||
| 114 | =head4 --percent P | ||
| 115 | |||
| 116 | With this option, user can specify the percentage cutoff for filtering. When | ||
| 117 | --percent is specified, filter.pl will remove all sense tags whose | ||
| 118 | frequency in FREQUENCY_OUTPUT is below P %. A DATA instance that has all sense | ||
| 119 | tags attached to it below P% is removed. In other words, only those DATA | ||
| 120 | instances are retained which have atleast one sense tag with frequency more | ||
| 121 | than or equal to P%. | ||
| 122 | |||
| 123 | =head4 --rank R | ||
| 124 | |||
| 125 | With this option, user can specify the rank cutoff for filtering. When | ||
| 126 | --rank is specified, filter.pl will remove those sense tags that are ranked | ||
| 127 | below R when senses are ordered according to their percentages. A DATA instance | ||
| 128 | that has all sense tags attached to it below the rank R will be removed. In | ||
| 129 | other words, only those DATA instances are retained which have atleast one | ||
| 130 | sense tag above rank R. | ||
| 131 | |||
| 132 | filter.pl allows only one of the above filter conditions to be specified. | ||
| 133 | |||
| 134 | If neither of the filter options is specified, it will set the default filter | ||
| 135 | condition as P = 1 and will filter DATA by removing sense tags less then 1%. | ||
| 136 | |||
| 137 | =head4 --nomulti | ||
| 138 | |||
| 139 | Removes multiple sense tags attached to an instance such that each instance is | ||
| 140 | tagged with the most frequent sense tag among the tags attached to it. | ||
| 141 | |||
| 142 | =head3 Other Options : | ||
| 143 | |||
| 144 | --count COUNT | ||
| 145 | |||
| 146 | Filters the corresponding COUNT file created by preprocess.pl | ||
| 147 | along with the DATA file. COUNT file is filtered such that it stays consistent | ||
| 148 | with the new filtered DATA file and contains only those instances left after | ||
| 149 | filtering, in the same order as they appear in the output. | ||
| 150 | |||
| 151 | Filtered COUNT is written to file COUNT.filtered and every ith line in | ||
| 152 | COUNT.filtered shows the instance data within <context> and </context> tags | ||
| 153 | for the ith instance in the output of filter. | ||
| 154 | |||
| 155 | =head4 --help | ||
| 156 | |||
| 157 | Displays this message. | ||
| 158 | |||
| 159 | =head4 --version | ||
| 160 | |||
| 161 | Displays the version information. | ||
| 162 | |||
| 163 | =head1 OUTPUT | ||
| 164 | |||
| 165 | Output is a sense filtered Senseval-2 file that shows only those DATA instances | ||
| 166 | which have at least one sense tag left after filtering. | ||
| 167 | |||
| 168 | =head1 AUTHORS | ||
| 169 | |||
| 170 | Ted Pedersen, University of Minnesota, Duluth | ||
| 171 | tpederse at d.umn.edu | ||
| 172 | |||
| 173 | Amruta Purandare, University of Pittsburgh | ||
| 174 | |||
| 175 | =head1 COPYRIGHT | ||
| 176 | |||
| 177 | Copyright (c) 2002-2008, Amruta Purandare and Ted Pedersen | ||
| 178 | |||
| 179 | This program is free software; you can redistribute it and/or modify it under | ||
| 180 | the terms of the GNU General Public License as published by the Free Software | ||
| 181 | Foundation; either version 2 of the License, or (at your option) any later | ||
| 182 | version. | ||
| 183 | |||
| 184 | This program is distributed in the hope that it will be useful, but WITHOUT | ||
| 185 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS | ||
| 186 | FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. | ||
| 187 | |||
| 188 | You should have received a copy of the GNU General Public License along with | ||
| 189 | this program; if not, write to | ||
| 190 | |||
| 191 | The Free Software Foundation, Inc., | ||
| 192 | 59 Temple Place - Suite 330, | ||
| 193 | Boston, MA 02111-1307, USA. | ||
| 194 | |||
| 195 | =cut | ||
| 196 | |||
| 197 | ############################################################################# | ||
| 198 | # | ||
| 199 | # PROGRAM NAME- filter.pl (A Component of SenseClusters Package) | ||
| 200 | # Filters given data by removing low % sense tags and instances using | ||
| 201 | # these tags. | ||
| 202 | # | ||
| 203 | ############################################################################# | ||
| 204 | # THE CODE STARTS HERE | ||
| 205 | |||
| 206 | #$0 contains the program name along with | ||
| 207 | #the complete path. Extract just the program | ||
| 208 | #name and use in error messages | ||
| 209 | $0=~s/.*\/(.+)/$1/; | ||
| 210 | |||
| 211 | ############################################################################### | ||
| 212 | |||
| 213 | # ================================ | ||
| 214 | # COMMAND LINE OPTIONS AND USAGE | ||
| 215 | # ================================ | ||
| 216 | |||
| 217 | # command line options | ||
| 218 | use Getopt::Long; | ||
| 219 | GetOptions ("help","version","percent=f","rank=i","count=s","nomulti"); | ||
| 220 | # show help option | ||
| 221 | if(defined $opt_help) | ||
| 222 | { | ||
| 223 | $opt_help=1; | ||
| 224 | &showhelp(); | ||
| 225 | exit; | ||
| 226 | } | ||
| 227 | |||
| 228 | # show version information | ||
| 229 | if(defined $opt_version) | ||
| 230 | { | ||
| 231 | $opt_version=1; | ||
| 232 | &showversion(); | ||
| 233 | exit; | ||
| 234 | } | ||
| 235 | |||
| 236 | # show minimal usage message if no arguments | ||
| 237 | if($#ARGV<1) | ||
| 238 | { | ||
| 239 | &showminimal(); | ||
| 240 | exit; | ||
| 241 | } | ||
| 242 | |||
| 243 | # --percent P will remove all sense tags | ||
| 244 | # occurring less than P% of the times | ||
| 245 | if(defined $opt_percent) | ||
| 246 | { | ||
| 247 | $percent=$opt_percent; | ||
| 248 | } | ||
| 249 | |||
| 250 | # --rank R will remove senses below rank R | ||
| 251 | if(defined $opt_rank) | ||
| 252 | { | ||
| 253 | $rank=$opt_rank; | ||
| 254 | } | ||
| 255 | |||
| 256 | # rank and percent can't be both used | ||
| 257 | if(defined $percent && defined $rank) | ||
| 258 | { | ||
| 259 | print STDERR "ERROR($0): | ||
| 260 | Program allows only one of the filter conditions. | ||
| 261 | Use either --rank or --percent options.\n"; | ||
| 262 | exit; | ||
| 263 | } | ||
| 264 | |||
| 265 | # if both the filter conditions are not specified | ||
| 266 | # program will set value of percent cutoff to 1 | ||
| 267 | # and will remove all sense tags appearing less than | ||
| 268 | # 1% in the given data | ||
| 269 | if(!defined $percent && !defined $rank) | ||
| 270 | { | ||
| 271 | $percent=1; | ||
| 272 | } | ||
| 273 | |||
| 274 | # filters count file created by preprocess.pl | ||
| 275 | # for a given input file | ||
| 276 | if(defined $opt_count) | ||
| 277 | { | ||
| 278 | $count_file=$opt_count; | ||
| 279 | } | ||
| 280 | |||
| 281 | ############################################################################## | ||
| 282 | |||
| 283 | # ================================ | ||
| 284 | # INITIALIZATION AND INPUT | ||
| 285 | # ================================ | ||
| 286 | |||
| 287 | #argv[0] should be the file to be filtered | ||
| 288 | if(!defined $ARGV[0]) | ||
| 289 | { | ||
| 290 | print STDERR "ERROR($0): | ||
| 291 | Please specify a Senseval-2 formatted data file to be filtered.\n"; | ||
| 292 | exit; | ||
| 293 | } | ||
| 294 | #accept the file name | ||
| 295 | $infile=$ARGV[0]; | ||
| 296 | #check if exists | ||
| 297 | if(!-e $infile) | ||
| 298 | { | ||
| 299 | print STDERR "ERROR($0): | ||
| 300 | Source file <$infile> doesn't exist...\n"; | ||
| 301 | exit; | ||
| 302 | } | ||
| 303 | open(IN,$infile) || die "Error($0): | ||
| 304 | Error(code=$!) in opening <$infile> file.\n"; | ||
| 305 | |||
| 306 | # argv[1] should be the output of frequency.pl | ||
| 307 | # that shows % frequency of each sense tag in the source | ||
| 308 | if(!defined $ARGV[1]) | ||
| 309 | { | ||
| 310 | print STDERR "ERROR($0): | ||
| 311 | Please specify the sense distribution file containing output of | ||
| 312 | frequency.pl for the given Source <$infile>.\n"; | ||
| 313 | exit; | ||
| 314 | } | ||
| 315 | #accept the file name | ||
| 316 | $freq_file=$ARGV[1]; | ||
| 317 | #check if exists | ||
| 318 | if(!-e $freq_file) | ||
| 319 | { | ||
| 320 | print STDERR "ERROR($0): | ||
| 321 | Sense distribution file <$freq_file> doesn't exist.\n"; | ||
| 322 | exit; | ||
| 323 | } | ||
| 324 | open(FREQ,$freq_file) || die "Error($0): | ||
| 325 | Error(code=$!) in opening <$freq_file> file.\n"; | ||
| 326 | |||
| 327 | # -------------------------- | ||
| 328 | # if count file is provided | ||
| 329 | # -------------------------- | ||
| 330 | if(defined $count_file) | ||
| 331 | { | ||
| 332 | if(!-e $count_file) | ||
| 333 | { | ||
| 334 | print STDERR "ERROR($0): | ||
| 335 | Count file <$count_file> doesn't exist.\n"; | ||
| 336 | exit; | ||
| 337 | } | ||
| 338 | open(COUNT,$count_file) || die "Error($0): | ||
| 339 | Error(code=$!) in opening <$count_file> file.\n"; | ||
| 340 | |||
| 341 | #----------------------------- | ||
| 342 | # Creating out file for count | ||
| 343 | #----------------------------- | ||
| 344 | $count_outfile=$count_file.".filtered"; | ||
| 345 | $ans="N"; | ||
| 346 | if(-e $count_outfile) | ||
| 347 | { | ||
| 348 | print STDERR "Warning($0): | ||
| 349 | Count filtered file <$count_outfile> already exists, overwrite (y/n)? "; | ||
| 350 | $ans=<STDIN>; | ||
| 351 | } | ||
| 352 | if(!-e $count_outfile || $ans=~/Y|y/) | ||
| 353 | { | ||
| 354 | open(COUNT_OUT,">$count_outfile") || die "Error($0): | ||
| 355 | Error(code=$!) in opening count filtered file <$count_outfile>.\n"; | ||
| 356 | } | ||
| 357 | else | ||
| 358 | { | ||
| 359 | undef $count_file; | ||
| 360 | } | ||
| 361 | } | ||
| 362 | |||
| 363 | ############################################################################## | ||
| 364 | |||
| 365 | # =========================== | ||
| 366 | # BUILD SENSE FREQUENCY TABLE | ||
| 367 | # =========================== | ||
| 368 | |||
| 369 | $line_num=0; | ||
| 370 | while(<FREQ>) | ||
| 371 | { | ||
| 372 | $line_num++; | ||
| 373 | # trimming extra spaces | ||
| 374 | chomp; | ||
| 375 | s/\s+$//g; | ||
| 376 | s/^\s+//g; | ||
| 377 | s/\s+/ /g; | ||
| 378 | # handling blank lines | ||
| 379 | if(/^\s*$/) | ||
| 380 | { | ||
| 381 | next; | ||
| 382 | } | ||
| 383 | # get the % of each sense tag | ||
| 384 | if(/<sense id=\"([^\"]+)\" percent=\"(\d*\.?[\d]+)\"\/>/) | ||
| 385 | { | ||
| 386 | if(defined $freq_hash{$1}) | ||
| 387 | { | ||
| 388 | print STDERR "ERROR($0): | ||
| 389 | Sense Tag <$1> is repeated in the sense distribution file <$freq_file>.\n"; | ||
| 390 | exit; | ||
| 391 | } | ||
| 392 | $freq_hash{$1}=$2; | ||
| 393 | if(defined $rank) | ||
| 394 | { | ||
| 395 | push @freq_array,$2; | ||
| 396 | } | ||
| 397 | # store the removed sense to update the KEY file later | ||
| 398 | if(defined $opt_key && defined $percent && $freq_hash{$1}<$percent) | ||
| 399 | { | ||
| 400 | push @removed,$1; | ||
| 401 | } | ||
| 402 | } | ||
| 403 | } | ||
| 404 | |||
| 405 | if(!%freq_hash) | ||
| 406 | { | ||
| 407 | print STDERR "ERROR($0): | ||
| 408 | No valid <sense id=\"S\" percent=\"P\"\/> entry found in the sense | ||
| 409 | distribution file <$freq_file>.\n"; | ||
| 410 | exit; | ||
| 411 | } | ||
| 412 | ############################################################################## | ||
| 413 | |||
| 414 | # ================ | ||
| 415 | # FIND SENSE RANKS | ||
| 416 | # ================ | ||
| 417 | |||
| 418 | # --rank R removes all senses whose ranks are below R | ||
| 419 | # ranking senses according to their percentages | ||
| 420 | if(defined $rank) | ||
| 421 | { | ||
| 422 | undef $old; | ||
| 423 | $myrank=1; | ||
| 424 | # sorting sense frequencies in descending order | ||
| 425 | @sorted=sort {$b <=> $a} @freq_array; | ||
| 426 | foreach $freq (@sorted) | ||
| 427 | { | ||
| 428 | # senses with this freq are already assigned ranks | ||
| 429 | # so go ahead | ||
| 430 | if(defined $old && $freq == $old) | ||
| 431 | { | ||
| 432 | next; | ||
| 433 | } | ||
| 434 | # increment rank only at % rise | ||
| 435 | if(defined $old && $freq<$old) | ||
| 436 | { | ||
| 437 | $myrank++; | ||
| 438 | } | ||
| 439 | $old=$freq; | ||
| 440 | # assign ranks to senses | ||
| 441 | foreach $sense (sort keys %freq_hash) | ||
| 442 | { | ||
| 443 | if($freq==$freq_hash{$sense}) | ||
| 444 | { | ||
| 445 | # each sense will get only one rank | ||
| 446 | if(!defined $rank_hash{$sense}) | ||
| 447 | { | ||
| 448 | $rank_hash{$sense}=$myrank; | ||
| 449 | # store the removed sense to update | ||
| 450 | # the KEY file later | ||
| 451 | if(defined $opt_key && $myrank>$rank) | ||
| 452 | { | ||
| 453 | push @removed,$sense; | ||
| 454 | } | ||
| 455 | } | ||
| 456 | } | ||
| 457 | } | ||
| 458 | } | ||
| 459 | } | ||
| 460 | |||
| 461 | ############################################################################## | ||
| 462 | |||
| 463 | # =============== | ||
| 464 | # SENSE FILTERING | ||
| 465 | # =============== | ||
| 466 | |||
| 467 | # if --nomulti is defined remove multiple sense tags for an instance | ||
| 468 | # keeping only the most frequent tag | ||
| 469 | if(defined $opt_nomulti) | ||
| 470 | { | ||
| 471 | #--------------------- | ||
| 472 | #creating a TEMP file | ||
| 473 | #--------------------- | ||
| 474 | #use the system_defined date for unique name for tempfile | ||
| 475 | |||
| 476 | $tempfile="temp".time().".filter"; | ||
| 477 | open(TEMP1,">$tempfile")||die"ERROR($0): | ||
| 478 | Internal System Error(code=$!).\n"; | ||
| 479 | while(<IN>) | ||
| 480 | { | ||
| 481 | # removing all but the most frequent tag | ||
| 482 | if(/sense\s*id=\"([^\"]+)\"/) | ||
| 483 | { | ||
| 484 | if((defined $freq_hash{$1}) && (!defined $current_max || $freq_hash{$1}>$current_max)) | ||
| 485 | { | ||
| 486 | $current_max=$freq_hash{$1}; | ||
| 487 | $max_sense=$_; | ||
| 488 | } | ||
| 489 | } | ||
| 490 | elsif(/<context>/) | ||
| 491 | { | ||
| 492 | if(defined $max_sense) | ||
| 493 | { | ||
| 494 | print TEMP1 $max_sense; | ||
| 495 | undef $max_sense; | ||
| 496 | undef $current_max; | ||
| 497 | } | ||
| 498 | print TEMP1 $_; | ||
| 499 | } | ||
| 500 | else | ||
| 501 | { | ||
| 502 | print TEMP1 $_; | ||
| 503 | } | ||
| 504 | } | ||
| 505 | close TEMP1; | ||
| 506 | close IN; | ||
| 507 | open(IN,$tempfile) || die "\nERROR($0): | ||
| 508 | Error in opening temporary file $tempfile.\n"; | ||
| 509 | } | ||
| 510 | |||
| 511 | #--------------------- | ||
| 512 | #creating a TEMP file | ||
| 513 | #--------------------- | ||
| 514 | #we hold data temporarily in tempfile till the program terminates | ||
| 515 | #without an error. In case of error, the tempfile would be | ||
| 516 | #retained and will hold partial output of the program. | ||
| 517 | |||
| 518 | #use the system_defined date for unique name for tempfile | ||
| 519 | |||
| 520 | $tempfile1="temp1".time().".filter"; | ||
| 521 | open(TEMP,">$tempfile1") || die"ERROR($0): | ||
| 522 | Internal System Error(code=$!).\n"; | ||
| 523 | |||
| 524 | #this is to keep track of which data instances are to be written | ||
| 525 | #from corresponding count file | ||
| 526 | $line_num=1; | ||
| 527 | |||
| 528 | # ---------------------------- | ||
| 529 | # Actual Filtering Starts Here | ||
| 530 | # ---------------------------- | ||
| 531 | |||
| 532 | # write flag indicates if the current instance is to be | ||
| 533 | # written or not | ||
| 534 | |||
| 535 | # initially write is set to allow standard XML tags before the actual | ||
| 536 | # instance data starts | ||
| 537 | $write=1; | ||
| 538 | # counts lines between the tags <context> and </context> | ||
| 539 | $count_lines=0; | ||
| 540 | $line_no=0; | ||
| 541 | while(<IN>) | ||
| 542 | { | ||
| 543 | $line_no++; | ||
| 544 | # we count data lines only within <context> & </context> tags | ||
| 545 | # to remember line nos to be written into filtered count output | ||
| 546 | if(/<\/context>/) | ||
| 547 | { | ||
| 548 | $count_lines=0; | ||
| 549 | } | ||
| 550 | if(/instance id=\"([^\"]+)\"/) | ||
| 551 | { | ||
| 552 | # hold temporarily as we don't know percent/rank used | ||
| 553 | # by this instance yet | ||
| 554 | if(defined $temp_buf) | ||
| 555 | { | ||
| 556 | $temp_buf.=$_; | ||
| 557 | } | ||
| 558 | else | ||
| 559 | { | ||
| 560 | $temp_buf=$_; | ||
| 561 | } | ||
| 562 | # write will be set only when program encounters | ||
| 563 | # atleast 1 sense tag used by this instance that | ||
| 564 | # passes the filter condition | ||
| 565 | undef $write; | ||
| 566 | } | ||
| 567 | elsif(/<\/instance>/) | ||
| 568 | { | ||
| 569 | undef $temp_buf; | ||
| 570 | if(defined $write && $write==1) | ||
| 571 | { | ||
| 572 | print TEMP $_; | ||
| 573 | } | ||
| 574 | # to allow any data between </instance> and <instance> | ||
| 575 | # or closing tags after last </instance> | ||
| 576 | $write=1; | ||
| 577 | } | ||
| 578 | # extract the sense id and check the filter conditions | ||
| 579 | elsif(/sense\s*id=\"([^\"]+)\"/) | ||
| 580 | { | ||
| 581 | $sense=$1; | ||
| 582 | #check percent/rank and set write flag appropriately | ||
| 583 | if(defined $percent) | ||
| 584 | { | ||
| 585 | if(defined $freq_hash{$sense}) | ||
| 586 | { | ||
| 587 | if($freq_hash{$sense}>=$percent) | ||
| 588 | { | ||
| 589 | # write this instance | ||
| 590 | $write=1; | ||
| 591 | # hold this answer tag till all answer | ||
| 592 | # tags are processed | ||
| 593 | $temp_buf.=$_; | ||
| 594 | } | ||
| 595 | } | ||
| 596 | } | ||
| 597 | elsif(defined $rank) | ||
| 598 | { | ||
| 599 | if(defined $rank_hash{$sense}) | ||
| 600 | { | ||
| 601 | if($rank_hash{$sense}<=$rank) | ||
| 602 | { | ||
| 603 | $write=1; | ||
| 604 | # hold this answer tag till all answer | ||
| 605 | # tags are processed | ||
| 606 | $temp_buf.=$_; | ||
| 607 | } | ||
| 608 | } | ||
| 609 | } | ||
| 610 | } | ||
| 611 | elsif(defined $write && $write==1) | ||
| 612 | { | ||
| 613 | if(defined $temp_buf) | ||
| 614 | { | ||
| 615 | print TEMP $temp_buf; | ||
| 616 | undef $temp_buf; | ||
| 617 | } | ||
| 618 | print TEMP $_; | ||
| 619 | # data on line numbers in @lines_for_count array will be | ||
| 620 | # written from the corresponding count file | ||
| 621 | if($count_lines==1) | ||
| 622 | { | ||
| 623 | # push current line number as data from .count | ||
| 624 | # file at this position needs to be written to | ||
| 625 | # filtered count | ||
| 626 | push @lines_for_count,$line_num; | ||
| 627 | } | ||
| 628 | } | ||
| 629 | # count lines when <context> tag is seen | ||
| 630 | if($count_lines==1) | ||
| 631 | { | ||
| 632 | $line_num++; | ||
| 633 | } | ||
| 634 | # start counting number of data lines when <context> comes | ||
| 635 | if(/<context>/) | ||
| 636 | { | ||
| 637 | $count_lines=1; | ||
| 638 | } | ||
| 639 | } | ||
| 640 | |||
| 641 | #now display to STDOUT | ||
| 642 | close TEMP; | ||
| 643 | open(TEMP,$tempfile1) || die "ERROR($0): | ||
| 644 | Internal System Error(code=$!).\n"; | ||
| 645 | @file_stuff=<TEMP>; | ||
| 646 | print @file_stuff; | ||
| 647 | #remove the tempfile1 | ||
| 648 | unlink "$tempfile1"; | ||
| 649 | |||
| 650 | if(defined $opt_nomulti) | ||
| 651 | { | ||
| 652 | unlink "$tempfile"; | ||
| 653 | } | ||
| 654 | ############################################################################## | ||
| 655 | |||
| 656 | # ============================== | ||
| 657 | # FILTERING DATA FROM COUNT FILE | ||
| 658 | # ============================== | ||
| 659 | |||
| 660 | # @lines_for_count is already sorted as it contains line numbers | ||
| 661 | # from xml file as they are read | ||
| 662 | if(defined $count_file) | ||
| 663 | { | ||
| 664 | $line_num=0; | ||
| 665 | $next_line=shift @lines_for_count; | ||
| 666 | while(<COUNT>) | ||
| 667 | { | ||
| 668 | $line_num++; | ||
| 669 | # write this line | ||
| 670 | if($line_num==$next_line) | ||
| 671 | { | ||
| 672 | print COUNT_OUT $_; | ||
| 673 | # get the next line number | ||
| 674 | if($#lines_for_count>=0) | ||
| 675 | { | ||
| 676 | $next_line=shift @lines_for_count; | ||
| 677 | } | ||
| 678 | else | ||
| 679 | { | ||
| 680 | last; | ||
| 681 | } | ||
| 682 | } | ||
| 683 | } | ||
| 684 | # catching inconsistency between given count and Data file | ||
| 685 | # all line nos in lines_for_count array must occur in count file | ||
| 686 | if($#lines_for_count>=0) | ||
| 687 | { | ||
| 688 | print STDERR "ERROR($0): | ||
| 689 | Data File <$infile> and Count file <$count_file> are inconsistent.\n"; | ||
| 690 | exit; | ||
| 691 | } | ||
| 692 | } | ||
| 693 | |||
| 694 | ############################################################################## | ||
| 695 | |||
| 696 | # ========================== | ||
| 697 | # SUBROUTINE SECTION | ||
| 698 | # ========================== | ||
| 699 | |||
| 700 | #----------------------------------------------------------------------------- | ||
| 701 | #show minimal usage message | ||
| 702 | sub showminimal() | ||
| 703 | { | ||
| 704 | print "Usage: filter.pl [OPTIONS] DATA FREQUENCY_OUTPUT"; | ||
| 705 | print "\nTYPE filter.pl --help for help\n"; | ||
| 706 | } | ||
| 707 | |||
| 708 | #----------------------------------------------------------------------------- | ||
| 709 | #show help | ||
| 710 | sub showhelp() | ||
| 711 | { | ||
| 712 | print "Usage: filter.pl [OPTIONS] DATA FREQUENCY_OUTPUT | ||
| 713 | |||
| 714 | Filters DATA by removing low percent sense tags using FREQUENCY_OUTPUT (output | ||
| 715 | of program frequency.pl showing percentage frequency of each sense in DATA). | ||
| 716 | A DATA instance is removed if all sense tags attached to it are removed by | ||
| 717 | applying a filter. | ||
| 718 | |||
| 719 | DATA | ||
| 720 | Specify a Senseval-2 formatted DATA file to be filtered. | ||
| 721 | |||
| 722 | FREQUENCY_OUTPUT | ||
| 723 | A file containing the output of frequency.pl by running it on the same | ||
| 724 | DATA file. FREQUENCY_OUTPUT should show tags | ||
| 725 | <sense id=\"S\" percent=\"P\"\/> | ||
| 726 | that specifies percent of sense tag S in the DATA file. | ||
| 727 | |||
| 728 | OPTIONS: | ||
| 729 | --percent P | ||
| 730 | Removes all senses whose frequency is below P%. Data instances having | ||
| 731 | all attached senses below P% are removed. | ||
| 732 | |||
| 733 | --rank R | ||
| 734 | Removes all senses ranking below R when arranged in descending order | ||
| 735 | of their frequencies. | ||
| 736 | |||
| 737 | Default Filter | ||
| 738 | If neither --percent P nor --rank R are specified, default filter will | ||
| 739 | be percent P = 1 and will remove senses below 1%. | ||
| 740 | |||
| 741 | --count COUNTFILE | ||
| 742 | This will filter data instances from the corresponding COUNTFILE | ||
| 743 | created by preprocess.pl program. This is to | ||
| 744 | keep the COUNTFILE consistent with the DATA file after filtering. | ||
| 745 | |||
| 746 | --nomulti | ||
| 747 | Removes all but the most frequent sense tag attached to a multi-tagged | ||
| 748 | instance. | ||
| 749 | |||
| 750 | --help | ||
| 751 | Displays this message. | ||
| 752 | |||
| 753 | --version | ||
| 754 | Displays the version information.\n"; | ||
| 755 | } | ||
| 756 | |||
| 757 | #------------------------------------------------------------------------------ | ||
| 758 | #version information | ||
| 759 | sub showversion() | ||
| 760 | { | ||
| 761 | # print "filter.pl - Version 0.11"; | ||
| 762 | print '$Id: filter.pl,v 1.13 2013/06/22 20:31:12 tpederse Exp $'; | ||
| 763 | print "\nRemove low frequency sense tags from a Senseval-2 file\n"; | ||
| 764 | # print "\nCopyright (c) 2002-2005, Amruta Purandare, Ted Pedersen.\n"; | ||
| 765 | # print "Date of Last Update: 05/07/2003\n"; | ||
| 766 | } | ||
| 767 | |||
| 768 | ############################################################################# | ||
| 769 |