| 1 | #!/usr/local/bin/perl -w | ||
| 2 | |||
| 3 | =head1 NAME | ||
| 4 | |||
| 5 | bitsimat.pl - Build a similarity matrix from binary context vectors | ||
| 6 | |||
| 7 | =head1 SYNOPSIS | ||
| 8 | |||
| 9 | bitsimat.pl [OPTIONS] VECTOR | ||
| 10 | |||
| 11 | The input file represents 5 vectors, each with 4 possible features. The | ||
| 12 | format of the input file is sparse, so if a feature has no value it is | ||
| 13 | not listed. | ||
| 14 | |||
| 15 | cat input | ||
| 16 | |||
| 17 | Output => | ||
| 18 | |||
| 19 | 5 4 12 | ||
| 20 | 1 1 3 1 4 1 | ||
| 21 | 2 1 3 1 | ||
| 22 | 1 1 3 1 4 1 | ||
| 23 | 2 1 3 1 | ||
| 24 | 2 1 3 1 | ||
| 25 | |||
| 26 | Compute the pairwise similarities between all 5 binary vectors and | ||
| 27 | display them in a 5x5 matrix. | ||
| 28 | |||
| 29 | bitsimat.pl input --format f4.2 | ||
| 30 | |||
| 31 | Output => | ||
| 32 | |||
| 33 | 5 25 | ||
| 34 | 1 1.00 2 0.41 3 1.00 4 0.41 5 0.41 | ||
| 35 | 1 0.41 2 1.00 3 0.41 4 1.00 5 1.00 | ||
| 36 | 1 1.00 2 0.41 3 1.00 4 0.41 5 0.41 | ||
| 37 | 1 0.41 2 1.00 3 0.41 4 1.00 5 1.00 | ||
| 38 | 1 0.41 2 1.00 3 0.41 4 1.00 5 1.00 | ||
| 39 | |||
| 40 | Type C<bitsimat.pl --help> for a quick summary of options | ||
| 41 | |||
| 42 | =head1 DESCRIPTION | ||
| 43 | |||
| 44 | Constructs a similarity matrix for the given binary context vectors. A | ||
| 45 | similarity matrix shows the pairwise similarities between all the | ||
| 46 | different vectors. Vectors are represented in an N x M matrix, where N | ||
| 47 | is the number of vectors and M is the number of features. All NxN | ||
| 48 | combinations of vector pairs will be measured for similarity and stored | ||
| 49 | in a matrix. | ||
| 50 | |||
| 51 | =head1 INPUT | ||
| 52 | |||
| 53 | =head2 Required Arguments: | ||
| 54 | |||
| 55 | =head3 VECTOR | ||
| 56 | |||
| 57 | A binary vector file as created by vector constructor programs in Toolkit/vector. | ||
| 58 | |||
| 59 | =head4 Sparse Format (default) | ||
| 60 | |||
| 61 | By default, VECTOR is assumed to be in sparse format. | ||
| 62 | |||
| 63 | For sparse vectors, the first line of the VECTOR file should show 3 | ||
| 64 | numbers separated in spaces as - | ||
| 65 | |||
| 66 | N M NNZ | ||
| 67 | |||
| 68 | where | ||
| 69 | |||
| 70 | N = Number of vectors/rows | ||
| 71 | M = Number of dimensions/columns | ||
| 72 | NNZ = Total number of non-zero values | ||
| 73 | |||
| 74 | Each line after this line should show a single sparse vector on each line. | ||
| 75 | A sparse vector is a list of pairs of numbers separated by space such | ||
| 76 | that the first number in a pair is the column index of a non-zero value in the | ||
| 77 | vector and the second number is the non-zero value itself corresponding to | ||
| 78 | that index. bitsimat treats all non-zero values as 1s in the bit vectors. | ||
| 79 | |||
| 80 | Column indices start with 1. | ||
| 81 | |||
| 82 | Sample Sparse Input | ||
| 83 | |||
| 84 | 9 12 19 | ||
| 85 | 4 1 7 1 | ||
| 86 | 1 1 | ||
| 87 | 1 1 4 1 8 1 11 1 | ||
| 88 | 1 1 7 1 | ||
| 89 | 4 1 | ||
| 90 | 5 1 10 1 | ||
| 91 | 1 1 | ||
| 92 | 5 1 8 1 12 1 | ||
| 93 | 1 1 2 1 8 1 | ||
| 94 | |||
| 95 | Explanation : | ||
| 96 | |||
| 97 | =over | ||
| 98 | |||
| 99 | =item 1. | ||
| 100 | |||
| 101 | The first line shows that there are total 9 sparse vectors | ||
| 102 | represented in 12 dimensions. Hence, bitsimat will consider next 9 lines | ||
| 103 | in the VECTOR file as 9 sparse vectors and will allow the column indices | ||
| 104 | only in the range [1-12]. Here, the total non-zero values are 19. | ||
| 105 | |||
| 106 | =item 2. | ||
| 107 | |||
| 108 | Note that, each line shows a single sparse vector as a list of | ||
| 109 | space separated 'index value' pairs. e.g. 2nd line shows that the 1st | ||
| 110 | vector is non-zero (with value 1) at column indices 4 and 7. 3rd line shows | ||
| 111 | that the 2nd sparse vector is non-zero only at index 1. And so on ... | ||
| 112 | |||
| 113 | =back | ||
| 114 | |||
| 115 | =head4 Dense Format | ||
| 116 | |||
| 117 | If VECTOR uses dense format, switch --dense should be selected. | ||
| 118 | |||
| 119 | The 1st line in the dense VECTOR file should show - | ||
| 120 | |||
| 121 | N M | ||
| 122 | |||
| 123 | for N vectors represented using M columns. | ||
| 124 | |||
| 125 | Each line thereafter should show a single dense vector. | ||
| 126 | All dense vectors should have M space separated numbers that indicate the | ||
| 127 | values at the corresponding columns in the vector. | ||
| 128 | |||
| 129 | All non-zero values are treated as 1s in the binary vectors. | ||
| 130 | |||
| 131 | Sample Dense Input | ||
| 132 | |||
| 133 | 9 12 | ||
| 134 | 0 0 0 1 0 0 1 0 0 0 0 0 | ||
| 135 | 1 0 0 0 0 0 0 0 0 0 0 0 | ||
| 136 | 1 0 0 1 0 0 0 1 0 0 1 0 | ||
| 137 | 1 0 0 0 0 0 1 0 0 0 0 0 | ||
| 138 | 0 0 0 1 0 0 0 0 0 0 0 0 | ||
| 139 | 0 0 0 0 1 0 0 0 0 1 0 0 | ||
| 140 | 1 0 0 0 0 0 0 0 0 0 0 0 | ||
| 141 | 0 0 0 0 1 0 0 1 0 0 0 1 | ||
| 142 | 1 1 0 0 0 0 0 1 0 0 0 0 | ||
| 143 | |||
| 144 | shows same vectors as shown in Sample Sparse Input in dense format. | ||
| 145 | |||
| 146 | VECTOR file could also optionally show the KEY file name on the first line. | ||
| 147 | If the VECTOR file starts with the <keyfile> name on the 1st line, the above | ||
| 148 | information should begin from the 2nd line onwards. | ||
| 149 | |||
| 150 | =head2 Optional Arguments: | ||
| 151 | |||
| 152 | =head4 --dense | ||
| 153 | |||
| 154 | This switch should be selected if the given VECTORs are in dense format. | ||
| 155 | This will also create the output similarity matrix in the dense format. By | ||
| 156 | default, sparse format is assumed for both input vectors and output | ||
| 157 | similarity matrix. | ||
| 158 | |||
| 159 | =head4 --measure SIM | ||
| 160 | |||
| 161 | Specify the similarity measure to be used to construct the similarity matrix. | ||
| 162 | |||
| 163 | Possible option values for --sim are | ||
| 164 | |||
| 165 | match Match Coefficient | ||
| 166 | dice Dice Coefficient | ||
| 167 | overlap Overlap Coefficient | ||
| 168 | jaccard Jaccard Coefficient | ||
| 169 | cosine Cosine Coefficient | ||
| 170 | |||
| 171 | The following table shows how the similarity values are computed by each | ||
| 172 | of these measures. | ||
| 173 | |||
| 174 | match |intersection(X,Y)| | ||
| 175 | dice 2*|intersection(X,Y)|/(|X|+|Y|) | ||
| 176 | overlap |intersection(X,Y)|/(min(|X|,|Y|)) | ||
| 177 | jaccard |intersection(X,Y)|/|union(X,Y)| | ||
| 178 | cosine |intersection(X,Y)|/sqrt(|X|*|Y|) | ||
| 179 | |||
| 180 | where X and Y represent any two binary vectors | ||
| 181 | |X| shows the norm or number of bits set in vector X | ||
| 182 | |||
| 183 | =head4 --format FORM | ||
| 184 | |||
| 185 | Specifies numeric format for representing output similarity values. | ||
| 186 | |||
| 187 | Possible values of FORM are | ||
| 188 | |||
| 189 | iN -> integer format allocating total N bytes/digits for each entry | ||
| 190 | |||
| 191 | fN.M -> floating point format allocating total N bytes/digits for each entry of which last M digits show the fractional part. | ||
| 192 | |||
| 193 | For matching coefficient, default is i8 and for other measures, default is | ||
| 194 | f16.10. | ||
| 195 | |||
| 196 | =head3 Other Options : | ||
| 197 | |||
| 198 | =head4 --help | ||
| 199 | |||
| 200 | Displays this message. | ||
| 201 | |||
| 202 | =head4 --version | ||
| 203 | |||
| 204 | Displays the version information. | ||
| 205 | |||
| 206 | =head1 OUTPUT | ||
| 207 | |||
| 208 | If the input VECTORs are in sparse format (default), output is also created | ||
| 209 | in sparse format, while, if the input vectors are in dense format, output | ||
| 210 | is also in dense format. | ||
| 211 | |||
| 212 | =head2 Sparse Output (Default) | ||
| 213 | |||
| 214 | The 1st line in the sparse output shows 2 space separated numbers - | ||
| 215 | |||
| 216 | N NNZ1 | ||
| 217 | |||
| 218 | where | ||
| 219 | |||
| 220 | N = Number of vectors (same as the N in the VECTOR file) | ||
| 221 | NNZ1 = Number of non-zero entries in the output similarity matrix | ||
| 222 | |||
| 223 | Each line after this will show the corresponding row of the | ||
| 224 | similarity matrix in sparse format. Specifically, each i'th line after the | ||
| 225 | above line shows the list of 'j SIM' pairs separated by space such that | ||
| 226 | SIM is the non-zero similarity value between the i'th and j'th vectors in the | ||
| 227 | given VECTOR file. | ||
| 228 | |||
| 229 | Note that, only those pairs are listed in which the SIM value is non-zero. | ||
| 230 | |||
| 231 | Sample Sparse Output | ||
| 232 | |||
| 233 | 9 43 | ||
| 234 | 1 1.000 3 0.354 4 0.500 5 0.707 | ||
| 235 | 2 1.000 3 0.500 4 0.707 7 1.000 9 0.577 | ||
| 236 | 1 0.354 2 0.500 3 1.000 4 0.354 5 0.500 7 0.500 8 0.289 9 0.577 | ||
| 237 | 1 0.500 2 0.707 3 0.354 4 1.000 7 0.707 9 0.408 | ||
| 238 | 1 0.707 3 0.500 5 1.000 | ||
| 239 | 6 1.000 8 0.408 | ||
| 240 | 2 1.000 3 0.500 4 0.707 7 1.000 9 0.577 | ||
| 241 | 3 0.289 6 0.408 8 1.000 9 0.333 | ||
| 242 | 2 0.577 3 0.577 4 0.408 7 0.577 8 0.333 9 1.000 | ||
| 243 | |||
| 244 | Shows the actual full similarity matrix - | ||
| 245 | |||
| 246 | 9 | ||
| 247 | 1.000 0.000 0.354 0.500 0.707 0.000 0.000 0.000 0.000 | ||
| 248 | 0.000 1.000 0.500 0.707 0.000 0.000 1.000 0.000 0.577 | ||
| 249 | 0.354 0.500 1.000 0.354 0.500 0.000 0.500 0.289 0.577 | ||
| 250 | 0.500 0.707 0.354 1.000 0.000 0.000 0.707 0.000 0.408 | ||
| 251 | 0.707 0.000 0.500 0.000 1.000 0.000 0.000 0.000 0.000 | ||
| 252 | 0.000 0.000 0.000 0.000 0.000 1.000 0.000 0.408 0.000 | ||
| 253 | 0.000 1.000 0.500 0.707 0.000 0.000 1.000 0.000 0.577 | ||
| 254 | 0.000 0.000 0.289 0.000 0.000 0.408 0.000 1.000 0.333 | ||
| 255 | 0.000 0.577 0.577 0.408 0.000 0.000 0.577 0.333 1.000 | ||
| 256 | |||
| 257 | Both these matrices show the pairwise cosine similarities among the same 9 | ||
| 258 | vectors shown in the Sample Sparse and Dense Input sections. | ||
| 259 | |||
| 260 | =head2 Dense Output | ||
| 261 | |||
| 262 | If --dense is selected, output is created in dense format and shows all | ||
| 263 | similarity values including 0s. | ||
| 264 | |||
| 265 | Dense output shows the number of vectors (N) on the first line. Each i'th line | ||
| 266 | after this shows N space separated numbers such that a number at column | ||
| 267 | index j shows the pairwise similarity among the i'th and j'th vectors. | ||
| 268 | |||
| 269 | Sample Dense Output | ||
| 270 | |||
| 271 | 9 | ||
| 272 | 1.000 0.000 0.354 0.500 0.707 0.000 0.000 0.000 0.000 | ||
| 273 | 0.000 1.000 0.500 0.707 0.000 0.000 1.000 0.000 0.577 | ||
| 274 | 0.354 0.500 1.000 0.354 0.500 0.000 0.500 0.289 0.577 | ||
| 275 | 0.500 0.707 0.354 1.000 0.000 0.000 0.707 0.000 0.408 | ||
| 276 | 0.707 0.000 0.500 0.000 1.000 0.000 0.000 0.000 0.000 | ||
| 277 | 0.000 0.000 0.000 0.000 0.000 1.000 0.000 0.408 0.000 | ||
| 278 | 0.000 1.000 0.500 0.707 0.000 0.000 1.000 0.000 0.577 | ||
| 279 | 0.000 0.000 0.289 0.000 0.000 0.408 0.000 1.000 0.333 | ||
| 280 | 0.000 0.577 0.577 0.408 0.000 0.000 0.577 0.333 1.000 | ||
| 281 | |||
| 282 | Shows the pairwise similarities among the 9 vectors shown in the Sample | ||
| 283 | Dense Input section. | ||
| 284 | |||
| 285 | Note that the similarity matrix will always be square and symmetric. | ||
| 286 | |||
| 287 | If the first line of the input VECTOR file shows the <keyfile> tag, | ||
| 288 | bitsimat.pl will display the same <keyfile> tag on the first line of | ||
| 289 | output. | ||
| 290 | |||
| 291 | =head1 SYSTEM REQUIREMENTS | ||
| 292 | |||
| 293 | bitsimat is dependent on the following CPAN modules : | ||
| 294 | |||
| 295 | =over | ||
| 296 | |||
| 297 | =item Bit::Vector - L<http://search.cpan.org/dist/Bit-Vector/> | ||
| 298 | |||
| 299 | =item Set::Scalar - L<http://search.cpan.org/dist/Set-Scalar/> | ||
| 300 | |||
| 301 | =back | ||
| 302 | |||
| 303 | |||
| 304 | =head1 AUTHORS | ||
| 305 | |||
| 306 | Amruta Purandare, University of Pittsburgh | ||
| 307 | |||
| 308 | Ted Pedersen, University of Minnesota, Duluth | ||
| 309 | tpederse at d.umn.edu | ||
| 310 | |||
| 311 | =head1 COPYRIGHT | ||
| 312 | |||
| 313 | Copyright (c) 2002-2008, Amruta Purandare and Ted Pedersen | ||
| 314 | |||
| 315 | This program is free software; you can redistribute it and/or modify it under | ||
| 316 | the terms of the GNU General Public License as published by the Free Software | ||
| 317 | Foundation; either version 2 of the License, or (at your option) any later | ||
| 318 | version. | ||
| 319 | |||
| 320 | This program is distributed in the hope that it will be useful, but WITHOUT | ||
| 321 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS | ||
| 322 | FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. | ||
| 323 | |||
| 324 | You should have received a copy of the GNU General Public License along with | ||
| 325 | this program; if not, write to | ||
| 326 | |||
| 327 | The Free Software Foundation, Inc., | ||
| 328 | 59 Temple Place - Suite 330, | ||
| 329 | Boston, MA 02111-1307, USA. | ||
| 330 | |||
| 331 | =cut | ||
| 332 | |||
| 333 | ############################################################################### | ||
| 334 | |||
| 335 | # THE CODE STARTS HERE | ||
| 336 | |||
| 337 | use Bit::Vector; | ||
| 338 | use Set::Scalar; | ||
| 339 | |||
| 340 | #$0 contains the program name along with | ||
| 341 | #the complete path. Extract just the program | ||
| 342 | #name and use in error messages | ||
| 343 | $0=~s/.*\/(.+)/$1/; | ||
| 344 | |||
| 345 | ############################################################################### | ||
| 346 | |||
| 347 | # ================================ | ||
| 348 | # COMMAND LINE OPTIONS AND USAGE | ||
| 349 | # ================================ | ||
| 350 | |||
| 351 | # command line options | ||
| 352 | use Getopt::Long; | ||
| 353 | GetOptions ("help","version","measure=s","format=s","dense"); | ||
| 354 | # show help option | ||
| 355 | if(defined $opt_help) | ||
| 356 | { | ||
| 357 | $opt_help=1; | ||
| 358 | &showhelp(); | ||
| 359 | exit; | ||
| 360 | } | ||
| 361 | |||
| 362 | # show version information | ||
| 363 | if(defined $opt_version) | ||
| 364 | { | ||
| 365 | $opt_version=1; | ||
| 366 | &showversion(); | ||
| 367 | exit; | ||
| 368 | } | ||
| 369 | |||
| 370 | # show minimal usage message if no arguments | ||
| 371 | if($#ARGV<0) | ||
| 372 | { | ||
| 373 | &showminimal(); | ||
| 374 | exit; | ||
| 375 | } | ||
| 376 | |||
| 377 | if(!defined $opt_measure) | ||
| 378 | { | ||
| 379 | $opt_measure="cosine"; | ||
| 380 | } | ||
| 381 | if($opt_measure !~ /(mat(ch)?)|(cos(ine)?)|(jac(card)?)|(dice)|(over(lap)?)/) | ||
| 382 | { | ||
| 383 | print STDERR "ERROR($0): | ||
| 384 | Wrong value of --measure=$opt_measure\n"; | ||
| 385 | exit; | ||
| 386 | } | ||
| 387 | |||
| 388 | ############################################################################# | ||
| 389 | |||
| 390 | # ================================ | ||
| 391 | # INITIALIZATION AND INPUT | ||
| 392 | # ================================ | ||
| 393 | |||
| 394 | if(!defined $ARGV[0]) | ||
| 395 | { | ||
| 396 | print STDERR "ERROR($0): | ||
| 397 | Please specify the Vector file name...\n"; | ||
| 398 | exit; | ||
| 399 | } | ||
| 400 | #accept the input file name | ||
| 401 | $infile=$ARGV[0]; | ||
| 402 | if(!-e $infile) | ||
| 403 | { | ||
| 404 | print STDERR "ERROR($0): | ||
| 405 | Vector file <$infile> doesn't exist...\n"; | ||
| 406 | exit; | ||
| 407 | } | ||
| 408 | open(IN,$infile) || die "Error($0): | ||
| 409 | Error(code=$!) in opening <$infile> file.\n"; | ||
| 410 | |||
| 411 | # format for printing output | ||
| 412 | if(defined $opt_format) | ||
| 413 | { | ||
| 414 | # integer | ||
| 415 | if($opt_format=~/^i(\d+)$/) | ||
| 416 | { | ||
| 417 | $format="%$1d"; | ||
| 418 | # as no sim. measure takes -ve value | ||
| 419 | # no need to check for underflow | ||
| 420 | $upper_format=""; | ||
| 421 | while(length($upper_format)<($1-1)) | ||
| 422 | { | ||
| 423 | $upper_format.="9"; | ||
| 424 | } | ||
| 425 | } | ||
| 426 | # float | ||
| 427 | elsif($opt_format=~/^f(\d+)\.(\d+)$/) | ||
| 428 | { | ||
| 429 | $format="%$1\.$2f"; | ||
| 430 | $upper_format=""; | ||
| 431 | while(length($upper_format)<($1-$2-2)) | ||
| 432 | { | ||
| 433 | $upper_format.="9"; | ||
| 434 | } | ||
| 435 | $upper_format.="."; | ||
| 436 | while(length($upper_format)<($1-1)) | ||
| 437 | { | ||
| 438 | $upper_format.="9"; | ||
| 439 | } | ||
| 440 | } | ||
| 441 | else | ||
| 442 | { | ||
| 443 | print STDERR "ERROR($0): | ||
| 444 | Wrong format value --format=$opt_format.\n"; | ||
| 445 | exit; | ||
| 446 | } | ||
| 447 | } | ||
| 448 | # default | ||
| 449 | else | ||
| 450 | { | ||
| 451 | # i8 for match | ||
| 452 | if($opt_measure=~/mat/) | ||
| 453 | { | ||
| 454 | $format="%8d"; | ||
| 455 | $upper_format="9999999"; | ||
| 456 | } | ||
| 457 | # f16.10 for other measures | ||
| 458 | else | ||
| 459 | { | ||
| 460 | $format="%16.10f"; | ||
| 461 | $upper_format="9999.9999999999"; | ||
| 462 | } | ||
| 463 | } | ||
| 464 | |||
| 465 | |||
| 466 | ############################################################################## | ||
| 467 | |||
| 468 | # ========================== | ||
| 469 | # CODE SECTION | ||
| 470 | # ========================== | ||
| 471 | |||
| 472 | if(defined $opt_dense) | ||
| 473 | { | ||
| 474 | $line_num=0; | ||
| 475 | while(<IN>) | ||
| 476 | { | ||
| 477 | $line_num++; | ||
| 478 | # trimming extra spaces | ||
| 479 | chomp; | ||
| 480 | s/\s*$//g; | ||
| 481 | s/^\s*//g; | ||
| 482 | s/\s+/ /g; | ||
| 483 | # line 1 should show either KEY file or rows cols | ||
| 484 | if($line_num==1) | ||
| 485 | { | ||
| 486 | $line=$_; | ||
| 487 | if(/keyfile/) | ||
| 488 | { | ||
| 489 | # when input starts with <keyfile> tag | ||
| 490 | # output displays same on the 1st line | ||
| 491 | print "$_\n"; | ||
| 492 | $line=<IN>; | ||
| 493 | $line_num++; | ||
| 494 | } | ||
| 495 | if($line=~/^\s*(\d+)\s+(\d+)\s*$/) | ||
| 496 | { | ||
| 497 | $rows=$1; | ||
| 498 | $cols=$2; | ||
| 499 | print "$rows\n"; | ||
| 500 | } | ||
| 501 | else | ||
| 502 | { | ||
| 503 | print STDERR "ERROR($0): | ||
| 504 | Line $line_num in Vector file <$infile> should show the number of rows | ||
| 505 | and columns.\n"; | ||
| 506 | exit; | ||
| 507 | } | ||
| 508 | next; | ||
| 509 | } | ||
| 510 | # each vector should have exactly specified number of elements | ||
| 511 | @row_ele=split(/\s+/); | ||
| 512 | if(scalar(@row_ele) != $cols) | ||
| 513 | { | ||
| 514 | print STDERR "ERROR($0): | ||
| 515 | Line $line_num in Vector file <$infile> should show $cols number of | ||
| 516 | elements.\n"; | ||
| 517 | exit; | ||
| 518 | } | ||
| 519 | # loading a bit vector | ||
| 520 | $vector=Bit::Vector->new($cols); | ||
| 521 | foreach $ind (0..$#row_ele) | ||
| 522 | { | ||
| 523 | if($row_ele[$ind] != 0) | ||
| 524 | { | ||
| 525 | $vector->Bit_On($ind); | ||
| 526 | } | ||
| 527 | } | ||
| 528 | push @vectors,$vector; | ||
| 529 | } | ||
| 530 | |||
| 531 | # number of vectors should be same as the number specified earlier at | ||
| 532 | # line 1 or 2 | ||
| 533 | if(scalar(@vectors) != $rows) | ||
| 534 | { | ||
| 535 | print STDERR "ERROR($0): | ||
| 536 | Vector file <$infile> doesn't contain $rows number of vectors.\n"; | ||
| 537 | exit; | ||
| 538 | } | ||
| 539 | |||
| 540 | # computing similarity measure between each pair of vectors | ||
| 541 | foreach $i (0..$rows-1) | ||
| 542 | { | ||
| 543 | foreach $j (0..$rows-1) | ||
| 544 | { | ||
| 545 | if($i != $j) | ||
| 546 | { | ||
| 547 | $inter=Bit::Vector->new($cols); | ||
| 548 | $inter->And($vectors[$i],$vectors[$j]); | ||
| 549 | $inter_size=$inter->Norm(); | ||
| 550 | if($opt_measure =~ /^mat(ch)?$/) | ||
| 551 | { | ||
| 552 | $sim=$inter_size; | ||
| 553 | } | ||
| 554 | elsif($opt_measure =~ /^dice$/) | ||
| 555 | { | ||
| 556 | $sim=(2*$inter_size)/($vectors[$i]->Norm()+$vectors[$j]->Norm()); | ||
| 557 | } | ||
| 558 | elsif($opt_measure =~ /^jac(card)?$/) | ||
| 559 | { | ||
| 560 | $union=Bit::Vector->new($cols); | ||
| 561 | $union->Union($vectors[$i],$vectors[$j]); | ||
| 562 | if($union->Norm()!=0) | ||
| 563 | { | ||
| 564 | $sim=($inter_size)/($union->Norm()); | ||
| 565 | } | ||
| 566 | else | ||
| 567 | { | ||
| 568 | $sim=0; | ||
| 569 | } | ||
| 570 | } | ||
| 571 | elsif($opt_measure =~ /^over(lap)?$/) | ||
| 572 | { | ||
| 573 | $size1=$vectors[$i]->Norm(); | ||
| 574 | $size2=$vectors[$j]->Norm(); | ||
| 575 | $min=($size1 < $size2) ? $size1 : $size2; | ||
| 576 | $sim=($inter_size)/$min; | ||
| 577 | } | ||
| 578 | elsif($opt_measure =~ /^cos(ine)?$/) | ||
| 579 | { | ||
| 580 | $size1=$vectors[$i]->Norm(); | ||
| 581 | $size2=$vectors[$j]->Norm(); | ||
| 582 | $denom=sqrt($size1*$size2); | ||
| 583 | if($denom != 0) | ||
| 584 | { | ||
| 585 | $sim=($inter_size)/$denom; | ||
| 586 | } | ||
| 587 | else | ||
| 588 | { | ||
| 589 | $sim=0; | ||
| 590 | } | ||
| 591 | } | ||
| 592 | } | ||
| 593 | else | ||
| 594 | { | ||
| 595 | if($vectors[$i]->Norm()) | ||
| 596 | { | ||
| 597 | $sim=1; | ||
| 598 | } | ||
| 599 | else | ||
| 600 | { | ||
| 601 | $sim=0; | ||
| 602 | } | ||
| 603 | } | ||
| 604 | $value=sprintf $format, $sim; | ||
| 605 | if($value>$upper_format) | ||
| 606 | { | ||
| 607 | print STDERR "ERROR($0): | ||
| 608 | Floating point overflow. | ||
| 609 | Value <$value> can't be represented with format $format.\n"; | ||
| 610 | exit 1; | ||
| 611 | } | ||
| 612 | print $value; | ||
| 613 | } | ||
| 614 | print "\n"; | ||
| 615 | } | ||
| 616 | } | ||
| 617 | else | ||
| 618 | { | ||
| 619 | $line_num=1; | ||
| 620 | $line=<IN>; | ||
| 621 | if($line=~/keyfile/) | ||
| 622 | { | ||
| 623 | print "$line"; | ||
| 624 | $line=<IN>; | ||
| 625 | $line_num++; | ||
| 626 | } | ||
| 627 | if($line=~/^\s*(\d+)\s+(\d+)\s+(\d+)\s*$/) | ||
| 628 | { | ||
| 629 | $rows=$1; | ||
| 630 | $cols=$2; | ||
| 631 | $nnz1=$3; | ||
| 632 | } | ||
| 633 | else | ||
| 634 | { | ||
| 635 | print STDERR "ERROR($0): | ||
| 636 | Line$line_num in Vector file <$infile> should show #rows #cols #nnz.\n"; | ||
| 637 | exit; | ||
| 638 | } | ||
| 639 | $nnz=0; | ||
| 640 | while(<IN>) | ||
| 641 | { | ||
| 642 | $line_num++; | ||
| 643 | chomp; | ||
| 644 | s/^\s*//; | ||
| 645 | s/\s*$//; | ||
| 646 | s/\s+/ /; | ||
| 647 | $set=Set::Scalar->new; | ||
| 648 | @pairs=split; | ||
| 649 | for($i=0;$i<$#pairs;$i=$i+2) | ||
| 650 | { | ||
| 651 | $index=$pairs[$i]; | ||
| 652 | if($index>$cols) | ||
| 653 | { | ||
| 654 | print STDERR "ERROR($0): | ||
| 655 | Index <$index> at line <$line_num> in VECTOR file <$infile> | ||
| 656 | exceeds #cols = <$cols> specified in the header line.\n"; | ||
| 657 | exit; | ||
| 658 | } | ||
| 659 | $value=$pairs[$i+1]; | ||
| 660 | if($value==0) | ||
| 661 | { | ||
| 662 | print STDERR "ERROR($0): | ||
| 663 | 0 value found in sparse vector at line <$line_num> in VECTOR file | ||
| 664 | <$infile>.\n"; | ||
| 665 | exit; | ||
| 666 | } | ||
| 667 | # add index to set | ||
| 668 | $set->insert($index); | ||
| 669 | $nnz++; | ||
| 670 | } | ||
| 671 | push @vectors,$set; | ||
| 672 | } | ||
| 673 | close IN; | ||
| 674 | if(scalar(@vectors) != $rows) | ||
| 675 | { | ||
| 676 | print STDERR "ERROR($0): | ||
| 677 | #rows = $rows specified in the header line of the VECTOR file <$infile> | ||
| 678 | does not match the actual #rows = " . scalar(@vectors) . " found in the file.\n"; | ||
| 679 | exit; | ||
| 680 | } | ||
| 681 | if($nnz != $nnz1) | ||
| 682 | { | ||
| 683 | print STDERR "ERROR($0): | ||
| 684 | #nnz = $nnz1 specified in the header line of the VECTOR file <$infile> | ||
| 685 | does not match the actual #nnz = $nnz found in the file.\n"; | ||
| 686 | exit; | ||
| 687 | } | ||
| 688 | |||
| 689 | # writing output to a TEMP file | ||
| 690 | $tempfile="tempfile" . time() . ".tmp"; | ||
| 691 | if(-e $tempfile) | ||
| 692 | { | ||
| 693 | print STDERR "ERROR($0): | ||
| 694 | Temporary file <$tempfile> should not already exist.\n"; | ||
| 695 | exit; | ||
| 696 | } | ||
| 697 | open(TEMP,">$tempfile") || die "Error($0): | ||
| 698 | Error(code=$!) in opening tempfile <$tempfile>.\n"; | ||
| 699 | |||
| 700 | $nnz=0; | ||
| 701 | foreach $i (0..$#vectors) | ||
| 702 | { | ||
| 703 | foreach $j (0..$#vectors) | ||
| 704 | { | ||
| 705 | undef $simcount; | ||
| 706 | if($i==$j && (!$vectors[$i]->is_null)) | ||
| 707 | { | ||
| 708 | $simcount=1; | ||
| 709 | } | ||
| 710 | else | ||
| 711 | { | ||
| 712 | $s=$vectors[$i]; | ||
| 713 | $t=$vectors[$j]; | ||
| 714 | # checking for null vectors | ||
| 715 | if((!$s->is_null) || (!$t->is_null)) | ||
| 716 | { | ||
| 717 | $inter_st=$s->intersection($t); | ||
| 718 | # match coefficient | ||
| 719 | if($opt_measure =~ /mat/) | ||
| 720 | { | ||
| 721 | # match = |(s,t)| | ||
| 722 | $simcount=$inter_st->size; | ||
| 723 | } | ||
| 724 | # dice | ||
| 725 | elsif($opt_measure =~ /dic/) | ||
| 726 | { | ||
| 727 | $sum_st=$s->size+$t->size; | ||
| 728 | # dice = 2* |(s,t)|/(|s|+|t|) | ||
| 729 | $simcount=(2*$inter_st->size)/$sum_st; | ||
| 730 | } | ||
| 731 | # jaccard | ||
| 732 | elsif($opt_measure =~ /jac/) | ||
| 733 | { | ||
| 734 | $union_st=$s->union($t); | ||
| 735 | # jaccard = |(s,t)|/(|sUt|) | ||
| 736 | $simcount=($inter_st->size)/($union_st->size); | ||
| 737 | } | ||
| 738 | # overlap | ||
| 739 | elsif($opt_measure =~ /ove?r/) | ||
| 740 | { | ||
| 741 | # overlap = |(s,t)|/min(|s|,|t|) | ||
| 742 | $min_st=($s->size<$t->size) ? $s->size:$t->size; | ||
| 743 | if($min_st!=0) | ||
| 744 | { | ||
| 745 | $simcount=($inter_st->size)/$min_st; | ||
| 746 | } | ||
| 747 | } | ||
| 748 | # cosine | ||
| 749 | elsif($opt_measure =~ /cos/) | ||
| 750 | { | ||
| 751 | if((!$s->is_null) && (!$t->is_null)) | ||
| 752 | { | ||
| 753 | $denom=sqrt(($s->size)*($t->size)); | ||
| 754 | # cosine = |(s,t)|/sqrt(|s|*|t|) | ||
| 755 | $simcount=($inter_st->size)/$denom; | ||
| 756 | } | ||
| 757 | } | ||
| 758 | } | ||
| 759 | } | ||
| 760 | if(defined $simcount && $simcount != 0) | ||
| 761 | { | ||
| 762 | $value=sprintf $format, $simcount; | ||
| 763 | print TEMP "" . ($j+1) ." $value "; | ||
| 764 | $nnz++; | ||
| 765 | } | ||
| 766 | } | ||
| 767 | print TEMP "\n"; | ||
| 768 | } | ||
| 769 | |||
| 770 | close TEMP; | ||
| 771 | open(TEMP,$tempfile) || die "Error($0): | ||
| 772 | Error(code=$!) in opening tempfile <$tempfile>.\n"; | ||
| 773 | |||
| 774 | # printing to stdout | ||
| 775 | print "$rows $nnz\n"; | ||
| 776 | while(<TEMP>) | ||
| 777 | { | ||
| 778 | print; | ||
| 779 | } | ||
| 780 | close TEMP; | ||
| 781 | unlink "$tempfile"; | ||
| 782 | } | ||
| 783 | |||
| 784 | undef $opt_dense; | ||
| 785 | |||
| 786 | ############################################################################## | ||
| 787 | |||
| 788 | # ========================== | ||
| 789 | # SUBROUTINE SECTION | ||
| 790 | # ========================== | ||
| 791 | |||
| 792 | #----------------------------------------------------------------------------- | ||
| 793 | #show minimal usage message | ||
| 794 | sub showminimal() | ||
| 795 | { | ||
| 796 | print "Usage: bitsimat.pl [OPTIONS] VECTOR"; | ||
| 797 | print "\nTYPE bitsimat.pl --help for help\n"; | ||
| 798 | } | ||
| 799 | |||
| 800 | #----------------------------------------------------------------------------- | ||
| 801 | #show help | ||
| 802 | sub showhelp() | ||
| 803 | { | ||
| 804 | print "Usage: bitsimat.pl [OPTIONS] VECTOR | ||
| 805 | |||
| 806 | Constructs a similarity matrix for given binary context vectors. | ||
| 807 | |||
| 808 | VECTOR | ||
| 809 | A file containing binary context vectors. | ||
| 810 | |||
| 811 | OPTIONS: | ||
| 812 | |||
| 813 | --dense | ||
| 814 | Should be selected if the given VECTORs are in dense format. By default, | ||
| 815 | sparse format is assumed. | ||
| 816 | |||
| 817 | --measure SIM | ||
| 818 | Specify a measure of similarity to be used for creating similarity | ||
| 819 | matrix. Possible option values for --measure are | ||
| 820 | |||
| 821 | match Match Coefficient | ||
| 822 | dice Dice Coefficient | ||
| 823 | overlap Overlap Coefficient | ||
| 824 | jaccard Jaccard Coefficient | ||
| 825 | cosine Cosine Coefficient | ||
| 826 | |||
| 827 | --format FORM | ||
| 828 | Specifies numeric format for representing output similarity values. | ||
| 829 | Default format for match coefficient is i8. For other measures, | ||
| 830 | default is f16.10. | ||
| 831 | |||
| 832 | --help | ||
| 833 | Displays this message. | ||
| 834 | |||
| 835 | --version | ||
| 836 | Displays the version information. | ||
| 837 | |||
| 838 | Type 'perldoc bitsimat.pl' to view detailed documentation of bitsimat.\n"; | ||
| 839 | } | ||
| 840 | |||
| 841 | #------------------------------------------------------------------------------ | ||
| 842 | #version information | ||
| 843 | sub showversion() | ||
| 844 | { | ||
| 845 | # print "bitsimat.pl - Version 0.04\n"; | ||
| 846 | print '$Id'; | ||
| 847 | print "\nConstructs a similarity matrix from binary context vectors\n"; | ||
| 848 | # print "Copyright (c) 2002-2005, Amruta Purandare, Ted Pedersen.\n"; | ||
| 849 | # print "Date of Last Update: 06/01/2004\n"; | ||
| 850 | } | ||
| 851 | |||
| 852 | ############################################################################# | ||
| 853 |