script/report.pl untested

filter: Statementstmt 0.0% 0 / 159 Branchbran 0.0% 0 / 54 Conditioncond 0.0% 0 / 7 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 0.0% 0 / 4 Podpod 0.0% 0 / 3 totaltotal 0.0% 0 / 231 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 report.pl - Summarize SenseClusters results with precision, recall, and confusion matrix
6  
7 =head1 SYNOPSIS
8  
9 report.pl [OPTIONS] LABEL PRELABEL
10  
11 Type C<report.pl --help> for a quick summary of options
12  
13 =head1 DESCRIPTION
14  
15 Reports the performance of discrimination in terms of the precision, recall
16 and confusion table.
17  
18 =head1 INPUT
19  
20 =head2 Required Arguments:
21  
22 =head3 LABEL
23  
24 An output created by label.pl showing sense labels attached to
25 the discovered clusters.
26  
27 Sample LABEL files =>
28  
29 =over
30  
31 =item 1. report.pl will minimally expect LABEL in this format -
32  
33 C0 -> fine%5:00:00:elegant:00
34 C1 -> fine%3:00:00::
35 C2 -> fine%5:00:00:superior:02
36 C3 -> fine%5:00:00:satisfactory:00
37 C4 -> fine%5:00:00:thin:01
38  
39 report will only read those lines from LABEL file that contain right arrow
40 (->), all other lines will be ignored.
41  
42 Lines containing '->' should show the cluster id on the left of the arrow
43 and a sense tag on the right.
44  
45 =item 2.
46  
47 ClusterID -> SenseID
48 0 -> fine%5:00:00:elegant:00
49 1 -> fine%3:00:00::
50 2 -> fine%5:00:00:superior:02
51 3 -> fine%5:00:00:satisfactory:00
52 4 -> fine%5:00:00:thin:01
53 Score = 60.00
54  
55 Shows the actual output of label which contains a descriptive header line
56 on the 1st line and the score of the mapping scheme on the last line.
57  
58 =back
59  
60 =head3 PRELABEL
61  
62 Should be an output created by cluto2label.pl program showing the
63 distribution of instances from each sense class in each of the clusters.
64  
65 This distribution should be shown in a cluster by sense matrix where the
66 rows represent the clusters and the columns represent the senses. Cell entry
67 at CS[i][j] shows the number of instances belonging to cluster Ci that
68 have the true true sense tag Sj.
69  
70 e.g.
71  
72 0
73 //phone cord txt div form
74 0 2 1 0 0
75 0 1 2 4 0
76 5 2 2 25 4
77 0 1 9 0 0
78 0 1 0 1 0
79  
80 Note that -
81  
82 =over
83  
84 =item 1. 1st line shows the number of instances unclustered.
85  
86 =item 2. 2nd line starts with // and shows the sense labels of corresponding columns.
87  
88 =item 3. 3rd line and onwards show the cluster by sense distribution matrix.
89  
90 =back
91  
92 =head2 Optional Arguments:
93  
94 =head3 Other Options :
95  
96 =head4 --help
97  
98 Displays this message.
99  
100 =head4 --version
101  
102 Displays the version information.
103  
104 =head1 OUTPUT
105  
106 Output will display a confusion table whose rows represent the discovered
107 clusters and columns represent the actual sense classes such that cell value at
108 (i,j) indicates the number of instances belonging to cluster Ci that have true
109 sense id Sk where Sk is the column label on the top of the jth column.
110 Columns are reordered such that the sense representing the rth column most
111 accurately represents the rth cluster Cr and diagonal value at (r,r) shows the
112 number of instances in the rth cluster that belong to their correct sense class.
113  
114 When #clusters > #senses, clusters that aren't assigned a sense tag will
115 have star (*) on them. When #senses > #clusters, senses that aren't assigned
116 to any cluster will be hash (#) marked.
117  
118 The sum of the diagonal entries shows the total number of instances that are
119 correctly discriminated(#hits). From this number, report computes precision
120 and recall where
121  
122 precision = #hits / #clustered
123  
124 #clustered = Number of instances clustered = total #instances - #instances
125 that belong to the unlabelled clusters - #thrown shown in PRELABEL input file.
126  
127 recall = #hits / #total instances
128  
129 Sample Output :
130  
131 S1 S2 S0 S3 TOTAL
132 C0: 221 11 3 15 250 (5.71)
133 C1: 295 395 448 144 1282 (29.28)
134 C6: 430 233 441 68 1172 (26.77)
135 C9: 145 44 149 105 443 (10.12)
136 C2:* 0 1 135 2 138 (3.15)
137 C3:* 138 4 4 2 148 (3.38)
138 C4:* 0 0 182 0 182 (4.16)
139 C5:* 2 6 150 6 164 (3.75)
140 C7:* 41 159 99 97 396 (9.05)
141 C8:* 0 0 203 0 203 (4.64)
142 1272 853 1814 439 4378
143 (29.05) (19.48) (41.43) (10.03)
144 Precision = 36.92(1162/3147)
145 Recall = 26.54(1162/4378+0)
146  
147 Legend of Sense Tags
148 S0 = SERVE10
149 S1 = SERVE12
150 S2 = SERVE2
151 S3 = SERVE6
152
153 shows
154  
155 =over
156  
157 =item 1. 9 clusters(C0-C8) and 4 senses (S0-S3).
158  
159 =item 2. Cluster C0 represents sense S1 which stands for actual sense SERVE12
160  
161 C1 represents S2 (stands for SERVE2),
162 C6 represents S0 (stands for SERVE10)
163 C9 represents S3 (stands for SERVE6)
164  
165 =item 3. The above maximal mapping gives precision of 36.92% and recall of 26.54%
166 where total 1162 instances are correctly discriminated among the total 4378
167 instances.
168  
169 =item 4. The last two columns show the total number and percentage of instances in
170 each cluster(row marginal totals) while the last two rows indicate the total
171 number and percentage of instances in each sense class(column marginal totals).
172  
173 =back
174  
175 =head1 AUTHORS
176  
177 Ted Pedersen, University of Minnesota, Duluth
178  
179 Amruta Purandare, University of Pittsburgh
180  
181 Anagha Kulkarni, Carnegie-Mellon University
182  
183 =head1 COPYRIGHT
184  
185 Copyright (c) 2002-2008, Ted Pedersen, Amruta Purandare, Anagha Kulkarni
186
187 This program is free software; you can redistribute it and/or modify it under
188 the terms of the GNU General Public License as published by the Free Software
189 Foundation; either version 2 of the License, or (at your option) any later
190 version.
191  
192 This program is distributed in the hope that it will be useful, but WITHOUT
193 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
194 FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
195  
196 You should have received a copy of the GNU General Public License along with
197 this program; if not, write to
198  
199 The Free Software Foundation, Inc.,
200 59 Temple Place - Suite 330,
201 Boston, MA 02111-1307, USA.
202  
203 =cut
204  
205 ###############################################################################
206  
207 # THE CODE STARTS HERE
208  
209 ###############################################################################
210  
211 # ================================
212 # COMMAND LINE OPTIONS AND USAGE
213 # ================================
214  
215 # show minimal usage message if no arguments
216 if($#ARGV<0)
217 {
218 &showminimal();
219 exit;
220 }
221  
222 # command line options
223 use Getopt::Long;
224 GetOptions ("help","version");
225 # show help option
226 if(defined $opt_help)
227 {
228 $opt_help=1;
229 &showhelp();
230 exit;
231 }
232  
233 # show version information
234 if(defined $opt_version)
235 {
236 $opt_version=1;
237 &showversion();
238 exit;
239 }
240  
241 #############################################################################
242  
243 # ================================
244 # INITIALIZATION AND INPUT
245 # ================================
246  
247 #$0 contains the program name along with
248 #the complete path. Extract just the program
249 #name and use in error messages
250 $0=~s/.*\/(.+)/$1/;
251  
252 if(!defined $ARGV[0])
253 {
254 print STDERR "ERROR($0):
255 Specify the Label file ...\n";
256 exit;
257 }
258 #accept the label file name
259 $labelfile=$ARGV[0];
260 if(!-e $labelfile)
261 {
262 print STDERR "ERROR($0):
263 Label file <$labelfile> doesn't exist...\n";
264 exit;
265 }
266 open(LAB,$labelfile) || die "Error($0):
267 Error(code=$!) in opening <$labelfile> file.\n";
268  
269 if(!defined $ARGV[1])
270 {
271 print STDERR "ERROR($0):
272 Please specify the Prelabel file ...\n";
273 exit;
274 }
275 #accept the cluster by sense matrix file
276 $prelabfile=$ARGV[1];
277 if(!-e $prelabfile)
278 {
279 print STDERR "ERROR($0):
280 Prelabel file <$prelabfile> doesn't exist...\n";
281 exit;
282 }
283 open(PRE,$prelabfile) || die "Error($0):
284 Error(code=$!) in opening <$prelabfile> file.\n";
285  
286 ##############################################################################
287  
288 # ========================
289 # ACCEPTING MATRIX
290 # ========================
291 $line_num=0;
292 # first line of pre-label output should show the number of instances
293 # unclustered
294 # this should be counted while computing recall
295 $thrown=<PRE>;
296 if(!defined $thrown)
297 {
298 print STDERR "ERROR($0):
299 1st line of the Prelabel file <$prelabfile> should show the number of
300 instances unclustered or 0.\n";
301 exit;
302 }
303 chomp $thrown;
304 $thrown=~s/^\s*//g;
305 $thrown=~s/\s*$//g;
306 if(!($thrown=~/^\d+$/))
307 {
308 print STDERR "ERROR($0):
309 1st line of the Prelabel file <$prelabfile> should show number of
310 instances unclustered or 0.\n";
311 exit;
312 }
313  
314 # 2nd line should show the Sense Label list starting with //
315 $sense_string=<PRE>;
316 if(defined $sense_string && $sense_string=~/\/\//)
317 {
318 # remove //
319 $sense_string=$';
320 $sense_string=~s/\s*$//g;
321 $sense_string=~s/^\s*//g;
322 @all_senses=split(/\s+/,$sense_string);
323 }
324 else
325 {
326 print STDERR "ERROR($0):
327 The 2nd line of the Prelabel file <$prelabfile> must contain
328 the sense labels starting with //.\n";
329 exit;
330 }
331  
332 foreach $sid (0..$#all_senses)
333 {
334 $sids{$all_senses[$sid]}=$sid;
335 }
336  
337 $total=0;
338 while(<PRE>)
339 {
340 $line_num++;
341 # trimming extra spaces
342 chomp;
343 s/\s*$//g;
344 s/^\s*//g;
345 s/\s+/ /g;
346 # handling blank lines
347 if(/^\s*$/)
348 {
349 next;
350 }
351  
352 if($_ =~ /\s*:\s*/)
353 {
354 ($cid,$row)=split(/\s*:\s*/);
355 }
356 else
357 {
358 print STDERR "ERROR($0):
359 Wrong format of Prelabel file <$prelabfile> at line <$line_num>.\n";
360 exit;
361 }
362  
363 @row_elements=split(/\s+/,$row);
364 if($#row_elements!=$#all_senses)
365 {
366 print STDERR "ERROR($0):
367 Number of columns (". scalar(@row_elements) . ") at line <$line_num>
368 in the Prelabel file <$prelabfile> doesn't match the number of
369 senses (" . scalar(@all_senses) . ") specified on Line 2 of the
370 same file.\n";
371 exit;
372 }
373 foreach $sense(@all_senses)
374 {
375 if(!($row_elements[0]=~/\s+/))
376 {
377 if(!($row_elements[0]=~/^[0-9]+$/))
378 {
379 print STDERR "ERROR($0):
380 Line <$line_num> in the Prelabel file <$prelabfile> contains
381 a non-integer matrix value.\n";
382 exit;
383 }
384 # make an entry in the matrix
385 $cluster_sense{$cid}{$sense}=shift(@row_elements);
386 $total+=$cluster_sense{$cid}{$sense};
387 }
388 }
389 }
390  
391 ##############################################################################
392  
393 # ==============================
394 # ACCEPT MAPPINGS
395 # ==============================
396  
397 $line_num=0;
398 while(<LAB>)
399 {
400 $line_num++;
401 # trimming extra spaces
402 chomp;
403 s/\s*$//g;
404 s/^\s*//g;
405 # handling blank lines
406 if(/^\s*$/)
407 {
408 next;
409 }
410 if(/^(.*)\s*->\s*(.*)$/)
411 {
412 # skip header
413 if($1 !~ /ClusterID/)
414 {
415 $sense=$2;
416 $cid=$1;
417 push @senses,$sense;
418 $labels{$sense}=1;
419 $cid=~s/\s*$//g;
420 push @clusters,$cid;
421 $labelled{$cid}=1;
422 if(!defined $cluster_sense{$cid}{$sense})
423 {
424 print STDERR "ERROR($0):
425 Cluster-Sense pair <$cid,$sense> in the Label file <$labelfile> doesn't
426 have any entry in the Prelabel file <$prelabfile>.\n";
427 exit;
428 }
429 }
430 }
431 }
432  
433 # senses that are not assigned to
434 # any clusters appear on the right
435 # side of the confusion matrix
436  
437 foreach $sense (@all_senses)
438 {
439 if(!defined $labels{$sense})
440 {
441 push @senses,$sense;
442 }
443 }
444  
445 # clusters that are not assigned any
446 # tags appear at the bottom of the
447 # confusion table
448  
449 foreach $cluster (sort keys %cluster_sense)
450 {
451 if(!defined $labelled{$cluster})
452 {
453 push @clusters,$cluster;
454 }
455 }
456 ##############################################################################
457  
458 # ===========================
459 # FINDING MARGINALS
460 # ===========================
461  
462 # finding marginals
463 $attempted=0;
464 foreach $cluster (@clusters)
465 {
466 $row_margin=0;
467 foreach $sense (0..$#senses)
468 {
469 if(!defined $cluster_sense{$cluster}{$senses[$sense]})
470 {
471 print STDERR "ERROR($0):
472 Prelabel file <$prelabfile> and Label File <$labelfile> are
473 inconsistent.\n";
474 exit;
475 }
476 if(!defined $col_marginals{$senses[$sense]})
477 {
478 $col_marginals{$senses[$sense]}=0;
479 }
480 $col_marginals{$senses[$sense]}+=$cluster_sense{$cluster}{$senses[$sense]};
481 if(defined $labelled{$cluster})
482 {
483 $attempted+=$cluster_sense{$cluster}{$senses[$sense]};
484 }
485 $row_margin+=$cluster_sense{$cluster}{$senses[$sense]};
486 }
487 push @row_marginals,$row_margin;
488 }
489  
490  
491 ##############################################################################
492  
493 # =========================
494 # OUTPUT SECTION
495 # =========================
496  
497 printf "%6s"," ";
498 foreach $sense (@senses)
499 {
500 printf("%8s","S$sids{$sense}");
501 if(!defined $labels{$sense})
502 {
503 print "#";
504 }
505 print "\t";
506 }
507 printf("%8s\t","TOTAL");
508 $hits=0;
509 foreach $cluster (0..$#clusters)
510 {
511 printf "\n%4s:","$clusters[$cluster]";
512 if(!defined $labelled{$clusters[$cluster]})
513 {
514 print "*";
515 }
516 else
517 {
518 print " ";
519 }
520 foreach $sense (0..$#senses)
521 {
522 printf("%8s\t",$cluster_sense{$clusters[$cluster]}{$senses[$sense]});
523 if($cluster==$sense)
524 {
525 # counting number of instances tagged correctly
526 $hits+=$cluster_sense{$clusters[$cluster]}{$senses[$sense]};
527 }
528 }
529 printf("%8s\t",$row_marginals[0]);
530 printf "(%2.2f)", $row_marginals[0]/$total*100;
531 shift @row_marginals;
532 }
533  
534 printf "\n%6s","TOTAL";
535 foreach (@senses)
536 {
537 printf("%8s\t",$col_marginals{$_});
538 }
539 printf("%8s\n",$total);
540  
541 printf "%6s"," ";
542  
543 foreach (@senses)
544 {
545 printf "%3s", " ";
546 printf("(%2.2f)",$col_marginals{$_}/$total*100," ");
547 }
548  
549 my $precision = $hits/$attempted*100;
550 my $recall = $hits/($total+$thrown)*100;
551 my $fmeasure = 2 * $precision * $recall / ($precision + $recall);
552  
553 printf "\nPrecision = %2.2f($hits/$attempted)\n",$precision;
554 printf "Recall = %2.2f($hits/$total+$thrown)\n",$recall;
555 printf "F-Measure = %2.2f\n",$fmeasure;
556  
557 print "\nLegend of Sense Tags\n";
558 foreach $sense (@all_senses)
559 {
560 print "S$sids{$sense} = $sense\n";
561 }
562 ##############################################################################
563  
564 # ==========================
565 # SUBROUTINE SECTION
566 # ==========================
567  
568 #-----------------------------------------------------------------------------
569 #show minimal usage message
570 sub showminimal()
571 {
572 print "Usage: report.pl [OPTIONS] LABEL PRELABEL";
573 print "\nTYPE report.pl --help for help\n";
574 }
575  
576 #-----------------------------------------------------------------------------
577 #show help
578 sub showhelp()
579 {
580 print "Usage: report.pl [OPTIONS] LABEL PRELABEL
581  
582 Evaluates the discrimination performance by printing the precision, recall
583 and confusion matrix.
584  
585 LABEL
586 Output of label.pl program.
587 PRELABEL
588 Output of cluto2label.pl program.
589  
590 OPTIONS:
591 --help
592 Displays this message.
593 --version
594 Displays the version information.\n";
595 }
596  
597 #------------------------------------------------------------------------------
598 #version information
599 sub showversion()
600 {
601 print '$Id: report.pl,v 1.14 2008/03/30 05:06:07 tpederse Exp $';
602 # print "\nCopyright (C) 2002-2006, Ted Pedersen, Amruta Purandare & Anagha Kulkarni\n";
603 # print "report.pl - Version 0.06\n";
604 print "\nReports Precision, Recall, F-Measure and Confusion table\n";
605 # print "Date of Last Update: 01/19/2005\n";
606 }
607  
608 #############################################################################
609