-
Notifications
You must be signed in to change notification settings - Fork 0
/
wordnet_sim_sample.pl
211 lines (162 loc) · 6.56 KB
/
wordnet_sim_sample.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
#! /usr/local/bin/perl -w
use strict;
use warnings;
# Sample Perl program, showing how to use the
# WordNet::Similarity measures.
# WordNet::QueryData is required by all the
# relatedness modules.
use WordNet::QueryData;
# 'use' each module that you wish to use.
use WordNet::Similarity::random;
use WordNet::Similarity::path;
use WordNet::Similarity::wup;
use WordNet::Similarity::lch;
use WordNet::Similarity::jcn;
use WordNet::Similarity::res;
use WordNet::Similarity::lin;
use WordNet::Similarity::hso;
use WordNet::Similarity::lesk;
use WordNet::Similarity::vector;
use WordNet::Similarity::vector_pairs;
# Get the concepts.
my $wps1 = shift;
my $wps2 = shift;
unless (defined $wps1 and defined $wps2) {
print STDERR "Undefined input\n";
print STDERR "Usage: sample.pl synset1 synset2\n";
print STDERR "\tSynsets must be in word#pos#sense format (ex., dog#n#1)\n";
exit 1;
}
# Load WordNet::QueryData
print STDERR "Loading WordNet... ";
my $wn = WordNet::QueryData->new;
die "Unable to create WordNet object.\n" if(!$wn);
print STDERR "done.\n";
# Create an object for each of the measures of
# semantic relatedness.
print STDERR "Creating jcn object... ";
my $jcn = WordNet::Similarity::jcn->new($wn, "config-files/config-jcn.conf");
die "Unable to create jcn object.\n" if(!defined $jcn);
my ($error, $errString) = $jcn->getError();
die $errString if($error > 1);
print STDERR "done.\n";
print STDERR "Creating res object... ";
my $res = WordNet::Similarity::res->new($wn, "config-files/config-res.conf");
die "Unable to create res object.\n" if(!defined $res);
($error, $errString) = $res->getError();
die $errString if($error > 1);
print STDERR "done.\n";
print STDERR "Creating lin object... ";
my $lin = WordNet::Similarity::lin->new($wn, "config-files/config-lin.conf");
die "Unable to create lin object.\n" if(!defined $lin);
($error, $errString) = $lin->getError();
die $errString if($error > 1);
print STDERR "done.\n";
print STDERR "Creating wup object... ";
my $wup = WordNet::Similarity::wup->new($wn, "config-files/config-wup.conf");
die "Unable to create wup object.\n" if(!defined $wup);
($error, $errString) = $wup->getError();
die $errString if($error > 1);
print STDERR "done.\n";
print STDERR "Creating lch object... ";
my $lch = WordNet::Similarity::lch->new($wn, "config-files/config-lch.conf");
die "Unable to create lch object.\n" if(!defined $lch);
($error, $errString) = $lch->getError();
die $errString if($error > 1);
print STDERR "done.\n";
print STDERR "Creating hso object... ";
my $hso = WordNet::Similarity::hso->new($wn, "config-files/config-hso.conf");
die "Unable to create hso object.\n" if(!defined $hso);
($error, $errString) = $hso->getError();
die $errString if($error > 1);
print STDERR "done.\n";
print STDERR "Creating path object... ";
my $path = WordNet::Similarity::path->new($wn, "config-files/config-path.conf");
die "Unable to create path object.\n" if(!defined $path);
($error, $errString) = $path->getError();
die $errString if($error > 1);
print STDERR "done.\n";
print STDERR "Creating random object... ";
my $random = WordNet::Similarity::random->new($wn, "config-files/config-random.conf");
die "Unable to create random object.\n" if(!defined $random);
($error, $errString) = $random->getError();
die $errString if($error > 1);
print STDERR "done.\n";
print STDERR "Creating lesk object... ";
my $lesk = WordNet::Similarity::lesk->new($wn, "config-files/config-lesk.conf");
die "Unable to create lesk object.\n" if(!defined $lesk);
($error, $errString) = $lesk->getError();
die $errString if($error > 1);
print STDERR "done.\n";
print STDERR "Creating vector object... ";
my $vector = WordNet::Similarity::vector->new($wn, "config-files/config-vector.conf");
die "Unable to create vector object.\n" if(!defined $vector);
($error, $errString) = $vector->getError();
die $errString if($error > 1);
print STDERR "done.\n";
print STDERR "Creating vector_pairs object... ";
my $vector_pairs = WordNet::Similarity::vector_pairs->new($wn, "config-files/config-vector_pairs.conf");
die "Unable to create vector_pairs object.\n" if(!defined $vector_pairs);
($error, $errString) = $vector_pairs->getError();
die $errString if($error > 1);
print STDERR "done.\n";
# Find the relatedness of the concepts using each of
# the measures.
my $value = $jcn->getRelatedness($wps1, $wps2);
($error, $errString) = $jcn->getError();
die $errString if($error > 1);
print "JCN Similarity = $value\n";
print "JCN ErrorString = $errString\n" if $error;
$value = $res->getRelatedness($wps1, $wps2);
($error, $errString) = $res->getError();
die $errString if($error > 1);
print "RES Similarity = $value\n";
print "RES ErrorString = $errString\n" if $error;
$value = $lin->getRelatedness($wps1, $wps2);
($error, $errString) = $lin->getError();
die $errString if($error > 1);
print "LIN Similarity = $value\n";
print "LIN ErrorString = $errString\n" if $error;
$value = $wup->getRelatedness($wps1, $wps2);
($error, $errString) = $wup->getError();
die $errString if($error > 1);
print "WUP Similarity = $value\n";
print "WUP ErrorString = $errString\n" if $error;
$value = $lch->getRelatedness($wps1, $wps2);
($error, $errString) = $lch->getError();
die $errString if($error > 1);
print "LCH Similarity = $value\n";
print "LCH ErrorString = $errString\n" if $error;
$value = $hso->getRelatedness($wps1, $wps2);
($error, $errString) = $hso->getError();
die $errString if($error > 1);
my $trace = $hso->getTraceString();
print "HSO Similarity = $value\n";
print "HSO ErrorString = $errString\n" if $error;
print "HSO TRACE\n\n$trace\n";
$value = $path->getRelatedness($wps1, $wps2);
($error, $errString) = $path->getError();
die $errString if($error > 1);
print "PATH Similarity = $value\n";
print "PATH ErrorString = $errString\n" if $error;
$value = $random->getRelatedness($wps1, $wps2);
($error, $errString) = $random->getError();
die $errString if($error > 1);
print "RANDOM Similarity = $value\n";
print "RANDOM ErrorString = $errString\n" if $error;
$value = $lesk->getRelatedness($wps1, $wps2);
($error, $errString) = $lesk->getError();
die $errString if($error > 1);
print "LESK Similarity = $value\n";
print "LESK ErrorString = $errString\n" if $error;
$value = $vector->getRelatedness($wps1, $wps2);
($error, $errString) = $vector->getError();
die $errString if($error > 1);
print "VECTOR Similarity = $value\n";
print "VECTOR ErrorString = $errString\n" if $error;
$value = $vector_pairs->getRelatedness($wps1, $wps2);
($error, $errString) = $vector_pairs->getError();
die $errString if($error > 1);
print "VECTOR_PAIRS Similarity = $value\n";
print "VECTOR_PAIRS ErrorString = $errString\n" if $error;
__END__