script/label.pl untested

filter: Statementstmt 0.0% 0 / 100 Branchbran 0.0% 0 / 28 Conditioncond 0.0% 0 / 5 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 0.0% 0 / 5 Podpod 0.0% 0 / 3 totaltotal 0.0% 0 / 144 CC 1 SCAR 6.9
CC
1
Cov
0%
minimal
2.0
showhelp
2.0
showversion
2.0
CRAP 2.0
1 #!/usr/local/bin/perl -w
2  
3 =head1 NAME
4  
5 label.pl - Assign labels to clusters in a confusion matrix to maximize agreement
6  
7 =head1 SYNOPSIS
8  
9 label.pl [OPTIONS] PRELABEL
10  
11 Type C<label.pl --help> for a quick summary of options
12  
13 =head1 DESCRIPTION
14  
15 Labels the discovered clusters with sense tags such that maximum number of
16 contexts are correctly assigned.
17  
18 =head1 INPUT
19  
20 =head2 Required Arguments:
21  
22 PRELABEL
23  
24 Should be the output of cluto2label.pl.
25  
26 Sample CLUTO2LABEL format
27  
28 2
29 // cord phone text div
30 C0: 4 3 0 0
31 C1: 2 2 2 2
32 C2: 1 3 3 2
33  
34 where the 1st line shows the number of unclustereted instances = 2
35  
36 2nd line shows a space separated list of sense classes starting with // mark.
37  
38 Each line thereafter shows the sense distribution of the instances belonging
39 to each discovered cluster in the form of a cluster by sense distribution
40 matrix. A cell value at (i,j) in the matrix shows the number of instances
41 belonging to cluster Ci that have the sense tag Sj.
42  
43 Note that each row begins with the cluster id that precedes a colon (:).
44 Also, the number of sense classes on 2nd line should be same as the number
45 of columns in the cluster by sense distribution table.
46  
47 =head2 Optional Arguments:
48  
49 =head3 --help
50  
51 Displays this message.
52  
53 =head3 --version
54  
55 Displays the version information.
56  
57 =head1 OUTPUT
58  
59 Output shows the sense labels attached to each of the discovered
60 clusters along with the score. Score tells the percentage of the total
61 number of instances correctly clustered if the clusters are tagged with
62 the sense labels as suggested.
63  
64 Example :
65  
66 Prelabel file =>
67  
68 0
69 // cord divi form phon prod text
70 C0: 35 26 44 18 23 43
71 C1: 64 34 50 43 57 52
72 C2: 0 3 1 2 0 3
73 C3: 0 0 2 31 0 0
74 C4: 1 28 0 4 6 0
75 C5: 0 9 3 2 14 2
76  
77 Label Output =>
78  
79 ClusterID -> SenseID
80 C0 -> form
81 C1 -> cord
82 C2 -> text
83 C3 -> phon
84 C4 -> divi
85 C5 -> prod
86 Score = 30.67
87  
88 shows that
89  
90 cluster C0 represents the 'form' sense
91 cluster C1 represents the 'cord' sense
92 cluster C2 represents the 'text' sense
93 cluster C3 represents the 'phon' sense
94 cluster C4 represents the 'divi' sense
95 and cluster C5 represents the 'prod' sense
96  
97 Also, 30.67 % of the total instances are in their right sense classes
98 if the clusters are tagged with this labeling scheme.
99  
100 =cut
101  
102 # ===============================
103 # COMMAND LINE OPTIONS AND USAGE
104 # ===============================
105  
106 use Algorithm::Munkres;
107  
108 use Getopt::Long;
109 GetOptions ("help","version");
110  
111 #command option for help
112 if(defined $opt_help)
113 {
114 $opt_help=1;
115 &showhelp();
116 exit;
117 }
118 #version information
119 if(defined $opt_version)
120 {
121 $opt_version=1;
122 &showversion();
123 exit;
124 }
125  
126 #show minimal usage
127 if($#ARGV<0)
128 {
129 &minimal();
130 exit;
131 }
132  
133 #truncate $0 which contains the complete path to
134 #the program. Keep just the program name
135 #this is used in the error messages
136 $0=~s/.*\/(.+)/$1/;
137  
138 # input file
139 if(defined $ARGV[0])
140 {
141 $infile=$ARGV[0];
142 if(-e $infile)
143 {
144 open(IN,$infile) || die "Error($0): Error(code=$!) in opening file <$infile>.\n";
145 }
146 else
147 {
148 print STDERR "ERROR($0): PRELABEL file <$infile> doesn't exist...\n";
149 exit;
150 }
151 }
152 else
153 {
154 print STDERR "ERROR($0): Please specify the PRELABEL file name...\n";
155 exit;
156 }
157  
158 # first line of prelabel file should show number of instances thrown or
159 # unclustered
160 # label.pl should just pass this information to next program that computes
161 # precision and recall
162 $thrown=<IN>;
163 if(!defined $thrown)
164 {
165 print STDERR "ERROR($0):
166 1st line in the PRELABEL file <$infile> should show
167 the number of instances unclustered or 0.\n";
168 exit;
169 }
170 chomp $thrown;
171 if(!($thrown=~/^\s*\d+\s*$/))
172 {
173 print STDERR "ERROR($0):
174 1st line in the PRELABEL file <$infile> should show
175 the number of instances unclustered or 0.\n";
176 exit;
177 }
178  
179 # 2nd line of prelabel output should list all the Sense Classes
180 # senses should be space separated
181 $sense_string=<IN>;
182 if(defined $sense_string && $sense_string=~/\/\//)
183 {
184 $sense_string=$';
185 $sense_string=~s/\s+$//g;
186 $sense_string=~s/^\s+//g;
187 $sense_string=~s/\s+/ /g;
188 # stores all sense classes listed on this line
189 @all_senses=split(/\s+/,$sense_string);
190 }
191 else
192 {
193 print STDERR "ERROR($0):
194 2nd line in the input file <$infile> should list
195 the sense labels starting with //.\n";
196 exit;
197 }
198  
199 # accept matrix entries row wise
200 $i=0;
201 # total is the total #instances
202 $total=0;
203 # read the input file with each row on each line
204 $line_num=0;
205 while(<IN>)
206 {
207 chomp;
208 s/\s+$//g;
209 s/^\s+//g;
210 s/\s+/ /g;
211 if(/^\s*$/)
212 {
213 next;
214 }
215  
216 # we use cluster ids only while printing
217 # the final output
218 # during processing, we use the serial
219 # numbers 0,1,... for clusters in the order
220 # as they appear in the prelabel file
221 ($cid,$row)=split(/\s*:\s*/);
222 push @cluster_ids,$cid;
223
224 #extract the matrix cells
225 @row_elements=split(/\s+/,$row);
226  
227 if($#row_elements!=$#all_senses)
228 {
229 print STDERR "ERROR($0):
230 Number of columns (". scalar(@row_elements) . ") at line <$line_num>
231 in PRELABEL file <$infile> doesn't match the number of senses (" .
232 scalar(@all_senses) . ") specified on Line 2 of the same file.\n";
233 exit;
234 }
235
236 for($cnt = 0; $cnt <= $#row_elements; $cnt++)
237 {
238 if($row_elements[$cnt]!~/^[0-9]+$/)
239 {
240 print STDERR "ERROR($0): Line <" . $line_num+1 . "> in PRELABEL file <$infile> contains a non-integer matrix value.\n";
241 exit;
242 }
243 else
244 {
245 $inp_mat[$line_num][$cnt] = -1 * $row_elements[$cnt];
246 $total += $row_elements[$cnt];
247 }
248 }
249  
250 $line_num++;
251 }
252  
253 my @soln_mat = ();
254  
255 assign(\@inp_mat, \@soln_mat);
256 $clus_total = 0;
257  
258 print "ClusterID -> SenseID\n";
259 for($i=0;$i<=$#inp_mat;$i++)
260 {
261 if(defined $inp_mat[$i][$soln_mat[$i]])
262 {
263 $clus_total += $inp_mat[$i][$soln_mat[$i]];
264 }
265 if(defined $all_senses[$soln_mat[$i]])
266 {
267 print $cluster_ids[$i] . " -> " . $all_senses[$soln_mat[$i]] . "\n";
268 }
269  
270 }
271 if($total != 0)
272 {
273 $score = $clus_total/$total * -100;
274 $score = sprintf("%.2f",$score);
275 print "Score = $score\n";
276 }
277 else
278 {
279 print "Score = NA\n";
280  
281 }
282  
283  
284 #show minimal usage message
285 sub minimal()
286 {
287 print "Usage: label.pl [OPTIONS] PRELABEL";
288 print "\nTYPE label.pl --help for help\n";
289 }
290  
291 #show help
292 sub showhelp()
293 {
294 print "Usage: label.pl [OPTIONS] PRELABEL\n";
295 print "Labels the discovered clusters with sense tags such\n";
296 print "that maximum number of contexts are correctly assigned.\n";
297  
298 print "\nPRELABEL\n";
299 print "Should be an output created by cluto2label.pl\n";
300 print "showing a cluster by sense distribution matrix.\n\n";
301  
302 print "OPTIONS:\n";
303  
304 print "--help
305 Displays this message.\n";
306  
307 print "--version
308 Displays the version information.\n";
309 }
310  
311 #version information
312 sub showversion()
313 {
314 print '$Id: label.pl,v 1.15 2008/03/30 05:06:07 tpederse Exp $';
315 # print "\nCopyright (C) 2002-2006, Ted Pedersen, Amruta Purandare, & Anagha Kulkarni\n";
316 # print "label.pl - Version 0.11\n";
317 print "\nLabel discovered clusters with sense tags to maximize agreement\n";
318 # print "Date of Last Update: 11/30/2004\n";
319
320 }
321  
322 =head1 AUTHORS
323  
324 Ted Pedersen, University of Minnesota, Duluth
325 tpederse at d.umn.edu
326  
327 Amruta Purandare, University of Pittsburgh
328  
329 Anagha Kukarni, Carnegie-Mellon University
330  
331 =head1 COPYRIGHT
332  
333 Copyright (c) 2002-2008, Ted Pedersen, Amruta Purandare, Anagha Kulkarni
334  
335 This program is free software; you can redistribute it and/or modify it under
336 the terms of the GNU General Public License as published by the Free Software
337 Foundation; either version 2 of the License, or (at your option) any later
338 version.
339  
340 This program is distributed in the hope that it will be useful, but WITHOUT
341 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
342 FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
343  
344 You should have received a copy of the GNU General Public License along with
345 this program; if not, write to
346  
347 The Free Software Foundation, Inc.,
348 59 Temple Place - Suite 330,
349 Boston, MA 02111-1307, USA.
350