| 1 | #!/usr/local/bin/perl -w | ||
| 2 | |||
| 3 | =head1 NAME | ||
| 4 | |||
| 5 | clusterlabeling.pl - Label discovered clusters based on their content | ||
| 6 | |||
| 7 | =head1 SYNOPSIS | ||
| 8 | |||
| 9 | clusterlabeling.pl [OPTIONS] INPUTFILE | ||
| 10 | |||
| 11 | =head1 DESCRIPTION | ||
| 12 | |||
| 13 | Assigns labels to each cluster with the significant word pairs found in | ||
| 14 | the cluster contexts. Also separates the clusters in different files. | ||
| 15 | This is particularly useful for the web-interface. | ||
| 16 | |||
| 17 | Two types of labels are assigned to each cluster : Descriptive and | ||
| 18 | Discriminating. Descriptive labels are the top n significant word pairs. | ||
| 19 | Discriminating labels are the word-pairs unique to the cluster out of the | ||
| 20 | top n significant word-pairs for the cluster. | ||
| 21 | |||
| 22 | =head2 Required Arguments: | ||
| 23 | |||
| 24 | =head3 INPUTFILE | ||
| 25 | |||
| 26 | File created by Toolkit/evaluate/format_clusters.pl with --context option. | ||
| 27 | |||
| 28 | =head2 Optional Arguments: | ||
| 29 | |||
| 30 | =head4 --token TOKEN | ||
| 31 | |||
| 32 | A file containing Perl regex/s that define the tokenization scheme in INPUTFILE file. | ||
| 33 | |||
| 34 | If --token is not specified, default token regex file | ||
| 35 | token.regex is searched in the current directory. | ||
| 36 | |||
| 37 | =head4 --prefix PRE | ||
| 38 | |||
| 39 | Specify a prefix to be used for the file names of the cluster files. | ||
| 40 | e.g. If the PRE is the prefix specified then cluster with id=0 will | ||
| 41 | have file name: PRE.cluster.0 | ||
| 42 | |||
| 43 | If prefix is not specified then prefix is created by concatenating | ||
| 44 | time stamp to the string "expr". | ||
| 45 | |||
| 46 | =head4 --stop STOPFILE | ||
| 47 | |||
| 48 | A file of Perl regexes that define the stop list of words to be | ||
| 49 | excluded from the features. | ||
| 50 | |||
| 51 | STOPFILE could be specified with two modes : | ||
| 52 | |||
| 53 | =over 4 | ||
| 54 | |||
| 55 | =item * AND mode - declared by including '@stop.mode=AND' on the first | ||
| 56 | line of the STOPFILE | ||
| 57 | |||
| 58 | =item * OR mode - declared by including '@stop.mode=OR' on the first line | ||
| 59 | of the STOPFILE [Default] | ||
| 60 | |||
| 61 | =back | ||
| 62 | |||
| 63 | AND mode ignores word pairs in which both words are stop words. | ||
| 64 | |||
| 65 | OR mode ignores word pairs in which either word is a stop word. | ||
| 66 | |||
| 67 | =head4 --ngram n | ||
| 68 | |||
| 69 | Allows user to set the size of the ngrams that will be used for the | ||
| 70 | labels. Valid values are 2, 3, and 4. | ||
| 71 | |||
| 72 | Default value for this option is 2 (i.e. default feature selection) | ||
| 73 | |||
| 74 | =head4 --remove N | ||
| 75 | |||
| 76 | Removes bigrams that occur less than N times. | ||
| 77 | |||
| 78 | Default value for this option is 5 | ||
| 79 | |||
| 80 | =head4 --window W | ||
| 81 | |||
| 82 | Specifies the window size for bigrams. Pairs of words that co-occur | ||
| 83 | within the specified window from each other (window W allows at most | ||
| 84 | W-2 intervening words) will form the bigram features. | ||
| 85 | |||
| 86 | Default window size is 2 which allows only consecutive word pairs. | ||
| 87 | |||
| 88 | =head4 --stat STAT | ||
| 89 | |||
| 90 | Specifies the statistical scores of association. The following are | ||
| 91 | available: | ||
| 92 | |||
| 93 | ll - Log Likelihood Ratio [default] | ||
| 94 | pmi - Point-Wise Mutual Information | ||
| 95 | tmi - True Mutual Information | ||
| 96 | x2 - Chi-Squared Test | ||
| 97 | phi - Phi Coefficient | ||
| 98 | tscore - T-Score | ||
| 99 | dice - Dice Coefficient | ||
| 100 | odds - Odds Ratio | ||
| 101 | leftFisher - Left Fisher's Test | ||
| 102 | rightFisher - Right Fisher's Test | ||
| 103 | |||
| 104 | =head4 --rank R | ||
| 105 | |||
| 106 | Word pairs ranking below R when arranged in descending order of | ||
| 107 | their test scores are ignored. | ||
| 108 | |||
| 109 | Default value for this option is 10 | ||
| 110 | |||
| 111 | =head4 --newLine | ||
| 112 | |||
| 113 | If turned on, word pair selection process will not span across newlines. | ||
| 114 | |||
| 115 | By default this option is turned off, that is, word pair selection spans | ||
| 116 | across lines. | ||
| 117 | |||
| 118 | =head3 Other Options : | ||
| 119 | |||
| 120 | =head4 --help | ||
| 121 | |||
| 122 | Displays the quick summary of program options. | ||
| 123 | |||
| 124 | =head4 --version | ||
| 125 | |||
| 126 | Displays the version information. | ||
| 127 | |||
| 128 | =head4 --verbose | ||
| 129 | |||
| 130 | Displays to STDERR the current program status. | ||
| 131 | |||
| 132 | =head1 OUTPUT | ||
| 133 | |||
| 134 | =over | ||
| 135 | |||
| 136 | =item 1. Cluster ids followed by the assigned labels are directed to STDOUT: | ||
| 137 | |||
| 138 | Cluster 0 (Descriptive): Bill Clinton, Mariana Islands, Northern Mariana, Pacific island, World Cup, per hour | ||
| 139 | |||
| 140 | Cluster 0 (Discriminating): Mariana Islands, Northern Mariana, Pacific island, World Cup, per hour | ||
| 141 | |||
| 142 | Cluster 2 (Descriptive): Bill Clinton, Erik wrote, Inc Within, Jersey And, Lyle Menendez | ||
| 143 | |||
| 144 | Cluster 2 (Discriminating): Erik wrote, Inc Within, Jersey And, Lyle Menendez | ||
| 145 | |||
| 146 | Cluster 1: | ||
| 147 | |||
| 148 | Cluster 3: | ||
| 149 | |||
| 150 | Cluster -1 (Descriptive): York Times, Undated _ | ||
| 151 | |||
| 152 | Cluster -1 (Discriminating): York Times, Undated _ | ||
| 153 | |||
| 154 | =item 2. Cluster files, named with the specified prefix or the generated prefix. | ||
| 155 | |||
| 156 | =back | ||
| 157 | |||
| 158 | =head1 SYSTEM REQUIREMENTS | ||
| 159 | |||
| 160 | Input to this program should be created by L<format_clusters.pl> | ||
| 161 | |||
| 162 | =head1 BUGS | ||
| 163 | |||
| 164 | =head1 AUTHOR | ||
| 165 | |||
| 166 | Anagha Kulkarni, Carnegie-Mellon University | ||
| 167 | |||
| 168 | Ted Pedersen, University of Minnesota, Duluth | ||
| 169 | tpederse at d.umn.edu | ||
| 170 | |||
| 171 | =cut | ||
| 172 | |||
| 173 | ############################################################################### | ||
| 174 | |||
| 175 | # THE CODE STARTS HERE | ||
| 176 | |||
| 177 | #$0 contains the program name along with | ||
| 178 | #the complete path. Extract just the program | ||
| 179 | #name and use in error messages | ||
| 180 | $0=~s/.*\/(.+)/$1/; | ||
| 181 | |||
| 182 | ############################################################################### | ||
| 183 | |||
| 184 | # ================================ | ||
| 185 | # COMMAND LINE OPTIONS AND USAGE | ||
| 186 | # ================================ | ||
| 187 | |||
| 188 | # command line options | ||
| 189 | use Getopt::Long; | ||
| 190 | GetOptions ("help","version","verbose","stop=s","remove=i","window=i","stat=s","rank=i","prefix=s","token=s","newLine", "ngram=n"); | ||
| 191 | |||
| 192 | # show help option | ||
| 193 | if(defined $opt_help) | ||
| 194 | { | ||
| 195 | $opt_help=1; | ||
| 196 | &showhelp(); | ||
| 197 | exit; | ||
| 198 | } | ||
| 199 | |||
| 200 | # show version information | ||
| 201 | if(defined $opt_version) | ||
| 202 | { | ||
| 203 | $opt_version=1; | ||
| 204 | &showversion(); | ||
| 205 | exit; | ||
| 206 | } | ||
| 207 | |||
| 208 | # show minimal usage message if no arguments | ||
| 209 | if($#ARGV<0) | ||
| 210 | { | ||
| 211 | &showminimal(); | ||
| 212 | exit 1; | ||
| 213 | } | ||
| 214 | |||
| 215 | ############################################################################# | ||
| 216 | |||
| 217 | # ================================ | ||
| 218 | # INITIALIZATION AND INPUT | ||
| 219 | # ================================ | ||
| 220 | |||
| 221 | # ---------- | ||
| 222 | # Input file | ||
| 223 | # ---------- | ||
| 224 | if(!defined $ARGV[0]) | ||
| 225 | { | ||
| 226 | print STDERR "ERROR($0): | ||
| 227 | Please specify the INPUTFILE file name...\n"; | ||
| 228 | exit 1; | ||
| 229 | } | ||
| 230 | $inpfile=$ARGV[0]; | ||
| 231 | |||
| 232 | if(!-e $inpfile) | ||
| 233 | { | ||
| 234 | print STDERR "ERROR($0): | ||
| 235 | Could not locate the INPUTFILE file $inpfile.\n"; | ||
| 236 | exit 1; | ||
| 237 | } | ||
| 238 | |||
| 239 | # -------------- | ||
| 240 | # Prefix | ||
| 241 | # -------------- | ||
| 242 | |||
| 243 | if(defined $opt_prefix) | ||
| 244 | { | ||
| 245 | $prefix=$opt_prefix; | ||
| 246 | } | ||
| 247 | else | ||
| 248 | { | ||
| 249 | $prefix="expr" . time(); | ||
| 250 | } | ||
| 251 | |||
| 252 | |||
| 253 | # --------------- | ||
| 254 | # Tokenfile | ||
| 255 | # --------------- | ||
| 256 | |||
| 257 | if(defined $opt_token) | ||
| 258 | { | ||
| 259 | $token=$opt_token; | ||
| 260 | } | ||
| 261 | else | ||
| 262 | { | ||
| 263 | $token="token.regex"; | ||
| 264 | } | ||
| 265 | |||
| 266 | if(!-e $token) | ||
| 267 | { | ||
| 268 | print STDERR "ERROR($0): | ||
| 269 | Could not locate the TOKEN file $token.\n"; | ||
| 270 | exit 1; | ||
| 271 | } | ||
| 272 | |||
| 273 | $cwd = `pwd`; | ||
| 274 | chomp($cwd); | ||
| 275 | |||
| 276 | #********************************************************************* | ||
| 277 | |||
| 278 | # String to hold ngram option. | ||
| 279 | $ngram_str = ""; | ||
| 280 | |||
| 281 | # Added: NGRAM option in the program. | ||
| 282 | if(defined $opt_ngram){ | ||
| 283 | $ngram_str = " --ngram $opt_ngram "; | ||
| 284 | |||
| 285 | |||
| 286 | if($opt_ngram < 2 || $opt_ngram > 4) | ||
| 287 | { | ||
| 288 | print STDERR "\n ERROR($0): | ||
| 289 | Labeling mechanism only supports bigrams, trigrams and 4-grams for feature selection.\n"; | ||
| 290 | exit 1; | ||
| 291 | } | ||
| 292 | |||
| 293 | }else{ | ||
| 294 | $ngram_str .= " --ngram 2 "; | ||
| 295 | } | ||
| 296 | |||
| 297 | |||
| 298 | |||
| 299 | # form the parameter string for count.pl | ||
| 300 | $count_str = ""; | ||
| 301 | |||
| 302 | if(defined $opt_window) | ||
| 303 | { | ||
| 304 | $count_str .= " --window $opt_window "; | ||
| 305 | } | ||
| 306 | |||
| 307 | if(defined $opt_stop) | ||
| 308 | { | ||
| 309 | $count_str .= " --stop $opt_stop "; | ||
| 310 | } | ||
| 311 | |||
| 312 | if(defined $opt_remove) | ||
| 313 | { | ||
| 314 | $count_str .= " --remove $opt_remove "; | ||
| 315 | } | ||
| 316 | else | ||
| 317 | { | ||
| 318 | $count_str .= " --remove 5 "; | ||
| 319 | } | ||
| 320 | |||
| 321 | if(defined $opt_newLine) | ||
| 322 | { | ||
| 323 | $opt_newLine = $opt_newLine; # to avoid warnings regarding variable used only once... | ||
| 324 | $count_str .= " --newLine "; | ||
| 325 | } | ||
| 326 | |||
| 327 | $count_str .= " --token $token "; | ||
| 328 | |||
| 329 | # Adding the new ngram option here. | ||
| 330 | $count_str .= $ngram_str; | ||
| 331 | |||
| 332 | # form the parameter string for statistic.pl | ||
| 333 | $stat_str = ""; | ||
| 334 | |||
| 335 | if(defined $opt_stat) | ||
| 336 | { | ||
| 337 | $stat_str .= " $opt_stat "; | ||
| 338 | } | ||
| 339 | else | ||
| 340 | { | ||
| 341 | $stat_str .= " ll "; | ||
| 342 | } | ||
| 343 | |||
| 344 | if(defined $opt_rank) | ||
| 345 | { | ||
| 346 | $stat_str .= " --rank $opt_rank "; | ||
| 347 | } | ||
| 348 | else | ||
| 349 | { | ||
| 350 | $stat_str .= " --rank 10 "; | ||
| 351 | } | ||
| 352 | |||
| 353 | # Adding the new ngram option in statistical calculation. | ||
| 354 | $stat_str .= $ngram_str; | ||
| 355 | |||
| 356 | |||
| 357 | |||
| 358 | |||
| 359 | |||
| 360 | # open the input file in read mode | ||
| 361 | open(INP,"$inpfile") || die "Error while opening the $inpfile for reading"; | ||
| 362 | |||
| 363 | # read the complete file in single instruction instead of reading line by line. | ||
| 364 | my $temp_delimiter = $/; | ||
| 365 | $/ = undef; | ||
| 366 | my $inp_str = <INP>; | ||
| 367 | $/ = $temp_delimiter; | ||
| 368 | |||
| 369 | # check if at least one cluster present in the input file. | ||
| 370 | # if not then cannot generate cluster labels. Probably incorrect file format. | ||
| 371 | if($inp_str !~ /<cluster/ || $inp_str !~ /<\/cluster>/) | ||
| 372 | { | ||
| 373 | print STDERR "ERROR($0): | ||
| 374 | No clusters found in the input file. Probably incorrect input file format. | ||
| 375 | Please use a file created by Toolkit/evaluate/format_clusters.pl with | ||
| 376 | --context option.\n"; | ||
| 377 | exit 1; | ||
| 378 | } | ||
| 379 | |||
| 380 | # separate the clusters | ||
| 381 | my @clusters = split(/<\/cluster>/,$inp_str); | ||
| 382 | |||
| 383 | my $first_cId = ""; | ||
| 384 | |||
| 385 | # label hash counter | ||
| 386 | my $array_cnt = 0; | ||
| 387 | |||
| 388 | # String for all the clusters without any labels | ||
| 389 | my $no_label_clusters = ""; | ||
| 390 | |||
| 391 | # label each cluster at a time | ||
| 392 | while($#clusters) | ||
| 393 | { | ||
| 394 | my $cluster = shift @clusters; | ||
| 395 | |||
| 396 | # extract the cluster id | ||
| 397 | $cluster =~ /<cluster id=\"(.+?)\">/; | ||
| 398 | my $cId = $1; | ||
| 399 | |||
| 400 | $cluster .= "</cluster>"; | ||
| 401 | |||
| 402 | # | ||
| 403 | # write out the cluster to a file | ||
| 404 | open(CLS,">$prefix.cluster.$cId") || die "Error while creating $prefix.cluster.$cId.xml"; | ||
| 405 | print CLS $cluster; | ||
| 406 | close CLS; | ||
| 407 | |||
| 408 | # add time-stamps to the temp files | ||
| 409 | my $time_stamp = time(); | ||
| 410 | my $tmp_txt = "tmp.$time_stamp.cluster.$cId.txt"; | ||
| 411 | my $tmp_cnt = "tmp.$time_stamp.cluster.$cId.cnt"; | ||
| 412 | |||
| 413 | if(defined $opt_verbose) | ||
| 414 | { | ||
| 415 | print STDERR "Starting sval2text.pl $prefix.cluster.$cId.xml > $tmp_txt\n"; | ||
| 416 | } | ||
| 417 | |||
| 418 | # call sval2plain.pl for above created cluster file to convert it to plain text | ||
| 419 | $status=system("sval2plain.pl $prefix.cluster.$cId > $cwd/$tmp_txt "); | ||
| 420 | die "Error while running sval2text.pl $prefix.cluster.$cId.xml > $cwd/$tmp_txt" unless $status==0; | ||
| 421 | |||
| 422 | if(defined $opt_verbose) | ||
| 423 | { | ||
| 424 | print STDERR "Finished sval2text.pl $prefix.cluster.$cId.xml > $cwd/$tmp_txt\n"; | ||
| 425 | } | ||
| 426 | |||
| 427 | if(defined $opt_verbose) | ||
| 428 | { | ||
| 429 | print STDERR "Starting count.pl $count_str $cwd/$tmp_cnt $cwd/$tmp_txt\n"; | ||
| 430 | } | ||
| 431 | |||
| 432 | # call count.pl for this plain text | ||
| 433 | $status=system("count.pl $count_str $cwd/$tmp_cnt $cwd/$tmp_txt "); | ||
| 434 | die "Error while running count.pl $count_str $cwd/$tmp_cnt $cwd/$tmp_txt" unless $status==0; | ||
| 435 | |||
| 436 | if(defined $opt_verbose) | ||
| 437 | { | ||
| 438 | print STDERR "Finished count.pl $count_str $cwd/$tmp_cnt $cwd/$tmp_txt\n"; | ||
| 439 | } | ||
| 440 | |||
| 441 | # check the $tmp_cnt file. If does not have any bigram do not proceed to statistic.pl | ||
| 442 | open(TP,"$cwd/$tmp_cnt") || die "Error opening $cwd/$tmp_cnt file\n"; | ||
| 443 | |||
| 444 | # check the no. of bigrams specified by count.pl on 1st line of o/p file | ||
| 445 | $cnt = <TP>; | ||
| 446 | |||
| 447 | close TP; | ||
| 448 | |||
| 449 | # if no. of bigrams more than 0 then proceed | ||
| 450 | if($cnt > 0) | ||
| 451 | { | ||
| 452 | my $tmp_stat = "tmp.$time_stamp.cluster.$cId.stat"; | ||
| 453 | |||
| 454 | if(defined $opt_verbose) | ||
| 455 | { | ||
| 456 | print STDERR "Starting statistic.pl $stat_str $cwd/$tmp_stat $cwd/$tmp_cnt\n"; | ||
| 457 | } | ||
| 458 | |||
| 459 | # call statistic.pl on count.pl's o/p | ||
| 460 | $status=system("statistic.pl $stat_str $cwd/$tmp_stat $cwd/$tmp_cnt "); | ||
| 461 | die "Error while running statistic.pl $stat_str $cwd/$tmp_stat $cwd/$tmp_cnt" unless $status==0; | ||
| 462 | |||
| 463 | if(defined $opt_verbose) | ||
| 464 | { | ||
| 465 | print STDERR "Finished statistic.pl $stat_str $cwd/$tmp_stat $cwd/$tmp_cnt\n"; | ||
| 466 | } | ||
| 467 | |||
| 468 | if(defined $opt_verbose) | ||
| 469 | { | ||
| 470 | print STDERR "Starting selection of labels...\n"; | ||
| 471 | } | ||
| 472 | |||
| 473 | # format statistic.pl's o/p to be shown as labels for the cluster | ||
| 474 | open(FP,"$cwd/$tmp_stat") || die "Error while opening the file $cwd/$tmp_stat"; | ||
| 475 | |||
| 476 | <FP>; | ||
| 477 | |||
| 478 | |||
| 479 | while(<FP>) | ||
| 480 | { | ||
| 481 | @tmp = split(/<>/); | ||
| 482 | |||
| 483 | # Following code will support the ngram features for label. | ||
| 484 | $label = ""; | ||
| 485 | |||
| 486 | # If ngram is defined, use that as features. | ||
| 487 | if(defined $opt_ngram){ | ||
| 488 | $labelSize = $opt_ngram; | ||
| 489 | foreach $tempName (@tmp) { | ||
| 490 | if($labelSize > 0){ | ||
| 491 | $label = $label." ".$tempName; | ||
| 492 | $labelSize--; | ||
| 493 | }else{ | ||
| 494 | last; | ||
| 495 | } | ||
| 496 | } | ||
| 497 | }else{ | ||
| 498 | # If ngram is not defined then default feature is bigram. | ||
| 499 | $label = "$tmp[0] $tmp[1]"; | ||
| 500 | } | ||
| 501 | |||
| 502 | |||
| 503 | $l_aoh[$array_cnt]{$label} = $cId; | ||
| 504 | } | ||
| 505 | |||
| 506 | close FP; | ||
| 507 | |||
| 508 | # delete the temporary files | ||
| 509 | unlink "$cwd/$tmp_txt", "$cwd/$tmp_cnt", "$cwd/$tmp_stat"; | ||
| 510 | |||
| 511 | $array_cnt++; | ||
| 512 | } | ||
| 513 | else | ||
| 514 | { | ||
| 515 | if($cId ne "-1") | ||
| 516 | { | ||
| 517 | # no bigrams were returned by count.pl | ||
| 518 | # thus print just the cluster id | ||
| 519 | $no_label_clusters .= "Cluster $cId: \n"; | ||
| 520 | } | ||
| 521 | else # misc cluster | ||
| 522 | { | ||
| 523 | $no_lbl_misc_clust = "Cluster -1:"; | ||
| 524 | } | ||
| 525 | |||
| 526 | # and delete the temporary files (note *.stat never gets created in this case) | ||
| 527 | unlink "$cwd/$tmp_txt", "$cwd/$tmp_cnt"; | ||
| 528 | } | ||
| 529 | } | ||
| 530 | |||
| 531 | # find the unique/discriminating labels | ||
| 532 | |||
| 533 | # check each label for its uniqueness. | ||
| 534 | # if unique add to the label string else add to the hash of non-unique labels | ||
| 535 | |||
| 536 | $non_uni = {}; | ||
| 537 | |||
| 538 | for $i ( 0 .. $#l_aoh ) | ||
| 539 | { | ||
| 540 | $labels = ""; | ||
| 541 | for $key (keys %{$l_aoh[$i]} ) | ||
| 542 | { | ||
| 543 | $clusId = $l_aoh[$i]{$key}; | ||
| 544 | $c_lab = $key; | ||
| 545 | |||
| 546 | # first check in the non-unique hash | ||
| 547 | if(!exists $non_uni{$c_lab}) | ||
| 548 | { | ||
| 549 | # now check in all the other hashes i.e.clusters | ||
| 550 | $flag = 0; | ||
| 551 | for $j ( 0 .. $#l_aoh ) | ||
| 552 | { | ||
| 553 | if($j == $i) | ||
| 554 | { | ||
| 555 | next; | ||
| 556 | } | ||
| 557 | |||
| 558 | if(exists $l_aoh[$j]{$c_lab}) | ||
| 559 | { | ||
| 560 | $non_uni{$c_lab} = $c_lab; | ||
| 561 | $flag = 1; | ||
| 562 | last; | ||
| 563 | } | ||
| 564 | } | ||
| 565 | |||
| 566 | # found unique label | ||
| 567 | if($flag == 0) | ||
| 568 | { | ||
| 569 | $labels .= $c_lab . ", "; | ||
| 570 | } | ||
| 571 | } | ||
| 572 | } | ||
| 573 | |||
| 574 | # remove the extra ',' and space at the end | ||
| 575 | $labels = substr($labels, 0, length($labels)-2); | ||
| 576 | |||
| 577 | |||
| 578 | # for descriptive labels | ||
| 579 | $desc_labels = ""; | ||
| 580 | for $key (keys %{$l_aoh[$i]} ) | ||
| 581 | { | ||
| 582 | $desc_labels .= $key . ", "; | ||
| 583 | } | ||
| 584 | |||
| 585 | # remove the extra ',' and space at the end | ||
| 586 | $desc_labels = substr($desc_labels, 0, length($desc_labels)-2); | ||
| 587 | |||
| 588 | if($clusId ne "-1") | ||
| 589 | { | ||
| 590 | # print the descriptive labels with the cluster id | ||
| 591 | print "Cluster $clusId (Descriptive): $desc_labels\n\n"; | ||
| 592 | |||
| 593 | # print the discriminating labels with the cluster id | ||
| 594 | print "Cluster $clusId (Discriminating): $labels\n\n"; | ||
| 595 | } | ||
| 596 | else # misc cluster (Cluster -1) | ||
| 597 | { | ||
| 598 | $desc_misc_clust = $desc_labels; | ||
| 599 | $disc_misc_clust = $labels; | ||
| 600 | } | ||
| 601 | } | ||
| 602 | |||
| 603 | # print the clusters without any labels at the end. | ||
| 604 | print $no_label_clusters; | ||
| 605 | |||
| 606 | # print the labels for the misc cluster (Cluster -1) | ||
| 607 | # If: Misc cluster present but no labels found | ||
| 608 | if(defined $no_lbl_misc_clust) | ||
| 609 | { | ||
| 610 | print $no_lbl_misc_clust; | ||
| 611 | } | ||
| 612 | elsif(defined $desc_misc_clust) # Misc cluster present and labels identified too | ||
| 613 | { | ||
| 614 | print "Cluster -1 (Descriptive): $desc_misc_clust\n\n"; | ||
| 615 | print "Cluster -1 (Discriminating): $disc_misc_clust\n\n"; | ||
| 616 | } | ||
| 617 | |||
| 618 | if(defined $opt_verbose) | ||
| 619 | { | ||
| 620 | print STDERR "Finished selection of labels...\n"; | ||
| 621 | } | ||
| 622 | |||
| 623 | |||
| 624 | #----------------------------------------------------------------------------- | ||
| 625 | #show minimal usage message | ||
| 626 | sub showminimal() | ||
| 627 | { | ||
| 628 | print "Usage: clusterlabeling.pl [OPTIONS] INPUTFILE"; | ||
| 629 | print "\nTYPE clusterlabeling.pl --help for help\n"; | ||
| 630 | } | ||
| 631 | |||
| 632 | #----------------------------------------------------------------------------- | ||
| 633 | #show help | ||
| 634 | sub showhelp() | ||
| 635 | { | ||
| 636 | print "Usage: clusterlabeling.pl [OPTIONS] INPUTFILE | ||
| 637 | |||
| 638 | Assigns labels to each cluster with the significant word pairs | ||
| 639 | found in the cluster contexts. Also separates the clusters in | ||
| 640 | different files. This is particularly useful for the web-interface. | ||
| 641 | |||
| 642 | Two types of labels are assigned to each cluster : Descriptive and | ||
| 643 | Discriminating. Descriptive labels are the top n significant word | ||
| 644 | pairs. Discriminating labels are the top word-pairs unique to the | ||
| 645 | cluster out of the top n significant word-pairs for the cluster. | ||
| 646 | |||
| 647 | INPUTFILE | ||
| 648 | |||
| 649 | File created by format_clusters.pl with --context option. | ||
| 650 | |||
| 651 | Optional Arguments: | ||
| 652 | |||
| 653 | --token TOKEN | ||
| 654 | |||
| 655 | A file containing Perl regex/s that define the tokenization scheme | ||
| 656 | in INPUTFILE file. | ||
| 657 | |||
| 658 | If --token is not specified, default token regex file | ||
| 659 | token.regex is searched in the current directory. | ||
| 660 | |||
| 661 | --prefix PRE | ||
| 662 | |||
| 663 | Specify a prefix to be used for the file names of the cluster files. | ||
| 664 | e.g. If the PRE is the prefix specified then cluster with id=0 will | ||
| 665 | have file name: PRE.cluster.0 | ||
| 666 | |||
| 667 | If prefix is not specified then prefix is created by concatenating | ||
| 668 | time stamp to the string expr. | ||
| 669 | |||
| 670 | --stop STOPFILE | ||
| 671 | |||
| 672 | A file of Perl regexes that define the stop list of words to be | ||
| 673 | excluded from the features. | ||
| 674 | |||
| 675 | STOPFILE could be specified with two modes - | ||
| 676 | |||
| 677 | 1. AND mode - declared by including \@stop.mode=AND on the | ||
| 678 | first line of the STOPFILE. Ignores word pairs in which | ||
| 679 | both words are stop words. | ||
| 680 | |||
| 681 | 2. OR mode - declared by including \@stop.mode=OR on the first | ||
| 682 | line of the STOPFILE. Ignores word pairs in which either | ||
| 683 | word (or both) is a stop word. [Default] | ||
| 684 | |||
| 685 | --remove N | ||
| 686 | |||
| 687 | Removes bigrams that occur less than N times. | ||
| 688 | |||
| 689 | Default value for this option is 5 | ||
| 690 | |||
| 691 | --window W | ||
| 692 | |||
| 693 | Specifies the window size for bigrams. Pairs of words that co-occur | ||
| 694 | within the specified window from each other (window W allows at most | ||
| 695 | W-2 intervening words) will form the bigram features. | ||
| 696 | |||
| 697 | Default window size is 2 which allows only consecutive word pairs. | ||
| 698 | |||
| 699 | --stat STAT | ||
| 700 | |||
| 701 | Specifies the statistical scores of association. The following are | ||
| 702 | available: | ||
| 703 | |||
| 704 | ll - Log Likelihood Ratio [default] | ||
| 705 | pmi - Point-Wise Mutual Information | ||
| 706 | tmi - True Mutual Information | ||
| 707 | x2 - Chi-Squared Test | ||
| 708 | phi - Phi Coefficient | ||
| 709 | tscore - T-Score | ||
| 710 | dice - Dice Coefficient | ||
| 711 | odds - Odds Ratio | ||
| 712 | leftFisher - Left Fisher's Test | ||
| 713 | rightFisher - Right Fisher's Test | ||
| 714 | |||
| 715 | --rank R | ||
| 716 | |||
| 717 | Word pairs ranking below R when arranged in descending order of | ||
| 718 | their test scores are ignored. | ||
| 719 | |||
| 720 | Default value for this option is 10 | ||
| 721 | |||
| 722 | --ngram n | ||
| 723 | |||
| 724 | This parameter allows user to enter the value of ngram for feature selections. | ||
| 725 | The supported values for n are 2, 3 and 4. | ||
| 726 | |||
| 727 | Default value for this option is 2 (i.e. default feature selectection is bigram). | ||
| 728 | |||
| 729 | --newLine | ||
| 730 | |||
| 731 | If turned on, word pair selection process will not span across newlines. | ||
| 732 | |||
| 733 | By default this option is turned off, that is, word pair selection spans | ||
| 734 | across lines. | ||
| 735 | |||
| 736 | Other Options: | ||
| 737 | |||
| 738 | --verbose | ||
| 739 | Displays to STDERR the current program status. | ||
| 740 | |||
| 741 | --help | ||
| 742 | Displays this message. | ||
| 743 | |||
| 744 | --version | ||
| 745 | Displays the version information. | ||
| 746 | |||
| 747 | Type 'perldoc clusterlabeling.pl' to view the detailed documentation.\n"; | ||
| 748 | } | ||
| 749 | |||
| 750 | #------------------------------------------------------------------------------ | ||
| 751 | #version information | ||
| 752 | sub showversion() | ||
| 753 | { | ||
| 754 | print '$Id: clusterlabeling.pl,v 1.35 2013/06/27 14:44:48 tpederse Exp $'; | ||
| 755 | print "\nLabel discovered clusters based on their content\n"; | ||
| 756 | # print "\nCopyright (c) 2004-2006, Ted Pedersen, & Anagha Kulkarni\n"; | ||
| 757 | # print "clusterlabeling.pl - Version 0.04\n"; | ||
| 758 | # print "Cluster labeling program.\n"; | ||
| 759 | # print "Date of Last Update: 01/22/2006\n"; | ||
| 760 | } | ||
| 761 | |||
| 762 | ############################################################################# | ||
| 763 | |||
| 764 | =head1 COPYRIGHT | ||
| 765 | |||
| 766 | Copyright (c) 2004-2008,2013 Anagha Kulkarni and Ted Pedersen | ||
| 767 | |||
| 768 | This program is free software; you can redistribute it and/or modify it | ||
| 769 | under the terms of the GNU General Public License as published by the Free | ||
| 770 | Software Foundation; either version 2 of the License, or (at your option) | ||
| 771 | any later version. | ||
| 772 | |||
| 773 | This program is distributed in the hope that it will be useful, but | ||
| 774 | WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY | ||
| 775 | or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | ||
| 776 | for more details. | ||
| 777 | |||
| 778 | You should have received a copy of the GNU General Public License along | ||
| 779 | with this program; if not, write to | ||
| 780 | |||
| 781 | The Free Software Foundation, Inc., | ||
| 782 | 59 Temple Place - Suite 330, | ||
| 783 | Boston, MA 02111-1307, USA. | ||
| 784 | |||
| 785 | =cut | ||
| 786 | |||
| 787 |