| 1 | #!/usr/local/bin/perl -w | ||
| 2 | |||
| 3 | =head1 NAME | ||
| 4 | |||
| 5 | windower.pl - Limit window of context around a target word specified in a Senseval-2 input file | ||
| 6 | |||
| 7 | =head1 SYNOPSIS | ||
| 8 | |||
| 9 | Suppose we have a very small Senseval-2 file (small-test.xml) with just | ||
| 10 | 2 instances. We would like to limit the surrounding context to 5 words | ||
| 11 | to the left and 5 words to the right of the target word: | ||
| 12 | |||
| 13 | windower.pl small.xml 5 | ||
| 14 | |||
| 15 | Output => | ||
| 16 | |||
| 17 | <?xml version="1.0" encoding="iso-8859-1" ?> | ||
| 18 | <corpus lang='english' tagged="NO"> | ||
| 19 | <lexelt item="begin.v"> | ||
| 20 | <instance id="begin.555"> | ||
| 21 | <answer instance="begin.555" senseid="begin%2:30:01::"/> | ||
| 22 | <context> | ||
| 23 | greats hardly knowns and unknowns <head>begin</head> a game three month season | ||
| 24 | </context> | ||
| 25 | </instance> | ||
| 26 | <instance id="begin.557"> | ||
| 27 | <answer instance="begin.557" senseid="begin%2:30:01::"/> | ||
| 28 | <context> | ||
| 29 | late november it expects to <head>begin</head> construction by year end and | ||
| 30 | </context> | ||
| 31 | </instance> | ||
| 32 | </lexelt> | ||
| 33 | </corpus> | ||
| 34 | |||
| 35 | This is from the first two lines of the file begin.v-test.xml. You can | ||
| 36 | see the full contexts at /samples/Data. | ||
| 37 | |||
| 38 | Type C<windower.pl --help> for a quick summary of options | ||
| 39 | |||
| 40 | =head1 DESCRIPTION | ||
| 41 | |||
| 42 | Limits the contexts of given instances to W tokens around the target word. | ||
| 43 | |||
| 44 | =head1 USAGE | ||
| 45 | |||
| 46 | windower.pl [OPTIONS] SVAL2 W | ||
| 47 | |||
| 48 | =head1 INPUT | ||
| 49 | |||
| 50 | =head2 Required Arguments: | ||
| 51 | |||
| 52 | =head3 SVAL2 | ||
| 53 | |||
| 54 | SVAL2 must be a tokenized and preprocessed instance file in the Senseval-2 | ||
| 55 | format. | ||
| 56 | |||
| 57 | =head3 W | ||
| 58 | |||
| 59 | Should be a positive integer number specifying the window size. windower | ||
| 60 | will display only the tokens that appear in the window of [-W, +W] centered | ||
| 61 | around the target word. | ||
| 62 | |||
| 63 | =head2 Optional Arguments: | ||
| 64 | |||
| 65 | =head3 --plain | ||
| 66 | |||
| 67 | Output will be displayed in plain text format showing context of each instance | ||
| 68 | on a single separate line. i.e. each i'th line on stdout will show the context | ||
| 69 | of the i'th instance in the given SVAL2 file. By default, output is created in | ||
| 70 | Senseval-2 format. | ||
| 71 | |||
| 72 | =head3 --token TOKENREGEX | ||
| 73 | |||
| 74 | TOKENREGEX should be a file containing Perl regular expressions that define | ||
| 75 | the tokenization scheme in SVAL2. windower recognizes only those character | ||
| 76 | sequences from SVAL2 that match the specified token regex/s, everything else | ||
| 77 | will be ignored. If --token is not specified, windower searches the default | ||
| 78 | token.regex file in the current directory. | ||
| 79 | |||
| 80 | =head3 --target TARGETREGEX | ||
| 81 | |||
| 82 | Specify a file containing Perl regular expressions that define the target | ||
| 83 | word/s. Target words must be valid tokens recognizable by the specified | ||
| 84 | tokenization scheme (via --token or token.regex) | ||
| 85 | |||
| 86 | Following are some of the examples of TARGET word regex files - | ||
| 87 | |||
| 88 | =over 4 | ||
| 89 | |||
| 90 | =item 1. | ||
| 91 | |||
| 92 | /<head>[Ll]ines?<\/head>/ | ||
| 93 | |||
| 94 | which specifies that the target word could be | ||
| 95 | |||
| 96 | line, Line, lines or Lines | ||
| 97 | |||
| 98 | delimited in <head> and </head> tags. | ||
| 99 | |||
| 100 | =item 2. | ||
| 101 | |||
| 102 | Above regex can also be specified as multiple regexes in TARGET as - | ||
| 103 | |||
| 104 | /<head>line<\/head>/ | ||
| 105 | |||
| 106 | /<head>lines<\/head>/ | ||
| 107 | |||
| 108 | /<head>Line<\/head>/ | ||
| 109 | |||
| 110 | /<head>Lines<\/head>/ | ||
| 111 | |||
| 112 | with a single regex per line | ||
| 113 | |||
| 114 | =item 3. | ||
| 115 | |||
| 116 | Regex | ||
| 117 | |||
| 118 | /<head>\w+<\/head>/ | ||
| 119 | |||
| 120 | shows a more general regex for target words marked in <head> tags | ||
| 121 | |||
| 122 | =item 4. | ||
| 123 | |||
| 124 | Regex | ||
| 125 | |||
| 126 | /<head.*>\w+<\/head>/ | ||
| 127 | |||
| 128 | Shows the regex for matching target words in the original Senseval-2 | ||
| 129 | data. | ||
| 130 | |||
| 131 | =item 5. | ||
| 132 | |||
| 133 | /[Ll]ines?/ | ||
| 134 | |||
| 135 | shows that any occurrence of words - Line, line, Lines, lines are target words | ||
| 136 | (that are not delimited in any special tags). | ||
| 137 | |||
| 138 | =back | ||
| 139 | |||
| 140 | =head3 Other Options : | ||
| 141 | |||
| 142 | =head4 --help | ||
| 143 | |||
| 144 | Displays this message. | ||
| 145 | |||
| 146 | =head4 --version | ||
| 147 | |||
| 148 | Displays the version information. | ||
| 149 | |||
| 150 | =head1 OUTPUT | ||
| 151 | |||
| 152 | When --plain is not selected, OUTPUT is in Senseval-2 format that looks same | ||
| 153 | as the input SVAL2 file except the context of each instance shows atmost W | ||
| 154 | words around the target word. | ||
| 155 | |||
| 156 | When --plain is ON, OUTPUT shows each context on a single line i.e. context of i'th instance in the given SVAL2 file is shown on the i'th line on stdout. | ||
| 157 | |||
| 158 | =head1 AUTHORS | ||
| 159 | |||
| 160 | Amruta Purandare, University of Pittsburgh | ||
| 161 | |||
| 162 | Ted Pedersen, University of Minnesota, Duluth | ||
| 163 | tpederse at d.umn.edu | ||
| 164 | |||
| 165 | =head1 COPYRIGHT | ||
| 166 | |||
| 167 | Copyright (c) 2002-2008, Amruta Purandare and Ted Pedersen | ||
| 168 | |||
| 169 | This program is free software; you can redistribute it and/or modify it under | ||
| 170 | the terms of the GNU General Public License as published by the Free Software | ||
| 171 | Foundation; either version 2 of the License, or (at your option) any later | ||
| 172 | version. | ||
| 173 | |||
| 174 | This program is distributed in the hope that it will be useful, but WITHOUT | ||
| 175 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS | ||
| 176 | FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. | ||
| 177 | |||
| 178 | You should have received a copy of the GNU General Public License along with | ||
| 179 | this program; if not, write to | ||
| 180 | |||
| 181 | The Free Software Foundation, Inc., | ||
| 182 | 59 Temple Place - Suite 330, | ||
| 183 | Boston, MA 02111-1307, USA. | ||
| 184 | |||
| 185 | =cut | ||
| 186 | |||
| 187 | ############################################################################### | ||
| 188 | |||
| 189 | # THE CODE STARTS HERE | ||
| 190 | |||
| 191 | ############################################################################### | ||
| 192 | |||
| 193 | # ================================ | ||
| 194 | # COMMAND LINE OPTIONS AND USAGE | ||
| 195 | # ================================ | ||
| 196 | |||
| 197 | |||
| 198 | # command line options | ||
| 199 | use Getopt::Long; | ||
| 200 | GetOptions ("help","version","target=s","token=s","plain"); | ||
| 201 | # show help option | ||
| 202 | if(defined $opt_help) | ||
| 203 | { | ||
| 204 | $opt_help=1; | ||
| 205 | &showhelp(); | ||
| 206 | exit; | ||
| 207 | } | ||
| 208 | |||
| 209 | # show version information | ||
| 210 | if(defined $opt_version) | ||
| 211 | { | ||
| 212 | $opt_version=1; | ||
| 213 | &showversion(); | ||
| 214 | exit; | ||
| 215 | } | ||
| 216 | |||
| 217 | # show minimal usage message if no arguments | ||
| 218 | if($#ARGV<1) | ||
| 219 | { | ||
| 220 | &showminimal(); | ||
| 221 | exit; | ||
| 222 | } | ||
| 223 | |||
| 224 | ############################################################################# | ||
| 225 | |||
| 226 | # ================================ | ||
| 227 | # INITIALIZATION AND INPUT | ||
| 228 | # ================================ | ||
| 229 | |||
| 230 | #$0 contains the program name along with | ||
| 231 | #the complete path. Extract just the program | ||
| 232 | #name and use in error messages | ||
| 233 | $0=~s/.*\/(.+)/$1/; | ||
| 234 | |||
| 235 | #check if the source file is specified | ||
| 236 | if(!defined $ARGV[0]) | ||
| 237 | { | ||
| 238 | print STDERR "ERROR($0): | ||
| 239 | Please specify the input SVAL2 file ...\n"; | ||
| 240 | exit 1; | ||
| 241 | } | ||
| 242 | #accept the input file name | ||
| 243 | $infile=$ARGV[0]; | ||
| 244 | |||
| 245 | #check if exists | ||
| 246 | if(!-e $infile) | ||
| 247 | { | ||
| 248 | print STDERR "ERROR($0): | ||
| 249 | SVAL2 file <$infile> doesn't exist...\n"; | ||
| 250 | exit 1; | ||
| 251 | } | ||
| 252 | #open and get handle | ||
| 253 | open(IN,$infile) || die "Error($0): | ||
| 254 | Error(code=$!) in opening input SVAL2 file <$infile>.\n"; | ||
| 255 | |||
| 256 | # ------- | ||
| 257 | # Window | ||
| 258 | # ------- | ||
| 259 | #check if the window value is specified | ||
| 260 | if(!defined $ARGV[1]) | ||
| 261 | { | ||
| 262 | print STDERR "ERROR($0): | ||
| 263 | Please specify the window size...\n"; | ||
| 264 | exit 1; | ||
| 265 | } | ||
| 266 | #accept the window size | ||
| 267 | $window=$ARGV[1]; | ||
| 268 | |||
| 269 | # ------------------- | ||
| 270 | # Target Word regex | ||
| 271 | # ------------------- | ||
| 272 | |||
| 273 | #file containing regex/s for target word | ||
| 274 | if(defined $opt_target) | ||
| 275 | { | ||
| 276 | $target_file=$opt_target; | ||
| 277 | if(!(-e $target_file)) | ||
| 278 | { | ||
| 279 | print STDERR "ERROR($0): | ||
| 280 | Target regex file <$target_file> doesn't exist.\n"; | ||
| 281 | exit 1; | ||
| 282 | } | ||
| 283 | } | ||
| 284 | else | ||
| 285 | { | ||
| 286 | $target_file="target.regex"; | ||
| 287 | if(!-e $target_file) | ||
| 288 | { | ||
| 289 | print STDERR "ERROR($0): | ||
| 290 | Please copy the target.regex file into the current directory or specify | ||
| 291 | the target regex file via --target option.\n"; | ||
| 292 | exit 1; | ||
| 293 | } | ||
| 294 | } | ||
| 295 | |||
| 296 | # ------------------------ | ||
| 297 | # creating target regex | ||
| 298 | # ------------------------ | ||
| 299 | |||
| 300 | open(REG,$target_file) || die "ERROR($0): | ||
| 301 | Error(error code=$!) in opening the target regex file <$target_file>.\n"; | ||
| 302 | |||
| 303 | while(<REG>) | ||
| 304 | { | ||
| 305 | chomp; | ||
| 306 | s/^\s+//g; | ||
| 307 | s/\s+$//g; | ||
| 308 | if(/^\s*$/) | ||
| 309 | { | ||
| 310 | next; | ||
| 311 | } | ||
| 312 | if(/^\//) | ||
| 313 | { | ||
| 314 | s/^\///; | ||
| 315 | } | ||
| 316 | else | ||
| 317 | { | ||
| 318 | print STDERR "ERROR($0): | ||
| 319 | Regular Expression <$_> should start with '/'\n"; | ||
| 320 | exit 1; | ||
| 321 | } | ||
| 322 | if(/\/$/) | ||
| 323 | { | ||
| 324 | s/\/$//; | ||
| 325 | } | ||
| 326 | else | ||
| 327 | { | ||
| 328 | print STDERR "ERROR($0): | ||
| 329 | Regular Expression <$_> should end with '/'\n"; | ||
| 330 | exit 1; | ||
| 331 | } | ||
| 332 | $target.="(".$_.")|"; | ||
| 333 | } | ||
| 334 | |||
| 335 | if(!defined $target) | ||
| 336 | { | ||
| 337 | print STDERR "ERROR($0): | ||
| 338 | No valid Perl regular expression found in the target regex file | ||
| 339 | <$target_file>.\n"; | ||
| 340 | exit 1; | ||
| 341 | } | ||
| 342 | else | ||
| 343 | { | ||
| 344 | chop $target; | ||
| 345 | } | ||
| 346 | |||
| 347 | # ----------------------- | ||
| 348 | # creating token regex | ||
| 349 | # ----------------------- | ||
| 350 | if(defined $opt_token) | ||
| 351 | { | ||
| 352 | $token_file=$opt_token; | ||
| 353 | if(!(-e $token_file)) | ||
| 354 | { | ||
| 355 | print STDERR "ERROR($0): | ||
| 356 | Token regex file <$token_file> doesn't exist.\n"; | ||
| 357 | exit 1; | ||
| 358 | } | ||
| 359 | } | ||
| 360 | else | ||
| 361 | { | ||
| 362 | $token_file="token.regex"; | ||
| 363 | if(!(-e $token_file)) | ||
| 364 | { | ||
| 365 | print STDERR "ERROR($0): | ||
| 366 | Please copy the file token.regex into the current directory | ||
| 367 | or specify the token regex file via --token option.\n"; | ||
| 368 | exit 1; | ||
| 369 | } | ||
| 370 | } | ||
| 371 | |||
| 372 | open(TOK,$token_file) || die "ERROR($0): | ||
| 373 | Error(error code=$!) in opening token regex file <$token_file>.\n"; | ||
| 374 | while(<TOK>) | ||
| 375 | { | ||
| 376 | chomp; | ||
| 377 | s/^\s+//g; | ||
| 378 | s/\s+$//g; | ||
| 379 | if(/^\s*$/) | ||
| 380 | { | ||
| 381 | next; | ||
| 382 | } | ||
| 383 | if(/^\//) | ||
| 384 | { | ||
| 385 | s/^\///; | ||
| 386 | } | ||
| 387 | else | ||
| 388 | { | ||
| 389 | print STDERR "ERROR($0): | ||
| 390 | Regular Expression <$_> should start with '/'\n"; | ||
| 391 | exit 1; | ||
| 392 | } | ||
| 393 | if(/\/$/) | ||
| 394 | { | ||
| 395 | s/\/$//; | ||
| 396 | } | ||
| 397 | else | ||
| 398 | { | ||
| 399 | print STDERR "ERROR($0): | ||
| 400 | Regular Expression <$_> should end with '/'\n"; | ||
| 401 | exit 1; | ||
| 402 | } | ||
| 403 | $token_regex.="(".$_.")|"; | ||
| 404 | } | ||
| 405 | |||
| 406 | if(!defined $token_regex) | ||
| 407 | { | ||
| 408 | print STDERR "ERROR($0): | ||
| 409 | No valid Perl regular expression found in token regex file | ||
| 410 | <$token_file>.\n"; | ||
| 411 | exit 1; | ||
| 412 | } | ||
| 413 | else | ||
| 414 | { | ||
| 415 | chop $token_regex; | ||
| 416 | } | ||
| 417 | |||
| 418 | ############################################################################## | ||
| 419 | |||
| 420 | # ========================= | ||
| 421 | # CODE SECTION | ||
| 422 | # ========================= | ||
| 423 | |||
| 424 | $tempfile="tempfile" . time() . ".windower"; | ||
| 425 | if(-e $tempfile) | ||
| 426 | { | ||
| 427 | print STDERR "ERROR($0): | ||
| 428 | Temporary file <$tempfile> already exists.\n"; | ||
| 429 | exit 1; | ||
| 430 | } | ||
| 431 | open(TEMP,">$tempfile") || die "ERROR($0): | ||
| 432 | Error(code=$!) in opening temporary internal file <$tempfile>.\n"; | ||
| 433 | |||
| 434 | $line_num=0; | ||
| 435 | while(<IN>) | ||
| 436 | { | ||
| 437 | $line_num++; | ||
| 438 | # instance start | ||
| 439 | if(/instance id\s*=\s*\"([^"]+)\"/) | ||
| 440 | { | ||
| 441 | $instance=$1; | ||
| 442 | } | ||
| 443 | # instance ends | ||
| 444 | if(/<\/instance>/) | ||
| 445 | { | ||
| 446 | undef $instance; | ||
| 447 | } | ||
| 448 | # end of context | ||
| 449 | if(/<\/context>/) | ||
| 450 | { | ||
| 451 | undef $data_start; | ||
| 452 | if(!defined $got_target) | ||
| 453 | { | ||
| 454 | print STDERR "ERROR($0): | ||
| 455 | No matching target word found in the context of instance <$instance> | ||
| 456 | in SVAL2 file <$infile>.\n"; | ||
| 457 | exit 1; | ||
| 458 | } | ||
| 459 | |||
| 460 | # actual windowing now ! | ||
| 461 | foreach $index (0..$#text_line) | ||
| 462 | { | ||
| 463 | #check if the target word | ||
| 464 | if($text_line[$index] =~ /$target/) | ||
| 465 | { | ||
| 466 | #find the lower and upper bounds for window | ||
| 467 | $lower=($index-$window)<0 ? 0 : $index-$window; | ||
| 468 | $upper=($index+$window)>$#text_line ? $#text_line : $index+$window; | ||
| 469 | # display the window words | ||
| 470 | foreach $windex ($lower..$upper) | ||
| 471 | { | ||
| 472 | print TEMP "$text_line[$windex] "; | ||
| 473 | } | ||
| 474 | } | ||
| 475 | } | ||
| 476 | print TEMP "\n"; | ||
| 477 | } | ||
| 478 | # context data | ||
| 479 | if(defined $data_start) | ||
| 480 | { | ||
| 481 | # tokenize | ||
| 482 | while(/$token_regex/) | ||
| 483 | { | ||
| 484 | $token=$&; | ||
| 485 | $_=$'; | ||
| 486 | # check if target | ||
| 487 | if($token =~ /$target/) | ||
| 488 | { | ||
| 489 | # error on multiple targets | ||
| 490 | if(defined $got_target) | ||
| 491 | { | ||
| 492 | print STDERR "ERROR($0): | ||
| 493 | Multiple target words matched in the context of instance <$instance> | ||
| 494 | in SVAL2 file <$infile>.\n"; | ||
| 495 | exit 1; | ||
| 496 | } | ||
| 497 | $got_target=1; | ||
| 498 | } | ||
| 499 | push @text_line,$token; | ||
| 500 | } | ||
| 501 | } | ||
| 502 | if(!defined $data_start && !defined $opt_plain) | ||
| 503 | { | ||
| 504 | print TEMP $_; | ||
| 505 | } | ||
| 506 | # context start | ||
| 507 | if(/<context>/) | ||
| 508 | { | ||
| 509 | $data_start=1; | ||
| 510 | if(!defined $instance) | ||
| 511 | { | ||
| 512 | print STDERR "ERROR($0): | ||
| 513 | No instance id found for the context at line <$line_num> in SVAL2 file | ||
| 514 | <$infile>.\n"; | ||
| 515 | exit 1; | ||
| 516 | } | ||
| 517 | undef $got_target; | ||
| 518 | undef @text_line; | ||
| 519 | } | ||
| 520 | } | ||
| 521 | |||
| 522 | # ----------------------- | ||
| 523 | # printing to stdout | ||
| 524 | # ----------------------- | ||
| 525 | |||
| 526 | close TEMP; | ||
| 527 | open(TEMP,$tempfile) || die "ERROR($0): | ||
| 528 | Error(code=$!) in opening temporary internal file <$tempfile>.\n"; | ||
| 529 | while(<TEMP>) | ||
| 530 | { | ||
| 531 | print; | ||
| 532 | } | ||
| 533 | close TEMP; | ||
| 534 | unlink "$tempfile"; | ||
| 535 | |||
| 536 | undef $opt_plain; | ||
| 537 | |||
| 538 | ############################################################################## | ||
| 539 | |||
| 540 | # ========================== | ||
| 541 | # SUBROUTINE SECTION | ||
| 542 | # ========================== | ||
| 543 | |||
| 544 | #----------------------------------------------------------------------------- | ||
| 545 | #show minimal usage message | ||
| 546 | sub showminimal() | ||
| 547 | { | ||
| 548 | print "Usage: windower.pl SVAL2 W"; | ||
| 549 | print "\nTYPE windower.pl --help for help\n"; | ||
| 550 | } | ||
| 551 | |||
| 552 | #----------------------------------------------------------------------------- | ||
| 553 | #show help | ||
| 554 | sub showhelp() | ||
| 555 | { | ||
| 556 | print "Usage: windower.pl SVAL2 W | ||
| 557 | |||
| 558 | Context of each instance in the given SVAL2 file is limited to W tokens around | ||
| 559 | the target word. Input and output are both in Senseval-2 format. | ||
| 560 | |||
| 561 | SVAL2 | ||
| 562 | A tokenized and preprocessed input instance file in Senseval-2 format. | ||
| 563 | |||
| 564 | W | ||
| 565 | Window size. Limits the contexts to W tokens on left and right of the | ||
| 566 | target word. | ||
| 567 | |||
| 568 | OPTIONS: | ||
| 569 | |||
| 570 | --plain | ||
| 571 | Output will be in plain text format showing context of each instance | ||
| 572 | on a single line. By default, output is in Senseval-2 format. | ||
| 573 | |||
| 574 | --target TARGETREGEX | ||
| 575 | A file containing Perl regex/s that define the target word/s. | ||
| 576 | By default, file target.regex is searched in the current directory. | ||
| 577 | |||
| 578 | --token TOKENREGEX | ||
| 579 | A file containing Perl regex/s that define valid tokens in the SVAL2 | ||
| 580 | file. By default, file token.regex is searched in the current directory. | ||
| 581 | |||
| 582 | --help | ||
| 583 | Displays this message. | ||
| 584 | |||
| 585 | --version | ||
| 586 | Displays the version information. | ||
| 587 | |||
| 588 | Type 'perldoc windower.pl' to view detailed documentation of windower.\n"; | ||
| 589 | } | ||
| 590 | |||
| 591 | #------------------------------------------------------------------------------ | ||
| 592 | #version information | ||
| 593 | sub showversion() | ||
| 594 | { | ||
| 595 | # print "windower.pl - Version 0.07\n"; | ||
| 596 | print '$Id: windower.pl,v 1.13 2008/03/29 20:52:30 tpederse Exp $'; | ||
| 597 | print "\nLimit contexts in a Senseval-2 file to N tokens around the target word\n"; | ||
| 598 | # print "Copyright (c) 2002-2005, Amruta Purandare & Ted Pedersen.\n"; | ||
| 599 | # print "Date of Last Update: 27/07/2006\n"; | ||
| 600 | } | ||
| 601 | |||
| 602 | ############################################################################# | ||
| 603 |