| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
1
|
|
|
|
|
|
|
# WordNet::Similarity::res.pm version 2.04 |
|
2
|
|
|
|
|
|
|
# (Last updated $Id: res.pm,v 1.21 2008/03/27 06:21:17 sidz1979 Exp $) |
|
3
|
|
|
|
|
|
|
# |
|
4
|
|
|
|
|
|
|
# Semantic Similarity Measure package implementing the measure |
|
5
|
|
|
|
|
|
|
# described by Resnik (1995). |
|
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::res; |
|
38
|
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
=head1 NAME |
|
40
|
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
WordNet::Similarity::res - Perl module for computing semantic relatedness |
|
42
|
|
|
|
|
|
|
of word senses using an information content based measure described by |
|
43
|
|
|
|
|
|
|
Resnik (1995). |
|
44
|
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
=head1 SYNOPSIS |
|
46
|
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
use WordNet::Similarity::res; |
|
48
|
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
use WordNet::QueryData; |
|
50
|
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
my $wn = WordNet::QueryData->new(); |
|
52
|
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
my $object = WordNet::Similarity::res->new($wn); |
|
54
|
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
my $value = $object->getRelatedness("car#n#1", "bus#n#2"); |
|
56
|
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
($error, $errorString) = $object->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
|
|
|
|
|
|
|
Resnik (1995) uses the information content of concepts, computed from their |
|
66
|
|
|
|
|
|
|
frequency of occurrence in a large corpus, to determine the semantic |
|
67
|
|
|
|
|
|
|
relatedness of word senses. This module implements this measure of semantic |
|
68
|
|
|
|
|
|
|
relatedness. |
|
69
|
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
The following methods are defined: |
|
71
|
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
=over |
|
73
|
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
=cut |
|
75
|
|
|
|
|
|
|
|
|
76
|
4
|
|
|
4
|
|
30079
|
use strict; |
|
|
4
|
|
|
|
|
8
|
|
|
|
4
|
|
|
|
|
89
|
|
|
77
|
4
|
|
|
4
|
|
704
|
use WordNet::Similarity::ICFinder; |
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
our @ISA = qw/WordNet::Similarity::ICFinder/; |
|
80
|
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
our $VERSION = '2.04'; |
|
82
|
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
# The 'new' method for this class is supplied by WordNet::Similarity |
|
84
|
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
=item $res->getRelatedness ($synset1, $synset2) |
|
86
|
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
Computes the relatedness of two word senses using an information content |
|
88
|
|
|
|
|
|
|
scheme. The relatedness is equal to the information content of the least |
|
89
|
|
|
|
|
|
|
common subsumer of the input synsets. |
|
90
|
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
Parameters: two word senses in "word#pos#sense" format. |
|
92
|
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
Returns: Unless a problem occurs, the return value is the relatedness |
|
94
|
|
|
|
|
|
|
score. If no path exists between |
|
95
|
|
|
|
|
|
|
the two word senses, then a large negative number is returned. If an |
|
96
|
|
|
|
|
|
|
error occurs, then the error level is set to non-zero and an error string |
|
97
|
|
|
|
|
|
|
is created (see the description of getError()). Note: the error level |
|
98
|
|
|
|
|
|
|
will also be set to 1 and an error string will be created if no path |
|
99
|
|
|
|
|
|
|
exists between the words. |
|
100
|
|
|
|
|
|
|
|
|
101
|
|
|
|
|
|
|
=cut |
|
102
|
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
sub getRelatedness |
|
104
|
|
|
|
|
|
|
{ |
|
105
|
|
|
|
|
|
|
my $self = shift; |
|
106
|
|
|
|
|
|
|
my $wps1 = shift; |
|
107
|
|
|
|
|
|
|
my $wps2 = shift; |
|
108
|
|
|
|
|
|
|
my $wn = $self->{wn}; |
|
109
|
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
my $class = ref $self || $self; |
|
111
|
|
|
|
|
|
|
# Check the existence of the WordNet::QueryData object. |
|
112
|
|
|
|
|
|
|
unless ($wn) { |
|
113
|
|
|
|
|
|
|
$self->{errorString} .= "\nError (${class}::getRelatedness()) - "; |
|
114
|
|
|
|
|
|
|
$self->{errorString} .= "A WordNet::QueryData object is required."; |
|
115
|
|
|
|
|
|
|
$self->{error} = 2; |
|
116
|
|
|
|
|
|
|
return undef; |
|
117
|
|
|
|
|
|
|
} |
|
118
|
|
|
|
|
|
|
|
|
119
|
|
|
|
|
|
|
# JM 1-21-04 |
|
120
|
|
|
|
|
|
|
# moved input validation code to parseWps() in a super-class |
|
121
|
|
|
|
|
|
|
my $ret = $self->parseWps ($wps1, $wps2); |
|
122
|
|
|
|
|
|
|
ref $ret or return $ret; |
|
123
|
|
|
|
|
|
|
my ($word1, $pos1, undef, $offset1, $word2, $pos2, undef, $offset2) = @{$ret}; |
|
124
|
|
|
|
|
|
|
|
|
125
|
|
|
|
|
|
|
# Initialize traces. |
|
126
|
|
|
|
|
|
|
$self->{traceString} = ""; |
|
127
|
|
|
|
|
|
|
|
|
128
|
|
|
|
|
|
|
my $pos = $pos1; |
|
129
|
|
|
|
|
|
|
|
|
130
|
|
|
|
|
|
|
# Now check if the similarity value for these two synsets is in |
|
131
|
|
|
|
|
|
|
# fact in the cache... if so return the cached value. |
|
132
|
|
|
|
|
|
|
my $relatedness = |
|
133
|
|
|
|
|
|
|
$self->{doCache} ? $self->fetchFromCache ($wps1, $wps2) : undef; |
|
134
|
|
|
|
|
|
|
defined $relatedness and return $relatedness; |
|
135
|
|
|
|
|
|
|
|
|
136
|
|
|
|
|
|
|
# Now get down to really finding the relatedness of these two. |
|
137
|
|
|
|
|
|
|
$self->{traceString} = ""; |
|
138
|
|
|
|
|
|
|
|
|
139
|
|
|
|
|
|
|
unless ($offset1 and $offset2) { |
|
140
|
|
|
|
|
|
|
$self->{errorString} .= "\nWarning (${class}::getRelatedness()) - "; |
|
141
|
|
|
|
|
|
|
$self->{errorString} .= "Input senses not found in WordNet."; |
|
142
|
|
|
|
|
|
|
$self->{error} = ($self->{'error'} < 1) ? 1 : $self->{'error'}; |
|
143
|
|
|
|
|
|
|
return undef; |
|
144
|
|
|
|
|
|
|
} |
|
145
|
|
|
|
|
|
|
|
|
146
|
|
|
|
|
|
|
my @LCSs = $self->getLCSbyIC ($offset1, $offset2, $pos1, "offset"); |
|
147
|
|
|
|
|
|
|
|
|
148
|
|
|
|
|
|
|
my $ref = shift @LCSs; |
|
149
|
|
|
|
|
|
|
|
|
150
|
|
|
|
|
|
|
unless (defined $ref) { |
|
151
|
|
|
|
|
|
|
return $self->UNRELATED; |
|
152
|
|
|
|
|
|
|
} |
|
153
|
|
|
|
|
|
|
|
|
154
|
|
|
|
|
|
|
my ($lcs, $ic) = @{$ref}; |
|
155
|
|
|
|
|
|
|
|
|
156
|
|
|
|
|
|
|
my $score = $ic; |
|
157
|
|
|
|
|
|
|
|
|
158
|
|
|
|
|
|
|
$self->{doCache} and $self->storeToCache ($wps1, $wps2, $score); |
|
159
|
|
|
|
|
|
|
return $score; |
|
160
|
|
|
|
|
|
|
} |
|
161
|
|
|
|
|
|
|
|
|
162
|
|
|
|
|
|
|
# JM 1-16-04 |
|
163
|
|
|
|
|
|
|
# moved subroutine _getLeastCommonSubsumers to ICFinder.pm |
|
164
|
|
|
|
|
|
|
|
|
165
|
|
|
|
|
|
|
1; |
|
166
|
|
|
|
|
|
|
|
|
167
|
|
|
|
|
|
|
__END__ |