| 1 | #!/usr/local/bin/perl -w | ||
| 2 | |||
| 3 | =head1 NAME | ||
| 4 | |||
| 5 | format_clusters.pl - Map Cluto output to Senseval-2 format input file | ||
| 6 | |||
| 7 | =head1 SYNOPSIS | ||
| 8 | |||
| 9 | format_clusters.pl [OPTIONS] CLUTO_SOLUTION RLABEL | ||
| 10 | |||
| 11 | =head1 DESCRIPTION | ||
| 12 | |||
| 13 | This program maps Cluto's clustering solution file into Senseval2 input file | ||
| 14 | to give more legible forms of output. | ||
| 15 | |||
| 16 | =head1 INPUT | ||
| 17 | |||
| 18 | =head2 Required Arguments: | ||
| 19 | |||
| 20 | =head3 CLUTO_SOLUTION | ||
| 21 | |||
| 22 | This is an output file from Cluto that shows which cluster each context | ||
| 23 | is assigned to. This is referred to as *.cluster_solution by the | ||
| 24 | SenseClusters Web interface, or can be specified via the -clustfile option | ||
| 25 | in Cluto. It consists of N lines, where N is the number of contexts, each | ||
| 26 | each line contains an integer value indicating the cluster to which the | ||
| 27 | context represented by that line is assigned. | ||
| 28 | |||
| 29 | Each line of this file shows the cluster id assigned to the instance id, | ||
| 30 | specified at the same line number in *.rlabel file. The number of lines | ||
| 31 | in the CLUTO_SOLUTION file should be the same as in the RLABEL file. | ||
| 32 | |||
| 33 | =head3 RLABEL | ||
| 34 | |||
| 35 | Row Label shows the instance id to which the cluster id, specified at the same | ||
| 36 | line number in *.cluster_solution is assigned. | ||
| 37 | The file name has an extension as .rlabel | ||
| 38 | |||
| 39 | =head3 Other Options : | ||
| 40 | |||
| 41 | =head4 --context SENSEVAL2 | ||
| 42 | |||
| 43 | SENSEVAL2 should be a file of contexts formatted in the Senseval2 format. | ||
| 44 | These are the contexts that have been clustered. The --context option | ||
| 45 | causes the contexts to be reorganized such that those that occur in the | ||
| 46 | same cluster are grouped together. | ||
| 47 | |||
| 48 | =head4 --senseval2 SENSEVAL2 | ||
| 49 | |||
| 50 | SENSEVAL2 should be a file of contexts formatted in the Senseval2 format. | ||
| 51 | These are the contexts that have been clustered. The --senseval2 option | ||
| 52 | causes the contexts to be assigned (or tagged) with the cluster value | ||
| 53 | assigned by Cluto. This cluster value will be put into the answer tag. | ||
| 54 | They are displayed in their original order. | ||
| 55 | |||
| 56 | =head4 --help | ||
| 57 | |||
| 58 | Displays the summary of command line options. | ||
| 59 | |||
| 60 | =head4 --version | ||
| 61 | |||
| 62 | Displays the version information. | ||
| 63 | |||
| 64 | =head1 OUTPUT | ||
| 65 | |||
| 66 | If neither of the options (--context or --senseval2) are specified, | ||
| 67 | the default behavior is that contexts are identified by instance id *only* | ||
| 68 | and grouped together by clusters. Thus, the actual written contexts | ||
| 69 | are not displayed in this case. | ||
| 70 | |||
| 71 | Each line is formatted as - | ||
| 72 | |||
| 73 | <cluster id="CID"> | ||
| 74 | [<instance id="IID"/>]+ | ||
| 75 | </cluster> | ||
| 76 | |||
| 77 | If --context option is used, then all the instances | ||
| 78 | along with the actual context data, grouped by clusters are displayed. | ||
| 79 | The output sent to STDOUT looks like: | ||
| 80 | |||
| 81 | <cluster id="CID"> | ||
| 82 | [<instance id="IID"><context>DATA</context></instance>]+ | ||
| 83 | </cluster> | ||
| 84 | |||
| 85 | If --senseval2 option is used, then output is copy of the | ||
| 86 | input senseval2 file except that now, answer tags contain | ||
| 87 | cluster id assigned to the instance. | ||
| 88 | The output is sent to STDOUT. | ||
| 89 | |||
| 90 | Note: --context and --senseval2 cannot be used together. | ||
| 91 | |||
| 92 | =head1 BUGS | ||
| 93 | |||
| 94 | =head1 SYSTEM REQUIREMENTS | ||
| 95 | |||
| 96 | =over | ||
| 97 | |||
| 98 | =item Cluto - L<http://www-users.cs.umn.edu/~karypis/cluto/> | ||
| 99 | |||
| 100 | =back | ||
| 101 | |||
| 102 | =head1 AUTHORS | ||
| 103 | |||
| 104 | Ted Pedersen, University of Minnesota, Duluth | ||
| 105 | |||
| 106 | Amruta Purandare, University of Pittsburgh | ||
| 107 | |||
| 108 | Anagha Kulkarni, Carnegie-Mellon University | ||
| 109 | |||
| 110 | =head1 COPYRIGHT | ||
| 111 | |||
| 112 | Copyright (c) 2002-2008, Ted Pedersen, Amruta Purandare, Anagha Kulkarni | ||
| 113 | |||
| 114 | This program is free software; you can redistribute it and/or modify it under | ||
| 115 | the terms of the GNU General Public License as published by the Free Software | ||
| 116 | Foundation; either version 2 of the License, or (at your option) any later | ||
| 117 | version. | ||
| 118 | |||
| 119 | This program is distributed in the hope that it will be useful, but WITHOUT | ||
| 120 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS | ||
| 121 | FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. | ||
| 122 | |||
| 123 | You should have received a copy of the GNU General Public License along with | ||
| 124 | this program; if not, write to | ||
| 125 | |||
| 126 | The Free Software Foundation, Inc., | ||
| 127 | 59 Temple Place - Suite 330, | ||
| 128 | Boston, MA 02111-1307, USA. | ||
| 129 | |||
| 130 | =cut | ||
| 131 | |||
| 132 | ############################################################################## | ||
| 133 | # | ||
| 134 | # =============================== | ||
| 135 | # COMMAND LINE OPTIONS AND USAGE | ||
| 136 | # =============================== | ||
| 137 | |||
| 138 | #$0 contains the program name along with | ||
| 139 | #the complete path. Extract just the program | ||
| 140 | #name and use in error messages | ||
| 141 | $0=~s/.*\/(.+)/$1/; | ||
| 142 | |||
| 143 | use Getopt::Long; | ||
| 144 | GetOptions ("context=s","senseval2=s","help","version"); | ||
| 145 | |||
| 146 | #if context and senseval2 both the options are specified | ||
| 147 | #display an error message and exit. | ||
| 148 | if(defined $opt_context && defined $opt_senseval2) | ||
| 149 | { | ||
| 150 | print STDERR "ERROR($0): | ||
| 151 | --context and --senseval2 options cannot be specified in a single run!\n"; | ||
| 152 | exit 1; | ||
| 153 | } | ||
| 154 | |||
| 155 | #command option for context | ||
| 156 | if(defined $opt_context) | ||
| 157 | { | ||
| 158 | $inp_context = $opt_context; | ||
| 159 | if(!-e $inp_context) | ||
| 160 | { | ||
| 161 | print STDERR "ERROR($0): | ||
| 162 | Senseval2 formatted input file $inp_context does not exist.\n"; | ||
| 163 | exit 1; | ||
| 164 | } | ||
| 165 | |||
| 166 | # check if the file $inp_context is a Senseval2 formatted file | ||
| 167 | open(SCON, $inp_context) || die "Error in opening the senseval2 input file $inp_context. \n"; | ||
| 168 | |||
| 169 | # read the complete file in single instruction instead of reading line by line. | ||
| 170 | my $temp_delimiter = $/; | ||
| 171 | $/ = undef; | ||
| 172 | |||
| 173 | my $inp_str = <SCON>; | ||
| 174 | |||
| 175 | $/ = $temp_delimiter; | ||
| 176 | close SCON; | ||
| 177 | |||
| 178 | if($inp_str !~ m/<corpus/i || $inp_str !~ m/<lexelt/i || $inp_str !~ m/<instance/i || $inp_str !~ m/<context/i) | ||
| 179 | { | ||
| 180 | print STDERR "ERROR($0): | ||
| 181 | File <$inp_context> is not in Senseval-2 format. \n"; | ||
| 182 | exit 1; | ||
| 183 | } | ||
| 184 | } | ||
| 185 | |||
| 186 | #command option for senseval2 | ||
| 187 | if(defined $opt_senseval2) | ||
| 188 | { | ||
| 189 | $inp_sval = $opt_senseval2; | ||
| 190 | if(!-e $inp_sval) | ||
| 191 | { | ||
| 192 | print STDERR "ERROR($0): | ||
| 193 | Senseval2 formatted input file $inp_sval does not exist.\n"; | ||
| 194 | exit 1; | ||
| 195 | } | ||
| 196 | |||
| 197 | # check if the file $inp_sval is a Senseval2 formatted file | ||
| 198 | open(SVAL, $inp_sval) || die "Error in opening the senseval2 input file $inp_sval. \n"; | ||
| 199 | |||
| 200 | # read the complete file in single instruction instead of reading line by line. | ||
| 201 | my $temp_delimiter = $/; | ||
| 202 | $/ = undef; | ||
| 203 | |||
| 204 | my $inp_str = <SVAL>; | ||
| 205 | |||
| 206 | $/ = $temp_delimiter; | ||
| 207 | close SVAL; | ||
| 208 | |||
| 209 | if($inp_str !~ m/<corpus/i || $inp_str !~ m/<lexelt/i || $inp_str !~ m/<instance/i || $inp_str !~ m/<context/i) | ||
| 210 | { | ||
| 211 | print STDERR "ERROR($0): | ||
| 212 | File <$inp_sval> is not in Senseval-2 format. \n"; | ||
| 213 | |||
| 214 | exit 1; | ||
| 215 | } | ||
| 216 | } | ||
| 217 | |||
| 218 | #command option for help | ||
| 219 | if(defined $opt_help) | ||
| 220 | { | ||
| 221 | $opt_help=1; | ||
| 222 | &showhelp(); | ||
| 223 | exit; | ||
| 224 | } | ||
| 225 | |||
| 226 | #version information | ||
| 227 | if(defined $opt_version) | ||
| 228 | { | ||
| 229 | $opt_version=1; | ||
| 230 | &showversion(); | ||
| 231 | exit; | ||
| 232 | } | ||
| 233 | |||
| 234 | #show minimal usage | ||
| 235 | if($#ARGV<1) | ||
| 236 | { | ||
| 237 | &showminimal(); | ||
| 238 | exit; | ||
| 239 | } | ||
| 240 | |||
| 241 | $cluster_solution=$ARGV[0]; | ||
| 242 | |||
| 243 | if(!-e $cluster_solution) | ||
| 244 | { | ||
| 245 | print STDERR "ERROR($0): | ||
| 246 | Cluster solution file <$cluster_solution> does not exist.\n"; | ||
| 247 | exit 1; | ||
| 248 | } | ||
| 249 | $rlabel=$ARGV[1]; | ||
| 250 | |||
| 251 | if(!-e $rlabel) | ||
| 252 | { | ||
| 253 | print STDERR "ERROR($0): | ||
| 254 | Rlabel file <$rlabel> does not exist.\n"; | ||
| 255 | exit 1; | ||
| 256 | } | ||
| 257 | |||
| 258 | # check if the rlabel and the cluster_solution files have equal number of lines. | ||
| 259 | my $csol_lines = `wc $cluster_solution` || die "Error in executing the command \"wc $cluster_solution\"\n"; | ||
| 260 | my $rlabel_lines = `wc $rlabel` || die "Error in executing the command \"wc $rlabel\"\n"; | ||
| 261 | |||
| 262 | $csol_lines =~ /^\s*(\d+)\s*/; | ||
| 263 | $csol_lines = $1; | ||
| 264 | |||
| 265 | $rlabel_lines =~ /^\s*(\d+)\s*/; | ||
| 266 | $rlabel_lines = $1; | ||
| 267 | |||
| 268 | if($csol_lines != $rlabel_lines) | ||
| 269 | { | ||
| 270 | print STDERR "ERROR($0): | ||
| 271 | Number of lines in files <$cluster_solution> and <$rlabel> do not match. | ||
| 272 | File <$cluster_solution> contains $csol_lines lines while <$rlabel> | ||
| 273 | contains $rlabel_lines lines.\n"; | ||
| 274 | exit 1; | ||
| 275 | } | ||
| 276 | |||
| 277 | if(defined $inp_context) | ||
| 278 | { | ||
| 279 | open(SCON, $inp_context) || die "Error in opening the senseval2 input file $inp_context. \n"; | ||
| 280 | } | ||
| 281 | |||
| 282 | if(defined $inp_sval) | ||
| 283 | { | ||
| 284 | open(SVAL, $inp_sval) || die "Error in opening the senseval2 input file $inp_sval. \n"; | ||
| 285 | } | ||
| 286 | |||
| 287 | open(CLSOL,$cluster_solution) || die "Error in opening the cluster solution file <$cluster_solution>\n"; | ||
| 288 | open(RLAB, $rlabel) || die "Error in opening the rlabel file <$rlabel>\n"; | ||
| 289 | |||
| 290 | while($cluster = <CLSOL>) | ||
| 291 | { | ||
| 292 | chomp($cluster); | ||
| 293 | |||
| 294 | if($cluster !~ /\-?\d\d*/) | ||
| 295 | { | ||
| 296 | print STDERR "ERROR($0): | ||
| 297 | File <$cluster_solution> contains non-numeric values. | ||
| 298 | It should contain only numeric cluster-IDs.\n"; | ||
| 299 | exit 1; | ||
| 300 | } | ||
| 301 | |||
| 302 | $rlabel = <RLAB>; | ||
| 303 | chomp($rlabel); | ||
| 304 | |||
| 305 | #if context option specified, extract the context for each instance | ||
| 306 | if(defined $inp_context) | ||
| 307 | { | ||
| 308 | #read the senseval2 file for instance tag | ||
| 309 | do{ | ||
| 310 | $sentence = <SCON>; | ||
| 311 | }until($sentence =~ /instance id=\"(.+?)\" /g || $sentence =~ /instance id=\"(.+?)\">/g); | ||
| 312 | $instance_id = $1; | ||
| 313 | |||
| 314 | #if the instance tag id matches with the rlabel, which should be. | ||
| 315 | if($instance_id eq $rlabel) | ||
| 316 | { | ||
| 317 | # check if the next tag is answer tag or context tag | ||
| 318 | $sentence = <SCON>; | ||
| 319 | |||
| 320 | # if answer tag - discard and continue with the next | ||
| 321 | # tag - context tag | ||
| 322 | if($sentence =~ m/<answer/) | ||
| 323 | { | ||
| 324 | #extract the context data along with the tags | ||
| 325 | $context = ""; | ||
| 326 | #extract the context tag | ||
| 327 | $sentence = <SCON>; | ||
| 328 | $context = " " . $sentence . " "; | ||
| 329 | } | ||
| 330 | else | ||
| 331 | { | ||
| 332 | #extracted tag is the context tag | ||
| 333 | $context = " " . $sentence . " "; | ||
| 334 | } | ||
| 335 | |||
| 336 | $sentence = <SCON>; | ||
| 337 | while($sentence !~ /\/context/) | ||
| 338 | { | ||
| 339 | $context .= $sentence; | ||
| 340 | $sentence = <SCON>; | ||
| 341 | } | ||
| 342 | $context .= " " . $sentence; | ||
| 343 | |||
| 344 | #update the hash with instance tag, context tag and data | ||
| 345 | $cluster{$cluster}.= " <instance id=\"$rlabel\">\n " . $context . " <\/instance>\n"; | ||
| 346 | } | ||
| 347 | } | ||
| 348 | elsif(defined $inp_sval) | ||
| 349 | { | ||
| 350 | $instance_id = ""; | ||
| 351 | #read the senseval2 file for instance tag | ||
| 352 | do{ | ||
| 353 | $sentence = <SVAL>; | ||
| 354 | print $sentence; | ||
| 355 | }until($sentence =~ /instance id=\"(.+?)\"/); | ||
| 356 | $instance_id = $1; | ||
| 357 | |||
| 358 | # }until($sentence =~ /instance id=\"(.+)\" /g || $sentence =~ /instance id=\"(.+)\">/g); | ||
| 359 | # }until($sentence =~ /instance id=\"([^\"]+)\"/g); | ||
| 360 | |||
| 361 | #if the instance tag id matches with the rlabel, which should be. | ||
| 362 | if($instance_id eq $rlabel) | ||
| 363 | { | ||
| 364 | #modify the answer tag line. | ||
| 365 | $sentence = <SVAL>; | ||
| 366 | |||
| 367 | #check if answer tag present, if present add the cluster id | ||
| 368 | #else add add an answer tag with the instance id and cluster id | ||
| 369 | if($sentence =~ /(<answer.+senseid=\".+?\")/) | ||
| 370 | { | ||
| 371 | $new_answer = $1 . ' cluster="' .$cluster . '"/>' . "\n"; | ||
| 372 | print $new_answer; | ||
| 373 | } | ||
| 374 | else | ||
| 375 | { | ||
| 376 | $new_answer = "<answer instance=\"$instance_id\" cluster=\"$cluster\"/>\n"; | ||
| 377 | print $new_answer; | ||
| 378 | print $sentence; | ||
| 379 | } | ||
| 380 | |||
| 381 | #extract the context data along with the tags | ||
| 382 | $context = ""; | ||
| 383 | $sentence = <SVAL>; | ||
| 384 | print $sentence; | ||
| 385 | $context = " " . $sentence . " "; | ||
| 386 | |||
| 387 | $sentence = <SVAL>; | ||
| 388 | print $sentence; | ||
| 389 | while($sentence !~ /\/context/) | ||
| 390 | { | ||
| 391 | $context .= $sentence; | ||
| 392 | $sentence = <SVAL>; | ||
| 393 | print $sentence; | ||
| 394 | } | ||
| 395 | $context .= " " . $sentence; | ||
| 396 | } | ||
| 397 | } | ||
| 398 | else | ||
| 399 | { | ||
| 400 | $cluster{$cluster}.= " <instance id=\"$rlabel\"\/>\n"; | ||
| 401 | } | ||
| 402 | } | ||
| 403 | |||
| 404 | if(defined $inp_sval) | ||
| 405 | { | ||
| 406 | while($sentence = <SVAL>) | ||
| 407 | { | ||
| 408 | print $sentence; | ||
| 409 | } | ||
| 410 | } | ||
| 411 | else | ||
| 412 | { | ||
| 413 | # numerically sort the cluster ids. | ||
| 414 | foreach $key (sort {$a+0 <=> $b+0} keys %cluster) | ||
| 415 | { | ||
| 416 | print "<cluster id=\"$key\">\n"; | ||
| 417 | print "$cluster{$key}<\/cluster>\n"; | ||
| 418 | } | ||
| 419 | } | ||
| 420 | |||
| 421 | #show minimal usage message | ||
| 422 | sub showminimal() | ||
| 423 | { | ||
| 424 | print STDERR "Usage: format_clusters.pl [OPTIONS] CLUTO_SOLUTION RLABEL\n"; | ||
| 425 | print STDERR "TYPE format_clusters.pl --help for help\n"; | ||
| 426 | } | ||
| 427 | |||
| 428 | #show help | ||
| 429 | sub showhelp() | ||
| 430 | { | ||
| 431 | print "Usage: format_clusters.pl [OPTIONS] CLUTO_SOLUTION RLABEL | ||
| 432 | |||
| 433 | Maps Cluto's clustering solution file into Senseval2 input file | ||
| 434 | to give more legible forms of output. | ||
| 435 | |||
| 436 | CLUTO_SOLUTION | ||
| 437 | Cluto's clustering solution file. | ||
| 438 | |||
| 439 | RLABEL | ||
| 440 | Row Label file given to Cluto. | ||
| 441 | |||
| 442 | OPTIONS: | ||
| 443 | |||
| 444 | --context SENSEVAL2 | ||
| 445 | |||
| 446 | SENSEVAL2 is the file in Senseval2 format that has been clustered. | ||
| 447 | Displays contexts (actual data) along with the instance tag, | ||
| 448 | grouped by the cluster id. This reorders SENSEVAL2 by cluster id. | ||
| 449 | Note: --context and --senseval2 cannot be used together. | ||
| 450 | |||
| 451 | --senseval2 SENSEVAL2 | ||
| 452 | |||
| 453 | SENSEVAL2 is the file in Senseval2 format that has been clustered. | ||
| 454 | Displays the cluster id that has been assigned to the instance | ||
| 455 | by Cluto, in the answer tag. This does not reorder SENSEVAL2. | ||
| 456 | Note: --context and --senseval2 cannot be used together. | ||
| 457 | |||
| 458 | --help | ||
| 459 | Displays this message. | ||
| 460 | |||
| 461 | --version | ||
| 462 | Displays the version information.\n | ||
| 463 | |||
| 464 | Note: For more detailed description type 'perldoc format_clusters.pl' | ||
| 465 | at command prompt.\n"; | ||
| 466 | } | ||
| 467 | |||
| 468 | #version information | ||
| 469 | sub showversion() | ||
| 470 | { | ||
| 471 | print '$Id: format_clusters.pl,v 1.23 2008/03/30 05:06:07 tpederse Exp $'; | ||
| 472 | # print "\nCopyright (c) 2002-2006, Ted Pedersen, Amruta Purandare, & Anagha Kulkarni\n"; | ||
| 473 | # print "format_clusters.pl - Version 0.02\n"; | ||
| 474 | print "\nMap Cluto solution into Senseval-2 data to make output more readable\n"; # print "Date of Last Update: 09/11/2005\n"; | ||
| 475 | } | ||
| 476 | |||
| 477 |