| 1 | # UMLS::Similarity::pks.pm | ||
| 2 | # | ||
| 3 | # Module implementing the semantic relatedness measure described | ||
| 4 | # by Pekar and Staab (2002) | ||
| 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::pks; | ||
| 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::pks->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("pks", $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 | if($concept1 eq $concept2) { return 1; } | ||
| 88 | |||
| 89 | # get the interfaceg | ||
| 90 | my $interface = $self->{'interface'}; | ||
| 91 | |||
| 92 | # get the least common subsumer(s) | ||
| 93 | my $lcses = $interface->findLeastCommonSubsumer($concept1, $concept2); | ||
| 94 | |||
| 95 | # if there aren't any return zero | ||
| 96 | if($#{$lcses} < 0) { return -1; } | ||
| 97 | |||
| 98 | # get the root | ||
| 99 | my $root = $interface->root(); | ||
| 100 | |||
| 101 | # get the shortest path of the lcs | ||
| 102 | my $lcs_sp_length = 9999999999999; my $lcs = ""; | ||
| 103 | foreach my $l (@{$lcses}) { | ||
| 104 | my $path_length = $interface->findShortestPathLength($root, $l); | ||
| 105 | if(defined $path_length) { | ||
| 106 | if($path_length < $lcs_sp_length) { | ||
| 107 | $lcs_sp_length = $path_length; $lcs = $l; | ||
| 108 | } | ||
| 109 | } | ||
| 110 | } | ||
| 111 | |||
| 112 | my $c1_sp_length = $interface->findShortestPathLength($lcs, $concept1); | ||
| 113 | my $c2_sp_length = $interface->findShortestPathLength($lcs, $concept2); | ||
| 114 | |||
| 115 | # if one of the shortest path lengths is less than zero return -1 | ||
| 116 | if($c1_sp_length < 0) { return -1; } | ||
| 117 | if($c2_sp_length < 0) { return -1; } | ||
| 118 | if($lcs_sp_length < 0) { return -1; } | ||
| 119 | |||
| 120 | # calculate pks | ||
| 121 | my $a = $lcs_sp_length; | ||
| 122 | my $b = $c1_sp_length + $c2_sp_length + $lcs_sp_length; | ||
| 123 | |||
| 124 | if($b <= 0) { return -1.0000; } | ||
| 125 | |||
| 126 | my $score = $a / $b; | ||
| 127 | |||
| 128 | return $score; | ||
| 129 | |||
| 130 | } | ||
| 131 | |||
| 132 | 1; | ||
| 133 | __END__ |