| 1 | #!/usr/local/bin/perl -w | ||
| 2 | |||
| 3 | =head1 NAME | ||
| 4 | |||
| 5 | cluto2label.pl - Convert Cluto output to a confusion matrix | ||
| 6 | |||
| 7 | =head1 SYNOPSIS | ||
| 8 | |||
| 9 | cluto2label.pl [OPTIONS] CLUTO KEY | ||
| 10 | |||
| 11 | =head1 SYNOPSIS | ||
| 12 | |||
| 13 | Converts Cluto's clustering solution file to a cluster by sense distribution matrix to | ||
| 14 | then be input to SenseClusters evaluation program L<label.pl>. | ||
| 15 | |||
| 16 | =head1 INPUT | ||
| 17 | |||
| 18 | =head2 Required Arguments: | ||
| 19 | |||
| 20 | =head3 CLUTO | ||
| 21 | |||
| 22 | 1st argument should be a clustering solution file (described in section 3.4.1 | ||
| 23 | on page 34 in Cluto's manual) as created by Cluto's scluster and vcluster | ||
| 24 | programs. | ||
| 25 | |||
| 26 | For N instances, CLUTO file will have exactly N lines, each ith line showing | ||
| 27 | the cluster number(start from 0) to which the ith instance belongs. | ||
| 28 | |||
| 29 | e.g. | ||
| 30 | |||
| 31 | Cluto's clustering solution file => | ||
| 32 | |||
| 33 | 0 | ||
| 34 | 1 | ||
| 35 | 1 | ||
| 36 | 2 | ||
| 37 | 0 | ||
| 38 | 0 | ||
| 39 | 1 | ||
| 40 | 2 | ||
| 41 | |||
| 42 | shows the cluster ids of each of the 8 instances clustered by Cluto's program. | ||
| 43 | |||
| 44 | 1st, 5th and 6th instance belong to 1st cluster (Cluster No 0) | ||
| 45 | |||
| 46 | 2nd, 3rd and 7th instance belong to 2nd cluster (Cluster No 1) | ||
| 47 | |||
| 48 | And | ||
| 49 | |||
| 50 | 4th and 8th instance belong to 3rd cluster (Cluster No 2) | ||
| 51 | |||
| 52 | Note: cluster id could be possibly -1 which means the corresponding instance | ||
| 53 | is not assigned to any cluster | ||
| 54 | |||
| 55 | =head3 KEY | ||
| 56 | |||
| 57 | 2nd argument should be a KEY file (in SenseCluster's format) showing true sense | ||
| 58 | class labels of instances listed in CLUTO. | ||
| 59 | |||
| 60 | For N lines in file CLUTO, KEY should have exactly N lines. Each ith line in | ||
| 61 | KEY should minimally show a space separated list of true sense labels of | ||
| 62 | ith instance in following format - | ||
| 63 | |||
| 64 | <sense id="S"/>+ | ||
| 65 | |||
| 66 | e.g. | ||
| 67 | |||
| 68 | <sense id="art2"/> <sense id="art4"/> | ||
| 69 | <sense id="art1"/> | ||
| 70 | <sense id="art3"/><sense id="art4"/> | ||
| 71 | <sense id="art3"/> | ||
| 72 | <sense id="art4"/> <sense id="art1"/> | ||
| 73 | <sense id="art1"/> | ||
| 74 | <sense id="art5"/> <sense id="art2"/> <sense id="art3"/> | ||
| 75 | <sense id="art2"/> <sense id="art4"/> | ||
| 76 | |||
| 77 | Shows the true sense ids of instances in the CLUTO file described in (1). | ||
| 78 | |||
| 79 | If KEY is an actual KEY created by SenseClusters programs, KEY will also show | ||
| 80 | the instance ids of corresponding instances in the beginning of each line. | ||
| 81 | |||
| 82 | e.g. | ||
| 83 | |||
| 84 | <instance id="line-n.w7_098:6515:"/> <sense id="art2"/> <sense id="art4"/> | ||
| 85 | |||
| 86 | <instance id="line-n.w8_083:14771:"/> <sense id="art1"/> | ||
| 87 | |||
| 88 | <instance id="line-n.art} aphb 02700649:"/> <sense id="art3"/><sense id="art4"/> | ||
| 89 | |||
| 90 | <instance id="line-n.art} aphb 53900889:"/> <sense id="art3"/> | ||
| 91 | |||
| 92 | <instance id="line-n.w7_066:11025:"/> <sense id="art4"/> <sense id="art1"/> | ||
| 93 | |||
| 94 | <instance id="line-n.art} aphb 42100373:"/> <sense id="art1"/> | ||
| 95 | |||
| 96 | <instance id="line-n.w8_109:8774:"/> <sense id="art5"/> <sense id="art2"/> <sense id="art3"/> | ||
| 97 | |||
| 98 | <instance id="line-n.w7_004:10784:"/> <sense id="art2"/> <sense id="art4"/> | ||
| 99 | |||
| 100 | =head2 Optional Arguments: | ||
| 101 | |||
| 102 | =head4 --numthrow N | ||
| 103 | |||
| 104 | Ignores clusters containing less than N instances. | ||
| 105 | |||
| 106 | =head4 --perthrow P | ||
| 107 | |||
| 108 | Ignores clusters containing less than P percent of the instances. | ||
| 109 | |||
| 110 | Number of instances contained in the thrown clusters will be counted as the | ||
| 111 | unclustered instances. | ||
| 112 | |||
| 113 | =head4 --help | ||
| 114 | |||
| 115 | Displays this message. | ||
| 116 | |||
| 117 | =head4 --version | ||
| 118 | |||
| 119 | Displays the version information. | ||
| 120 | |||
| 121 | =head1 OUTPUT | ||
| 122 | |||
| 123 | This will show | ||
| 124 | |||
| 125 | =over 4 | ||
| 126 | |||
| 127 | =item * | ||
| 128 | |||
| 129 | Number of unclustered instances on 1st line. | ||
| 130 | |||
| 131 | =item * | ||
| 132 | |||
| 133 | Sense Lables of corresponding columns in Cluster Sense Matrix on 2nd line | ||
| 134 | starting with marker // | ||
| 135 | |||
| 136 | =item * | ||
| 137 | |||
| 138 | Cluster Sense Matrix starting from 3rd line and onwards. The matrix | ||
| 139 | shows the | ||
| 140 | distribution of instances from each sense class (represented by column labels) | ||
| 141 | in each of the clusters (represented by rows in the Cluster Sense Matrix). | ||
| 142 | |||
| 143 | Each cell entry at [i][j] in Cluster Sense distribution matrix shows the number | ||
| 144 | of instances from ith cluster having true sense class label represented by | ||
| 145 | label of jth column. | ||
| 146 | |||
| 147 | e.g. | ||
| 148 | |||
| 149 | 0 | ||
| 150 | // art1 art2 art3 art4 art5 | ||
| 151 | 2 1 0 2 0 | ||
| 152 | 1 1 2 1 1 | ||
| 153 | 0 1 1 1 0 | ||
| 154 | |||
| 155 | Shows that there are no unclustered instances, | ||
| 156 | |||
| 157 | 1st cluster contains 2 instances having sense id art1 and art4, 1 instance | ||
| 158 | having sense id art2 and no instances of sense id art3 and art5. | ||
| 159 | |||
| 160 | Similar description applies to 2nd and 3rd clusters. | ||
| 161 | |||
| 162 | =back | ||
| 163 | |||
| 164 | =head1 SYSTEM REQUIREMENTS | ||
| 165 | |||
| 166 | =over | ||
| 167 | |||
| 168 | =item Cluto - L<http://www-users.cs.umn.edu/~karypis/cluto/> | ||
| 169 | |||
| 170 | =back | ||
| 171 | |||
| 172 | =head1 AUTHORS | ||
| 173 | |||
| 174 | Amruta Purandare, University of Pittsburgh | ||
| 175 | |||
| 176 | Ted Pedersen, University of Minnesota, Duluth | ||
| 177 | tpederse at d.umn.edu | ||
| 178 | |||
| 179 | =head1 COPYRIGHT | ||
| 180 | |||
| 181 | Copyright (c) 2002-2008, Amruta Purandare and Ted Pedersen | ||
| 182 | |||
| 183 | This program is free software; you can redistribute it and/or modify it under | ||
| 184 | the terms of the GNU General Public License as published by the Free Software | ||
| 185 | Foundation; either version 2 of the License, or (at your option) any later | ||
| 186 | version. | ||
| 187 | |||
| 188 | This program is distributed in the hope that it will be useful, but WITHOUT | ||
| 189 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS | ||
| 190 | FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. | ||
| 191 | |||
| 192 | You should have received a copy of the GNU General Public License along with | ||
| 193 | this program; if not, write to | ||
| 194 | |||
| 195 | The Free Software Foundation, Inc., | ||
| 196 | 59 Temple Place - Suite 330, | ||
| 197 | Boston, MA 02111-1307, USA. | ||
| 198 | |||
| 199 | =cut | ||
| 200 | |||
| 201 | ############################################################################### | ||
| 202 | |||
| 203 | # THE CODE STARTS HERE | ||
| 204 | |||
| 205 | ############################################################################### | ||
| 206 | |||
| 207 | # ================================ | ||
| 208 | # COMMAND LINE OPTIONS AND USAGE | ||
| 209 | # ================================ | ||
| 210 | |||
| 211 | # show minimal usage message if no arguments | ||
| 212 | if($#ARGV<0) | ||
| 213 | { | ||
| 214 | &showminimal(); | ||
| 215 | exit; | ||
| 216 | } | ||
| 217 | |||
| 218 | # command line options | ||
| 219 | use Getopt::Long; | ||
| 220 | GetOptions ("help","version","numthrow=i","perthrow=f"); | ||
| 221 | # show help option | ||
| 222 | if(defined $opt_help) | ||
| 223 | { | ||
| 224 | $opt_help=1; | ||
| 225 | &showhelp(); | ||
| 226 | exit; | ||
| 227 | } | ||
| 228 | |||
| 229 | # show version information | ||
| 230 | if(defined $opt_version) | ||
| 231 | { | ||
| 232 | $opt_version=1; | ||
| 233 | &showversion(); | ||
| 234 | exit; | ||
| 235 | } | ||
| 236 | |||
| 237 | ############################################################################# | ||
| 238 | |||
| 239 | # ================================ | ||
| 240 | # INITIALIZATION AND INPUT | ||
| 241 | # ================================ | ||
| 242 | |||
| 243 | #$0 contains the program name along with | ||
| 244 | #the complete path. Extract just the program | ||
| 245 | #name and use in error messages | ||
| 246 | $0=~s/.*\/(.+)/$1/; | ||
| 247 | |||
| 248 | # argv[0] should be cluto output | ||
| 249 | if(!defined $ARGV[0]) | ||
| 250 | { | ||
| 251 | print STDERR "ERROR($0): | ||
| 252 | Please specify the clustering solution file created by Cluto ...\n"; | ||
| 253 | exit; | ||
| 254 | } | ||
| 255 | #accept the output of cluto | ||
| 256 | $infile=$ARGV[0]; | ||
| 257 | if(!-e $infile) | ||
| 258 | { | ||
| 259 | print STDERR "ERROR($0): | ||
| 260 | Cluto solution file <$infile> doesn't exist...\n"; | ||
| 261 | exit; | ||
| 262 | } | ||
| 263 | open(IN,$infile) || die "Error($0): | ||
| 264 | Error(code=$!) in opening <$infile> file.\n"; | ||
| 265 | |||
| 266 | # argv[1] should be the KEY file in SenseCluster's format | ||
| 267 | if(!defined $ARGV[1]) | ||
| 268 | { | ||
| 269 | print STDERR "ERROR($0): | ||
| 270 | Please specify the KEY file ...\n"; | ||
| 271 | exit; | ||
| 272 | } | ||
| 273 | |||
| 274 | #accept the KEY file | ||
| 275 | $keyfile=$ARGV[1]; | ||
| 276 | if(!-e $keyfile) | ||
| 277 | { | ||
| 278 | print STDERR "ERROR($0): | ||
| 279 | KEY file <$keyfile> doesn't exist...\n"; | ||
| 280 | exit; | ||
| 281 | } | ||
| 282 | open(KEY,$keyfile) || die "Error($0): | ||
| 283 | Error(code=$!) in opening <$keyfile> file.\n"; | ||
| 284 | |||
| 285 | ############################################################################## | ||
| 286 | |||
| 287 | # =========================== | ||
| 288 | # Building KEY Table | ||
| 289 | # =========================== | ||
| 290 | |||
| 291 | $line_num=0; | ||
| 292 | while(<KEY>) | ||
| 293 | { | ||
| 294 | $line_num++; | ||
| 295 | chomp; | ||
| 296 | # trimming extra spaces from beginning and end | ||
| 297 | s/^\s+//g; | ||
| 298 | s/\s+$//g; | ||
| 299 | s/\s+/ /g; | ||
| 300 | # handling blank lines | ||
| 301 | if(/^\s*$/) | ||
| 302 | { | ||
| 303 | print STDERR "ERROR($0): | ||
| 304 | Blank file found in KEY file <$keyfile> at line <$line_num>.\n"; | ||
| 305 | exit; | ||
| 306 | } | ||
| 307 | @senses_on_line=(); | ||
| 308 | # get sense ids now | ||
| 309 | while(/<sense id=\"([^\"]+)\"\/>/) | ||
| 310 | { | ||
| 311 | push @senses_on_line,$1; | ||
| 312 | $_=$'; | ||
| 313 | } | ||
| 314 | if($#senses_on_line<0) | ||
| 315 | { | ||
| 316 | print STDERR "ERROR($0): | ||
| 317 | No Sense Id found on line <$line_num> in KEY file <$keyfile>.\n"; | ||
| 318 | exit; | ||
| 319 | } | ||
| 320 | @{$key_table[$line_num-1]}=@senses_on_line; | ||
| 321 | } | ||
| 322 | |||
| 323 | ############################################################################### | ||
| 324 | |||
| 325 | # ====================== | ||
| 326 | # Reading KEY file now | ||
| 327 | # ====================== | ||
| 328 | |||
| 329 | $line_num=0; | ||
| 330 | $max_cluster_id=0; | ||
| 331 | $throw=0; | ||
| 332 | $instances=0; | ||
| 333 | while(<IN>) | ||
| 334 | { | ||
| 335 | $line_num++; | ||
| 336 | chomp; | ||
| 337 | # trimming extra spaces from beginning and end | ||
| 338 | s/^\s+//g; | ||
| 339 | s/\s+$//g; | ||
| 340 | s/\s+/ /g; | ||
| 341 | # handling blank lines | ||
| 342 | if(/^(\d+)$/) | ||
| 343 | { | ||
| 344 | $cluster_id=$1; | ||
| 345 | $seen_clusters{$cluster_id}=1; | ||
| 346 | if($cluster_id>$max_cluster_id) | ||
| 347 | { | ||
| 348 | $max_cluster_id=$cluster_id; | ||
| 349 | } | ||
| 350 | ## if(!defined @{$key_table[$line_num-1]}) | ||
| 351 | ## removed the defined @array per perl deprecation tdp oct 3, 2015 | ||
| 352 | if(!@{$key_table[$line_num-1]}) | ||
| 353 | { | ||
| 354 | print "ERROR($0): | ||
| 355 | Key file <$keyfile> doesn't have enough entries for Cluto solution file | ||
| 356 | <$infile>.\n"; | ||
| 357 | exit; | ||
| 358 | } | ||
| 359 | foreach $sense (@{$key_table[$line_num-1]}) | ||
| 360 | { | ||
| 361 | $cluster_sense[$cluster_id]{$sense}++; | ||
| 362 | $seen_senses{$sense}=1; | ||
| 363 | } | ||
| 364 | $histo{$cluster_id}++; | ||
| 365 | $instances++; | ||
| 366 | } | ||
| 367 | else | ||
| 368 | { | ||
| 369 | if(/^-1$/) | ||
| 370 | { | ||
| 371 | $throw++; | ||
| 372 | $instances++; | ||
| 373 | } | ||
| 374 | else | ||
| 375 | { | ||
| 376 | print STDERR "ERROR($0): | ||
| 377 | Line <$line_num> in Cluto solution file <$infile> should show the | ||
| 378 | number of the cluster to an instance belongs.\n"; | ||
| 379 | exit; | ||
| 380 | } | ||
| 381 | } | ||
| 382 | } | ||
| 383 | |||
| 384 | if($line_num != ($#key_table+1)) | ||
| 385 | { | ||
| 386 | print STDERR "ERROR($0): | ||
| 387 | Key file <$keyfile> and Cluto solution file <$infile> are incosistent.\n"; | ||
| 388 | exit; | ||
| 389 | } | ||
| 390 | |||
| 391 | if(defined $opt_perthrow || defined $opt_numthrow) | ||
| 392 | { | ||
| 393 | foreach $cluster (keys %histo) | ||
| 394 | { | ||
| 395 | if(defined $opt_numthrow && $histo{$cluster}<$opt_numthrow) | ||
| 396 | { | ||
| 397 | $throw+=$histo{$cluster}; | ||
| 398 | $thrown{$cluster}=1; | ||
| 399 | } | ||
| 400 | if(defined $opt_perthrow && ($histo{$cluster}/$instances*100)<$opt_perthrow) | ||
| 401 | { | ||
| 402 | $throw+=$histo{$cluster}; | ||
| 403 | $thrown{$cluster}=1; | ||
| 404 | } | ||
| 405 | } | ||
| 406 | } | ||
| 407 | |||
| 408 | print "$throw\n"; | ||
| 409 | print "\/\/"; | ||
| 410 | foreach $sense (sort keys %seen_senses) | ||
| 411 | { | ||
| 412 | print "\t$sense"; | ||
| 413 | } | ||
| 414 | print "\n"; | ||
| 415 | |||
| 416 | foreach $cluster (0..$max_cluster_id) | ||
| 417 | { | ||
| 418 | if(!defined $thrown{$cluster}) | ||
| 419 | { | ||
| 420 | print "C$cluster:"; | ||
| 421 | foreach $sense (sort keys %seen_senses) | ||
| 422 | { | ||
| 423 | if(!defined $seen_clusters{$cluster}) | ||
| 424 | { | ||
| 425 | print STDERR "ERROR($0): | ||
| 426 | Cluster Ids do not range from 0 to $max_cluster_id in Cluto solution | ||
| 427 | file <$infile>.\n"; | ||
| 428 | exit; | ||
| 429 | } | ||
| 430 | if(defined $cluster_sense[$cluster]{$sense}) | ||
| 431 | { | ||
| 432 | print "\t$cluster_sense[$cluster]{$sense}"; | ||
| 433 | } | ||
| 434 | else | ||
| 435 | { | ||
| 436 | print "\t0"; | ||
| 437 | } | ||
| 438 | } | ||
| 439 | print "\n"; | ||
| 440 | } | ||
| 441 | } | ||
| 442 | ############################################################################## | ||
| 443 | |||
| 444 | # ========================== | ||
| 445 | # SUBROUTINE SECTION | ||
| 446 | # ========================== | ||
| 447 | |||
| 448 | #----------------------------------------------------------------------------- | ||
| 449 | #show minimal usage message | ||
| 450 | sub showminimal() | ||
| 451 | { | ||
| 452 | print "Usage: cluto2label.pl [OPTIONS] CLUTO KEY"; | ||
| 453 | print "\nTYPE cluto2label.pl --help for help\n"; | ||
| 454 | } | ||
| 455 | |||
| 456 | #----------------------------------------------------------------------------- | ||
| 457 | #show help | ||
| 458 | sub showhelp() | ||
| 459 | { | ||
| 460 | print "Usage: cluto2label.pl [OPTIONS] CLUTO KEY | ||
| 461 | |||
| 462 | Displays a Cluster by Sense matrix for the Cluto's clustering solution file | ||
| 463 | using a given KEY file. | ||
| 464 | |||
| 465 | CLUTO | ||
| 466 | Specify Cluto's clustering solution file. This should have exactly n | ||
| 467 | lines for n instances clustsred and ith line should show the cluster | ||
| 468 | number to which the ith instance belongs. | ||
| 469 | |||
| 470 | KEY | ||
| 471 | Specify the corresponding KEY file showing true sense tags for each | ||
| 472 | instance in CLUTO file in <sense id=\"S\"\/> tags on each line. | ||
| 473 | |||
| 474 | OPTIONS: | ||
| 475 | |||
| 476 | --numthrow N | ||
| 477 | Ignores clusters containing less than N instances. | ||
| 478 | |||
| 479 | --perthrow P | ||
| 480 | Ignores clusters containing less than P percent of the instances. | ||
| 481 | |||
| 482 | --help | ||
| 483 | Displays this message. | ||
| 484 | |||
| 485 | --version | ||
| 486 | Displays the version information.\n"; | ||
| 487 | } | ||
| 488 | |||
| 489 | #------------------------------------------------------------------------------ | ||
| 490 | #version information | ||
| 491 | sub showversion() | ||
| 492 | { | ||
| 493 | print '$Id: cluto2label.pl,v 1.11 2015/10/03 14:05:57 tpederse Exp $'; | ||
| 494 | # print "\nCopyright (c) 2002-2006, Ted Pedersen & Amruta Purandare\n"; | ||
| 495 | # print "cluto2label.pl - Version 0.16"; | ||
| 496 | print "\nConvert Cluto output to confusion matrix\n"; | ||
| 497 | # print "Date of Last Update: 12/17/2003\n"; | ||
| 498 | } | ||
| 499 | |||
| 500 | ############################################################################# | ||
| 501 |