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

filter: Statementstmt 0.0% 0 / 58 Branchbran 0.0% 0 / 14 Conditioncond 0.0% 0 / 3 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 7 Podpod 0.0% 0 / 2 totaltotal 0.0% 0 / 84 SCAR 42.8
CC
8
Cov
0%
getRelatedness
30.0
new
20.0
BEGIN
2.0
CRAP 72.0
1 # UMLS::Similarity::sanchez.pm
2 #
3 # Module implementing the semantic relatedness measure described
4 # by S{\'a}nchez, et al. (2012)
5 #
6 # Copyright (c) 2004-2015,
7 #
8 # Bridget T McInnes, University of Minnesota, Twin Cities
9 # bthomson at cs.umn.edu
10 #
11 # Siddharth Patwardhan, University of Utah, Salt Lake City
12 # sidd at cs.utah.edu
13 #
14 # Serguei Pakhomov, University of Minnesota, Twin Cities
15 # pakh002 at umn.edu
16 #
17 # Ted Pedersen, University of Minnesota, Duluth
18 # tpederse at d.umn.edu
19 #
20 # This program is free software; you can redistribute it and/or
21 # modify it under the terms of the GNU General Public License
22 # as published by the Free Software Foundation; either version 2
23 # of the License, or (at your option) any later version.
24 #
25 # This program is distributed in the hope that it will be useful,
26 # but WITHOUT ANY WARRANTY; without even the implied warranty of
27 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28 # GNU General Public License for more details.
29 #
30 # You should have received a copy of the GNU General Public License
31 # along with this program; if not, write to
32 #
33 # The Free Software Foundation, Inc.,
34 # 59 Temple Place - Suite 330,
35 # Boston, MA 02111-1307, USA.
36  
37  
38 package UMLS::Similarity::sanchez;
39  
40 use strict;
41 use warnings;
42 use UMLS::Similarity;
43 use UMLS::Similarity::ErrorHandler;
44  
45 use vars qw($VERSION);
46 $VERSION = '0.07';
47  
48 my $debug = 0;
49  
50 sub new
51 {
52 my $className = shift;
53 return undef if(ref $className);
54  
55 if($debug) { print STDERR "In UMLS::Similarity::sanchez->new()\n"; }
56  
57 my $interface = shift;
58  
59 my $self = {};
60
61 # Bless the object.
62 bless($self, $className);
63
64 # The backend interface object.
65 $self->{'interface'} = $interface;
66  
67 # check the configuration file if defined
68 my $errorhandler = UMLS::Similarity::ErrorHandler->new("sanchez", $interface);
69 if(!$errorhandler) {
70 print STDERR "The UMLS::Similarity::ErrorHandler did not load properly\n";
71 exit;
72 }
73  
74 return $self;
75 }
76  
77  
78 sub getRelatedness
79 {
80 my $self = shift;
81  
82 return undef if(!defined $self || !ref $self);
83  
84 my $concept1 = shift;
85 my $concept2 = shift;
86  
87 if($concept1 eq $concept2) { return 1; }
88  
89 # get the interface
90 my $interface = $self->{'interface'};
91  
92 # get the ancestors of each of the concepts
93 my $ancestors1 = $interface->findAncestors($concept1);
94 my $ancestors2 = $interface->findAncestors($concept2);
95  
96 my %union = (); my %intersection = ();
97  
98 foreach my $cui (sort keys %{$ancestors1}) {
99 if(exists ${$ancestors2}{$cui}) { $intersection{$cui}++; }
100 $union{$cui}++;
101 }
102  
103 foreach my $cui (sort keys %{$ancestors2}) {
104 $union{$cui}++;
105 }
106
107  
108 # calculate sanchez
109 my $a = keys %intersection;
110 my $b = keys %union;
111  
112 if($b <= 0) { return -1.0000; }
113  
114 my $score = -1 * (log( ($b-$a)/$b ) / log(2));
115
116 return $score;
117  
118 }
119  
120 1;
121 __END__