blib/lib/UMLS/Similarity/vector.pm untested

filter: Statementstmt 0.0% 0 / 481 Branchbran 0.0% 0 / 208 Conditioncond 0.0% 0 / 38 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 11 Podpod 0.0% 0 / 5 totaltotal 0.0% 0 / 743 SCAR 93.2
CC
105
Cov
0%
getRelatedness
5112.0
new
600.0
findCompoundWord
72.0
CRAP 11130.0
1 # UMLS::Similarity::vector.pm
2 #
3 # Module implementing the vector semantic relatedness measure
4 # based on the measure proposed by Patwardhan and Pedersen
5 # (2006)
6 #
7 # Copyright (c) 2009-2011,
8 #
9 # Bridget T McInnes, University of Minnesota, Twin Cities
10 # bthomson at umn.edu
11 #
12 # Siddharth Patwardhan, University of Utah, Salt Lake City
13 # sidd at cs.utah.edu
14 #
15 # Serguei Pakhomov, University of Minnesota, Twin Cities
16 # pakh002 at umn.edu
17 #
18 # Ted Pedersen, University of Minnesota, Duluth
19 # tpederse at d.umn.edu
20 #
21 # Ying Liu, University of Minnesota
22 # liux0935 at umn.edu
23 #
24 # This program is free software; you can redistribute it and/or
25 # modify it under the terms of the GNU General Public License
26 # as published by the Free Software Foundation; either version 2
27 # of the License, or (at your option) any later version.
28 #
29 # This program is distributed in the hope that it will be useful,
30 # but WITHOUT ANY WARRANTY; without even the implied warranty of
31 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
32 # GNU General Public License for more details.
33 #
34 # You should have received a copy of the GNU General Public License
35 # along with this program; if not, write to
36 #
37 # The Free Software Foundation, Inc.,
38 # 59 Temple Place - Suite 330,
39 # Boston, MA 02111-1307, USA.
40  
41  
42  
43 package UMLS::Similarity::vector;
44  
45 use strict;
46 use warnings;
47  
48 use UMLS::Similarity;
49 use Lingua::Stem::En;
50 use UMLS::Similarity::ErrorHandler;
51  
52  
53 use vars qw($VERSION);
54 $VERSION = '0.11';
55  
56 my $debug = 0;
57 my $defraw_option= 0;
58  
59 my $compoundfile = "";
60 my $vectormatrix = "";
61 my $vectorindex = "";
62 my $dictfile = "";
63 my $doubledef = "";
64 my $stoplist = "";
65 my $stem = "";
66 my $stopregex = "";
67 my $debugfile = "";
68 my $config = "";
69  
70 my %index = ();
71 my %reverse_index = ();
72 my %position = ();
73 my %length = ();
74 my %dictionary = ();
75 my %ddef = ();
76 my %stopwords = ();
77 my %complist = ();
78  
79  
80 local(*DEBUG);
81  
82 sub new
83 {
84 my $className = shift;
85  
86 return undef if(ref $className);
87  
88 if($debug) { print STDERR "In UMLS::Similarity::vector->new()\n"; }
89  
90 my $interface = shift;
91 my $params = shift;
92  
93 my $self = {};
94
95 # Bless the object.
96 bless($self, $className);
97
98 # The backend interface object.
99 $self->{'interface'} = $interface;
100
101 # check the configuration file if defined
102 my $errorhandler = UMLS::Similarity::ErrorHandler->new("vector", $interface);
103 if(!$errorhandler) {
104 print STDERR "The UMLS::Similarity::ErrorHandler did not load properly\n";
105 exit;
106 }
107
108 $params = {} if(!defined $params);
109  
110 $vectorindex = $params->{'vectorindex'};
111 $vectormatrix = $params->{'vectormatrix'};
112 $config = $params->{'config'};
113 $dictfile = $params->{'dictfile'};
114 $doubledef = $params->{'doubledef'};
115 $compoundfile = $params->{'compoundfile'};
116 $debugfile = $params->{'debugfile'};
117 $stoplist = $params->{'stoplist'};
118 $stem = $params->{'stem'};
119
120 my $defraw = $params->{'defraw'};
121
122 if(defined $defraw) {
123 $defraw_option = 1;
124 }
125
126 # read in the doubledef for --doubledef option
127 if (defined $doubledef) {
128 open(DDEF, "$doubledef")
129 or die("Error: cannot open doubledef file ($doubledef).\n");
130
131 while(<DDEF>) {
132 chomp;
133 if($_=~/^\s*$/) { next; }
134
135 my @defs = split (":", $_);
136 my $concept = $defs[0];
137 $concept =~ s/^\s+//;
138 $concept =~ s/\s+$//;
139 my $definition = $defs[1];
140 $ddef{$concept} = $definition;
141 }
142 close DDEF;
143 }
144  
145  
146 # read in the dictfile for --dictfile option
147 if (defined $dictfile) {
148 open(DICT, "$dictfile")
149 or die("Error: cannot open dictionary file ($dictfile).\n");
150
151 while(<DICT>) {
152 chomp;
153
154 if($_=~/^\s*$/) { next; }
155
156 my @defs = split (":", $_);
157 my $concept = $defs[0];
158 $concept =~ s/^\s+//;
159 $concept =~ s/\s+$//;
160 my $definition = $defs[1];
161 $dictionary{$concept} = $definition;
162 }
163 close DICT;
164 }
165  
166 # if the vector index is not defined - get the default
167 if(! (defined $vectorindex) ) {
168 foreach my $path (@INC) {
169 if(-e $path."/UMLS/vector-index.dat") {
170 $vectorindex = $path."/UMLS/vector-index.dat";
171 }
172 elsif(-e $path."\\UMLS\\vector-index.dat") {
173 $vectorindex = $path."\\UMLS\\vector-index.dat";
174 }
175 }
176 }
177  
178 # check to make certain that you found the vector index file
179 if(! (defined $vectorindex) ) {
180 print STDERR "Error: can not find the default vector index file (vector-index.dat).\n";
181 exit;
182 }
183  
184 # if the vector matrix is not defined - get the default
185 if(! (defined $vectormatrix) ) {
186 foreach my $path (@INC) {
187 if(-e $path."/UMLS/vector-matrix.dat") {
188 $vectormatrix = $path."/UMLS/vector-matrix.dat";
189 }
190 elsif(-e $path."\\UMLS\\vector-matrix.dat") {
191 $vectormatrix = $path."\\UMLS\\vector-matrix.dat";
192 }
193 }
194 }
195
196 # check to make certain that you found the vector matrix file
197 if(! (defined $vectormatrix) ) {
198 print STDERR "Error: can not find the default vector matrix file (vector-matrix.dat).\n";
199 exit;
200 }
201
202 # read in the vectorindex file
203 open(INDX, "<$vectorindex")
204 or die("Error: cannot open file '$vectorindex' for output index.\n");
205
206 while (my $line = <INDX>)
207 {
208 chomp($line);
209 my @terms = split(' ', $line);
210
211 $index{$terms[0]} = $terms[1];
212 $reverse_index{$terms[1]} = $terms[0];
213 $position{$terms[1]} = $terms[2];
214 $length{$terms[1]} = $terms[3];
215 }
216 close INDX;
217
218 if(defined $debugfile) {
219 if(-e $debugfile) {
220 print "Debug file $debugfile already exists! Overwrite (Y/N)? ";
221 my $reply = <STDIN>;
222 chomp $reply;
223 $reply = uc $reply;
224 exit 0 if ($reply ne "Y");
225 }
226
227 open(DEBUG, ">$debugfile") || die "Could not open debug file: $debugfile\n";
228 }
229
230 if (defined $stoplist) {
231
232 open(STP, "$stoplist")
233 or die("Error: cannot open stop list file ($stoplist).\n");
234
235 $stopregex = "(";
236 while(<STP>) {
237 chomp;
238 if($_ ne ""){
239 $_=~s/\///g;
240 $stopregex .= "$_|";
241 }
242 }
243 chop $stopregex; $stopregex .= ")";
244 close STP;
245
246 }
247  
248 if(defined $compoundfile) {
249
250 #replace the compound words in the definition
251 open(LST, "$compoundfile") or die ("Error: cannot open file $compoundfile for input.\n");
252  
253 # read the compound txt and put them in the hash array.
254 while (my $line = <LST>)
255 {
256 chomp($line);
257 my $lower_case = lc($line);
258 my @string = split('_', $lower_case);
259 my $head = shift(@string);
260  
261 my $rest = join (' ', @string);
262 push (@{$complist{$head}}, $rest);
263 }
264 close LST;
265
266 # sort the compound txt
267 foreach my $h (sort (keys (%complist)) )
268 {
269 my @sort_list = sort(@{$complist{$h}});
270 for my $i (0..$#sort_list)
271 {
272 $complist{$h}[$i] = $sort_list[$i];
273 }
274 }
275 }
276 return $self;
277 }
278  
279  
280 sub getRelatedness
281 {
282 my $self = shift;
283
284 return undef if(!defined $self || !ref $self);
285
286 my $concept1 = shift;
287 my $concept2 = shift;
288
289 if(defined $debugfile) {
290 print DEBUG "$concept1<>$concept2\n";
291 }
292
293 my $interface = $self->{'interface'};
294
295 my $d1 = "";
296 my $d2 = "";
297
298 if (!defined $dictfile) {
299 if ($concept1 =~ /C[0-9]+/)
300 {
301 my $defs1 = $interface->getExtendedDefinition($concept1);
302 if(defined $debugfile) {
303 print DEBUG "DEFINITIONS FOR CONCEPT 1 $concept1: \n";
304 }
305 my $i = 1;
306 foreach my $def (@{$defs1}) {
307 if(defined $debugfile) {
308 print DEBUG "$i. $def\n";
309 $i++;
310 }
311 $def=~/(C[0-9]+) ([A-Za-z]+) ([A-Za-z0-9]+) ([A-Za-z0-9\.]+) \s*\:\s*(.*?)$/;
312 my $temp = $5;
313 if(defined $temp) {
314 $d1 .= $temp . " ";
315 }
316 }
317  
318 #if the definition is empty, return -1
319 if ($d1 eq "")
320 {
321 return -1;
322 }
323 }
324 if($concept2 =~ /C[0-9]+/)
325 {
326 my $defs2 = $interface->getExtendedDefinition($concept2);
327 if(defined $debugfile) {
328 print DEBUG "DEFINITIONS FOR CONCEPT 2 $concept2: \n";
329 }
330 my $i = 1;
331 foreach my $def (@{$defs2}) {
332 if(defined $debugfile) {
333 print DEBUG "$i. $def\n";
334 $i++;
335 }
336 $def=~/(C[0-9]+) ([A-Za-z]+) ([A-Za-z0-9]+) ([A-Za-z0-9\.]+) \s*\:\s*(.*?)$/;
337 my $yrmp = $5;
338 if(defined $yrmp) {
339 $d2 .= $yrmp . " ";
340 }
341 }
342
343 #if the definition is empty, return -1
344 if ($d2 eq "")
345 {
346 return -1;
347 }
348 }
349 } # end of without --dictfile option
350
351 if (defined $dictfile)
352 {
353 my $defs1;
354 my $defs2;
355 my $term1;
356 my $term2;
357 my $term1_def = "";
358 my $term2_def = "";
359  
360 my @dictfile_term1;
361 my @dictfile_term2;
362
363 if(defined $debugfile) { print DEBUG "DEFINITIONS FOR CONCEPT 1: $concept1\n"; }
364  
365 # the input format is cui#term
366 if($concept1 =~ /^(C[0-9]+)(\#)(.*?)$/)
367 {
368 my $cui1 = $1;
369 $term1 = $3;
370
371 $defs1 = $interface->getExtendedDefinition($cui1);
372 $term1_def = $dictionary{$term1} if (defined $dictionary{$term1});
373
374 my $i = 1;
375 foreach my $extendeddef (@{$defs1}) {
376 if (defined $debugfile) {
377 print DEBUG "$i. $extendeddef\n";
378 $i++;
379 }
380
381 # seperate definition from the other information
382 # sent by the getExtendedDefinition function
383 $extendeddef=~/(C[0-9]+) ([A-Za-z]+) ([A-Za-z0-9]+) ([A-Za-z0-9\.]+) \s*\:\s*(.*?)$/;
384 my $def = $5;
385
386 # store the definition in the string d1
387 $d1 .= $def . " ";
388 }
389
390 $d1 .= $term1_def . " " if $term1_def ne "";
391  
392 # check the if the cui's assoicated terms are defined in the dictfile
393 #@dictfile_term1 = $interface->getTermList($cui1);
394 #foreach my $t (@dictfile_term1)
395 #{
396 #if (defined $dictionary{$t})
397 #{
398 #my $term1_def = $dictionary{$t};
399 #$d1 .= "$term1_def" . " ";
400 #}
401 #}
402  
403 if(defined $debugfile)
404 {
405 print DEBUG "$i. $term1_def\n" if (defined $term1_def);
406 }
407
408 #if the definition is empty, return -1
409 if ($d1 eq "")
410 {
411 return -1;
412 }
413 }
414 else #the input concept is a term
415 {
416 if (defined $dictionary{$concept1}) {
417 $d1 = $dictionary{$concept1};
418 if (defined $debugfile) {
419 print DEBUG "$concept1: $d1\n";
420 }
421 }
422 else{
423 if (defined $debugfile) {
424 print DEBUG "$concept1: not defined\n";
425 }
426 return -1; }
427 }
428
429 if(defined $debugfile) { print DEBUG "DEFINITIONS FOR CONCEPT 2: $concept2\n"; }
430
431 if($concept2 =~ /^(C[0-9]+)(\#)(.*?)$/)
432 {
433 my $cui2 = $1;
434 $term2 = $3;
435
436 $defs2 = $interface->getExtendedDefinition($cui2);
437 $term2_def = $dictionary{$term2} if (defined $dictionary{$term2});
438
439 my $i = 1;
440 foreach my $extendeddef (@{$defs2}) {
441 if (defined $debugfile) {
442 print DEBUG "$i. $extendeddef\n";
443 $i++;
444 }
445
446 # seperate definition from the other information
447 # sent by the getExtendedDefinition function
448 $extendeddef=~/(C[0-9]+) ([A-Za-z]+) ([A-Za-z0-9]+) ([A-Za-z0-9\.]+) \s*\:\s*(.*?)$/;
449 my $def = $5;
450
451 # store the definition in the string d1
452 $d2 .= $def . " ";
453 }
454
455 $d2 .= $term2_def . " " if $term2_def ne "";
456
457  
458 # check the if the cui's assoicated terms are defined in the dictfile
459 #@dictfile_term2 = $interface->getTermList($cui2);
460 #foreach my $t (@dictfile_term2)
461 #{
462 #if (defined $dictionary{$t})
463 #{
464 #my $term2_def = $dictionary{$t};
465 #$d2 .= "$term2_def" . " ";
466 #}
467 #}
468  
469 if(defined $debugfile)
470 {
471 print DEBUG "$i. $term2_def\n" if (defined $term2_def);
472 }
473
474 #if the definition is empty, return -1
475 if ($d2 eq "")
476 {
477 return -1;
478 }
479 }
480 else
481 {
482 if (defined $dictionary{$concept2}) {
483 $d2 = $dictionary{$concept2};
484 if (defined $debugfile) {
485 print DEBUG "$concept2: $d2\n"; }
486 }
487 else{
488 if (defined $debugfile) {
489 print DEBUG "$concept2: not defined\n"; }
490 return -1; }
491 }
492
493 } #end of defined --dictfile option
494  
495  
496 # if define --doubledef option
497 if (defined $doubledef)
498 {
499 my @def1 = split(/\s/, $d1);
500 my @def2 = split(/\s/, $d2);
501
502 my %unique = (); # for every word, only check its definition once
503 foreach my $w (@def1)
504 {
505 $unique{$w}++;
506 if ((defined $ddef{$w}) and ($unique{$w}==1))
507 {
508 my $def = $ddef{$w};
509 if (defined $debugfile)
510 {
511 print DEBUG "ddef1 $w: $def\n";
512 }
513 $d1 .= "$def" . " ";
514 }
515 }
516  
517 %unique = ();
518 foreach my $w (@def2)
519 {
520 $unique{$w}++;
521 if ((defined $ddef{$w}) and ($unique{$w}==1))
522 {
523 my $def = $ddef{$w};
524 if (defined $debugfile)
525 {
526 print DEBUG "ddef2 $w: $def\n";
527 }
528 $d2 .= "$def" . " ";
529 }
530 }
531  
532 if (defined $debugfile)
533 {
534 print DEBUG "after --doubledef processing\n";
535 print DEBUG "concept 1: $d1\n";
536 print DEBUG "concept 2: $d2\n";
537 }
538  
539  
540 } #end of defined --doubledef option
541
542
543 # if the --defraw option is not set clean up the defintions
544 if($defraw_option == 0) {
545 $d1 = lc($d1); $d2 = lc($d2);
546
547 # remove punctuation doesn't contain '<' and '>' and '_'
548 $d1=~s/[\.\,\?\/\'\"\;\:\[\]\{\}\!\@\#\$\%\^\&\*\(\)\-\+\-\=]//g;
549 $d2=~s/[\.\,\?\/\'\"\;\:\[\]\{\}\!\@\#\$\%\^\&\*\(\)\-\+\-\=]//g;
550  
551 if (defined $debugfile)
552 {
553 print DEBUG "after --defraw processing\n";
554 print DEBUG "concept 1: $d1\n";
555 print DEBUG "concept 2: $d2\n";
556 }
557 }
558
559  
560 # find compound words
561 if(defined $compoundfile)
562 {
563 $d1 = findCompoundWord($d1, \%complist);
564 $d2 = findCompoundWord($d2, \%complist);
565 if (defined $debugfile)
566 {
567 print DEBUG "after --compoundfile processing\n";
568 print DEBUG "concept 1: $d1\n";
569 print DEBUG "concept 2: $d2\n";
570 }
571 }
572  
573  
574 # if --stopword option is set remove stop words
575 if (defined $stoplist)
576 {
577 my @def1 = split(/\s/, $d1);
578 my @def2 = split(/\s/, $d2);
579 my @new_def1 = ();
580 my @new_def2 = ();
581 foreach my $w (@def1) {
582 if (!($w =~ /$stopregex/)) {
583 push (@new_def1, $w);}
584 }
585 foreach my $w (@def2) {
586 if (!($w =~ /$stopregex/)) {
587 push (@new_def2, $w);}
588 }
589
590 $d1 = join (" ", @new_def1);
591 $d2 = join (" ", @new_def2);
592  
593 if (defined $debugfile)
594 {
595 print DEBUG "after --stoplist processing\n";
596 print DEBUG "concept 1: $d1\n";
597 print DEBUG "concept 2: $d2\n";
598 }
599 } #end of stoplist option
600  
601  
602 if(defined $stem)
603 {
604 my @def_words1 = split(/\s/, $d1);
605 my $stemmed_words1 = Lingua::Stem::En::stem({ -words => \@def_words1, -locale => 'en'});
606 $d1 = join(" ", @{$stemmed_words1});
607
608 my @def_words2 = split(/\s/, $d2);
609 my $stemmed_words2 = Lingua::Stem::En::stem({ -words => \@def_words2, -locale => 'en'});
610 $d2 = join(" ", @{$stemmed_words2});
611  
612 if (defined $debugfile)
613 {
614 print DEBUG "after --stem processing\n";
615 print DEBUG "concept 1: $d1\n";
616 print DEBUG "concept 2: $d2\n";
617 }
618 }
619
620  
621 open(MATX, "<$vectormatrix")
622 or die("Error: cannot open file '$vectormatrix' for output index.\n");
623
624 my %vector1 = ();
625 my %vector2 = ();
626 my @defs1 = split(" ", $d1);
627 my @defs2 = split(" ", $d2);
628
629 my $def1_length = 0 ;
630
631 foreach my $def_term1 (@defs1){
632 if (defined $index{$def_term1})
633 {
634 my $index_term = $index{$def_term1};
635 my $p = $position{$index_term};
636 my $l = $length{$index_term};
637
638 if (($p==0) and (!defined $l))
639 {
640 next;
641 }
642 else
643 {
644 $def1_length++;
645
646 my ($data, $n);
647 seek MATX, $p, 0;
648 if (($n = read MATX, $data, $l) != 0)
649 {
650 if (defined $debugfile) {
651 print DEBUG "$def_term1: ";
652 }
653
654 chomp($data);
655 my @word_vector = split (' ', $data);
656 my $index = shift @word_vector;
657 $index =~ m/^(\d+)\:$/;
658
659 if ($index_term == $1)
660 {
661 for (my $z=0; $z<@word_vector; )
662 {
663 $vector1{$word_vector[$z]} += $word_vector[$z+1];
664
665 if (defined $debugfile) {
666 if(defined $word_vector[$z]) {
667 print DEBUG "$reverse_index{$word_vector[$z]} ";
668 }
669 }
670
671 $z += 2;
672 }
673
674 if (defined $debugfile)
675 {
676 print DEBUG "\n";
677 }
678 }
679 else
680 {
681 print STDERR "$def_term1 is not a correct word!\n";
682 exit;
683 }
684 }
685 }
686 }
687 } # end of def1
688
689 if (defined $debugfile) {
690 print DEBUG "def1 length: $def1_length\n";
691 }
692
693 my $def2_length = 0 ;
694 foreach my $def_term2 (@defs2)
695 {
696  
697 if (defined $index{$def_term2})
698 {
699 my $index_term = $index{$def_term2};
700 my $p = $position{$index_term};
701 my $l = $length{$index_term};
702
703 if (($p==0) and (!defined $l))
704 {
705 next;
706 }
707 else
708 {
709 $def2_length++;
710
711 my ($data, $n);
712 seek MATX, $p, 0;
713 if (($n = read MATX, $data, $l) != 0)
714 {
715 if (defined $debugfile) {
716 print DEBUG "$def_term2: ";
717 }
718 chomp($data);
719 my @word_vector = split (' ', $data);
720 my $index = shift @word_vector;
721 $index =~ m/^(\d+)\:$/;
722
723 if ($index_term == $1)
724 {
725 for (my $z=0; $z<@word_vector; )
726 {
727 $vector2{$word_vector[$z]} += $word_vector[$z+1];
728
729 if (defined $debugfile) {
730 if(defined $word_vector[$z]) {
731 print DEBUG "$reverse_index{$word_vector[$z]} "; }
732 }
733  
734 $z += 2;
735 }
736
737 if (defined $debugfile) {
738 print DEBUG "\n"; }
739 }
740 else
741 {
742 print STDERR "$def_term2 is not a correct word!\n";
743 exit;
744 }
745 }
746 }
747 }
748 }
749
750
751 if (defined $debugfile) {
752 print DEBUG "def2 length: $def2_length\n";
753 }
754
755 # normalize
756 my $vec1 = &norm(\%vector1);
757 my $vec2 = &norm(\%vector2);
758
759 # cosine
760 my $score = &_inner($vec1, $vec2);
761
762 return $score;
763 }
764  
765 sub findCompoundWord
766 {
767 my $def = shift;
768 my $ref_complist = shift;
769 my $new_def = "";
770  
771 my @words = split(' ', $def);
772 my $size_line = @words;
773 for (my $i=0; $i<$size_line; $i++)
774 {
775 my $w = $words[$i];
776 my $flag_print_w = 0;
777 my $flag_comp = 0;
778 if(defined $ref_complist->{$w})
779 {
780 # get the compound list start with word $w
781 my @comps = @{$ref_complist->{$w}};
782 foreach my $c (@comps)
783 {
784 #compare the rest of the compound word
785 my @string = split(' ', $c);
786  
787 my $count = 1;
788 foreach my $s (@string)
789 {
790 if (($i+$count)<$size_line)
791 {
792 if ($s eq $words[$i+$count])
793 {
794 $flag_comp = 1;
795 $count++;
796 }
797 else
798 {
799 $flag_comp = 0;
800 last;
801 }
802 }
803 } # test one compound word start by $w
804
805 # connect the compound word
806 if ($flag_comp==1)
807 {
808 unshift(@string, "$w");
809 my $comp = join('_', @string);
810 $new_def .= "$comp ";
811 if (defined $debugfile)
812 {
813 print DEBUG "compounds: $comp\n";
814 }
815 my $skip = @string-1;
816 $i = $i + $skip;
817 last;
818 }
819 } # test all the compound word start by $w
820  
821 # print out the $w if it doesn't match any compound words
822 if (($flag_print_w==0) and ($flag_comp==0))
823 {
824 $new_def .= "$w ";
825 $flag_print_w = 1;
826 }
827  
828 } # end of defined compound word start by $w
829  
830 if(!defined $ref_complist->{$w})
831 {
832 $new_def .= "$w ";
833 }
834 } # end of one definition
835
836 return $new_def;
837
838 }
839  
840  
841 # Subroutine to normalize a vector.
842 sub norm
843 {
844 my $vec = shift;
845 my $out = {};
846 my $lent = 0;
847 my $ind = 0;
848  
849 return {} if(!defined $vec);
850 foreach $ind (keys %{$vec})
851 {
852 $lent += (($vec->{$ind}) * ($vec->{$ind}));
853 }
854 $lent = sqrt($lent);
855 if($lent)
856 {
857 foreach $ind (keys %{$vec})
858 {
859 $out->{$ind} = $vec->{$ind}/$lent;
860 }
861 }
862  
863 return $out;
864 }
865  
866  
867 # Subroutine to find the dot-product of two vectors.
868 sub _inner
869 {
870 my $a = shift;
871 my $b = shift;
872 my $ind;
873 my $dotProduct = 0;
874  
875 return 0 if(!defined $a || !defined $b);
876 foreach $ind (keys %{$a})
877 {
878 $dotProduct += $a->{$ind} * $b->{$ind} if(defined $a->{$ind} && defined $b->{$ind});
879 }
880  
881 return $dotProduct;
882 }
883  
884 1;
885  
886 __END__