utils/vector-input.pl

filter: Statementstmt 61.0% 80 / 131 Branchbran 59.2% 32 / 54 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 20.0% 1 / 5 Podpod n/a totaltotal 59.4% 113 / 190 SCAR 0.6
CC
1
Cov
61%
minimalUsageNotes
2.0
askHelp
2.0
showHelp
2.0
CRAP 1.1
« blib/script/vector-input.pl ↑ Summary
1 #!/usr/local/bin/perl -w
2  
3 =head1 NAME
4  
5 vector-input.pl - This program builds the term index file and co-occrrence matrix for umls-similarity.pl to calculate the vector relatedness.
6  
7 =head1 SYNOPSIS
8  
9 vector-input.pl takes the bigrams frequency input and build the index and the
10 co-occurrence matrix.
11  
12 =head1 DESCRIPTION
13  
14 We build the index and co-occurrence matrix for the vector method of UMLS-Similarity.
15 The index file helps to locate each term's vector by recording the start position
16 and the length of its vector. The matrix file records every term's vector.
17  
18 See perldoc vector-input.pl
19  
20 =head1 USAGE
21  
22 vector-input.pl INDEX MATRIX BIGRAMFILE
23  
24 example: vector-input.pl Index.txt Matrix.txt BigramsList.txt
25  
26 =head1 INPUT
27  
28 =head2 Required Arguments:
29  
30 =head3 INDEX
31  
32 output file of the vector-input.pl. It records the index of each term
33 and the vector start position and length f the co-occurrence matrix.
34  
35 =head3 MATRIX
36  
37 output file of the vector-input.pl. Each line is a vector for the
38 term and its co-occurrence term and their frequency.
39  
40 =head3 BIGRAMFILE
41  
42 Input to vector-input.pl should be a single flat file generated by huge-count.pl
43 of Text-NSP package. If the bigrams list is generated by count.pl, pleasue use
44 count2huge.pl to convert the results to huge-count.pl. It sorts the bigrams in
45 the alphabet order. When vector-input.pl generates the index and co-occurrence
46 matrix file, it requires the bigrams which starts the same term t1 grouped together
47 and lists next to each other. Because at this step, bigrams are not stored in
48 memory. If the first term of the bigrams changes, it prints the output and index
49 position of the vector for the term t1. Especially, if the bigrams are sorted in
50 the alphabet order, it is faster for vector method of UMLS-Similarity to build the
51 vector. Because for each concept, it searches the co-occurrence matrix to build
52 the second order vector. If every term of the vector are sorted, the vector
53 method can search the co-occurrence matrix from the beginning to the end by the
54 index position and length. If the co-occurrence matrix is a huge file, it could
55 save lots of execute time.
56  
57 =head3 Other Options:
58  
59 =head4 --stat
60  
61 The bigram file is from statistics.pl rather than count.pl
62  
63 =head4 --cutoff SCORE
64  
65 Only use those ngrams that are greater than SCORE
66  
67 =head4 --help
68  
69 Displays the help information.
70  
71 =head4 --version
72  
73 Displays the version information.
74  
75 =head1 AUTHOR
76  
77 Ying Liu, liux0395 at umn.edu
78  
79 =head1 SEE ALSO
80  
81 home page: www.tc.umn.edu/~liux0395
82  
83 =head1 COPYRIGHT
84  
85 Copyright (C) 2010, Ying Liu
86  
87 This program is free software; you can redistribute it and/or
88 modify it under the terms of the GNU General Public License
89 as published by the Free Software Foundation; either version 2
90 of the License, or (at your option) any later version.
91 This program is distributed in the hope that it will be useful,
92 but WITHOUT ANY WARRANTY; without even the implied warranty of
93 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
94 GNU General Public License for more details.
95  
96 You should have received a copy of the GNU General Public License
97 along with this program; if not, write to the Free Software
98 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
99  
100 =cut
101  
102 ###############################################################################
103  
104 #-----------------------------------------------------------------------------
105 # Start of program
106 #-----------------------------------------------------------------------------
107  
108 # we have to use commandline options, so use the necessary package!
109 1 ▶use Getopt::Long;
Subroutine BEGIN: called CC=1 SCAR=0.0
110  
111 # first check if no commandline options have been provided... in which case
112 # print out the usage notes!
113 1 ▶if ( $#ARGV == -1 )
Branch: 1 branch
BranchTrueFalseTotal
if ($#ARGV == -1) 0 1 1
114 {
115 0 &minimalUsageNotes();
116 0 exit;
117 }
118  
119 # now get the options!
120 1 GetOptions( "version", "help", "stat", "cutoff=s" );
121  
122 # if help has been requested, print out help!
123 1 ▶if ( defined $opt_help )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opt_help) 0 1 1
124 {
125 0 $opt_help = 1;
126 0 &showHelp();
127 0 exit;
128 }
129  
130 # if version has been requested, show version!
131 1 ▶if ( defined $opt_version )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opt_version) 0 1 1
132 {
133 0 $opt_version = 1;
134 0 &showVersion();
135 0 exit;
136 }
137  
138 1 my $cutoff = 0;
139 1 ▶if(defined $opt_cutoff) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opt_cutoff) 0 1 1
140 0 $cutoff = $opt_cutoff;
141 }
142  
143  
144 1 my $start_bigram = time();
145  
146 1 my $index_file = $ARGV[0];
147 # check to see if a destination has been supplied at all...
148 1 ▶if ( !($index_file ) )
Branch: 1 branch
BranchTrueFalseTotal
unless ($index_file) 0 1 1
149 {
150 0 print STDERR "No output file (INDEX) supplied.\n";
151 0 askHelp();
152 0 exit;
153 }
154 1 ▶if (-e $index_file)
Branch: 1 branch
BranchTrueFalseTotal
if (-e $index_file) 0 1 1
155 {
156 0 print "Output file $index_file already exists! Overwrite (Y/N)? ";
157 0 my $reply = <STDIN>;
158 0 chomp $reply;
159 0 $reply = uc $reply;
160 0 ▶ exit 0 if ($reply ne "Y");
Branch: 1 branch
BranchTrueFalseTotal
if $reply ne "Y" 0 0 0
161 }
162 1 ▶open(INDX, ">$index_file")
Branch: 1 branch
BranchTrueFalseTotal
open INDX, ">$index_file" or die "Error: cannot open file '${index_file}' for output index.\n" 0 1 1
163 or die("Error: cannot open file '$index_file' for output index.\n");
164  
165  
166 1 my $matrix_file = $ARGV[1];
167 # check to see if a destination has been supplied at all...
168 1 ▶if ( !($matrix_file ) )
Branch: 1 branch
BranchTrueFalseTotal
unless ($matrix_file) 0 1 1
169 {
170 0 print STDERR "No output file (MATRIX) supplied.\n";
171 0 askHelp();
172 0 exit;
173 }
174 1 ▶if (-e $matrix_file)
Branch: 1 branch
BranchTrueFalseTotal
if (-e $matrix_file) 0 1 1
175 {
176 0 print "Output file $matrix_file already exists! Overwrite (Y/N)? ";
177 0 my $reply = <STDIN>;
178 0 chomp $reply;
179 0 $reply = uc $reply;
180 0 ▶ exit 0 if ($reply ne "Y");
Branch: 1 branch
BranchTrueFalseTotal
if $reply ne "Y" 0 0 0
181 }
182 1 ▶open(MATX, ">$matrix_file")
Branch: 1 branch
BranchTrueFalseTotal
open MATX, ">$matrix_file" or die "Error: cannot open file '${matrix_file}' for output index.\n" 0 1 1
183 or die("Error: cannot open file '$matrix_file' for output index.\n");
184  
185  
186 1 $bigrams_file = $ARGV[2];
187 # check to see if a source has been supplied at all...
188 1 ▶if ( !($bigrams_file ) )
Branch: 1 branch
BranchTrueFalseTotal
unless ($bigrams_file) 0 1 1
189 {
190 0 print STDERR "No output file (BIGRAMFILE) supplied.\n";
191 0 askHelp();
192 0 exit;
193 }
194 1 ▶open(BIGM, "<$bigrams_file")
Branch: 1 branch
BranchTrueFalseTotal
open BIGM, "<$bigrams_file" or die "Error: cannot open file '${bigrams_file}' for output index.\n" 0 1 1
195 or die("Error: cannot open file '$bigrams_file' for output index.\n");
196  
197 # read in the bigrams file
198 1 my %index;
199 1 my $index_num1 = 1;
200 1 my $total = <BIGM>;
201 1 ▶while (my $line = <BIGM>)
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $line = readline BIGM)) 53 1 54
202 {
203 53 chomp($line);
204 53 my @terms = split('<>', $line);
205
206 53 my @nums = split/\s+/, $terms[2];
207
208 53 my $score = shift @nums;
209 53 ▶ if(defined $opt_stat) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opt_stat) 0 53 53
210 0 $score = shift @nums;
211 }
212
213 53 ▶ if($score < $cutoff) { next; }
Branch: 1 branch
BranchTrueFalseTotal
if ($score < $cutoff) 0 53 53
214
215 # index every term of the bigram list
216 53 ▶ if(!defined $index{$terms[0]})
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $index{$terms[0]}) 34 19 53
217 {
218 34 $index{$terms[0]} = $index_num1;
219 34 $index_num1++;
220 }
221 53 ▶ if(!defined $index{$terms[1]})
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $index{$terms[1]}) 36 17 53
222 {
223 36 $index{$terms[1]} = $index_num1;
224 36 $index_num1++;
225 }
226 }
227  
228 # sort the index terms of %index and
229 # initilize the position length array
230 1 my $index_num2 = 1;
231 1 my @position_length;
232 1 $position_length[0] = 0; #index starts from 1
233 1 foreach my $t (sort (keys %index))
234 {
235 70 $index{$t} = $index_num2;
236 70 $position_length[$index_num2] = 0;
237 70 $index_num2++;
238 }
239  
240 # go the beginning of the bigrams file
241 1 ▶seek BIGM, 0, 0 or die $!;
Branch: 1 branch
BranchTrueFalseTotal
seek BIGM, 0, 0 or die $! 0 1 1
242 1 my $word = "";
243 1 my $position = 0;
244 1 my $bigrams = "";
245 1 $total = <BIGM>;
246 1 ▶while (my $line = <BIGM>)
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $line = readline BIGM)) 53 1 54
247 {
248 53 chomp($line);
249 53 my @terms = split('<>', $line);
250 53 my @freqs = split (' ', $terms[2]);
251
252 53 my $score = $freqs[0];
253 53 ▶ if(defined $opt_stat) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opt_stat) 0 53 53
254 0 $score = $freqs[1];
255 }
256
257 53 ▶ if($score < $cutoff) { next; }
Branch: 1 branch
BranchTrueFalseTotal
if ($score < $cutoff) 0 53 53
258  
259 # if it is still the same term.
260 # print out the vector to the matrix file
261 53 ▶ if( $word eq $terms[0] )
Branch: 1 branch
BranchTrueFalseTotal
if ($word eq $terms[0]) { } 4 49 53
262 {
263 #print "word: $word\n";
264 4 $bigrams .= "$index{$terms[1]} $freqs[0] ";
265 4 printf MATX "$index{$terms[1]} $freqs[0] ";
266 }
267 else
268 {
269 # the first term of the bigrams changes, record
270 # the vector position and length of the term
271 49 ▶ if ($word ne "")
Branch: 1 branch
BranchTrueFalseTotal
if ($word ne "") 48 1 49
272 {
273 48 $bigrams .= "\n";
274 48 my $length = length($bigrams);
275 48 $position_length[$index{$word}] = "$position" . " $length";
276 48 $position += $length;
277 48 $bigrams = "";
278 48 printf MATX "\n";
279 }
280
281 # for a new term, print the term and its first bigrams frequency
282 49 $word = $terms[0];
283 49 ▶ if(defined $opt_stat) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opt_stat) { } 0 49 49
284 0 $bigrams .= "$index{$word}: $index{$terms[1]} $freqs[1] ";
285 0 printf MATX "$index{$word}: $index{$terms[1]} $freqs[1] ";
286 }
287 else {
288 49 $bigrams .= "$index{$word}: $index{$terms[1]} $freqs[0] ";
289 49 printf MATX "$index{$word}: $index{$terms[1]} $freqs[0] ";
290 }
291 }
292 # reach the end of the bigrams file, record the
293 # vector position and length of the last term.
294 53 ▶ if (eof(BIGM))
Branch: 1 branch
BranchTrueFalseTotal
if (eof BIGM) 1 52 53
295 {
296 1 $bigrams .= "\n";
297 1 my $length = length($bigrams);
298 1 $position_length[$index{$word}] = "$position" . " $length";
299 1 printf MATX "\n";
300 }
301
302 }
303 1 close MATX;
304 1 close BIGM;
305  
306 # out put the index file
307 1 foreach my $t (sort (keys %index))
308 {
309 70 printf INDX "$t $index{$t} $position_length[$index{$t}]\n"
310 }
311 1 close INDX;
312  
313  
314 #-----------------------------------------------------------------------------
315 # User Defined Function Definitions
316 #-----------------------------------------------------------------------------
317  
318 # function to output a minimal usage note when the user has not provided any
319 # commandline options
320 sub minimalUsageNotes
321 {
322 0 ▶ print STDERR "Usage: vector-input.pl INDEX MATRIX BIGRAMFILE\n";
Subroutine minimalUsageNotes: not called CC=1 SCAR=6.9
323 0 askHelp();
324 }
325  
326 # function to output "ask for help" message when the user's goofed up!
327 sub askHelp
328 {
329 0 ▶ print STDERR "Type vector-input.pl --help for help.\n";
Subroutine askHelp: not called CC=1 SCAR=6.9
330 }
331  
332 # function to output help messages for this program
333 sub showHelp
334 {
335 0 ▶ print "\n";
Subroutine showHelp: not called CC=1 SCAR=6.9
336 0 print "Usage: vector-input.pl INDEX MATRIX BIGRAMFILE\n\n";
337
338 0 print "build the index file for each term of the bigrams file and\n";
339 0 print "create the co-occurence matrix.INDEX is the output index file.\n";
340 0 print "MATRIX is the output matrix file. BIGRAMFILE is the output of\n";
341 0 print "huge-count.pl of Text-NSP. \n\n";
342
343 0 print "OPTIONS:\n\n";
344
345 0 print " --stat Bigram file is from statistics.pl\n\n";
346  
347 0 print " --cutoff SCORE Only include ngrams greater than SCORE\n\n";
348  
349 0 print " --version Prints the version number.\n\n";
350
351 0 print " --help Prints this help message.\n\n";
352 }
353  
354 # function to output the version number
355 sub showVersion
356 {
357 0 ▶ print STDERR "vector-input.pl - version 0.02\n";
Subroutine showVersion: not called CC=1 SCAR=6.9
358 0 print STDERR "Copyright (C) 2009, Ying Liu\n";
359 0 print STDERR "Date of Last Update 03/23/10\n";
360
361 }
« blib/script/vector-input.pl ↑ Summary