line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
# WordNet::Similarity::lin.pm version 2.04 |
2
|
|
|
|
|
|
|
# (Last updated $Id: lin.pm,v 1.22 2008/03/27 06:21:17 sidz1979 Exp $) |
3
|
|
|
|
|
|
|
# |
4
|
|
|
|
|
|
|
# Semantic Similarity Measure package implementing the measure |
5
|
|
|
|
|
|
|
# described by Lin (1998). |
6
|
|
|
|
|
|
|
# |
7
|
|
|
|
|
|
|
# Copyright (c) 2005, |
8
|
|
|
|
|
|
|
# |
9
|
|
|
|
|
|
|
# Ted Pedersen, University of Minnesota Duluth |
10
|
|
|
|
|
|
|
# tpederse at d.umn.edu |
11
|
|
|
|
|
|
|
# |
12
|
|
|
|
|
|
|
# Siddharth Patwardhan, University of Utah, Salt Lake City |
13
|
|
|
|
|
|
|
# sidd at cs.utah.edu |
14
|
|
|
|
|
|
|
# |
15
|
|
|
|
|
|
|
# Jason Michelizzi, Univeristy of Minnesota Duluth |
16
|
|
|
|
|
|
|
# mich0212 at d.umn.edu |
17
|
|
|
|
|
|
|
# |
18
|
|
|
|
|
|
|
# This program is free software; you can redistribute it and/or |
19
|
|
|
|
|
|
|
# modify it under the terms of the GNU General Public License |
20
|
|
|
|
|
|
|
# as published by the Free Software Foundation; either version 2 |
21
|
|
|
|
|
|
|
# of the License, or (at your option) any later version. |
22
|
|
|
|
|
|
|
# |
23
|
|
|
|
|
|
|
# This program is distributed in the hope that it will be useful, |
24
|
|
|
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
25
|
|
|
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
26
|
|
|
|
|
|
|
# GNU General Public License for more details. |
27
|
|
|
|
|
|
|
# |
28
|
|
|
|
|
|
|
# You should have received a copy of the GNU General Public License |
29
|
|
|
|
|
|
|
# along with this program; if not, write to |
30
|
|
|
|
|
|
|
# |
31
|
|
|
|
|
|
|
# The Free Software Foundation, Inc., |
32
|
|
|
|
|
|
|
# 59 Temple Place - Suite 330, |
33
|
|
|
|
|
|
|
# Boston, MA 02111-1307, USA. |
34
|
|
|
|
|
|
|
# |
35
|
|
|
|
|
|
|
# ------------------------------------------------------------------ |
36
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
package WordNet::Similarity::lin; |
38
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
=head1 NAME |
40
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
WordNet::Similarity::lin - Perl module for computing semantic relatedness |
42
|
|
|
|
|
|
|
of word senses using the information content based measure described by |
43
|
|
|
|
|
|
|
Lin (1998). |
44
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
=head1 SYNOPSIS |
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
use WordNet::Similarity::lin; |
48
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
use WordNet::QueryData; |
50
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
my $wn = WordNet::QueryData->new(); |
52
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
my $mymeasure = WordNet::Similarity::lin->new($wn); |
54
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
my $value = $mymeasure->getRelatedness("car#n#1", "bus#n#2"); |
56
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
($error, $errorString) = $mymeasure->getError(); |
58
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
die "$errorString\n" if($error); |
60
|
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
print "car (sense 1) <-> bus (sense 2) = $value\n"; |
62
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
=head1 DESCRIPTION |
64
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
Lin (1998) describes a method to compute the semantic relatedness of word |
66
|
|
|
|
|
|
|
senses using the information content of the concepts in WordNet and the |
67
|
|
|
|
|
|
|
'Similarity Theorem' (also described in the paper). This module implements |
68
|
|
|
|
|
|
|
this measure of semantic relatedness of concepts. |
69
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
=over |
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
=cut |
73
|
|
|
|
|
|
|
|
74
|
4
|
|
|
4
|
|
17818
|
use strict; |
|
4
|
|
|
|
|
10
|
|
|
4
|
|
|
|
|
165
|
|
75
|
4
|
|
|
4
|
|
265
|
use WordNet::Similarity::ICFinder; |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
our @ISA = qw(WordNet::Similarity::ICFinder); |
78
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
our $VERSION = '2.04'; |
80
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
=item $lin->getRelatedness ($synset1, $synset1) |
82
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
Computes the relatedness of two word senses using an information content |
84
|
|
|
|
|
|
|
scheme. The relatedness is equal to twice the information content of the |
85
|
|
|
|
|
|
|
LCS divided by the sum of the information content of each input synset. |
86
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
Parameters: two word senses in "word#pos#sense" format. |
88
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
Returns: Unless a problem occurs, the return value is the relatedness |
90
|
|
|
|
|
|
|
score. If no path exists between |
91
|
|
|
|
|
|
|
the two word senses, then a large negative number is returned. If an |
92
|
|
|
|
|
|
|
error occurs, then the error level is set to non-zero and an error string |
93
|
|
|
|
|
|
|
is created (see the description of getError()). Note: the error level |
94
|
|
|
|
|
|
|
will also be set to 1 and an error string will be created if no path |
95
|
|
|
|
|
|
|
exists between the words. |
96
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
=cut |
98
|
|
|
|
|
|
|
|
99
|
|
|
|
|
|
|
sub getRelatedness |
100
|
|
|
|
|
|
|
{ |
101
|
|
|
|
|
|
|
my $self = shift; |
102
|
|
|
|
|
|
|
my $wps1 = shift; |
103
|
|
|
|
|
|
|
my $wps2 = shift; |
104
|
|
|
|
|
|
|
my $wn = $self->{'wn'}; |
105
|
|
|
|
|
|
|
my $class = ref $self || $self; |
106
|
|
|
|
|
|
|
|
107
|
|
|
|
|
|
|
# Check the existence of the WordNet::QueryData object. |
108
|
|
|
|
|
|
|
unless ($wn) { |
109
|
|
|
|
|
|
|
$self->{errorString} .= "\nError (${class}::getRelatedness()) - "; |
110
|
|
|
|
|
|
|
$self->{errorString} .= "A WordNet::QueryData object is required."; |
111
|
|
|
|
|
|
|
$self->{error} = 2; |
112
|
|
|
|
|
|
|
return undef; |
113
|
|
|
|
|
|
|
} |
114
|
|
|
|
|
|
|
|
115
|
|
|
|
|
|
|
# Initialize traces. |
116
|
|
|
|
|
|
|
$self->{traceString} = ""; |
117
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
# JM 1-21-04 |
119
|
|
|
|
|
|
|
# moved input validation code to WordNet::Similarity::parseInput() |
120
|
|
|
|
|
|
|
my $ret = $self->parseWps ($wps1, $wps2); |
121
|
|
|
|
|
|
|
ref $ret or return $ret; |
122
|
|
|
|
|
|
|
my ($word1, $pos1, $sense1, $offset1, $word2, $pos2, $sense2, $offset2) |
123
|
|
|
|
|
|
|
= @{$ret}; |
124
|
|
|
|
|
|
|
|
125
|
|
|
|
|
|
|
my $synset1 = "$word1#$pos1#$sense1"; |
126
|
|
|
|
|
|
|
my $synset2 = "$word2#$pos2#$sense2"; |
127
|
|
|
|
|
|
|
|
128
|
|
|
|
|
|
|
# Now check if the similarity value for these two synsets is in |
129
|
|
|
|
|
|
|
# fact in the cache... if so return the cached value. |
130
|
|
|
|
|
|
|
my $relatedness = |
131
|
|
|
|
|
|
|
$self->{doCache} ? $self->fetchFromCache ($synset1, $synset2) : undef; |
132
|
|
|
|
|
|
|
defined $relatedness and return $relatedness; |
133
|
|
|
|
|
|
|
|
134
|
|
|
|
|
|
|
# Now get down to really finding the relatedness of these two. |
135
|
|
|
|
|
|
|
my @LCSs = $self->getLCSbyIC ($synset1, $synset2, $pos1, 'wps'); |
136
|
|
|
|
|
|
|
|
137
|
|
|
|
|
|
|
my $ref = shift @LCSs; |
138
|
|
|
|
|
|
|
|
139
|
|
|
|
|
|
|
unless (defined $ref) { |
140
|
|
|
|
|
|
|
return $self->UNRELATED; |
141
|
|
|
|
|
|
|
} |
142
|
|
|
|
|
|
|
|
143
|
|
|
|
|
|
|
my ($lcs, $lcsic) = @{$ref}; |
144
|
|
|
|
|
|
|
|
145
|
|
|
|
|
|
|
my $ic1 = $self->IC ($offset1, $pos1); |
146
|
|
|
|
|
|
|
my $ic2 = $self->IC ($offset2, $pos2); |
147
|
|
|
|
|
|
|
my $score = ($ic1 && $ic2) ? ((2 * $lcsic) / ($ic1 + $ic2)) : 0; |
148
|
|
|
|
|
|
|
|
149
|
|
|
|
|
|
|
# what does this do? |
150
|
|
|
|
|
|
|
$score = ($score == -1) ? 0 : $score; |
151
|
|
|
|
|
|
|
|
152
|
|
|
|
|
|
|
if ($self->{trace}) { |
153
|
|
|
|
|
|
|
$self->{traceString} .= "Concept1: $synset1 (IC="; |
154
|
|
|
|
|
|
|
$self->{traceString} .= sprintf ("%.6f", $ic1); |
155
|
|
|
|
|
|
|
$self->{traceString} .= ")\n"; |
156
|
|
|
|
|
|
|
$self->{traceString} .= "Concept2: $synset2 (IC="; |
157
|
|
|
|
|
|
|
$self->{traceString} .= sprintf ("%.6f", $ic2); |
158
|
|
|
|
|
|
|
$self->{traceString} .= ")\n"; |
159
|
|
|
|
|
|
|
} |
160
|
|
|
|
|
|
|
|
161
|
|
|
|
|
|
|
$self->{doCache} and $self->storeToCache ($wps1, $wps2, $score); |
162
|
|
|
|
|
|
|
return $score; |
163
|
|
|
|
|
|
|
} |
164
|
|
|
|
|
|
|
|
165
|
|
|
|
|
|
|
# JM 1-16-04 |
166
|
|
|
|
|
|
|
# moved subroutine _getLeastCommonSubsumers to ICFinder.pm |
167
|
|
|
|
|
|
|
|
168
|
|
|
|
|
|
|
1; |
169
|
|
|
|
|
|
|
|
170
|
|
|
|
|
|
|
__END__ |