script/keyconvert.pl untested

filter: Statementstmt 0.0% 0 / 78 Branchbran 0.0% 0 / 32 Conditioncond 0.0% 0 / 4 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 0.0% 0 / 4 Podpod 0.0% 0 / 3 totaltotal 0.0% 0 / 123 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 keyconvert.pl - Convert Senseval-2 answer key to Senseclusters format
6  
7 =head1 SYNOPSIS
8  
9 keyconvert.pl [OPTIONS] INPUTFILE OUTFILE
10  
11 =head1 DESCRIPTION
12  
13 Converts the Senseval formatted input key file to SenseClusters' formatted
14 output key file required by the SenseClusters package.
15  
16 =head2 Required Arguments:
17  
18 =head3 INPUTFILE
19  
20 The Senseval key file. The format for Senseval key file is:
21  
22 TARGET_WORD INSTANCE_ID SENSE_ID+
23  
24 on each line showing one or more SENSE_IDs for an INSTANCE_ID.
25  
26 =head3 OUTFILE
27  
28 The file name which would have the resultant SenseClusters' formatted
29 key file. The SenseClusters' format id:
30  
31 <instance id=\"I\"\/> <sense id=\"S\"\/>+
32  
33 on each line where Sense Tags S are attached to an Instance I.
34  
35 =head2 Optional Arguments:
36  
37 =head4 --attach_P
38  
39 This will attach Sense tag P to the Sense Tag that follow it. Otherwise
40 the P tags are removed.
41  
42 =head4 --help
43  
44 Displays the quick summary of program options.
45  
46 =head4 --version
47  
48 Displays the version information.
49  
50 =head1 AUTHORS
51  
52 Amruta Purandare, University of Pittsburgh
53  
54 Ted Pedersen, University of Minnesota, Duluth
55 tpederse @ d.umn.edu
56  
57 =head1 COPYRIGHT
58  
59 Copyright (c) 2002-2008, Amruta Purandare and Ted Pedersen
60  
61 This program is free software; you can redistribute it and/or modify it under
62 the terms of the GNU General Public License as published by the Free Software
63 Foundation; either version 2 of the License, or (at your option) any later
64 version.
65  
66 This program is distributed in the hope that it will be useful, but WITHOUT
67 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
68 FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
69  
70 You should have received a copy of the GNU General Public License along with
71 this program; if not, write to
72  
73 The Free Software Foundation, Inc.,
74 59 Temple Place - Suite 330,
75 Boston, MA 02111-1307, USA.
76  
77 =cut
78  
79  
80 #############################################################################
81 #
82 # PROGRAM NAME- keyconvert.pl (A Component of SenseClusters Package)
83 #
84 #############################################################################
85  
86 # ================================
87 # COMMAND LINE OPTIONS AND USAGE
88 # ================================
89  
90 # show minimal usage message if no arguments
91 if($#ARGV<0)
92 {
93 &showminimal();
94 exit;
95 }
96  
97 # command line options
98 use Getopt::Long;
99 GetOptions ("help","version","attach_P");
100 # show help option
101 if(defined $opt_help)
102 {
103 $opt_help=1;
104 &showhelp();
105 exit;
106 }
107  
108 # show version information
109 if(defined $opt_version)
110 {
111 $opt_version=1;
112 &showversion();
113 exit;
114 }
115  
116 #############################################################################
117  
118 # ================================
119 # INITIALIZATION AND INPUT
120 # ================================
121  
122 #$0 contains the program name along with
123 #the complete path. Extract just the program
124 #name and use in error messages
125 $0=~s/.*\/(.+)/$1/;
126  
127 if(!defined $ARGV[0])
128 {
129 print STDERR "ERROR($0):
130 Please specify the Senseval KEY file name...\n";
131 exit;
132 }
133 #accept the input file name
134 $infile=$ARGV[0];
135 if(!-e $infile)
136 {
137 print STDERR "ERROR($0):
138 Source file <$infile> doesn't exist...\n";
139 exit;
140 }
141 open(IN,$infile) || die "Error($0):
142 Error(code=$!) in opening <$infile> file.\n";
143  
144 if(!defined $ARGV[1])
145 {
146 print STDERR "ERROR($0):
147 Please specify the Output KEY file name...\n";
148 exit;
149 }
150  
151 #accept the output file name
152 $outfile=$ARGV[1];
153 if(-e $outfile)
154 {
155 print STDERR "Warning($0):
156 Output file <$outfile> already exists, overwrite (y/n)? ";
157 $ans=<STDIN>;
158 if(!($ans=~/y|Y/))
159 {
160 exit;
161 }
162 }
163 open(OUT,">$outfile") || die "Error($0):
164 Error(code=$!) in opening OUTPUT KEY file <$outfile>.\n";
165  
166 ##############################################################################
167  
168 $line_num=0;
169 while($line=<IN>)
170 {
171 $line_num++;
172 # trimming extra spaces
173 chomp $line;
174 $line=~s/\s+$//g;
175 $line=~s/^\s+//g;
176 $line=~s/\s+/ /g;
177 # handling blank lines
178 if($line=~/^\s*$/)
179 {
180 next;
181 }
182 ($word,$instance_id,@senses)=split(/\s+/,$line);
183 undef $word;
184 if(!defined $instance_id)
185 {
186 print STDERR "ERROR($0):
187 Line <$line_num> in SOURCE KEY file <$infile> doesn't contain an
188 Instance-Id.\n";
189 exit;
190 }
191 if(defined %{$instance_sense{$instance_id}})
192 {
193 print "ERROR($0):
194 Instance-Id <$instance_id> occurs more than once in the SOURCE KEY
195 file <$infile>.\n";
196 exit;
197 }
198 if(!($#senses>=0))
199 {
200 print STDERR "ERROR($0):
201 Line <$line_num> in SOURCE KEY file <$infile> doesn't contain valid
202 Sense Tags.\n";
203 exit;
204 }
205 $ptag=0;
206 foreach $sense (@senses)
207 {
208 if($ptag==1)
209 {
210 if(defined $opt_attach_P)
211 {
212 $sense="P_".$sense;
213 $ptag=0;
214 }
215 $ptag=0;
216 }
217 if($sense eq "P")
218 {
219 $ptag=1;
220 }
221 if($ptag!=1)
222 {
223 $instance_sense{$instance_id}{$sense}++;
224 }
225 }
226 }
227  
228 undef $opt_attach_P;
229 #----------------------
230 #Write into a KEY file
231 #----------------------
232 foreach $instance (sort keys %instance_sense)
233 {
234 print OUT "<instance id=\"$instance\"\/> ";
235 foreach $sense (sort keys %{$instance_sense{$instance}})
236 {
237 print OUT "<sense id=\"$sense\"\/> ";
238 }
239 print OUT "\n";
240 }
241  
242 ##############################################################################
243  
244 # ==========================
245 # SUBROUTINE SECTION
246 # ==========================
247  
248 #-----------------------------------------------------------------------------
249 #show minimal usage message
250 sub showminimal()
251 {
252 print "Usage: keyconvert.pl [OPTIONS] SOURCE DESTINATION";
253 print "\nTYPE keyconvert.pl --help for help\n";
254 }
255  
256 #-----------------------------------------------------------------------------
257 #show help
258 sub showhelp()
259 {
260 print "Usage: keyconvert.pl [OPTIONS] SOURCE DESTINATION
261 Converts a SOURCE KEY file from Senseval format to DESTINATION KEY file
262 in SenseCluster's format.
263  
264 SOURCE
265 Specify the Senseval KEY file name. SOURCE should follow the standard
266 Senseval keyfile format with
267 TARGET_WORD INSTANCE_ID SENSE_ID+
268 on each line showing one or more SENSE_IDs for an INSTANCE_ID.
269  
270 DESTINATION
271 Specify the DESTINATION KEY file name where program can print the
272 equivalent KEY file in SenseClusters Format showing
273 <instance id=\"I\"\/> <sense id=\"S\"\/>+
274 on each line where Sense Tags S are attached to an Instance I.
275  
276 OPTIONS:
277 --attach_P
278 This will attach Sense tag P to the Sense Tag that follow it. Otherwise
279 the P tags are removed.
280 --help
281 To display this message.
282 --version
283 To display the version information.\n";
284 }
285  
286 #------------------------------------------------------------------------------
287 #version information
288 sub showversion()
289 {
290 # print "keyconvert.pl - Version 0.03";
291 print '$Id: keyconvert.pl,v 1.12 2013/06/25 14:58:11 tpederse Exp $';
292 print "\nConverts Senseval-2 KEY file into SenseCluster's KEY file\n";
293 # print "\nCopyright (C) 2002, Ted Pedersen and Amruta Purandare.\n";
294 # print "Date of Last Update: 03/17/2003\n";
295 }
296  
297 #############################################################################
298