| 1 | #!/usr/local/bin/perl -w | ||
| 2 | |||
| 3 | =head1 NAME | ||
| 4 | |||
| 5 | simat.pl - Build a similarity matrix from real-valued context vectors | ||
| 6 | |||
| 7 | =head1 SYNOPSIS | ||
| 8 | |||
| 9 | simat.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.5 3 2.5 4 1.0 | ||
| 21 | 2 2.5 3 2.5 | ||
| 22 | 1 1.5 3 2.5 4 1.0 | ||
| 23 | 2 2.5 3 2.5 | ||
| 24 | 2 2.5 3 2.5 | ||
| 25 | |||
| 26 | Compute the pairwise similarities between all 5 vectors and display them | ||
| 27 | in a 5x5 matrix. | ||
| 28 | |||
| 29 | simat.pl input --format f4.2 | ||
| 30 | |||
| 31 | Output => | ||
| 32 | |||
| 33 | 5 25 | ||
| 34 | 1 1.00 2 0.57 3 1.00 4 0.57 5 0.57 | ||
| 35 | 1 0.57 2 1.00 3 0.57 4 1.00 5 1.00 | ||
| 36 | 1 1.00 2 0.57 3 1.00 4 0.57 5 0.57 | ||
| 37 | 1 0.57 2 1.00 3 0.57 4 1.00 5 1.00 | ||
| 38 | 1 0.57 2 1.00 3 0.57 4 1.00 5 1.00 | ||
| 39 | |||
| 40 | Type C<simat.pl> for a quick summary of options | ||
| 41 | |||
| 42 | =head1 DESCRIPTION | ||
| 43 | |||
| 44 | Constructs a similarity matrix for the given real-valued context | ||
| 45 | vectors. A similarity matrix shows the pairwise similarities between all | ||
| 46 | the different vectors. Vectors are represented in an N x M matrix, where | ||
| 47 | N is the number of vectors and M is the number of features. All NxN | ||
| 48 | combinations of vector pairs will be measured for similarity | ||
| 49 | and stored in a matrix. | ||
| 50 | |||
| 51 | =head1 INPUT | ||
| 52 | |||
| 53 | =head2 Required Arguments: | ||
| 54 | |||
| 55 | =head3 VECTOR | ||
| 56 | |||
| 57 | Should be a file containing sparse/dense vectors as created by programs in | ||
| 58 | dir Toolkit/vector. | ||
| 59 | |||
| 60 | =head4 Sparse Format (default) | ||
| 61 | |||
| 62 | When the input vectors are in sparse format, the first line in the VECTOR | ||
| 63 | file should show 3 space separated numbers - | ||
| 64 | |||
| 65 | N M NNZ | ||
| 66 | |||
| 67 | where | ||
| 68 | |||
| 69 | N = Number of rows/vectors | ||
| 70 | M = Number of columns | ||
| 71 | NNZ = Number of non-zero values | ||
| 72 | |||
| 73 | Each line after this line should show a single sparse vector on each line. | ||
| 74 | A sparse vector is a list of pairs of numbers separated by space such | ||
| 75 | that the first number in a pair is the index of a non-zero value in the | ||
| 76 | vector and the second number is a non-zero value itself corresponding to | ||
| 77 | that index. | ||
| 78 | |||
| 79 | Column indices start with 1. | ||
| 80 | |||
| 81 | Sample Sparse Format - | ||
| 82 | |||
| 83 | 7 8 27 | ||
| 84 | 3 0.727 4 0.921 7 0.734 8 0.841 | ||
| 85 | 6 0.726 7 0.775 8 0.948 | ||
| 86 | 1 0.898 3 0.712 5 0.787 8 0.724 | ||
| 87 | 4 0.797 6 0.649 | ||
| 88 | 1 0.695 2 0.710 3 0.837 4 0.736 7 0.631 | ||
| 89 | 2 0.661 3 0.778 4 0.762 6 0.957 | ||
| 90 | 1 0.915 3 0.639 5 0.989 6 0.637 8 0.649 | ||
| 91 | |||
| 92 | Explanation : | ||
| 93 | |||
| 94 | =over | ||
| 95 | |||
| 96 | =item 1. | ||
| 97 | |||
| 98 | First line shows that there are 7 sparse vectors, represented using | ||
| 99 | 8 features, and total 27 non-zero values. | ||
| 100 | |||
| 101 | =item 2. | ||
| 102 | |||
| 103 | Each vector (all lines except the 1st line) is a list of 'INDEX VALUE' | ||
| 104 | pairs separated by space. | ||
| 105 | |||
| 106 | e.g. line 2 shows that the 1st vector has a non-zero value 0.727 at index 3, | ||
| 107 | 0.921 at 4, value 0.734 at index 7 and 0.841 at 8. | ||
| 108 | |||
| 109 | Only those 'INDEX VALUE' pairs are listed in which value is non-zero. | ||
| 110 | |||
| 111 | Column indices start from 1, to be consistent with Cluto's matrix format | ||
| 112 | standard. | ||
| 113 | |||
| 114 | =back | ||
| 115 | |||
| 116 | Note that, the values could be integer and negative. | ||
| 117 | |||
| 118 | =head4 Dense Format | ||
| 119 | |||
| 120 | If VECTORs are in dense format, switch --dense should be selected. | ||
| 121 | |||
| 122 | For N vectors each having M components, the first line of dense VECTOR file | ||
| 123 | should show exactly 2 integers N M i.e. number of vectors and number of | ||
| 124 | components, separated in space. This should be followed by actual vectors | ||
| 125 | each on a separate line. Each dense vector should only list the vector | ||
| 126 | component values (and not the corresponding column indices), and should | ||
| 127 | show all values including 0s. Thus, all vectors should be equal in length and | ||
| 128 | should show M space separated numbers. | ||
| 129 | |||
| 130 | Sample Dense Format - | ||
| 131 | |||
| 132 | 7 8 | ||
| 133 | 0.000 0.000 0.727 0.921 0.000 0.000 0.734 0.841 | ||
| 134 | 0.000 0.000 0.000 0.000 0.000 0.726 0.775 0.948 | ||
| 135 | 0.898 0.000 0.712 0.000 0.787 0.000 0.000 0.724 | ||
| 136 | 0.000 0.000 0.000 0.797 0.000 0.649 0.000 0.000 | ||
| 137 | 0.695 0.710 0.837 0.736 0.000 0.000 0.631 0.000 | ||
| 138 | 0.000 0.661 0.778 0.762 0.000 0.957 0.000 0.000 | ||
| 139 | 0.915 0.000 0.639 0.000 0.989 0.637 0.000 0.649 | ||
| 140 | |||
| 141 | Shows same VECTOR file as shown in section Sample Sparse Format, in dense | ||
| 142 | format. | ||
| 143 | |||
| 144 | VECTOR file could also optionally show the <keyfile> tag on the first line. | ||
| 145 | If the first line shows the <keyfile> name, then the above VECTOR | ||
| 146 | description should start from the 2nd line onwards. | ||
| 147 | |||
| 148 | =head2 Optional Arguments: | ||
| 149 | |||
| 150 | =head3 --dense | ||
| 151 | |||
| 152 | This switch should be selected when the given VECTORs are in dense format. | ||
| 153 | This will also create the output similarity matrix in dense format. By | ||
| 154 | default, sparse format is assumed and used for both input vectors and | ||
| 155 | output similarity matrix. | ||
| 156 | |||
| 157 | =head3 --format FORM | ||
| 158 | |||
| 159 | Specifies the numeric representation format for output similarity matrix. | ||
| 160 | |||
| 161 | Acceptable FORM value is | ||
| 162 | |||
| 163 | fN.M -> each similarity value is represented as a floating point number | ||
| 164 | occupying total N byte space with last M bytes showing the fractional part. | ||
| 165 | |||
| 166 | Default format used is f16.10. | ||
| 167 | |||
| 168 | Since the cosine similarity values computed by simat are always in the range | ||
| 169 | [0-1], simat supports only floating point format. | ||
| 170 | |||
| 171 | =head3 Other Options : | ||
| 172 | |||
| 173 | =head4 --help | ||
| 174 | |||
| 175 | Displays this message. | ||
| 176 | |||
| 177 | =head4 --version | ||
| 178 | |||
| 179 | Displays the version information. | ||
| 180 | |||
| 181 | =head1 OUTPUT | ||
| 182 | |||
| 183 | =head2 Sparse Format [default] | ||
| 184 | |||
| 185 | By default (when --dense is not selected), OUTPUT will be created in sparse | ||
| 186 | format. In sparse format, the 1st line will show two space separated numbers, | ||
| 187 | |||
| 188 | N NNZ1 | ||
| 189 | |||
| 190 | where | ||
| 191 | N = Number of vectors, same as the N in the given VECTOR file | ||
| 192 | NNZ1 = Number of non-zero values in the output similarity matrix | ||
| 193 | |||
| 194 | Each i'th line after the above line shows the list of 'j COSINE' pairs | ||
| 195 | separated by space such that COSINE is the non-zero cosine similarity value | ||
| 196 | between the i'th and j'th vectors in the given VECTOR file. | ||
| 197 | |||
| 198 | Only those pairs are listed in which the COSINE values are non-zeroes. | ||
| 199 | |||
| 200 | Sample Sparse Output | ||
| 201 | |||
| 202 | 7 29 | ||
| 203 | 1 1.000 4 0.441 6 0.491 7 0.357 | ||
| 204 | 2 1.000 3 0.308 4 0.322 5 0.212 | ||
| 205 | 2 0.308 3 1.000 | ||
| 206 | 1 0.441 2 0.322 4 1.000 5 0.352 6 0.750 7 0.230 | ||
| 207 | 2 0.212 4 0.352 5 1.000 6 0.651 | ||
| 208 | 1 0.491 4 0.750 5 0.651 6 1.000 7 0.398 | ||
| 209 | 1 0.357 4 0.230 6 0.398 7 1.000 | ||
| 210 | |||
| 211 | Shows the actual full similarity matrix - | ||
| 212 | |||
| 213 | 7 | ||
| 214 | 1.000 0.000 0.000 0.441 0.000 0.491 0.357 | ||
| 215 | 0.000 1.000 0.308 0.322 0.212 0.000 0.000 | ||
| 216 | 0.000 0.308 1.000 0.000 0.000 0.000 0.000 | ||
| 217 | 0.441 0.322 0.000 1.000 0.352 0.750 0.230 | ||
| 218 | 0.000 0.212 0.000 0.352 1.000 0.651 0.000 | ||
| 219 | 0.491 0.000 0.000 0.750 0.651 1.000 0.398 | ||
| 220 | 0.357 0.000 0.000 0.230 0.000 0.398 1.000 | ||
| 221 | |||
| 222 | Both the matrices show the pair-wise similarities among 7 vectors. | ||
| 223 | e.g. Row 1 shows that the cosine similarity among the 1st vector and | ||
| 224 | the 4th vector is 0.441, between 1st and 6th is 0.491, and the similarity | ||
| 225 | between 1st and 7th vectors is 0.357. In sparse format, each line shows | ||
| 226 | only those values that are non-zeroes. | ||
| 227 | |||
| 228 | Note that, the similarity matrix (sparse/dense) always represents a square | ||
| 229 | symmetric matrix. | ||
| 230 | |||
| 231 | =head2 Dense Format | ||
| 232 | |||
| 233 | For N input vectors, the first line of the dense output shows a single | ||
| 234 | integer number N (the total number of vectors). Thereafter, each i'th line | ||
| 235 | shows pair-wise similarities of i'th vector with each of the N vectors. i.e. | ||
| 236 | each row contains N columns such that the value at j'th column in i'th row | ||
| 237 | shows the pair-wise similarity between i'th and j'th vectors. | ||
| 238 | |||
| 239 | Sample Dense Output | ||
| 240 | |||
| 241 | 7 | ||
| 242 | 1.000 0.000 0.000 0.441 0.000 0.491 0.357 | ||
| 243 | 0.000 1.000 0.308 0.322 0.212 0.000 0.000 | ||
| 244 | 0.000 0.308 1.000 0.000 0.000 0.000 0.000 | ||
| 245 | 0.441 0.322 0.000 1.000 0.352 0.750 0.230 | ||
| 246 | 0.000 0.212 0.000 0.352 1.000 0.651 0.000 | ||
| 247 | 0.491 0.000 0.000 0.750 0.651 1.000 0.398 | ||
| 248 | 0.357 0.000 0.000 0.230 0.000 0.398 1.000 | ||
| 249 | |||
| 250 | where each cell shows the similarity between the corresponding pair of | ||
| 251 | vectors. | ||
| 252 | |||
| 253 | =head1 SYSTEM REQUIREMENTS | ||
| 254 | |||
| 255 | simat.pl is dependent on the following CPAN modules : | ||
| 256 | |||
| 257 | =over | ||
| 258 | |||
| 259 | =item PDL - L<http://search.cpan.org/dist/PDL/> | ||
| 260 | |||
| 261 | =item Math::SparseVector - L<http://search.cpan.org/dist/Math-SparseVector/> | ||
| 262 | |||
| 263 | =back | ||
| 264 | |||
| 265 | =head1 AUTHORS | ||
| 266 | |||
| 267 | Amruta Purandare, University of Pittsburgh | ||
| 268 | |||
| 269 | Ted Pedersen, University of Minnesota, Duluth | ||
| 270 | tpederse at d.umn.edu | ||
| 271 | |||
| 272 | =head1 COPYRIGHT | ||
| 273 | |||
| 274 | Copyright (c) 2002-2008, Amruta Purandare and Ted Pedersen | ||
| 275 | |||
| 276 | This program is free software; you can redistribute it and/or modify it under | ||
| 277 | the terms of the GNU General Public License as published by the Free Software | ||
| 278 | Foundation; either version 2 of the License, or (at your option) any later | ||
| 279 | version. | ||
| 280 | |||
| 281 | This program is distributed in the hope that it will be useful, but WITHOUT | ||
| 282 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS | ||
| 283 | FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. | ||
| 284 | |||
| 285 | You should have received a copy of the GNU General Public License along with | ||
| 286 | this program; if not, write to | ||
| 287 | |||
| 288 | The Free Software Foundation, Inc., | ||
| 289 | 59 Temple Place - Suite 330, | ||
| 290 | Boston, MA 02111-1307, USA. | ||
| 291 | |||
| 292 | =cut | ||
| 293 | |||
| 294 | ############################################################################### | ||
| 295 | |||
| 296 | # THE CODE STARTS HERE | ||
| 297 | |||
| 298 | #$0 contains the program name along with | ||
| 299 | #the complete path. Extract just the program | ||
| 300 | #name and use in error messages | ||
| 301 | $0=~s/.*\/(.+)/$1/; | ||
| 302 | |||
| 303 | # loading PDL modules | ||
| 304 | use PDL; | ||
| 305 | use PDL::NiceSlice; | ||
| 306 | use PDL::Primitive; | ||
| 307 | use PDL::IO::FastRaw; | ||
| 308 | |||
| 309 | # loading Math::SparseVector module | ||
| 310 | use Math::SparseVector; | ||
| 311 | |||
| 312 | ############################################################################### | ||
| 313 | |||
| 314 | # ================================ | ||
| 315 | # COMMAND LINE OPTIONS AND USAGE | ||
| 316 | # ================================ | ||
| 317 | |||
| 318 | # command line options | ||
| 319 | use Getopt::Long; | ||
| 320 | GetOptions ("help","version","format=s","dense"); | ||
| 321 | # show help option | ||
| 322 | if(defined $opt_help) | ||
| 323 | { | ||
| 324 | $opt_help=1; | ||
| 325 | &showhelp(); | ||
| 326 | exit; | ||
| 327 | } | ||
| 328 | |||
| 329 | # show version information | ||
| 330 | if(defined $opt_version) | ||
| 331 | { | ||
| 332 | $opt_version=1; | ||
| 333 | &showversion(); | ||
| 334 | exit; | ||
| 335 | } | ||
| 336 | |||
| 337 | # show minimal usage message if no arguments | ||
| 338 | if($#ARGV<0) | ||
| 339 | { | ||
| 340 | &showminimal(); | ||
| 341 | exit; | ||
| 342 | } | ||
| 343 | |||
| 344 | # output format | ||
| 345 | if(defined $opt_format) | ||
| 346 | { | ||
| 347 | # only floating point format allowed | ||
| 348 | if($opt_format=~/^f(\d+)\.(\d+)$/) | ||
| 349 | { | ||
| 350 | $format="%$1\.$2f"; | ||
| 351 | } | ||
| 352 | else | ||
| 353 | { | ||
| 354 | print STDERR "ERROR($0): | ||
| 355 | Wrong format value --format=$opt_format.\n"; | ||
| 356 | exit; | ||
| 357 | } | ||
| 358 | } | ||
| 359 | # default is f16.10 | ||
| 360 | else | ||
| 361 | { | ||
| 362 | $format="%16.10f"; | ||
| 363 | } | ||
| 364 | |||
| 365 | |||
| 366 | ############################################################################# | ||
| 367 | |||
| 368 | # ================================ | ||
| 369 | # INITIALIZATION AND INPUT | ||
| 370 | # ================================ | ||
| 371 | |||
| 372 | if(!defined $ARGV[0]) | ||
| 373 | { | ||
| 374 | print STDERR "ERROR($0): | ||
| 375 | Please specify the Vector file ...\n"; | ||
| 376 | exit; | ||
| 377 | } | ||
| 378 | # accept the vector file name | ||
| 379 | $infile=$ARGV[0]; | ||
| 380 | |||
| 381 | open(IN,$infile) || die "Error($0): | ||
| 382 | Error(code=$!) in opening Vector file <$infile>.\n"; | ||
| 383 | |||
| 384 | ############################################################################## | ||
| 385 | |||
| 386 | # ======================================= | ||
| 387 | # Finding Pair-wise Similarities | ||
| 388 | # ======================================= | ||
| 389 | |||
| 390 | $line_num=1; | ||
| 391 | # reading first line | ||
| 392 | $_=<IN>; | ||
| 393 | if(/keyfile/) | ||
| 394 | { | ||
| 395 | # when input starts with <keyfile> | ||
| 396 | # output also starts with <keyfile> | ||
| 397 | print; | ||
| 398 | # read next line | ||
| 399 | $_=<IN>; | ||
| 400 | $line_num++; | ||
| 401 | } | ||
| 402 | |||
| 403 | if(defined $opt_dense) | ||
| 404 | { | ||
| 405 | if(/^\s*(\d+)\s+(\d+)\s*$/) | ||
| 406 | { | ||
| 407 | $rows=$1; | ||
| 408 | $cols=$2; | ||
| 409 | } | ||
| 410 | else | ||
| 411 | { | ||
| 412 | print STDERR "ERROR($0): | ||
| 413 | Line$line_num in Vector file <$infile> should show | ||
| 414 | #nrows #ncols when --dense is ON.\n"; | ||
| 415 | exit; | ||
| 416 | } | ||
| 417 | } | ||
| 418 | else | ||
| 419 | { | ||
| 420 | if(/^\s*(\d+)\s+(\d+)\s+(\d+)\s*$/) | ||
| 421 | { | ||
| 422 | $rows=$1; | ||
| 423 | $cols=$2; | ||
| 424 | $nnz1=$3; | ||
| 425 | } | ||
| 426 | else | ||
| 427 | { | ||
| 428 | print STDERR "ERROR($0): | ||
| 429 | Line$line_num in Vector file <$infile> should show | ||
| 430 | #nrows #ncols #nnz.\n"; | ||
| 431 | exit; | ||
| 432 | } | ||
| 433 | } | ||
| 434 | |||
| 435 | if(defined $opt_dense) | ||
| 436 | { | ||
| 437 | # mapping vectors to a temporary file | ||
| 438 | $matrix_file="matrix".time().".simat"; | ||
| 439 | $map_pdl=mapfraw("$matrix_file",{Creat=>1, Dims=>[$cols,$rows], Datatype=>double}); | ||
| 440 | |||
| 441 | # reading vectors and storing in mapped piddle | ||
| 442 | $row=0; | ||
| 443 | while(<IN>) | ||
| 444 | { | ||
| 445 | s/^\s*//; | ||
| 446 | s/\s*$//; | ||
| 447 | @vector_comps=split(/\s+/); | ||
| 448 | if(scalar(@vector_comps)!=$cols) | ||
| 449 | { | ||
| 450 | print STDERR "ERROR($0): | ||
| 451 | Vector $row in Vector file <$infile> doesn't have $cols components.\n"; | ||
| 452 | exit; | ||
| 453 | } | ||
| 454 | $map_pdl(:,$row).=pdl @vector_comps; | ||
| 455 | $row++; | ||
| 456 | } | ||
| 457 | |||
| 458 | if($row!=$rows) | ||
| 459 | { | ||
| 460 | print STDERR "ERROR($0): | ||
| 461 | Vector file <$infile> doesn't contain $rows vectors.\n"; | ||
| 462 | exit; | ||
| 463 | } | ||
| 464 | |||
| 465 | # normalizing | ||
| 466 | $map_pdl.=$map_pdl->norm; | ||
| 467 | |||
| 468 | # mapping cosine matrix | ||
| 469 | $cos_file="cosine".time().".simat"; | ||
| 470 | $cosine_pdl=mapfraw("$cos_file",{Creat=>1, Dims=>[$rows,$rows], Datatype=>double}); | ||
| 471 | |||
| 472 | # taking inner product | ||
| 473 | $cosine_pdl.=matmult($map_pdl,$map_pdl->mv(0,1)); | ||
| 474 | |||
| 475 | # printing | ||
| 476 | print "$rows\n"; | ||
| 477 | foreach $row (0..$cosine_pdl->getdim(1)-1) | ||
| 478 | { | ||
| 479 | foreach $col (0..$cosine_pdl->getdim(0)-1) | ||
| 480 | { | ||
| 481 | printf($format,$cosine_pdl->at($col,$row)); | ||
| 482 | } | ||
| 483 | print "\n"; | ||
| 484 | } | ||
| 485 | |||
| 486 | unlink "$cos_file"; | ||
| 487 | unlink "$cos_file.hdr"; | ||
| 488 | unlink "$matrix_file"; | ||
| 489 | unlink "$matrix_file.hdr"; | ||
| 490 | } | ||
| 491 | # given vectors in sparse format | ||
| 492 | else | ||
| 493 | { | ||
| 494 | $row=0; | ||
| 495 | while(<IN>) | ||
| 496 | { | ||
| 497 | $line_num++; | ||
| 498 | $row++; | ||
| 499 | chomp; | ||
| 500 | s/^\s*//; | ||
| 501 | s/\s*$//; | ||
| 502 | $sparsevec=Math::SparseVector->new; | ||
| 503 | @pairs=split; | ||
| 504 | foreach($i=0; $i<$#pairs; $i=$i+2) | ||
| 505 | { | ||
| 506 | $index=$pairs[$i]; | ||
| 507 | if($index > $cols) | ||
| 508 | { | ||
| 509 | print STDERR "ERROR($0): | ||
| 510 | Index <$index> at line <$line_num> in Vector file <$infile> | ||
| 511 | exceeds #cols = <$cols> specified in the header line.\n"; | ||
| 512 | exit; | ||
| 513 | } | ||
| 514 | $value=$pairs[$i+1]; | ||
| 515 | if($value==0) | ||
| 516 | { | ||
| 517 | print STDERR "ERROR($0): | ||
| 518 | Caught value 0 at line <$line_num> in sparse Vector file <$infile>.\n"; | ||
| 519 | exit; | ||
| 520 | } | ||
| 521 | $sparsevec->set($index,$value); | ||
| 522 | $nnz++; | ||
| 523 | } | ||
| 524 | push @sparse_vectors,$sparsevec; | ||
| 525 | } | ||
| 526 | close IN; | ||
| 527 | if($row!=$rows) | ||
| 528 | { | ||
| 529 | print STDERR "ERROR($0): | ||
| 530 | #rows = $rows specified in the header line of the VECTOR file <$infile> | ||
| 531 | does not match the actual #rows = $row found in the file.\n"; | ||
| 532 | exit; | ||
| 533 | } | ||
| 534 | if($nnz != $nnz1) | ||
| 535 | { | ||
| 536 | print STDERR "ERROR($0): | ||
| 537 | #nnz = $nnz1 specified in the header line of the VECTOR file <$infile> | ||
| 538 | does not match the actual #nnz = $nnz found in the file.\n"; | ||
| 539 | exit; | ||
| 540 | } | ||
| 541 | # normalizing all rows | ||
| 542 | foreach (@sparse_vectors) | ||
| 543 | { | ||
| 544 | # @keys=$_->keys(); | ||
| 545 | # if($#keys > -1) | ||
| 546 | if(! $_->isnull) | ||
| 547 | { | ||
| 548 | $_->normalize; | ||
| 549 | } | ||
| 550 | } | ||
| 551 | $nnz=0; | ||
| 552 | |||
| 553 | # finding cosines | ||
| 554 | foreach $i (1..$rows) | ||
| 555 | { | ||
| 556 | $cosine{$i}{$i}=1; | ||
| 557 | $nnz++; | ||
| 558 | foreach $j ($i+1..$rows) | ||
| 559 | { | ||
| 560 | $dp=$sparse_vectors[$i-1]->dot($sparse_vectors[$j-1]); | ||
| 561 | if($dp!=0) | ||
| 562 | { | ||
| 563 | $cosine{$i}{$j}=$dp; | ||
| 564 | $cosine{$j}{$i}=$dp; | ||
| 565 | $nnz+=2; | ||
| 566 | } | ||
| 567 | } | ||
| 568 | } | ||
| 569 | # printing | ||
| 570 | print "$rows $nnz\n"; | ||
| 571 | @row_indices=keys %cosine; | ||
| 572 | @sorted_rows=sort {$a <=> $b} @row_indices; | ||
| 573 | foreach $row (@sorted_rows) | ||
| 574 | { | ||
| 575 | @col_indices=keys %{$cosine{$row}}; | ||
| 576 | @sorted_cols=sort {$a<=> $b} @col_indices; | ||
| 577 | foreach $col (@sorted_cols) | ||
| 578 | { | ||
| 579 | $cosine_value=sprintf $format,$cosine{$row}{$col}; | ||
| 580 | print "$col $cosine_value "; | ||
| 581 | } | ||
| 582 | print "\n"; | ||
| 583 | } | ||
| 584 | } | ||
| 585 | |||
| 586 | ############################################################################## | ||
| 587 | |||
| 588 | # ========================== | ||
| 589 | # SUBROUTINE SECTION | ||
| 590 | # ========================== | ||
| 591 | |||
| 592 | #----------------------------------------------------------------------------- | ||
| 593 | #show minimal usage message | ||
| 594 | sub showminimal() | ||
| 595 | { | ||
| 596 | print "Usage: simat.pl [OPTIONS] VECTOR"; | ||
| 597 | print "\nTYPE simat.pl --help for help\n"; | ||
| 598 | } | ||
| 599 | |||
| 600 | #----------------------------------------------------------------------------- | ||
| 601 | #show help | ||
| 602 | sub showhelp() | ||
| 603 | { | ||
| 604 | print "Usage: simat.pl [OPTIONS] VECTOR | ||
| 605 | |||
| 606 | Displays pair-wise cosine similarities among the given VECTORs. | ||
| 607 | |||
| 608 | VECTOR | ||
| 609 | Vector file as created by vector constructor programs in | ||
| 610 | Toolkit/vector. | ||
| 611 | |||
| 612 | OPTIONS: | ||
| 613 | |||
| 614 | --format FORM | ||
| 615 | Specifies numeric format of representing output similarity matrix. | ||
| 616 | Default is f16.10. | ||
| 617 | --dense | ||
| 618 | When selected, assumes input VECTORS in dense format and also creates | ||
| 619 | output similarity matrix in dense format. By default, both input | ||
| 620 | and output are assumed in sparse format. | ||
| 621 | --help | ||
| 622 | Displays this message. | ||
| 623 | --version | ||
| 624 | Displays the version information. | ||
| 625 | |||
| 626 | Type 'perldoc simat.pl' to view detailed documentation of simat.\n"; | ||
| 627 | } | ||
| 628 | |||
| 629 | #------------------------------------------------------------------------------ | ||
| 630 | #version information | ||
| 631 | sub showversion() | ||
| 632 | { | ||
| 633 | # print "simat.pl - Version 0.08\n"; | ||
| 634 | print '$Id: simat.pl,v 1.22 2008/03/29 23:36:03 tpederse Exp $'; | ||
| 635 | print "\nCalcuate a similarity matrix for a given set of context vectors\n"; | ||
| 636 | # print "Copyright (c) 2002-2005, Amruta Purandare & Ted Pedersen.\n"; | ||
| 637 | # print "Date of Last Update: 06/01/2004\n"; | ||
| 638 | } | ||
| 639 | |||
| 640 | ############################################################################# | ||
| 641 |