line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
# WordNet::Similarity::path.pm version 2.04 |
2
|
|
|
|
|
|
|
# (Last updated $Id: path.pm,v 1.19 2008/03/27 06:21:17 sidz1979 Exp $) |
3
|
|
|
|
|
|
|
# |
4
|
|
|
|
|
|
|
# N.B., this module was formerly named edge.pm |
5
|
|
|
|
|
|
|
# |
6
|
|
|
|
|
|
|
# Semantic Similarity Measure package implementing a simple |
7
|
|
|
|
|
|
|
# path-length (node-counting) semantic relatedness measure. |
8
|
|
|
|
|
|
|
# |
9
|
|
|
|
|
|
|
# Copyright (c) 2005, |
10
|
|
|
|
|
|
|
# |
11
|
|
|
|
|
|
|
# Ted Pedersen, University of Minnesota Duluth |
12
|
|
|
|
|
|
|
# tpederse at d.umn.edu |
13
|
|
|
|
|
|
|
# |
14
|
|
|
|
|
|
|
# Siddharth Patwardhan, University of Utah, Salt Lake City |
15
|
|
|
|
|
|
|
# sidd at cs.utah.edu |
16
|
|
|
|
|
|
|
# |
17
|
|
|
|
|
|
|
# Jason Michelizzi, Univeristy of Minnesota Duluth |
18
|
|
|
|
|
|
|
# mich0212 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
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
package WordNet::Similarity::path; |
40
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
=head1 NAME |
42
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
WordNet::Similarity::path - Perl module for computing semantic relatedness |
44
|
|
|
|
|
|
|
of word senses by counting nodes in the noun and verb WordNet 'is-a' |
45
|
|
|
|
|
|
|
hierarchies. |
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
=head1 SYNOPSIS |
48
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
use WordNet::Similarity::path; |
50
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
use WordNet::QueryData; |
52
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
my $wn = WordNet::QueryData->new(); |
54
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
my $path = WordNet::Similarity::path->new($wn); |
56
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
my $value = $measure->getRelatedness("car#n#1", "bus#n#2"); |
58
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
my ($error, $errorString) = $measure->getError(); |
60
|
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
die "$errorString\n" if($error); |
62
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
print "car (sense 1) <-> bus (sense 2) = $value\n"; |
64
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
=head1 DESCRIPTION |
66
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
This module computes the semantic relatedness of word senses by counting |
68
|
|
|
|
|
|
|
the number of nodes along the shortest path between the senses in the |
69
|
|
|
|
|
|
|
'is-a' hierarchies of WordNet. The path lengths include the end nodes. |
70
|
|
|
|
|
|
|
For example, the path between shrub#n#1 and tree#n#1 is |
71
|
|
|
|
|
|
|
S. |
72
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
Since a longer path length indicate less relatedness, the relatedness |
74
|
|
|
|
|
|
|
value returned is the multiplicative inverse of the path length (distance) |
75
|
|
|
|
|
|
|
between the two concepts: S. If the two |
76
|
|
|
|
|
|
|
concepts are identical, then the distance between them is one; therefore, |
77
|
|
|
|
|
|
|
their relatedness is also 1. If no path is found, then a large negative |
78
|
|
|
|
|
|
|
number is returned and an error is generated (see C). |
79
|
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
The following methods are defined: |
81
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
=over |
83
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
=cut |
85
|
|
|
|
|
|
|
|
86
|
4
|
|
|
4
|
|
14044
|
use strict; |
|
4
|
|
|
|
|
10
|
|
|
4
|
|
|
|
|
266
|
|
87
|
4
|
|
|
4
|
|
186
|
use WordNet::Similarity::PathFinder; |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
use constant MAX_DIST => 100; |
89
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
our @ISA = qw(WordNet::Similarity::PathFinder); |
91
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
our $VERSION = '2.04'; |
93
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
=item $path->getRelatedness() |
95
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
Computes the relatedness of two word senses using a node counting scheme. |
97
|
|
|
|
|
|
|
The relatedness score is inversely proportional to the number of nodes |
98
|
|
|
|
|
|
|
along the shortest path between the two word senses. |
99
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
Parameters: two word senses in "word#pos#sense" format. |
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
Returns: Unless a problem occurs, the return value is the relatedness |
103
|
|
|
|
|
|
|
score, which belongs to the interval (0, 1]. If no path exists between |
104
|
|
|
|
|
|
|
the two word senses, then a large negative number is returned. If an |
105
|
|
|
|
|
|
|
error occurs, then the error level is set to non-zero and an error string |
106
|
|
|
|
|
|
|
is created (see the description of getError()). Note: the error level |
107
|
|
|
|
|
|
|
will also be set to 1 and an error string will be created if no path |
108
|
|
|
|
|
|
|
exists between the words. |
109
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
=cut |
111
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
sub getRelatedness |
113
|
|
|
|
|
|
|
{ |
114
|
|
|
|
|
|
|
my $self = shift; |
115
|
|
|
|
|
|
|
my $wps1 = shift; |
116
|
|
|
|
|
|
|
my $wps2 = shift; |
117
|
|
|
|
|
|
|
my $wn = $self->{wn}; |
118
|
|
|
|
|
|
|
my $class = ref $self || $self; |
119
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
# Initialize traces. |
121
|
|
|
|
|
|
|
$self->{traceString} = "" if($self->{'trace'}); |
122
|
|
|
|
|
|
|
|
123
|
|
|
|
|
|
|
# JM 1-21-04 |
124
|
|
|
|
|
|
|
# moved input validation code to WordNet::Similarity::parseInput() |
125
|
|
|
|
|
|
|
my $ret = $self->parseWps ($wps1, $wps2); |
126
|
|
|
|
|
|
|
ref $ret or return $ret; |
127
|
|
|
|
|
|
|
|
128
|
|
|
|
|
|
|
my ($word1, $pos1, $sense1, $offset1, $word2, $pos2, $sense2, $offset2) |
129
|
|
|
|
|
|
|
= @{$ret}; |
130
|
|
|
|
|
|
|
|
131
|
|
|
|
|
|
|
$wps1 = "$word1#$pos1#$sense1"; |
132
|
|
|
|
|
|
|
$wps2 = "$word2#$pos2#$sense2"; |
133
|
|
|
|
|
|
|
|
134
|
|
|
|
|
|
|
# Now check if the similarity value for these two synsets is in |
135
|
|
|
|
|
|
|
# fact in the cache... if so return the cached value. |
136
|
|
|
|
|
|
|
my $relatedness = |
137
|
|
|
|
|
|
|
$self->{doCache} ? $self->fetchFromCache ($wps1, $wps2) : undef; |
138
|
|
|
|
|
|
|
defined $relatedness and return $relatedness; |
139
|
|
|
|
|
|
|
|
140
|
|
|
|
|
|
|
|
141
|
|
|
|
|
|
|
# JM 1/23/04 |
142
|
|
|
|
|
|
|
# Most of the code that does the work of finding the LCS and |
143
|
|
|
|
|
|
|
# hypernym trees has been moved into LCSFinder and PathFinder |
144
|
|
|
|
|
|
|
|
145
|
|
|
|
|
|
|
#my ($dist, $pathref) = $self->getShortestPath ($offset1, $offset2, |
146
|
|
|
|
|
|
|
# $pos1, 'offset'); |
147
|
|
|
|
|
|
|
|
148
|
|
|
|
|
|
|
# there can be multiple shortest paths (i.e., paths of the same length) |
149
|
|
|
|
|
|
|
my @paths = $self->getShortestPath ($offset1, $offset2, $pos1, 'offset'); |
150
|
|
|
|
|
|
|
|
151
|
|
|
|
|
|
|
my $path = shift @paths; |
152
|
|
|
|
|
|
|
|
153
|
|
|
|
|
|
|
# $path will be undef if no path was found (error messages already generated) |
154
|
|
|
|
|
|
|
unless (defined $path) { |
155
|
|
|
|
|
|
|
return $self->UNRELATED; |
156
|
|
|
|
|
|
|
} |
157
|
|
|
|
|
|
|
|
158
|
|
|
|
|
|
|
my $dist = $path->[0]; |
159
|
|
|
|
|
|
|
|
160
|
|
|
|
|
|
|
# JM 1-29-04 |
161
|
|
|
|
|
|
|
# most of the code that does path-finding is now in PathFinder |
162
|
|
|
|
|
|
|
|
163
|
|
|
|
|
|
|
if ($dist > 0) { |
164
|
|
|
|
|
|
|
my $score = 1.0 / $dist; |
165
|
|
|
|
|
|
|
|
166
|
|
|
|
|
|
|
$self->{doCache} and $self->storeToCache ($wps1, $wps2, $score); |
167
|
|
|
|
|
|
|
|
168
|
|
|
|
|
|
|
return $score; |
169
|
|
|
|
|
|
|
} |
170
|
|
|
|
|
|
|
else { |
171
|
|
|
|
|
|
|
$self->{errorString} .= "\nWarning (${class}::getRelatedness()) - "; |
172
|
|
|
|
|
|
|
$self->{errorString} .= "Internal error while finding relatedness."; |
173
|
|
|
|
|
|
|
$self->{error} = ($self->{error} < 1) ? 1 : $self->{error}; |
174
|
|
|
|
|
|
|
return undef; |
175
|
|
|
|
|
|
|
} |
176
|
|
|
|
|
|
|
} |
177
|
|
|
|
|
|
|
|
178
|
|
|
|
|
|
|
# JM |
179
|
|
|
|
|
|
|
# Much of the code that was shared between measures has been relocated. |
180
|
|
|
|
|
|
|
# WordNet::Similarity contains code common to (almost) all measures, |
181
|
|
|
|
|
|
|
# WordNet::Similarity::PathFinder has code common among path finding |
182
|
|
|
|
|
|
|
# measures. |
183
|
|
|
|
|
|
|
|
184
|
|
|
|
|
|
|
1; |
185
|
|
|
|
|
|
|
|
186
|
|
|
|
|
|
|
__END__ |