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

filter: Statementstmt 0.0% 0 / 55 Branchbran 0.0% 0 / 12 Conditioncond 0.0% 0 / 3 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 7 Podpod 0.0% 0 / 2 totaltotal 0.0% 0 / 79 SCAR 40.3
CC
7
Cov
0%
new
20.0
getRelatedness
20.0
BEGIN
2.0
CRAP 56.0
1 # UMLS::Similarity::cmatch.pm
2 #
3 # Module implementing the semantic relatedness measure described
4 # by Maedche and Staab (2001).
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::cmatch;
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::cmatch->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("cmatch", $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 # get the interface
88 my $interface = $self->{'interface'};
89  
90 # get the ancestors of each of the concepts
91 my $ancestors1 = $interface->findAncestors($concept1);
92 my $ancestors2 = $interface->findAncestors($concept2);
93  
94 my %union = (); my %intersection = ();
95  
96 foreach my $cui (sort keys %{$ancestors1}) {
97 if(exists ${$ancestors2}{$cui}) { $intersection{$cui}++; }
98 $union{$cui}++;
99 }
100  
101 foreach my $cui (sort keys %{$ancestors2}) {
102 $union{$cui}++;
103 }
104
105  
106 # calculate cmatch
107 my $a = keys %intersection;
108 my $b = keys %union;
109  
110 if($b <= 0) { return -1.0000; }
111  
112 my $score = $a / $b;
113
114 return $score;
115  
116 }
117  
118 1;
119 __END__