line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
# WordNet::vectorFile.pm version 2.04 |
2
|
|
|
|
|
|
|
# (Last updated $Id: vectorFile.pm,v 1.1 2008/03/27 05:13:01 sidz1979 Exp $) |
3
|
|
|
|
|
|
|
# |
4
|
|
|
|
|
|
|
# Package used by WordNet::Similarity::vector module that |
5
|
|
|
|
|
|
|
# computes semantic relatedness of word senses in WordNet |
6
|
|
|
|
|
|
|
# using gloss vectors. This module provides a read/write |
7
|
|
|
|
|
|
|
# interface into the word vectors file. |
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
|
|
|
|
|
|
|
# This program is free software; you can redistribute it and/or |
18
|
|
|
|
|
|
|
# modify it under the terms of the GNU General Public License |
19
|
|
|
|
|
|
|
# as published by the Free Software Foundation; either version 2 |
20
|
|
|
|
|
|
|
# of the License, or (at your option) any later version. |
21
|
|
|
|
|
|
|
# |
22
|
|
|
|
|
|
|
# This program is distributed in the hope that it will be useful, |
23
|
|
|
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
24
|
|
|
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
25
|
|
|
|
|
|
|
# GNU General Public License for more details. |
26
|
|
|
|
|
|
|
# |
27
|
|
|
|
|
|
|
# You should have received a copy of the GNU General Public License |
28
|
|
|
|
|
|
|
# along with this program; if not, write to |
29
|
|
|
|
|
|
|
# |
30
|
|
|
|
|
|
|
# The Free Software Foundation, Inc., |
31
|
|
|
|
|
|
|
# 59 Temple Place - Suite 330, |
32
|
|
|
|
|
|
|
# Boston, MA 02111-1307, USA. |
33
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
package WordNet::vectorFile; |
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
=head1 NAME |
37
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
WordNet::vectorFile - Provides access to the word vectors database (used |
39
|
|
|
|
|
|
|
by the vector and vector_pairs WordNet::Similarity measures). |
40
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
=head1 SYNOPSIS |
42
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
use WordNet::vectorFile; |
44
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
my ($dCount, $dims, $vecRef) = WordNet::vectorFile->readVectors($filename); |
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
WordNet::vectorFile->writeVectors($fname, $dCount, $dims, $vecRef); |
48
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
=head1 DESCRIPTION |
50
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
This module provides a read/write interface into the word vectors data |
52
|
|
|
|
|
|
|
file. This module is used by WordNet::Similarity::vector and |
53
|
|
|
|
|
|
|
WordNet::Similarity::vector_pairs as an interface into the word |
54
|
|
|
|
|
|
|
vectors database. This module abstracts the format of the data file |
55
|
|
|
|
|
|
|
away from the user. |
56
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
=head2 Methods |
58
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
=over |
60
|
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
=cut |
62
|
|
|
|
|
|
|
|
63
|
1
|
|
|
1
|
|
5
|
use strict; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
38
|
|
64
|
1
|
|
|
1
|
|
5
|
use Exporter; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
39
|
|
65
|
1
|
|
|
1
|
|
4
|
use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS); |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
903
|
|
66
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
@ISA = qw(Exporter); |
68
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
%EXPORT_TAGS = (); |
70
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
@EXPORT_OK = (); |
72
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
@EXPORT = (); |
74
|
|
|
|
|
|
|
|
75
|
|
|
|
|
|
|
$VERSION = '2.04'; |
76
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
=item readVectors |
78
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
This method reads in a word vectors file, and returns the document count, |
80
|
|
|
|
|
|
|
the words corresponding to the dimensions of the vectors, and a reference |
81
|
|
|
|
|
|
|
to an array containing the list of word vectors. |
82
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
Parameters: $file -- the file name of the word vectors file. |
84
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
Returns: ($dCount, $dims, $vecRef) |
86
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
=cut |
88
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
# Read the word vectors from a file. |
90
|
|
|
|
|
|
|
sub readVectors |
91
|
|
|
|
|
|
|
{ |
92
|
0
|
|
|
0
|
1
|
|
my $className = shift; |
93
|
0
|
|
|
|
|
|
my $fname = shift; |
94
|
0
|
|
|
|
|
|
my $state = 0; |
95
|
0
|
|
|
|
|
|
my $docCount = 0; |
96
|
0
|
|
|
|
|
|
my $dimensions = {}; |
97
|
0
|
|
|
|
|
|
my $vectors = {}; |
98
|
0
|
|
|
|
|
|
my @parts = (); |
99
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
# Check that input values are defined. |
101
|
0
|
0
|
0
|
|
|
|
return (undef, undef, undef) if(!defined $className || !defined $fname || ref $className); |
|
|
|
0
|
|
|
|
|
102
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
# Read the data. |
104
|
0
|
0
|
|
|
|
|
open(IPFILE, $fname) || return (undef, undef, undef); |
105
|
0
|
|
|
|
|
|
while() |
106
|
|
|
|
|
|
|
{ |
107
|
0
|
|
|
|
|
|
s/[\r\f\n]//g; |
108
|
0
|
|
|
|
|
|
s/^\s+//; |
109
|
0
|
|
|
|
|
|
s/\s+$//; |
110
|
0
|
0
|
|
|
|
|
if($state == 0) |
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
111
|
|
|
|
|
|
|
{ |
112
|
0
|
0
|
|
|
|
|
if(/DOCUMENTCOUNT\s*=\s*([0-9]+)/) |
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
113
|
|
|
|
|
|
|
{ |
114
|
0
|
|
|
|
|
|
$docCount = $1; |
115
|
|
|
|
|
|
|
} |
116
|
|
|
|
|
|
|
elsif(/--Dimensions Start--/) |
117
|
|
|
|
|
|
|
{ |
118
|
0
|
|
|
|
|
|
$state = 1; |
119
|
|
|
|
|
|
|
} |
120
|
|
|
|
|
|
|
elsif(/--Vectors Start--/) |
121
|
|
|
|
|
|
|
{ |
122
|
0
|
|
|
|
|
|
$state = 2; |
123
|
|
|
|
|
|
|
} |
124
|
|
|
|
|
|
|
} |
125
|
|
|
|
|
|
|
elsif($state == 1) |
126
|
|
|
|
|
|
|
{ |
127
|
0
|
0
|
0
|
|
|
|
if(/--Dimensions End--/) |
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
128
|
|
|
|
|
|
|
{ |
129
|
0
|
|
|
|
|
|
$state = 0; |
130
|
|
|
|
|
|
|
} |
131
|
|
|
|
|
|
|
elsif(/^--Dimensions/ || /^--Vectors/) |
132
|
|
|
|
|
|
|
{ |
133
|
0
|
|
|
|
|
|
return (undef, undef, undef); |
134
|
|
|
|
|
|
|
} |
135
|
|
|
|
|
|
|
elsif($_ ne "") |
136
|
|
|
|
|
|
|
{ |
137
|
0
|
|
|
|
|
|
@parts = split(/\s+/, $_, 2); |
138
|
0
|
|
|
|
|
|
$dimensions->{$parts[0]} = $parts[1]; |
139
|
|
|
|
|
|
|
} |
140
|
|
|
|
|
|
|
} |
141
|
|
|
|
|
|
|
elsif($state == 2) |
142
|
|
|
|
|
|
|
{ |
143
|
0
|
0
|
0
|
|
|
|
if(/--Vectors End--/) |
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
144
|
|
|
|
|
|
|
{ |
145
|
0
|
|
|
|
|
|
$state = 0; |
146
|
|
|
|
|
|
|
} |
147
|
|
|
|
|
|
|
elsif(/^--Dimensions/ || /^--Vectors/) |
148
|
|
|
|
|
|
|
{ |
149
|
0
|
|
|
|
|
|
return (undef, undef, undef); |
150
|
|
|
|
|
|
|
} |
151
|
|
|
|
|
|
|
elsif($_ ne "") |
152
|
|
|
|
|
|
|
{ |
153
|
0
|
|
|
|
|
|
@parts = split(/\s+/, $_, 2); |
154
|
0
|
|
|
|
|
|
$vectors->{$parts[0]} = $parts[1]; |
155
|
|
|
|
|
|
|
} |
156
|
|
|
|
|
|
|
} |
157
|
|
|
|
|
|
|
else |
158
|
|
|
|
|
|
|
{ |
159
|
0
|
|
|
|
|
|
return (undef, undef, undef); |
160
|
|
|
|
|
|
|
} |
161
|
|
|
|
|
|
|
} |
162
|
0
|
|
|
|
|
|
close(IPFILE); |
163
|
|
|
|
|
|
|
|
164
|
|
|
|
|
|
|
# Return the data read. |
165
|
0
|
|
|
|
|
|
return ($docCount, $dimensions, $vectors); |
166
|
|
|
|
|
|
|
} |
167
|
|
|
|
|
|
|
|
168
|
|
|
|
|
|
|
=item writeVectors |
169
|
|
|
|
|
|
|
|
170
|
|
|
|
|
|
|
This method writes out a list of word vectors to the word vectors file. |
171
|
|
|
|
|
|
|
|
172
|
|
|
|
|
|
|
Parameters: $fname, $dCount, $dims, $vecRef |
173
|
|
|
|
|
|
|
|
174
|
|
|
|
|
|
|
Returns: none |
175
|
|
|
|
|
|
|
|
176
|
|
|
|
|
|
|
=back |
177
|
|
|
|
|
|
|
|
178
|
|
|
|
|
|
|
=cut |
179
|
|
|
|
|
|
|
|
180
|
|
|
|
|
|
|
# Write the word vectors to a file. |
181
|
|
|
|
|
|
|
sub writeVectors |
182
|
|
|
|
|
|
|
{ |
183
|
0
|
|
|
0
|
1
|
|
my $className = shift; |
184
|
0
|
|
|
|
|
|
my $fname = shift; |
185
|
0
|
|
|
|
|
|
my $documentCount = shift; |
186
|
0
|
|
|
|
|
|
my $dimensions = shift; |
187
|
0
|
|
|
|
|
|
my $vectors = shift; |
188
|
|
|
|
|
|
|
|
189
|
|
|
|
|
|
|
# Check that all input values are defined. |
190
|
0
|
0
|
0
|
|
|
|
return 0 if(!defined $className || !defined $fname || !defined $documentCount || !defined $dimensions || !defined $vectors); |
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
191
|
|
|
|
|
|
|
|
192
|
|
|
|
|
|
|
# Check that the className and filename aren't references. |
193
|
0
|
0
|
0
|
|
|
|
return 0 if(ref $className || ref $fname); |
194
|
|
|
|
|
|
|
|
195
|
|
|
|
|
|
|
# Check that document count is numeric. |
196
|
0
|
0
|
|
|
|
|
return 0 if($documentCount !~ /^[0-9]+$/); |
197
|
|
|
|
|
|
|
|
198
|
|
|
|
|
|
|
# Write the data to the file... |
199
|
|
|
|
|
|
|
# WARNING: No integrity check of data is performed. |
200
|
0
|
0
|
|
|
|
|
open(OPFILE, ">$fname") || return 0; |
201
|
0
|
|
|
|
|
|
print OPFILE "DOCUMENTCOUNT=$documentCount\n"; |
202
|
0
|
|
|
|
|
|
print OPFILE "--Dimensions Start--\n"; |
203
|
0
|
|
|
|
|
|
foreach my $key (keys %{$dimensions}) |
|
0
|
|
|
|
|
|
|
204
|
|
|
|
|
|
|
{ |
205
|
0
|
|
|
|
|
|
print OPFILE "$key ".($dimensions->{$key})."\n"; |
206
|
|
|
|
|
|
|
} |
207
|
0
|
|
|
|
|
|
print OPFILE "--Dimensions End--\n"; |
208
|
0
|
|
|
|
|
|
print OPFILE "--Vectors Start--\n"; |
209
|
0
|
|
|
|
|
|
foreach my $key (keys %{$vectors}) |
|
0
|
|
|
|
|
|
|
210
|
|
|
|
|
|
|
{ |
211
|
0
|
|
|
|
|
|
print OPFILE "$key ".($vectors->{$key})."\n"; |
212
|
|
|
|
|
|
|
} |
213
|
0
|
|
|
|
|
|
print OPFILE "--Vectors End--\n"; |
214
|
0
|
|
|
|
|
|
close(OPFILE); |
215
|
|
|
|
|
|
|
|
216
|
|
|
|
|
|
|
# Success. |
217
|
0
|
|
|
|
|
|
return 1; |
218
|
|
|
|
|
|
|
} |
219
|
|
|
|
|
|
|
|
220
|
|
|
|
|
|
|
1; |
221
|
|
|
|
|
|
|
|
222
|
|
|
|
|
|
|
__END__ |