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

filter: Statementstmt 0.0% 0 / 68 Branchbran 0.0% 0 / 20 Conditioncond 0.0% 0 / 9 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 7 Podpod 0.0% 0 / 2 totaltotal 0.0% 0 / 106 SCAR 48.8
CC
11
Cov
0%
getRelatedness
56.0
new
30.0
BEGIN
2.0
CRAP 132.0
1 # UMLS::Similarity::zhong.pm
2 #
3 # Module implementing the semantic relatedness measure described
4 # by Zhong, et all (2002)
5 # Copyright (c) 2004-2011,
6 #
7 # Bridget T McInnes, University of Minnesota, Twin Cities
8 # bthomson at cs.umn.edu
9 #
10 # Serguei Pakhomov, University of Minnesota, Twin Cities
11 # pakh002 at umn.edu
12 #
13 # Ted Pedersen, University of Minnesota, Duluth
14 # tpederse at d.umn.edu
15 #
16 # Ying Liu, University of Minnesota
17 # liux0935 at umn.edu
18 #
19 # This program is free software; you can redistribute it and/or
20 # modify it under the terms of the GNU General Public License
21 # as published by the Free Software Foundation; either version 2
22 # of the License, or (at your option) any later version.
23 #
24 # This program is distributed in the hope that it will be useful,
25 # but WITHOUT ANY WARRANTY; without even the implied warranty of
26 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27 # GNU General Public License for more details.
28 #
29 # You should have received a copy of the GNU General Public License
30 # along with this program; if not, write to
31 #
32 # The Free Software Foundation, Inc.,
33 # 59 Temple Place - Suite 330,
34 # Boston, MA 02111-1307, USA.
35  
36  
37 package UMLS::Similarity::zhong;
38  
39 use strict;
40 use warnings;
41 use UMLS::Similarity;
42 use UMLS::Similarity::ErrorHandler;
43  
44 use vars qw($VERSION);
45 $VERSION = '0.07';
46  
47 my $debug = 0;
48  
49 my $originaloption = undef;
50  
51 sub new
52 {
53 my $className = shift;
54 my $interface = shift;
55 my $params = shift;
56  
57 return undef if(ref $className);
58  
59 if($debug) { print STDERR "In UMLS::Similarity::zhong->new()\n"; }
60  
61  
62 my $self = {};
63
64 # Bless the object.
65 bless($self, $className);
66
67 # The backend interface object.
68 $self->{'interface'} = $interface;
69
70 # check the configuration file if defined
71 my $errorhandler = UMLS::Similarity::ErrorHandler->new("zhong", $interface);
72 if(!$errorhandler) {
73 print STDERR "The UMLS::Similarity::ErrorHandler did not load properly\n";
74 exit;
75 }
76  
77 # check if the original distance score should be returned rather
78 # than the similarity score
79 if(defined $params->{"original"}) { $originaloption = 1; }
80
81 return $self;
82 }
83  
84 sub getRelatedness
85 {
86 my $self = shift;
87 return undef if(!defined $self || !ref $self);
88  
89 my $concept1 = shift;
90 my $concept2 = shift;
91  
92 # if concept 1 and 2 are the same just return 1
93 if($concept1 eq $concept2) { return 1; }
94  
95 # get the interface
96 my $interface = $self->{'interface'};
97
98 # find the lcses of the two concepts
99 my $lcses = $interface->findLeastCommonSubsumer($concept1, $concept2);
100
101 # if there aren't any return zero
102 if($#{$lcses} < 0) { return -1; }
103
104 # get the depth of the lowest lcs
105 my $lcs_depth = 0; my $lcs = "";
106 foreach my $l (@{$lcses}) {
107 my $depth = $interface->findMaximumDepth($l);
108 if(defined $depth and $lcs_depth < $depth) {
109 $lcs_depth = $depth; $lcs = $l;
110 }
111 }
112
113 # find the shortestpath between the concept and the lcses
114 my $c1_length = $interface->findShortestPathLength($lcs, $concept1);
115 my $c2_length = $interface->findShortestPathLength($lcs, $concept2);
116
117 # get the depth of the concepts taking that path
118 my $c1_depth = $lcs_depth + $c1_length;
119 my $c2_depth = $lcs_depth + $c2_length;
120
121 # if the depth of one of them is less than zero return zero
122 if($c1_depth < 0 or $c2_depth < 0) { return -1; }
123
124 # otherwise calculate zhong
125 my $k = 2;
126 my $m1 = 1 / ($k**($c1_depth+1));
127 my $m2 = 1 / ($k**($c2_depth+1));
128 my $mlcs = 1 / ($k**($lcs_depth+1));
129  
130 my $score = (2 * $mlcs) / ($m1 + $m2);
131
132 if(defined $originaloption) { return $score; }
133 else { return (1/$score); }
134 }
135  
136 1;
137 __END__