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

filter: Statementstmt 0.0% 0 / 58 Branchbran 0.0% 0 / 16 Conditioncond 0.0% 0 / 9 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 7 Podpod 0.0% 0 / 2 totaltotal 0.0% 0 / 92 SCAR 45.0
CC
9
Cov
0%
getRelatedness
42.0
new
20.0
BEGIN
2.0
CRAP 90.0
1 # UMLS::Similarity::wup.pm
2 #
3 # Module implementing the semantic relatedness measure described
4 # by Wu and Palmer (1994)
5 #
6 # Copyright (c) 2004-2011,
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 # Ying Liu, University of Minnesota
21 # liux0935 at umn.edu
22 #
23 # This program is free software; you can redistribute it and/or
24 # modify it under the terms of the GNU General Public License
25 # as published by the Free Software Foundation; either version 2
26 # of the License, or (at your option) any later version.
27 #
28 # This program is distributed in the hope that it will be useful,
29 # but WITHOUT ANY WARRANTY; without even the implied warranty of
30 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
31 # GNU General Public License for more details.
32 #
33 # You should have received a copy of the GNU General Public License
34 # along with this program; if not, write to
35 #
36 # The Free Software Foundation, Inc.,
37 # 59 Temple Place - Suite 330,
38 # Boston, MA 02111-1307, USA.
39  
40  
41 package UMLS::Similarity::wup;
42  
43 use strict;
44 use warnings;
45 use UMLS::Similarity;
46 use UMLS::Similarity::ErrorHandler;
47  
48 use vars qw($VERSION);
49 $VERSION = '0.07';
50  
51 my $debug = 0;
52  
53 sub new
54 {
55 my $className = shift;
56 return undef if(ref $className);
57  
58 if($debug) { print STDERR "In UMLS::Similarity::wup->new()\n"; }
59  
60 my $interface = shift;
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("wup", $interface);
72 if(!$errorhandler) {
73 print STDERR "The UMLS::Similarity::ErrorHandler did not load properly\n";
74 exit;
75 }
76
77 return $self;
78 }
79  
80  
81 sub getRelatedness
82 {
83 my $self = shift;
84 return undef if(!defined $self || !ref $self);
85  
86 my $concept1 = shift;
87 my $concept2 = shift;
88  
89 # if concept 1 and 2 are the same just return 1
90 if($concept1 eq $concept2) { return 1; }
91  
92 # get the interface
93 my $interface = $self->{'interface'};
94
95 # find the lcses of the two concepts
96 my $lcses = $interface->findLeastCommonSubsumer($concept1, $concept2);
97
98 # if there aren't any return zero
99 if($#{$lcses} < 0) { return -1; }
100
101 # get the depth of the lowest lcs
102 my $lcs_depth = 0; my $lcs = "";
103 foreach my $l (@{$lcses}) {
104 my $depth = $interface->findMaximumDepth($l);
105 if(defined $depth and $lcs_depth < $depth) {
106 $lcs_depth = $depth; $lcs = $l;
107 }
108 }
109
110 # find the shortestpath between the concept and the lcses
111 my $c1_length = $interface->findShortestPathLength($lcs, $concept1);
112 my $c2_length = $interface->findShortestPathLength($lcs, $concept2);
113
114 # get the depth of the concepts taking that path
115 my $c1_depth = $lcs_depth + $c1_length;
116 my $c2_depth = $lcs_depth + $c2_length;
117
118 # if the depth of one of them is less than zero return zero
119 if($c1_depth < 0 or $c2_depth < 0) { return -1; }
120
121 # otherwise calculate wup
122 my $score = (2 * $lcs_depth) / ($c1_depth + $c2_depth);
123
124 return $score;
125 }
126  
127 1;
128 __END__