blib/script/vector-input.pl untested

filter: Statementstmt 0.0% 0 / 133 Branchbran 0.0% 0 / 42 Conditioncond 0.0% 0 / 8 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 5 Podpod 0.0% 0 / 4 totaltotal 0.0% 0 / 192 SCAR 6.9
CC
1
Cov
0%
minimalUsageNotes
2.0
askHelp
2.0
showHelp
2.0
CRAP 2.0
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 use Getopt::Long;
110  
111 # first check if no commandline options have been provided... in which case
112 # print out the usage notes!
113 if ( $#ARGV == -1 )
114 {
115 &minimalUsageNotes();
116 exit;
117 }
118  
119 # now get the options!
120 GetOptions( "version", "help", "stat", "cutoff=s" );
121  
122 # if help has been requested, print out help!
123 if ( defined $opt_help )
124 {
125 $opt_help = 1;
126 &showHelp();
127 exit;
128 }
129  
130 # if version has been requested, show version!
131 if ( defined $opt_version )
132 {
133 $opt_version = 1;
134 &showVersion();
135 exit;
136 }
137  
138 my $cutoff = 0;
139 if(defined $opt_cutoff) {
140 $cutoff = $opt_cutoff;
141 }
142  
143  
144 my $start_bigram = time();
145  
146 my $index_file = $ARGV[0];
147 # check to see if a destination has been supplied at all...
148 if ( !($index_file ) )
149 {
150 print STDERR "No output file (INDEX) supplied.\n";
151 askHelp();
152 exit;
153 }
154 if (-e $index_file)
155 {
156 print "Output file $index_file already exists! Overwrite (Y/N)? ";
157 my $reply = <STDIN>;
158 chomp $reply;
159 $reply = uc $reply;
160 exit 0 if ($reply ne "Y");
161 }
162 open(INDX, ">$index_file")
163 or die("Error: cannot open file '$index_file' for output index.\n");
164  
165  
166 my $matrix_file = $ARGV[1];
167 # check to see if a destination has been supplied at all...
168 if ( !($matrix_file ) )
169 {
170 print STDERR "No output file (MATRIX) supplied.\n";
171 askHelp();
172 exit;
173 }
174 if (-e $matrix_file)
175 {
176 print "Output file $matrix_file already exists! Overwrite (Y/N)? ";
177 my $reply = <STDIN>;
178 chomp $reply;
179 $reply = uc $reply;
180 exit 0 if ($reply ne "Y");
181 }
182 open(MATX, ">$matrix_file")
183 or die("Error: cannot open file '$matrix_file' for output index.\n");
184  
185  
186 $bigrams_file = $ARGV[2];
187 # check to see if a source has been supplied at all...
188 if ( !($bigrams_file ) )
189 {
190 print STDERR "No output file (BIGRAMFILE) supplied.\n";
191 askHelp();
192 exit;
193 }
194 open(BIGM, "<$bigrams_file")
195 or die("Error: cannot open file '$bigrams_file' for output index.\n");
196  
197 # read in the bigrams file
198 my %index;
199 my $index_num1 = 1;
200 my $total = <BIGM>;
201 while (my $line = <BIGM>)
202 {
203 chomp($line);
204 my @terms = split('<>', $line);
205
206 my @nums = split/\s+/, $terms[2];
207
208 my $score = shift @nums;
209 if(defined $opt_stat) {
210 $score = shift @nums;
211 }
212
213 if($score < $cutoff) { next; }
214
215 # index every term of the bigram list
216 if(!defined $index{$terms[0]})
217 {
218 $index{$terms[0]} = $index_num1;
219 $index_num1++;
220 }
221 if(!defined $index{$terms[1]})
222 {
223 $index{$terms[1]} = $index_num1;
224 $index_num1++;
225 }
226 }
227  
228 # sort the index terms of %index and
229 # initilize the position length array
230 my $index_num2 = 1;
231 my @position_length;
232 $position_length[0] = 0; #index starts from 1
233 foreach my $t (sort (keys %index))
234 {
235 $index{$t} = $index_num2;
236 $position_length[$index_num2] = 0;
237 $index_num2++;
238 }
239  
240 # go the beginning of the bigrams file
241 seek BIGM, 0, 0 or die $!;
242 my $word = "";
243 my $position = 0;
244 my $bigrams = "";
245 $total = <BIGM>;
246 while (my $line = <BIGM>)
247 {
248 chomp($line);
249 my @terms = split('<>', $line);
250 my @freqs = split (' ', $terms[2]);
251
252 my $score = $freqs[0];
253 if(defined $opt_stat) {
254 $score = $freqs[1];
255 }
256
257 if($score < $cutoff) { next; }
258  
259 # if it is still the same term.
260 # print out the vector to the matrix file
261 if( $word eq $terms[0] )
262 {
263 #print "word: $word\n";
264 $bigrams .= "$index{$terms[1]} $freqs[0] ";
265 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 if ($word ne "")
272 {
273 $bigrams .= "\n";
274 my $length = length($bigrams);
275 $position_length[$index{$word}] = "$position" . " $length";
276 $position += $length;
277 $bigrams = "";
278 printf MATX "\n";
279 }
280
281 # for a new term, print the term and its first bigrams frequency
282 $word = $terms[0];
283 if(defined $opt_stat) {
284 $bigrams .= "$index{$word}: $index{$terms[1]} $freqs[1] ";
285 printf MATX "$index{$word}: $index{$terms[1]} $freqs[1] ";
286 }
287 else {
288 $bigrams .= "$index{$word}: $index{$terms[1]} $freqs[0] ";
289 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 if (eof(BIGM))
295 {
296 $bigrams .= "\n";
297 my $length = length($bigrams);
298 $position_length[$index{$word}] = "$position" . " $length";
299 printf MATX "\n";
300 }
301
302 }
303 close MATX;
304 close BIGM;
305  
306 # out put the index file
307 foreach my $t (sort (keys %index))
308 {
309 printf INDX "$t $index{$t} $position_length[$index{$t}]\n"
310 }
311 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 print STDERR "Usage: vector-input.pl INDEX MATRIX BIGRAMFILE\n";
323 askHelp();
324 }
325  
326 # function to output "ask for help" message when the user's goofed up!
327 sub askHelp
328 {
329 print STDERR "Type vector-input.pl --help for help.\n";
330 }
331  
332 # function to output help messages for this program
333 sub showHelp
334 {
335 print "\n";
336 print "Usage: vector-input.pl INDEX MATRIX BIGRAMFILE\n\n";
337
338 print "build the index file for each term of the bigrams file and\n";
339 print "create the co-occurence matrix.INDEX is the output index file.\n";
340 print "MATRIX is the output matrix file. BIGRAMFILE is the output of\n";
341 print "huge-count.pl of Text-NSP. \n\n";
342
343 print "OPTIONS:\n\n";
344
345 print " --stat Bigram file is from statistics.pl\n\n";
346  
347 print " --cutoff SCORE Only include ngrams greater than SCORE\n\n";
348  
349 print " --version Prints the version number.\n\n";
350
351 print " --help Prints this help message.\n\n";
352 }
353  
354 # function to output the version number
355 sub showVersion
356 {
357 print STDERR "vector-input.pl - version 0.02\n";
358 print STDERR "Copyright (C) 2009, Ying Liu\n";
359 print STDERR "Date of Last Update 03/23/10\n";
360
361 }