| 1 | # UMLS::Similarity::upath.pm | ||
| 2 | # | ||
| 3 | # Module implementing the simple edge counting measure of | ||
| 4 | # semantic relatedness. | ||
| 5 | # | ||
| 6 | # Copyright (c) 2004-2011, | ||
| 7 | # | ||
| 8 | # Bridget T McInnes, University of Minnesota, Twin Cities | ||
| 9 | # bthomson at cs.umn.edu | ||
| 10 | # | ||
| 11 | # Serguei Pakhomov, University of Minnesota, Twin Cities | ||
| 12 | # pakh002 at umn.edu | ||
| 13 | # | ||
| 14 | # Ted Pedersen, University of Minnesota, Duluth | ||
| 15 | # tpederse at d.umn.edu | ||
| 16 | # | ||
| 17 | # Ying Liu, University of Minnesota | ||
| 18 | # liux0935 at 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::upath; | ||
| 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 | my $weighted = 0; | ||
| 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 | my $self = {}; | ||
| 60 | |||
| 61 | # get options | ||
| 62 | $params = {} if(!defined $params); | ||
| 63 | $weighted = $params->{'weighted'}; | ||
| 64 | |||
| 65 | # Bless the object. | ||
| 66 | bless($self, $className); | ||
| 67 | |||
| 68 | # The backend interface object. | ||
| 69 | $self->{'interface'} = $interface; | ||
| 70 | |||
| 71 | # check the configuration file if defined | ||
| 72 | my $errorhandler = UMLS::Similarity::ErrorHandler->new("upath", $interface); | ||
| 73 | if(!$errorhandler) { | ||
| 74 | print STDERR "The UMLS::Similarity::ErrorHandler did not load properly\n"; | ||
| 75 | exit; | ||
| 76 | } | ||
| 77 | |||
| 78 | # set the undirected and realtime options | ||
| 79 | $interface->setRealtimeOption(1); | ||
| 80 | $interface->setUndirectedOption(1); | ||
| 81 | |||
| 82 | return $self; | ||
| 83 | } | ||
| 84 | |||
| 85 | |||
| 86 | sub getRelatedness | ||
| 87 | { | ||
| 88 | my $self = shift; | ||
| 89 | return undef if(!defined $self || !ref $self); | ||
| 90 | |||
| 91 | my $concept1 = shift; | ||
| 92 | my $concept2 = shift; | ||
| 93 | |||
| 94 | # if concept 1 and 2 are the same just return 1 | ||
| 95 | if($concept1 eq $concept2) { return 1; } | ||
| 96 | |||
| 97 | if($weighted) { | ||
| 98 | return _getWeightedRelatedness($self, $concept1, $concept2); | ||
| 99 | } | ||
| 100 | else { | ||
| 101 | return _getUnweightedRelatedness($self, $concept1, $concept2); | ||
| 102 | } | ||
| 103 | } | ||
| 104 | |||
| 105 | sub _getUnweightedRelatedness { | ||
| 106 | |||
| 107 | my $self = shift; | ||
| 108 | return undef if(!defined $self || !ref $self); | ||
| 109 | |||
| 110 | my $concept1 = shift; | ||
| 111 | my $concept2 = shift; | ||
| 112 | |||
| 113 | # get the interface | ||
| 114 | my $interface = $self->{'interface'}; | ||
| 115 | # find the shortest paths | ||
| 116 | my $length = $interface->findShortestPathLength($concept1, $concept2); | ||
| 117 | |||
| 118 | # if length is less than zero (this shouldn't happen) | ||
| 119 | # return a score of zero | ||
| 120 | if($length <= 0) { return $length; } | ||
| 121 | |||
| 122 | # otherwise return the reciprocal of the length | ||
| 123 | return (1/$length); | ||
| 124 | } | ||
| 125 | |||
| 126 | sub _getWeightedRelatedness | ||
| 127 | { | ||
| 128 | my $self = shift; | ||
| 129 | return undef if(!defined $self || !ref $self); | ||
| 130 | |||
| 131 | my $concept1 = shift; | ||
| 132 | my $concept2 = shift; | ||
| 133 | |||
| 134 | # get the interface | ||
| 135 | my $interface = $self->{'interface'}; | ||
| 136 | |||
| 137 | my $shortestpaths = $interface->findShortestPath($concept1, $concept2); | ||
| 138 | |||
| 139 | my $minturn = 0; my $length = 0; | ||
| 140 | foreach my $path (@{$shortestpaths}) { | ||
| 141 | |||
| 142 | my @shortestpath = split/\s+/, $path; | ||
| 143 | |||
| 144 | $length = $#shortestpath + 1; | ||
| 145 | |||
| 146 | my $turn = -1; my $prevrel = ""; | ||
| 147 | foreach my $i (0..$#shortestpath) { | ||
| 148 | # get the concept | ||
| 149 | my $concept = $shortestpath[$i]; | ||
| 150 | |||
| 151 | if( $i < $#shortestpath) { | ||
| 152 | my $second = $shortestpath[$i+1]; | ||
| 153 | my $relations = $interface->getRelationsBetweenCuis($concept, $second); | ||
| 154 | my $relsab = shift @{$relations}; | ||
| 155 | $relsab=~/([A-Z]+) \([A-Z\_]+\)/; | ||
| 156 | my $rel = $1; | ||
| 157 | if($rel ne $prevrel) { $turn++; } | ||
| 158 | $prevrel = $rel; | ||
| 159 | } | ||
| 160 | } | ||
| 161 | if($turn < $minturn || $minturn == 0) { $minturn = $turn; } | ||
| 162 | } | ||
| 163 | |||
| 164 | #print STDERR "$concept1 : $concept2 : $length : $minturn : \n"; | ||
| 165 | if($length <= 0) { return -1; } | ||
| 166 | |||
| 167 | my $score = 1 / ( $length - ( $minturn + .5) ); | ||
| 168 | my $temp = 1 / $length; | ||
| 169 | #print STDERR "$score : $temp\n"; | ||
| 170 | |||
| 171 | return (1 / ($minturn + 1)); | ||
| 172 | } | ||
| 173 | |||
| 174 | 1; | ||
| 175 | __END__ |