| 1 | # UMLS::Similarity::nam.pm | ||
| 2 | # | ||
| 3 | # Module implementing the semantic similarity measure described | ||
| 4 | # by Nguyen and Al-Mubaid (2006) | ||
| 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::nam; | ||
| 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 | my $originaloption = undef; | ||
| 54 | |||
| 55 | sub new | ||
| 56 | { | ||
| 57 | my $className = shift; | ||
| 58 | my $interface = shift; | ||
| 59 | my $params = shift; | ||
| 60 | |||
| 61 | return undef if(ref $className); | ||
| 62 | |||
| 63 | if($debug) { print STDERR "In UMLS::Similarity::nam->new()\n"; } | ||
| 64 | |||
| 65 | |||
| 66 | my $self = {}; | ||
| 67 | |||
| 68 | # Bless the object. | ||
| 69 | bless($self, $className); | ||
| 70 | |||
| 71 | # The backend interface object. | ||
| 72 | $self->{'interface'} = $interface; | ||
| 73 | |||
| 74 | # check the configuration file if defined | ||
| 75 | my $errorhandler = UMLS::Similarity::ErrorHandler->new("nam", $interface); | ||
| 76 | if(!$errorhandler) { | ||
| 77 | print STDERR "The UMLS::Similarity::ErrorHandler did not load properly\n"; | ||
| 78 | exit; | ||
| 79 | } | ||
| 80 | |||
| 81 | # check if the original distance score should be returned rather | ||
| 82 | # than the similarity score | ||
| 83 | if(defined $params->{"original"}) { $originaloption = 1; } | ||
| 84 | |||
| 85 | return $self; | ||
| 86 | } | ||
| 87 | |||
| 88 | |||
| 89 | sub getRelatedness | ||
| 90 | { | ||
| 91 | my $self = shift; | ||
| 92 | return undef if(!defined $self || !ref $self); | ||
| 93 | |||
| 94 | my $concept1 = shift; | ||
| 95 | my $concept2 = shift; | ||
| 96 | |||
| 97 | # set up the interface | ||
| 98 | my $interface = $self->{'interface'}; | ||
| 99 | |||
| 100 | # get the minimum depth of the two concepts | ||
| 101 | my $c1_depth = $interface->findMinimumDepth($concept1); | ||
| 102 | my $c2_depth = $interface->findMinimumDepth($concept2); | ||
| 103 | |||
| 104 | if( !(defined $c1_depth) or !(defined $c2_depth)) { | ||
| 105 | return -1; | ||
| 106 | } | ||
| 107 | |||
| 108 | # find the lcses | ||
| 109 | my $lcses = $interface->findLeastCommonSubsumer($concept1, $concept2); | ||
| 110 | |||
| 111 | # get the minimum depth of the lowest lcs | ||
| 112 | my $lcs_depth = -1; | ||
| 113 | foreach my $l (@{$lcses}) { | ||
| 114 | my $ldepth = $interface->findMinimumDepth($l); | ||
| 115 | if($lcs_depth == -1) { $lcs_depth = $ldepth; } | ||
| 116 | elsif($ldepth < $lcs_depth) { $lcs_depth = $ldepth; } | ||
| 117 | } | ||
| 118 | |||
| 119 | # if the lcs depth is zero just return zero | ||
| 120 | if($lcs_depth < 0) { return -1; } | ||
| 121 | |||
| 122 | # find the shortest path between the concepts | ||
| 123 | my $l = $interface->findShortestPathLength($concept1, $concept2); | ||
| 124 | |||
| 125 | # calculate nam | ||
| 126 | my $D = $interface->depth(); | ||
| 127 | my $score = log( $l * ($D-$lcs_depth) + 2 ) / log(2); | ||
| 128 | |||
| 129 | if(defined $originaloption) { return $score; } | ||
| 130 | else { return (1/$score); } | ||
| 131 | } | ||
| 132 | |||
| 133 | |||
| 134 | 1; | ||
| 135 | __END__ |