| 1 | # UMLS::Similarity::lch.pm | ||
| 2 | # | ||
| 3 | # Module implementing the semantic relatedness measure described | ||
| 4 | # by Leacock and Chodorow (1998). | ||
| 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 | # 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::lch; | ||
| 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::lch->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("lch", $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 interfaceg | ||
| 88 | my $interface = $self->{'interface'}; | ||
| 89 | |||
| 90 | # find the length of the shortest path | ||
| 91 | # if concept 1 and 2 are the same just return 1 | ||
| 92 | my $length = 0; | ||
| 93 | if($concept1 eq $concept2) { $length = 1; } | ||
| 94 | else { $length = $interface->findShortestPathLength($concept1, $concept2); } | ||
| 95 | |||
| 96 | # get the depth of the taxonomy | ||
| 97 | my $depth = $interface->depth(); | ||
| 98 | |||
| 99 | # if the length of hte path is less than zero return -1 | ||
| 100 | if($length < 0) { return -1; } | ||
| 101 | |||
| 102 | # calculate lch | ||
| 103 | my $score = -1 * log ($length / (2 * $depth)); | ||
| 104 | |||
| 105 | return $score; | ||
| 106 | |||
| 107 | } | ||
| 108 | |||
| 109 | 1; | ||
| 110 | __END__ |