script/prepare_sval2.pl untested

filter: Statementstmt 0.0% 0 / 146 Branchbran 0.0% 0 / 80 Conditioncond 0.0% 0 / 38 MC/DCmcdc 0.0% 0 / 24 Subroutinesub 0.0% 0 / 5 Podpod 0.0% 0 / 3 totaltotal 0.0% 0 / 296 CC 1 SCAR 6.9
CC
1
Cov
0%
showminimal
2.0
showhelp
2.0
showversion
2.0
CRAP 2.0
1 #!/usr/local/bin/perl -w
2  
3 =head1 NAME
4  
5 prepare_sval2.pl - Makes sure Senseval-2 data is cleaned and has sense
6 tags prior to invocation of SenseClusters
7  
8 =head1 SYNOPSIS
9  
10 prepare_sval2.pl [Options] SOURCE
11  
12 Here is a Senseval-2 file that is untagged
13  
14 cat notags.txt
15  
16 Output =>
17  
18 <corpus lang="english">
19 <lexelt item="line">
20 <instance id="0">
21 <context>
22 he played on the offensive <head>line</head> in college
23 </context>
24 </instance>
25 <instance id="1">
26 <context>
27 i think the phone <head>line</head> is down
28 </context>
29 </instance>
30 </lexelt>
31 </corpus>
32  
33 Here is a key file that contains sense tags for these instances:
34  
35 cat key.txt
36  
37 Output =>
38  
39 <instance id="0"/> <sense id="formation"/>
40 <instance id="1"/> <sense id="cable"/>
41  
42 Now we can apply the tags in the key file to the previously untagged
43 instances:
44  
45 prepare_sval2.pl notags.txt --key key.txt
46  
47 Output =>
48  
49 <corpus lang="english" tagged="NO">
50 <lexelt item="line">
51 <instance id="0">
52 <answer instance="0" senseid="formation"/>
53 <context>
54 he played on the offensive <head>line</head> in college
55 </context>
56 </instance>
57 <instance id="1">
58 <answer instance="1" senseid="cable"/>
59 <context>
60 i think the phone <head>line</head> is down
61 </context>
62 </instance>
63 </lexelt>
64 </corpus>
65  
66 Type C<prepare_sval2.pl --help> for quick summary of options
67  
68 =head1 DESCRIPTION
69  
70 This program prepares Senseval-2 Data for SenseClusters experiments by
71 making sure that all instances have sense tags. Sense tags can be
72 applied from a separate key file, and if any instances do not have
73 tags, then a NOTAG is inserted. This program also deals with P tags
74 that may exist in some Senseval data. The P tag indicates that the
75 target word is a proper noun. In may cases P tagged instances are
76 ommited from experiments since they are a different kind of sense. If
77 "bush" were the target word, some instances might refer to "George
78 Bush", which may not be one of the senses we wish to evaluate. Finally,
79 this program can also deal with satellite tags that exist in some
80 Senseval data. When the target word is a verb, in some cases it may have
81 a satellite (particle), that we may or may not want to consider as a
82 part of the target word. The satellite tags have identifiers in them
83 that may cause parsing trouble, so they are often removed.
84  
85 =head1 INPUT
86  
87 =head2 Required Arguments:
88  
89 =head4 SOURCE
90  
91 A Senseval-2 formatted Data file that is to be prepared for the SenseClusters
92 experiments.
93  
94 =head2 Optional Arguments:
95  
96 =head4 --key KEY
97  
98 Sense Tagging mechanism in prepare_sval2.pl -
99  
100 prepare_sval2.pl makes sure that all SOURCE instances are tagged with some
101 answer tags (or NOTAGs at least).
102  
103 If the sense tags are found in the same SOURCE file, these will
104 be retained, however if the SOURCE instances are not tagged, instances will be
105 either attached "NOTAG"s or will be attached the sense tags given in the
106 separate KEY file.
107  
108 A KEY file that has true answer keys of the SOURCE instances can be provided
109 via --key option. If the SOURCE instances are not sense tagged, they will be
110 tagged with the sense tags as given in the KEY file.
111  
112 KEY file should be in SenseClusters format showing
113  
114 <instance id="I"/> [<sense id="S"/>]+
115  
116 on each line where an instance id is followed by its true sense ids on a single line.
117  
118 prepare_sval2 takes into account following anamolies in SOURCE/KEY -
119  
120 =over 4
121  
122 =item 1.
123  
124 If the 1st SOURCE instance is sense tagged, it assumes that SOURCE is sense
125 tagged and will disable the KEY file option. If some of the SOURCE instances
126 are not tagged, regardless of whether they have keys in KEY file or not,
127 these are given "NOTAG"s.
128  
129 =item 2.
130  
131 If the 1st SOURCE instance is not sense tagged, it assumes that SOURCE is
132 untagged and will give an error if any SOURCE instance is found sense tagged
133 in the SOURCE file.
134  
135 =item 3.
136  
137 If the 1st SOURCE instance is not sense tagged and has an entry in the KEY
138 file, it will enable the KEY file and will attach the instances with their
139 answer keys as given in the KEY file. Any instance that doesn't have an answer
140 key in the KEY file is attached "NOTAG".
141  
142 =item 4.
143  
144 If the 1st SOURCE instance is not sense tagged and doesn't have an entry in
145 the KEY file, KEY file will be disabled and no instance will be attached a tag
146 from the KEY file. All instances are given "NOTAG"s.
147  
148 =back
149  
150 =head4 --attachP
151  
152 P tag handling mechanism in prepare_sval2.pl -
153  
154 prepare_sval2.pl by default removes the sense tags that have value P.
155 According to Senseval-2 standard, these are not true sense tags but indicate
156 that the target word is a proper noun.
157  
158 --attachP option will attach a P tag to an immediately following sense tag for
159 the same instance.
160  
161 e.g. If --attachP is selected,
162  
163 <instance id="art.40012" docsrc="bnc_A0E_130">
164 <answer instance="art.40012" senseid="P"/>
165 <answer instance="art.40012" senseid="arts%1:09:00::"/>
166  
167 will be modified to
168  
169 <instance id="art.40012" docsrc="bnc_A0E_130">
170 <answer instance="art.40012" senseid="P_arts%1:09:00::"/>
171  
172 and if --attachP is not selected, by default P tag will be removed as
173  
174 <instance id="art.40012" docsrc="bnc_A0E_130">
175 <answer instance="art.40012" senseid="arts%1:09:00::"/>
176
177  
178 =head4 --modifysat
179  
180 This switch if selected will remove the satellite tag ids from <head sats="
181 ID"/> and <sat id="ID"/> tags, retaining basic <head> and <sat> tag
182 information.
183  
184 e.g. by selecting --modifysat,
185  
186 Perhaps he 'd have <head sats="call_for.018:0">called</head> <sat
187 id="call_for.018:0">for</sat> a decentralized political and economic
188 system
189  
190 will be transformed to
191  
192 perhaps he 'd have <head> called </head> <sat> for </sat> a
193 decentralized political and economic system
194  
195 By not selecting --modifysat, the satellite ids would be retained.
196  
197 =head4 --nolc
198  
199 prepare_sval2 converts everything to lowercase by default. Select
200 this switch to not do any case conversion.
201  
202 =head4 --help
203  
204 Displays this message.
205  
206 =head4 --version
207  
208 Displays the version information.
209  
210 =head1 OUTPUT
211  
212 Output will be a Senseval-2 file displayed to stdout.
213  
214 =head1 AUTHORS
215  
216 Amruta Purandare, University of Pittsburgh
217  
218 Ted Pedersen, University of Minnesota, Duluth
219 tpederse at d.umn.edu
220  
221 =head1 COPYRIGHT
222  
223 Copyright (c) 2002-2008, Amruta Purandare and Ted Pedersen
224  
225 This program is free software; you can redistribute it and/or modify it under
226 the terms of the GNU General Public License as published by the Free Software
227 Foundation; either version 2 of the License, or (at your option) any later
228 version.
229  
230 This program is distributed in the hope that it will be useful, but WITHOUT
231 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
232 FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
233  
234 You should have received a copy of the GNU General Public License along with
235 this program; if not, write to
236  
237 The Free Software Foundation, Inc.,
238 59 Temple Place - Suite 330,
239 Boston, MA 02111-1307, USA.
240  
241 =cut
242  
243 #############################################################################
244  
245 # THE CODE STARTS HERE
246  
247 use utf8;
248  
249 ###############################################################################
250  
251 # ================================
252 # COMMAND LINE OPTIONS AND USAGE
253 # ================================
254  
255 # show minimal usage message if no arguments
256 if($#ARGV<0)
257 {
258 &showminimal();
259 exit;
260 }
261  
262 # command line options
263 use Getopt::Long;
264 GetOptions ("help","version","attachP","modifysat","key=s","nolc");
265 # show help option
266 if(defined $opt_help)
267 {
268 $opt_help=1;
269 &showhelp();
270 exit;
271 }
272  
273 # show version information
274 if(defined $opt_version)
275 {
276 $opt_version=1;
277 &showversion();
278 exit;
279 }
280  
281 #############################################################################
282  
283 # ================================
284 # INITIALIZATION AND INPUT
285 # ================================
286  
287 #$0 contains the program name along with
288 #the complete path. Extract just the program
289 #name and use in error messages
290 $0=~s/.*\/(.+)/$1/;
291  
292 if(!defined $ARGV[0])
293 {
294 print STDERR "ERROR($0):
295 Please specify the Senseval-2 Data file name...\n";
296 exit;
297 }
298 #accept the input file name
299 $infile=$ARGV[0];
300 if(!-e $infile)
301 {
302 print STDERR "ERROR($0):
303 Source file <$infile> doesn't exist...\n";
304 exit;
305 }
306 open(IN,$infile) || die "Error($0):
307 Error(code=$!) in opening <$infile> file.\n";
308  
309 ##############################################################################
310  
311 # ===========================
312 # KEY file handling
313 # ===========================
314  
315 # if the sense tags of the instances in Source file
316 # are provided in KEY file, we attach them to source
317 # instances
318 if(defined $opt_key)
319 {
320 $keyfile=$opt_key;
321  
322 if(!-e $keyfile)
323 {
324 print STDERR "ERROR($0):
325 KEY File <$keyfile> doesn't exist.\n";
326 exit;
327 }
328  
329 open(KEY,$keyfile) || die "Error($0):
330 Error(code=$!) in opening file <$keyfile>.\n";
331 $line_num=0;
332 while(<KEY>)
333 {
334 $line_num++;
335 chomp;
336 # trimming extra spaces from beginning and end
337 s/^\s+//g;
338 s/\s+$//g;
339 s/\s+/ /g;
340 # handling blank lines
341 if(/^\s*$/)
342 {
343 next;
344 }
345 #get the instance id from the key file
346 if(/<instance id=\"([^\"]+)\"\/>/)
347 {
348 $instance=$1;
349 $_=$';
350 if(defined $instance_hash{$instance})
351 {
352 print STDERR "ERROR($0):
353 Instance-Id <$instance> is repeated in the KEY file <$keyfile>.\n";
354 exit;
355 }
356 $instance_hash{$instance}++;
357 }
358 else
359 {
360 print STDERR "ERROR($0):
361 Line <$line_num> in the KEY file <$keyfile> doesn't contain any
362 <instance> tag.\n";
363 exit;
364 }
365 # get sense ids now
366 while(/<sense id=\"([^\"]+)\"\/>/)
367 {
368 $sense=$1;
369 $_=$';
370 if(defined $key_tab{$instance}{$sense})
371 {
372 print "ERROR($0):
373 The Instance-Id Sense-Tag pair <$instance $sense> is repeated in
374 the KEY file <$keyfile>.\n";
375 exit;
376 }
377 # making an entry for the instance in the keytab
378 $key_tab{$instance}{$sense}=1;
379 }
380 # checking if this instance has atleast one sense tag
381 if(!defined $key_tab{$instance})
382 {
383 print STDERR "ERROR($0):
384 No Sense Id found at line <$line_num> in KEY file <$keyfile>.\n";
385 exit;
386 }
387 }
388 }
389  
390 ##############################################################################
391  
392 #---------------------
393 #creating a TEMP file
394 #---------------------
395 #we hold the output in tempfile till the program terminates
396 #without an error. In case of error, the tempfile would be
397 #retained and will hold partial output of the program.
398  
399 #use the system_defined date for unique name for tempfile
400  
401 #$date_time=scalar localtime;
402 #@time_elements=split(/\s+/,$date_time);
403 #$tempfile=join "_",@time_elements;
404 $tempfile="temp".time().".prepare_sval2";
405 open(TEMP,">$tempfile")||die"ERROR($0):
406 Internal System Error(code=$!).\n";
407  
408 ##############################################################################
409  
410 # tag_flag=0 if data is untagged
411 # =1 if tagged
412 undef $tag_flag;
413 undef $data_start;
414 $line_num=0;
415 # if tag=1, sense tags must be found for all instances
416 $tag_found=0;
417 while(<IN>)
418 {
419 $line_num++;
420 # KEY handling
421 if(/instance id=\"([^\"]+)\"/)
422 {
423 $instance=$1;
424 # we access key table only if data in untagged
425 # otherwise key entries are ignored
426 if(!defined $tag_flag || $tag_flag==0)
427 {
428 if(defined $key_tab{$instance})
429 {
430 # attach_key = 1
431 # only if all instances have tags in KEY
432 # =0 otherwise
433 if(!defined $attach_key)
434 {
435 $attach_key=1;
436 }
437 foreach $sense (keys %{$key_tab{$instance}})
438 {
439 $instance_sense{$instance}{$sense}=1;
440 }
441 }
442 else
443 {
444 if(!defined $attach_key)
445 {
446 $attach_key=0;
447 }
448 }
449 }
450 }
451 if(/sense\s*id=\"([^\"]+)\"/)
452 {
453 if(!defined $tag_flag)
454 {
455 $tag_flag=1;
456 }
457 # error if sense id is not expected
458 elsif($tag_flag==0)
459 {
460 print STDERR "ERROR($0):
461 No Sense Id is expected in Source file <$infile> for instance
462 <$instance> as all earlier instances are untagged.\n";
463 exit;
464 }
465 if($1 ne "P")
466 {
467 $tag_found=1;
468 }
469 }
470 if(defined $data_start && !defined $opt_nolc)
471 {
472 tr/A-Z/a-z/;
473 }
474 if(/<context>/)
475 {
476 $data_start=1;
477 if(!defined $tag_flag)
478 {
479 $tag_flag=0;
480 }
481 # putting no tag if some instances aren't tagged
482 elsif($tag_flag==1 && $tag_found==0)
483 {
484 print TEMP "<answer instance=\"$instance\" senseid=\"NOTAG\"\/>\n";
485 }
486 $tag_found=0;
487 }
488 if(/<\/context>/)
489 {
490 undef $data_start;
491 undef $ptag;
492 }
493 if(defined $ptag && ($_ !~ /senseid=\"[^\"]+\"/))
494 {
495 print STDERR "ERROR($0):
496 P tag is not followed by any Sense tag at line<$line_num> in Senseval-2
497 file <$infile>\n.";
498 exit;
499 }
500 # by default remove P tag
501 if((!defined $opt_attachP) && /senseid=\"P\"/)
502 {
503 next;
504 }
505 # if --attachP defined attach P tag
506 if(defined $opt_attachP && /senseid=\"P\"/)
507 {
508 $ptag=1;
509 next;
510 }
511 if(defined $ptag && /senseid=\"([^\"]+)\"/)
512 {
513 $sense="P_".$1;
514 s/sense\s*id=\"$1\"/senseid=\"$sense\"/;
515 undef $ptag;
516 }
517 # if --modifysat used, remove sat ids from sat and head tags
518 if(defined $opt_modifysat && /<head sats=\"[^\"]+\">/)
519 {
520 s/<head sats=\"[^\"]+\">/<head>/g;
521 }
522 if(defined $opt_modifysat && /<sat id=\"[^\"]+\">/)
523 {
524 s/<sat id=\"[^\"]+\">/<sat>/g;
525 }
526 print TEMP $_;
527 }
528  
529 undef $opt_attachP;
530 undef $opt_modifysat;
531 undef $opt_nolc;
532  
533 #now display to STDOUT
534 close TEMP;
535 open(TEMP,$tempfile) || die "ERROR($0):
536 Internal System Error(code=$!).\n";
537 # read temp file and display with extra information
538 while(<TEMP>)
539 {
540 if(/<corpus\s*(.*)>/)
541 {
542 if($tag_flag==0)
543 {
544 print "<corpus $1 tagged=\"NO\">\n";
545 }
546 elsif($tag_flag==1)
547 {
548 print "<corpus $1 tagged=\"YES\">\n";
549 }
550 else
551 {
552 print STDERR "ERROR($0):
553 Error in Processing Data <$infile>.\n";
554 exit;
555 }
556 }
557 elsif(/instance id=\"([^\"]+)\"/)
558 {
559 print;
560 $instance=$1;
561 # data untagged - either attach tag from KEY or put NOTAG
562 if($tag_flag==0)
563 {
564 # get tag from the KEY file
565 if(defined $attach_key && $attach_key==1)
566 {
567 if(defined $instance_sense{$instance})
568 {
569 foreach $sense (keys %{$instance_sense{$instance}})
570 {
571 if($sense ne "P")
572 {
573 print "<answer instance=\"$instance\" senseid=\"$sense\"\/>\n";
574 }
575 }
576 }
577 else
578 {
579 print "<answer instance=\"$instance\" senseid=\"NOTAG\"\/>\n";
580 }
581 }
582 # put tag as NOTAG
583 else
584 {
585 print "<answer instance=\"$instance\" senseid=\"NOTAG\"\/>\n";
586 }
587 }
588 }
589 else
590 {
591 print;
592 }
593 }
594 #remove the tempfile
595 unlink "$tempfile";
596  
597 ##############################################################################
598  
599 # ==========================
600 # SUBROUTINE SECTION
601 # ==========================
602  
603 #-----------------------------------------------------------------------------
604 #show minimal usage message
605 sub showminimal()
606 {
607 print "Usage: prepare_sval2.pl [OPTIONS] SOURCE";
608 print "\nTYPE prepare_sval2.pl --help for help\n";
609 }
610  
611 #-----------------------------------------------------------------------------
612 #show help
613 sub showhelp()
614 {
615 print "Usage: prepare_sval2.pl [OPTIONS] SOURCE
616  
617 Prepares Senseval-2 Data by converting context data to lower case and some
618 other preprocessing tasks like attaching sense tags, handling P tags and Sat
619 tags. The modified file is displayed to stdout.
620  
621 Required Parameters -
622 SOURCE
623 Specify Senseval-2 Data file.
624  
625 Optional Parameters:
626 --key KEY
627 Tags SOURCE instances with their correct answer tags if these are
628 provided in a KEY file. The format of a KEY file should show
629 <instance id=\"I\"\/> [<sense id=\"S\"\/>]+
630 where an Instance-Id is followed by its true sense tag/s on each line.
631  
632 --attachP
633 Attaches P tags to the Sense Tags immediately following them. By
634 default, P tags are removed since they indicate proper nouns.
635 Note: attachP doesn't work when answer tags are provided in KEY file.
636 But an option --attachP is provided in keyconvert.pl program that
637 attaches P tags while converting format of KEY file to SenseClusters
638 format.
639  
640 --modifysat
641 Modifies satellite and head tags containing satellite ids like
642 <head sats> or <sat id>, by replacing them with markers <head> and
643 <sat>.
644  
645 --nolc
646 prepare_sval2.pl converts all characters to lowercase by default.
647 Select --nolc switch not to do any case conversion.
648
649 --help
650 To display this message.
651  
652 --version
653 To display the version information.\n";
654 }
655  
656 #------------------------------------------------------------------------------
657 #version information
658 sub showversion()
659 {
660 # print "prepare_sval2.pl - Version 0.19\n";
661 print '$id$';
662 print "\nEnsure Senseval-2 data is sense tagged and cleaned\n";
663 # print "\nCopyright (c) 2002-2005, Amruta Purandare, Ted Pedersen.\n";
664 # print "Date of Last Update: 07/18/2003\n";
665 }
666  
667 #############################################################################
668