script/order1vec.pl untested

filter: Statementstmt 0.0% 0 / 425 Branchbran 0.0% 0 / 172 Conditioncond 0.0% 0 / 75 MC/DCmcdc 0.0% 0 / 44 Subroutinesub 0.0% 0 / 9 Podpod 0.0% 0 / 3 totaltotal 0.0% 0 / 728 CC 1 SCAR 6.9
CC
1
Cov
0%
showminimal
2.0
showhelp
2.0
showversion
2.0
CRAP 2.0
1 #!/usr/local/bin/perl -w
2  
3 =head1 NAME
4  
5 order1vec.pl - Convert Senseval-2 format contexts into first order feature vectors in Cluto format
6  
7 =head1 SYNOPSIS
8  
9 order1vec.pl [OPTIONS] SVAL2 FEATURE_REGEX
10  
11 Type C<order1vec.pl --help> for a quick summary of options
12  
13 =head1 DESCRIPTION
14  
15 Convert a context into a first order feature vector which shows how which features
16 occured in the contexts. The possible features are identified via Perl regular
17 expressions of the form created by L<nsp2regex.pl>.
18  
19 =head1 INPUT
20  
21 =head2 Required Arguments:
22  
23 =head3 SVAL2
24  
25 A tokenized, preprocessed and well formatted Senseval-2 instance file showing
26 instances whose context vectors are to be generated.
27  
28 Context of each instance should be delimited within <context> and </context>
29 tags. It is required that each XML tag in the Senseval-2 file appears on a
30 separate line. Tokens should be space separated.
31  
32 =head3 FEATURE_REGEX
33  
34 A file containing Perl regular expressions for features as created by
35 nsp2regex.pl.
36  
37 Sample FEATURE_REGEX files -
38  
39 =over
40  
41 =item 1.
42  
43 /\s(<[^>]*>)*time(<[^>]*>)*\s/ @name = time
44 /\s(<[^>]*>)*task(<[^>]*>)*\s/ @name = task
45 /\s(<[^>]*>)*believe(<[^>]*>)*\s/ @name = believe
46 /\s(<[^>]*>)*life(<[^>]*>)*\s/ @name = life
47 /\s(<[^>]*>)*control(<[^>]*>)*\s/ @name = control
48 /\s(<[^>]*>)*words(<[^>]*>)*\s/ @name = words
49 /\s(<[^>]*>)*define(<[^>]*>)*\s/ @name = define
50  
51 Explanation :
52  
53 =over
54  
55 =item 1.
56  
57 The above FEATURE_REGEX file shows total 7 unigram features, single feature on each line.
58  
59 =item 2.
60  
61 Feature names are shown by "@name = FEATURE_NAME" that follows the actual
62 feature regex/s.
63  
64 =item 3.
65  
66 Tokens in the SVAL2 file should be separated by exactly one blank space. Any
67 non-tokens if exist should be put inside the angular brackets e.g. <item>, <sat>
68  
69 =back
70  
71 =item 2.
72  
73 /\s(<[^>]*>)*personal(<[^>]*>)*\s(<[^>]*>\s)*(<[^>]*>)*computer(<[^>]*>)*\s/ @name = personal<>computer
74 /\s(<[^>]*>)*stock(<[^>]*>)*\s(<[^>]*>\s)*(<[^>]*>)*market(<[^>]*>)*\s/ @name = stock<>market
75 /\s(<[^>]*>)*electronic(<[^>]*>)*\s(<[^>]*>\s)*(<[^>]*>)*systems(<[^>]*>)*\s/ @name = electronic<>systems
76 /\s(<[^>]*>)*toll(<[^>]*>)*\s(<[^>]*>\s)*(<[^>]*>)*free(<[^>]*>)*\s/ @name = toll<>free
77  
78 Shows a bigram feature file in which each feature includes two tokens
79 separated by single space or any number of non-token sequences in <> brackets.
80  
81 More explanation on feature regex creation is given in the perldoc
82 of the nsp2regex program.
83  
84 NOTE: Null columns are discarded i.e. the features which do not occur in any
85 of the contexts are dropped, and when --transpose option is specified (see
86 below for details), contexts that do not contain any features are dropped as
87 well.
88  
89 =back
90  
91 =head2 Optional Arguments:
92  
93 =head3 --binary
94  
95 By default, order1vec creates frequency context vectors that show how many
96 times each feature occurs in the context. --binary will instead create binary
97 context vectors where 1 indicates presence of feature and 0 indicates
98 absence of feature in the context.
99  
100 =head3 --dense
101  
102 By default, context vectors will have sparse format. --dense will display
103 output context vectors in dense format.
104  
105 =head3 --rlabel RLABELFILE
106  
107 Creates a RLABELFILE containing row labels for Cluto's --rlabelfile option.
108 Each line in the RLABELFILE shows an instance id of the instance whose context
109 vector is shown on the corresponding line on STDOUT.
110  
111 Instance ids are extracted from the SVAL2 file by matching regex
112  
113 /instance id\s*=\s*"IID"/
114  
115 where 'IID' is an instance id of the <context> that follows this <instance> tag.
116  
117 NOTE: When the --transpose option is specified, the contents of the RLABELFILE
118 and the CLABELFILE are swapped.
119  
120 =head3 --rclass RCLASSFILE
121  
122 Creates RCLASSFILE for Cluto's --rclassfile option. Each line in the
123 RCLASSFILE shows true sense id of the instance whose context vector appears on
124 the corresponding line on STDOUT.
125  
126 Sense ids are extracted from the SVAL2 file by matching regex
127  
128 /sense\s*id\s*=\s*"SID"\/>/
129  
130 where SID shows a true sense tag of the instance whose IID is recently
131 extracted by matching
132  
133 /instance id\s*=\s*"IID"/
134  
135 This option cannot be specified when the --transpose option is specified.
136  
137 =head3 --clabel CLABELFILE
138  
139 Creates a CLABELFILE containing column labels for Cluto's --clabelfile option.
140 Each line in the CLABELFILE shows a feature representing corresponding column
141 of the output context vectors.
142  
143 Features are extracted from the FEATURE_REGEX file by matching string
144 "@name = FEATURE" where FEATURE shows the feature name.
145  
146 NOTE: When the --transpose option is specified, the contents of the RLABELFILE
147 and the CLABELFILE are swapped.
148  
149 =head3 --transpose
150  
151 Creates feature vectors instead of the default context vectors. The output
152 is a Latent Semantic Analysis style feature-by-context matrix, instead of the
153 default context-by-feature matrix that is native to SenseClusters. As a
154 result, the contents of the RLABELFILE and CLABELFILE are swapped, i.e. the
155 list of features is output to the RLABELFILE and the list of contexts is
156 output to the CLABELFILE.
157  
158 =head3 --testregex TEST_REGEX
159  
160 Creates a TEST_REGEX file containing only those regular expressions from the
161 input FEATURE_REGEX file that matched at least once in the input SVAL2 file.
162 This list can be different from the original list in FEATURE_REGEX when
163 different training data has been used to identify features or when a different
164 scope has been used for training and test data creation.
165  
166 This option is required when the --transpose option is specified, in order to
167 ensure creation of a compatible TEST_REGEX file that corresponds to the
168 output of order1vec.pl in --transpose mode, so that both the output and the
169 TEST_REGEX can be directly passed as inputs to the order2vec.pl program.
170  
171 =head3 --showkey
172  
173 Displays the name of a system generated KEY file on the first line of STDOUT.
174 KEY file preserves the instance ids and sense tags of the instances in the
175 given SVAL2 file. This information will be automatically used by some of the
176 clustering and evaluation programs in SenseClusters that operate on purely
177 numeric instance formats. The option should be selected if the user is planning
178 to run SenseClusters' clustering code.
179  
180 This option cannot be specified when the --transpose option is specified, as
181 no KEY file is generated in --transpose mode.
182  
183 =head3 --target TARGETREGEX
184  
185 Specifies a file containing Perl regex/s that define the target word. By
186 default, target.regex file is assumed to exist in the current directory.
187  
188 =head3 --extarget
189  
190 This will exclude the target word from features if the target word (as
191 specified by the --target option or default target.regex file) appears
192 in the FEATURE_REGEX file. In other words, the feature dimensions
193 of the output context vectors will not include the target word even if
194 target word is listed in the FEATURE_REGEX file.
195  
196 =head2 Other Options :
197  
198 =head3 --help
199  
200 Displays this message.
201  
202 =head3 --version
203  
204 Displays the version information.
205  
206 =head1 OUTPUT
207  
208 =head2 KEY file
209  
210 When --transpose is not specified, order1vec automatically generates a KEY
211 file that preserves the instance ids and sense tags of the SVAL2 instances.
212  
213 Each line in the KEY file shows an instance id and one or more sense tags
214 of the instance represented by a context vector on the corresponding line
215 on STDOUT. i.e. the ith line in the KEY file shows the instance and sense ids
216 of the ith instance in the SVAL2 file or the ith vector displayed on stdout.
217  
218 Sample KEY file looks like
219  
220 <instance id="line-n.w8_020:7099:"/> <sense id="phone"/>
221 <instance id="line-n.w8_132:15431:"/> <sense id="phone"/>
222 <instance id="line-n.w8_027:13762:"/> <sense id="phone"/>
223 <instance id="line-n.w7_114:8965:"/> <sense id="text"/>
224 <instance id="line-n.w7_065:1553:"/> <sense id="product"/>
225 <instance id="line-n.w9_4:9437:"/> <sense id="product"/>
226  
227 Or
228  
229 <instance id="line-n.w8_020:7099:"/> <sense id="NOTAG"/>
230 <instance id="line-n.w7_111:238:"/> <sense id="NOTAG"/>
231 <instance id="line-n.w7_011:12078:"/> <sense id="NOTAG"/>
232 <instance id="line-n.w7_095:17576:"/> <sense id="NOTAG"/>
233 <instance id="line-n.w7_080:10129:"/> <sense id="NOTAG"/>
234 <instance id="line-n.w9_4:2358:"/> <sense id="NOTAG"/>
235  
236 when the sense ids of instances are not available in the input SVAL2 file.
237  
238 Or
239  
240 <instance id="hard-a.sjm-180_1:"/> <sense id="HARD1"/> <sense id="HARD2"/>
241 <instance id="hard-a.br-l15:"/> <sense id="HARD1"/>
242 <instance id="hard-a.sjm-242_12:"/> <sense id="HARD2"/>
243 <instance id="hard-a.sjm-070_4:"/> <sense id="HARD1"/> <sense id="HARD3"/>
244 <instance id="hard-a.sjm-168_4:"/> <sense id="HARD3"/>
245  
246 when some instances have multiple sense tags.
247  
248 =head2 Context Vectors on STDOUT (when --transpose is NOT specified)
249  
250 =head3 Sparse Format (SenseClusters Native Representation)
251  
252 By default (unless --dense is specified), output vectors will be created in
253 sparse format.
254  
255 The first line on stdout will show 3 numbers separated by blanks as
256  
257 N M NNZ
258  
259 where
260  
261 N = Number of instances in SVAL2 file
262  
263 M = Number of features from the FEATURE_REGEX file that were found at least once in the SVAL2 file
264  
265 NNZ = Total number of non-zero entries in all sparse vectors
266  
267 Each line thereafter shows a single sparse context vector on each line. In
268 short, every ith line after the 1st line shows the context vector of the
269 i'th instance in the given SVAL2 file.
270  
271 Each sparse vector is a list of pairs of numbers separated by space such
272 that the first number in a pair is the index of a non-zero value in the
273 vector and the second number is a non-zero value itself corresponding to
274 that index.
275  
276 =head4 Sample Sparse Output
277  
278 12 18 31
279 1 1 2 1
280 1 1 2 2 3 2 4 1
281 4 1
282 5 1 6 2
283 5 2 6 3 7 1 8 2 9 1
284 9 1
285 7 1
286 8 1 10 1
287 4 2 11 3 12 2 13 4 14 1
288 15 1
289 14 1 15 1
290 3 1 8 1 16 4 17 4 18 4
291  
292 Note that,
293  
294 =over
295  
296 =item 1.
297  
298 First Line shows that there are total 12 sparse vectors, represented
299 using total 18 features, and total 31 non-zero values.
300  
301 =item 2.
302  
303 Each vector (all lines except the 1st line) is a list of 'index value'
304 pairs separated by space. e.g. 1st vector (line 2) shows that features at
305 indices 1 and 2 appear once in the 1st instance. 2nd vector (3rd line)
306 shows that features at indices 1 and 4 appear once while those at indices
307 2 and 3 appear twice each in the 2nd instance.
308  
309 Feature indices start from 1, to be consistent with Cluto's matrix format
310 standard.
311  
312 =item 3.
313  
314 If --binary is set ON, all non-zero values will have value 1 showing
315 mere presence of feature in the context rather than the frequency counts.
316  
317 =back
318  
319 =head3 Dense Format (SenseClusters Native Representation)
320  
321 When --dense option is selected, order1vec will create output in dense
322 vector format.
323  
324 First line on STDOUT will show exactly two numbers separated by space.
325 The first number indicates the number of vectors and the second number
326 indicates the number of features (dimensions of the context vectors).
327  
328 Each line thereafter shows a single context vector such that ith line after
329 the 1st line shows the context vector of the ith instance in the SVAL2 file.
330  
331 =head4 Sample Dense Output
332  
333 12 18
334 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
335 1 2 2 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0
336 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0
337 0 0 0 0 1 2 0 0 0 0 0 0 0 0 0 0 0 0
338 0 0 0 0 2 3 1 2 1 0 0 0 0 0 0 0 0 0
339 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0
340 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0
341 0 0 0 0 0 0 0 1 0 1 0 0 0 0 0 0 0 0
342 0 0 0 2 0 0 0 0 0 0 3 2 4 1 0 0 0 0
343 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0
344 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 0 0
345 0 0 1 0 0 0 0 1 0 0 0 0 0 0 0 4 4 4
346  
347 shows same context vectors as shown in Sample Sparse Format but in dense
348 format.
349  
350 Note that
351  
352 =over
353  
354 =item 1. All vectors have same length and is same as the number of features
355 (here 18) from the given FEATURE_REGEX file that matched at least once in
356 the SVAL2 file.
357  
358 =item 2. When --binary is ON, value at column j in a vector will be 1
359 for every feature j that is found at least once in the context.
360  
361 =item 3. When --binary is not used, value at column j in a vector shows the
362 number of times the jth feature is found in the context.
363  
364 =item 4. A 0 at column j of any vector shows that the jth feature in the
365 FEATURE_REGEX file doesn't appear in that context.
366  
367 =back
368  
369 When --showkey is selected, output will be exactly same as described above
370 except the first line will show the KEY file name that is required by
371 the SenseClusters' programs.
372  
373 e.g.
374  
375 <keyfile name="KEY"/>
376 12 18 31
377 1 1 2 1
378 1 1 2 2 3 2 4 1
379 4 1
380 5 1 6 2
381 5 2 6 3 7 1 8 2 9 1
382 9 1
383 7 1
384 8 1 10 1
385 4 2 11 3 12 2 13 4 14 1
386 15 1
387 14 1 15 1
388 3 1 8 1 16 4 17 4 18 4
389  
390 Shows same vectors as shown in Sample Sparse Output when --showkey is ON.
391 Value of KEY shown in the <keyfile> tag will be the system generated KEY
392 file name.
393  
394 =head2 Features Vectors on STDOUT (when --transpose IS specified)
395  
396 Note that --testregex TEST_REGEX is a required option when --transpose is
397 specified.
398  
399 =head3 Sparse Format (Latent Semantic Analysis Representation)
400  
401 By default (unless --dense is specified), output vectors will be created in
402 sparse format.
403  
404 The first line on stdout will show 3 numbers separated by blanks as
405  
406 N M NNZ
407  
408 where
409  
410 N = Number of features from the FEATURE_REGEX file that were found at least once in the SVAL2 file
411  
412 M = Number of instances in SVAL2 file, for which at least one feature was identified
413  
414 NNZ = Total number of non-zero entries in all sparse vectors
415  
416 Each line thereafter shows a single sparse feature vector on each line. In
417 short, every ith line after the 1st line shows the feature vector of the
418 i'th feature in the created TEST_REGEX file.
419  
420 Each sparse vector is a list of pairs of numbers separated by space such
421 that the first number in a pair is the index of a non-zero value in the
422 vector and the second number is a non-zero value itself corresponding to
423 that index.
424  
425 =head4 Sample Sparse Output (Transpose of the Context Vectors output above)
426  
427 18 12 31
428 1 1 2 1
429 1 1 2 2
430 2 2 12 1
431 2 1 3 1 9 2
432 4 1 5 2
433 4 2 5 3
434 5 1 7 1
435 5 2 8 1 12 1
436 5 1 6 1
437 8 1
438 9 3
439 9 2
440 9 4
441 9 1 11 1
442 10 1 11 1
443 12 4
444 12 4
445 12 4
446  
447 Note that,
448  
449 =over
450  
451 =item 1.
452  
453 First Line shows that there are total 18 sparse feature vectors, represented
454 using total 12 contexts, and total 31 non-zero values.
455  
456 =item 2.
457  
458 Each vector (all lines except the 1st line) is a list of 'index value'
459 pairs separated by space. e.g. 1st vector (line 2) shows that contexts at
460 indices 1 and 2 contain the 1st feature once each. 3rd vector (4th line)
461 shows that context at index 2 contains the 3rd feature 2 times and the context
462 at index 12 contains the 3rd feature once.
463  
464 Context indices start from 1, to be consistent with Cluto's matrix format
465 standard.
466  
467 =item 3.
468  
469 If --binary is set ON, all non-zero values will have value 1 showing
470 mere presence of feature in the context rather than the frequency counts.
471  
472 =back
473  
474 =head3 Dense Format (Latent Semantic Analysis Representation)
475  
476 When --dense option is selected, order1vec will create output in dense
477 vector format.
478  
479 First line on STDOUT will show exactly two numbers separated by space.
480 The first number indicates the number of vectors and the second number
481 indicates the number of contexts (dimensions of the feature vectors).
482  
483 Each line thereafter shows a single feature vector such that ith line after
484 the 1st line shows the context vector of the ith instance in the SVAL2 file.
485  
486 =head4 Sample Dense Output (Transpose of the dense output of Context Vectors
487 above)
488  
489 18 12
490 1 1 0 0 0 0 0 0 0 0 0 0
491 1 2 0 0 0 0 0 0 0 0 0 0
492 0 2 0 0 0 0 0 0 0 0 0 1
493 0 1 1 0 0 0 0 0 2 0 0 0
494 0 0 0 1 2 0 0 0 0 0 0 0
495 0 0 0 2 3 0 0 0 0 0 0 0
496 0 0 0 0 1 0 1 0 0 0 0 0
497 0 0 0 0 2 0 0 1 0 0 0 1
498 0 0 0 0 1 1 0 0 0 0 0 0
499 0 0 0 0 0 0 0 1 0 0 0 0
500 0 0 0 0 0 0 0 0 3 0 0 0
501 0 0 0 0 0 0 0 0 2 0 0 0
502 0 0 0 0 0 0 0 0 4 0 0 0
503 0 0 0 0 0 0 0 0 1 0 1 0
504 0 0 0 0 0 0 0 0 0 1 1 0
505 0 0 0 0 0 0 0 0 0 0 0 4
506 0 0 0 0 0 0 0 0 0 0 0 4
507 0 0 0 0 0 0 0 0 0 0 0 4
508  
509 shows same context vectors as shown in Sample Sparse Format but in dense
510 format.
511  
512 Note that
513  
514 =over
515  
516 =item 1. All vectors have same length and is same as the number of contexts
517 (here 12) from the given SVAL2 file that contained at least one feature
518 from the TEST_REGEX file.
519  
520 =item 2. When --binary is ON, value at column j in a vector will be 1
521 for every context j that contains the feature at least once.
522  
523 =item 3. When --binary is not used, value at column j in a vector shows the
524 number of times the feature is found in the jth context.
525  
526 =item 4. A 0 at column j of any vector shows that the feature
527 doesn't appear in the jth context.
528  
529 =back
530  
531 =head1 SYSTEM REQUIREMENTS
532  
533 =over
534 =item PDL - L<http://search.cpan.org/dist/PDL/>
535  
536 =item Math::SparseVector - L<http://search.cpan.org/dist/Math-SparseVector/>
537 =back
538  
539 =head1 BUGS
540  
541 This program behaves unpredictably if the input file is not in
542 Senseval2 format. No error message is given, and it will produce
543 numeric output, but of course it has no real meaning. A check
544 should be added to make sure the input file is in Senseval2 format.
545  
546 =head1 AUTHOR
547  
548 Ted Pedersen, University of Minnesota, Duluth
549 tpederse at d.umn.edu
550  
551 Amruta Purandare, University of Pittsburgh
552  
553 Anagha Kulkarni, Carnegie-Mellon University
554  
555 Mahesh Joshi, Carnegie-Mellon University
556  
557 =head1 COPYRIGHT
558  
559 Copyright (c) 2002-2008, Ted Pedersen, Amruta Purandare, Anagha Kulkarni, Mahesh Joshi
560  
561 This program is free software; you can redistribute it and/or modify it under
562 the terms of the GNU General Public License as published by the Free Software
563 Foundation; either version 2 of the License, or (at your option) any later
564 version.
565  
566 This program is distributed in the hope that it will be useful, but WITHOUT
567 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
568 FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
569  
570 You should have received a copy of the GNU General Public License along with
571 this program; if not, write to
572  
573 The Free Software Foundation, Inc.,
574 59 Temple Place - Suite 330,
575 Boston, MA 02111-1307, USA.
576  
577 =cut
578  
579 ###############################################################################
580  
581 # ==============================
582 # THE CODE STARTS HERE
583 # ==============================
584  
585 #$0 contains the program name along with
586 #the complete path. Extract just the program
587 #name and use in error messages
588 $0=~s/.*\/(.+)/$1/;
589  
590 # PDL is used for dense vectors
591 use PDL;
592 use PDL::NiceSlice;
593 use PDL::Primitive;
594  
595 # Math::SparseVector is used for sparse vectors
596 use Math::SparseVector;
597  
598 # Math::SparseMatrix for sparse matrix transpose
599 # functionality
600 use Math::SparseMatrix;
601  
602 ###############################################################################
603  
604 # ================================
605 # COMMAND LINE OPTIONS AND USAGE
606 # ================================
607  
608 # command line options
609 use Getopt::Long;
610 GetOptions ("help","version","showkey","rlabel=s","rclass=s","clabel=s","binary","target=s","extarget","dense", "transpose", "testregex=s");
611 # show help option
612 if(defined $opt_help)
613 {
614 $opt_help=1;
615 &showhelp();
616 exit;
617 }
618  
619 # show version information
620 if(defined $opt_version)
621 {
622 $opt_version=1;
623 &showversion();
624 exit;
625 }
626  
627 # show minimal usage message if fewer arguments
628 if($#ARGV<1)
629 {
630 &showminimal();
631 exit 1;
632 }
633  
634 if (!defined $opt_transpose) {
635 $opt_transpose = 0;
636 }
637  
638 if ($opt_transpose != 0 && !defined $opt_testregex) {
639 print STDERR "ERROR($0):
640 --transpose cannot be specified without specifying --testregex
641 TEST_REGEX.\n";
642 exit 1;
643 }
644  
645 if ($opt_transpose != 0 && defined $opt_rclass) {
646 print STDERR "ERROR($0):
647 --rclass cannot be specified when using --transpose option.\n";
648 exit 1;
649 }
650  
651 if ($opt_transpose != 0 && defined $opt_showkey) {
652 print STDERR "ERROR($0):
653 --showkey cannot be specified when using --transpose option.\n";
654 exit 1;
655 }
656  
657 #############################################################################
658  
659 # ================================
660 # INITIALIZATION AND INPUT
661 # ================================
662  
663 # -------------
664 # SVAL2 file
665 # -------------
666 if(!defined $ARGV[0])
667 {
668 print STDERR "ERROR($0):
669 Please specify the SVAL2 file.\n";
670 exit 1;
671 }
672 #accept the SVAL2 file name
673 $infile=$ARGV[0];
674 if(!-e $infile)
675 {
676 print STDERR "ERROR($0):
677 SVAL2 file <$infile> doesn't exist...\n";
678 exit 1;
679 }
680  
681 open(IN,$infile) || die "Error($0):
682 Error(code=$!) in opening the SVAL2 file <$infile>\n";
683  
684 # -------------------
685 # Feature regex file
686 # -------------------
687 if(!defined $ARGV[1])
688 {
689 print STDERR "ERROR($0):
690 Please specify the Feature Regex file.\n";
691 exit 1;
692 }
693 #accept the feature file name
694 $featfile=$ARGV[1];
695 if(!-e $featfile)
696 {
697 print STDERR "ERROR($0):
698 Feature Regex file <$featfile> doesn't exist...\n";
699 exit 1;
700 }
701 open(FEAT,$featfile) || die "Error($0):
702 Error(code=$!) in opening Feature Regex file <$featfile>\n";
703  
704 # -------------------
705 # Target Word regex
706 # -------------------
707  
708 if(defined $opt_extarget)
709 {
710 #file containing regex/s for target word
711 if(defined $opt_target)
712 {
713 $target_file=$opt_target;
714 if(!(-e $target_file))
715 {
716 print STDERR "ERROR($0):
717 Target regex file <$target_file> doesn't exist.\n";
718 exit 1;
719 }
720 }
721 else
722 {
723 $target_file="target.regex";
724 if(!-e $target_file)
725 {
726 print STDERR "ERROR($0):
727 Please copy the target.regex file into the current directory or specify
728 the target regex file via --target option.\n";
729 exit 1;
730 }
731 }
732  
733 # ------------------------
734 # creating target regex
735 # ------------------------
736  
737 open(REG,$target_file) || die "ERROR($0):
738 Error(error code=$!) in opening the target regex file <$target_file>\n";
739  
740 while(<REG>)
741 {
742 chomp;
743 s/^\s+//g;
744 s/\s+$//g;
745 if(/^\s*$/)
746 {
747 next;
748 }
749 if(/^\//)
750 {
751 s/^\///;
752 }
753 else
754 {
755 print STDERR "ERROR($0):
756 Regular Expression <$_> should start with '/'\n";
757 exit 1;
758 }
759 if(/\/$/)
760 {
761 s/\/$//;
762 }
763 else
764 {
765 print STDERR "ERROR($0):
766 Regular Expression <$_> should end with '/'\n";
767 exit 1;
768 }
769 $target.="(".$_.")|";
770 }
771  
772 if(!defined $target)
773 {
774 print STDERR "ERROR($0):
775 No valid Perl regular expression found in the target regex file
776 <$target_file>\n";
777 exit 1;
778 }
779 else
780 {
781 chop $target;
782 }
783 }
784  
785 ##############################################################################
786  
787 # =======================
788 # Read Feature Regex/s
789 # =======================
790  
791 $line_num=0;
792 while(<FEAT>)
793 {
794 $line_num++;
795 chomp;
796 s/^\s*//;
797 s/\s*$//;
798  
799 if(/(.*)\s*\@name\s*=\s*(.*)/)
800 {
801 $feature_regex=$1;
802 $feature=$2;
803  
804 # removing leading and lagging blank spaces
805 $feature_regex=~s/^\s*//;
806 $feature_regex=~s/\s*$//;
807 $feature=~s/^\s*//;
808 $feature=~s/\s*$//;
809  
810 # removing the starting and ending slashes //
811 if($feature_regex=~/^\//) { $feature_regex=~s/^\///; }
812 else
813 {
814 print STDERR "ERROR($0):
815 Feature regex <$feature_regex>
816 at line <$line_num> in Feature Regex file <$featfile> should start
817 with '/'\n";
818 exit 1;
819 }
820 if($feature_regex=~/\/$/) { $feature_regex=~s/\/$//; }
821 else
822 {
823 print STDERR "ERROR($0):
824 Feature regex <$feature_regex>
825 at line <$line_num> in Feature Regex file <$featfile> should end
826 with '/'\n";
827 exit 1;
828 }
829 # target word is a feature only when --extarget is not
830 # selected or feature regex doesn't match with target
831 # regex
832 if(!defined $opt_extarget || $feature !~ /^$target$/)
833 {
834 push @features,$feature_regex;
835 # we require the @name part of the nsp2regex output if column labels
836 # or test regexes are requested
837 if(defined $opt_clabel || defined $opt_testregex)
838 {
839 push @clabels, $feature;
840 }
841 }
842 }
843 else
844 {
845 print STDERR "ERROR($0):
846 Line <$line_num> in Feature Regex file <$featfile> has an unexpected
847 format.\n";
848 exit 1;
849 }
850 }
851  
852 #output vector will have
853 #columns = #features
854 $cols=scalar(@features);
855 ##############################################################################
856  
857 # =================================================
858 # CREATING CONTEXT VECTORS
859 # =================================================
860  
861 # context vectors are temporarily written into a
862 # TEMP file
863  
864 # if the program finishes successfully, this TEMP file
865 # is printed to STDOUT and is deleted
866  
867 # otherwise TEMP file is retained and stores the partial
868 # program output
869  
870 $tempfile="tempfile" . time() . ".order1vec";
871 if(-e $tempfile)
872 {
873 print STDERR "ERROR($0):
874 Temporary file <$tempfile> should not already exist.\n";
875 exit 1;
876 }
877  
878 open(TEMP,">$tempfile") || die "ERROR($0):
879 Error(code=$!) in opening internal temporary file <$tempfile>\n";
880  
881 # reading the SVAL2 file
882 $line_num=0;
883  
884 if(defined $opt_dense)
885 {
886 # use PDL
887 $context_vector=zeroes($cols);
888 # PDL matrices are column major. Initially create a matrix
889 # with number of columns equal to number of features and
890 # number of rows = 1, filled with zeroes
891 $orig_matrix = zeroes($cols, 1);
892 }
893 else
894 {
895 # use Math::SparseVector module
896 $context_vector=Math::SparseVector->new;
897 $nnz=0;
898 }
899  
900 $context_count = 0;
901  
902 while(<IN>)
903 {
904 $line_num++;
905  
906 if(/instance id\s*=\s*\"([^"]+)\"/)
907 {
908 $instance=$1;
909 if(defined $instance_ids{$instance})
910 {
911 print STDERR "ERROR($0):
912 Instance Id <$instance> is repeated in the SVAL2 file <$infile>\n";
913 exit 1;
914 }
915 push @instances,$instance;
916 $instance_ids{$instance}=1;
917 }
918 if(/<\/instance>/)
919 {
920 undef $instance;
921 }
922 if(/sense\s*id\s*=\s*\"([^"]+)\"/)
923 {
924 # no <instance> open
925 if(!defined $instance)
926 {
927 print STDERR "ERROR($0):
928 Missing <instance> tag before the <sense> tag at line <$line_num>
929 in SVAL2 file <$infile>\n";
930 exit 1;
931 }
932  
933 $sense=$1;
934 if(defined $key_table{$instance}{$sense})
935 {
936 print STDERR "ERROR($0):
937 <instance-id, sense-tag> pair <$instance, $sense> is repeated in the
938 SVAL2 file <$infile>\n";
939 exit 1;
940 }
941 $key_table{$instance}{$sense}=1;
942 }
943  
944 if(/<\/context>/)
945 {
946  
947 undef $data_start;
948
949 # add dense vector to orig_matrix
950 if(defined $opt_dense)
951 {
952 # initially resize the original matrix to new number of contexts
953 # (actual increment in count is done later, since we use the current
954 # value of $context_count for indexing the orig_matrix)
955 $orig_matrix->reshape($cols, $context_count + 1);
956 # get the vector for the current context
957 $rowvec = $orig_matrix->slice(":,($context_count)");
958 # update the vector for the context in the the orig_matrix
959 $rowvec .= $context_vector;
960 }
961 # printing context vector to TEMP file
962 # sparse vector
963 else
964 {
965 foreach $key ($context_vector->keys)
966 {
967 print TEMP "$key " . $context_vector->get($key) . " ";
968 $nnz++;
969 }
970 print TEMP "\n";
971 }
972
973 # increment the number of contexts
974 $context_count++;
975 }
976  
977 # contextual data
978 if(defined $data_start)
979 {
980 # nsp2regex features have format
981 # /\sFEATURE\s/ which requires a space
982 # on each side of the token
983 s/^(\S)/ $1/;
984 s/(\S)$/$1 /;
985 # ---------------------------------------------------
986 # the logic of matching feature regex/s is borrowed
987 # from the xml2arff.pl program from the SenseTools
988 # package by Satanjeev Banerjee and Ted Pedersen
989 # ---------------------------------------------------
990 foreach $index (0..$#features)
991 {
992 $feature_regex=$features[$index];
993 if(defined $opt_binary)
994 {
995 # match or not
996 if(/$feature_regex/)
997 {
998 if(defined $opt_dense)
999 {
1000 $context_vector->set($index,1);
1001 }
1002 else
1003 {
1004 $context_vector->set($index+1,1);
1005 }
1006 }
1007 }
1008 else
1009 {
1010 # number of matches
1011 while(/$feature_regex/g)
1012 {
1013 if(defined $opt_dense)
1014 {
1015 $context_vector($index)++;
1016 }
1017 else
1018 {
1019 $context_vector->incr($index+1);
1020 }
1021 }
1022 }
1023 }
1024 }
1025  
1026 # beginning of the context
1027 if(/<context>/)
1028 {
1029 # no <instance> open
1030 if(!defined $instance)
1031 {
1032 print STDERR "ERROR($0):
1033 Missing <instance> tag before the <context> tag at line <$line_num>
1034 in SVAL2 file <$infile>\n";
1035 exit 1;
1036 }
1037
1038 # no sense tag for this instance
1039 if(!defined $key_table{$instance})
1040 {
1041 $sense="NOTAG";
1042 $key_table{$instance}{$sense}=1;
1043 }
1044 $data_start=1;
1045 if(defined $opt_dense)
1046 {
1047 $context_vector->inplace->zeroes;
1048 }
1049 else
1050 {
1051 $context_vector->free;
1052 }
1053 }
1054 }
1055  
1056 # if we are in dense mode, then TEMP file is
1057 # created here
1058 if(defined $opt_dense) {
1059 if ($opt_transpose != 0) {
1060 # create feature-by-context dense TEMP file
1061 $transpose_matrix = transpose($orig_matrix);
1062 for ($i = 0; $i < $cols; $i++) {
1063 for ($j = 0; $j < $context_count; $j++) {
1064 print TEMP $transpose_matrix->at($j,$i) . " ";
1065 }
1066 print TEMP "\n";
1067 }
1068 } else {
1069 # create context-by-feature dense TEMP file
1070 for ($i = 0; $i < $context_count; $i++) {
1071 for ($j = 0; $j < $cols; $j++) {
1072 print TEMP $orig_matrix->at($j,$i) . " ";
1073 }
1074 print TEMP "\n";
1075 }
1076 }
1077 }
1078  
1079 close TEMP;
1080  
1081 undef $opt_extarget;
1082  
1083 # added by AKK on 02/28/2005
1084 # work-around for eliminating the columns (i.e. the features) which
1085 # dont have any non-zero row entry i.e. the features that do not occur
1086 # in any of the contexts.
1087  
1088 my $mod_tempfile = "mod_tempfile" . time() . ".order1vec";
1089 my @col = ();
1090  
1091 if(!defined $opt_dense)
1092 {
1093 open(TEMP,$tempfile) || die "ERROR($0):
1094 Error(code=$!) in opening internal temporary file <$tempfile>\n";
1095  
1096 # go through each row of the file till either of the following occurs:
1097 # 1. we encounter atleast one entry for each column i.e. for each feature
1098 # 2. we reach end of the file
1099  
1100 my $flag = 0;
1101  
1102 for($i=1;$i<=$cols;$i++)
1103 {
1104 $col[$i] = 0;
1105 }
1106  
1107 while(<TEMP>)
1108 {
1109 @elem = split(/\s+/);
1110
1111 # mark the column for which an entry was found
1112 for($i=0;$i<=$#elem;$i=$i+2)
1113 {
1114 $col[$elem[$i]] = 1;
1115 }
1116  
1117 # check if an entry found for each column
1118 $flag = 0;
1119 for($i=1;$i<=$cols;$i++)
1120 {
1121 if($col[$i] == 0)
1122 {
1123 $flag = 1;
1124 last;
1125 }
1126 }
1127  
1128 # if an entry found for each column
1129 # then exit the while loop.
1130 # this situation suggests that we dont have any
1131 # no entry column in this input data.
1132 if($flag == 0)
1133 {
1134 last;
1135 }
1136 }
1137 close TEMP;
1138  
1139 # ON(1) state of flag variable suggests that the input matrix
1140 # has one or more columns with no non-zero entries.
1141 # Thus we need to remove these columns and adjust the column
1142 # indices for all the columns following the removed column.
1143 my %hash_col = ();
1144  
1145 if($flag == 1)
1146 {
1147 # create the new column indices
1148 $cnt = 1;
1149 for($i=1;$i<=$#col;$i++)
1150 {
1151 # for the remaining columns
1152 # adjust the column indices
1153 if($col[$i] == 1)
1154 {
1155 $hash_col{$i} = $cnt;
1156 $cnt++;
1157 }
1158 # when column dropped decrease
1159 # total # of cols
1160 else
1161 {
1162 $cols--;
1163 }
1164 }
1165  
1166 # write the modified TEMP file to another temp file with the changed column indices.
1167 open(TEMP,$tempfile) || die "ERROR($0):
1168 Error(code=$!) in opening internal temporary file <$tempfile>\n";
1169  
1170 open(MOD,">$mod_tempfile") || die "ERROR($0):
1171 Error(code=$!) in opening internal temporary file <$mod_tempfile>\n";
1172  
1173 while(<TEMP>)
1174 {
1175 @elem = split(/\s+/);
1176
1177 # print the column index and the cell value pairs for the context
1178 for($i=0;$i<=$#elem;$i=$i+2)
1179 {
1180 print MOD $hash_col{$elem[$i]} . " " . $elem[$i+1] . " ";
1181 }
1182 print MOD "\n";
1183 }
1184  
1185 close TEMP;
1186 close MOD;
1187 }
1188 }
1189  
1190 # end by AKK on 02/28/2005
1191  
1192 # for sparse mode, if --transpose is specified, we need to use
1193 # Math::SpaarseMatrix for the transpose functionality
1194  
1195 if (!defined $opt_dense && $opt_transpose != 0) {
1196 # first prepare a temporary file for transpose function input.
1197 # we need to eliminate any empty contexts from the original
1198 # output of order1 represenataion
1199 $transpose_in = "transpose_in" . time() . "order1vec";
1200 # process the temporary file created above, to eliminate empty
1201 # contexts, and create an input file for transposing
1202 if(-e $mod_tempfile)
1203 {
1204 open(TEMP,$mod_tempfile) || die "ERROR($0):
1205 Error(code=$!) in opening internal temporary file <$tempfile>\n";
1206 }
1207 else
1208 {
1209 open(TEMP,$tempfile) || die "ERROR($0):
1210 Error(code=$!) in opening internal temporary file <$tempfile>\n";
1211 }
1212 open(TRANS_IN, "> $transpose_in") or die "ERROR($0):
1213 Error(code=$!) while creating temporary input file <$transpose_in>
1214 for transposing.\n";
1215  
1216 # $linetowrite contains the content of output except
1217 # blank lines representing empty contexts
1218 $linetowrite = "";
1219 $rows = @instances;
1220 # in this process, instances might reduce, so we should create a
1221 # new array of only the remaining instances. initially, just create
1222 # an array containing all 1's indicating that no instances are dropped
1223 for ($i = 0; $i < @instances; $i++) {
1224 $nonempty_instances[$i] = 1;
1225 }
1226 # use index to determine which instances to ignore
1227 $index = 0;
1228 while ($line = <TEMP>) {
1229 chomp $line;
1230 if ($line ne "") {
1231 $linetowrite .= "$line\n";
1232 } else {
1233 # do no print the empty line and reduce the row count
1234 $rows--;
1235 # put a 0 in the nonempty_instances array, indicating that the
1236 # instance at this index in the @instances array is empty
1237 $nonempty_instances[$index] = 0;
1238 }
1239 $index++;
1240 }
1241 # write the reduced number of contexts back, without empty lines
1242 print TRANS_IN "$rows $cols $nnz\n";
1243 print TRANS_IN $linetowrite;
1244  
1245 close TEMP;
1246 close TRANS_IN;
1247
1248 $transpose_sparsematrix = Math::SparseMatrix->createTransposeFromFile(
1249 $transpose_in);
1250 # create the transpose output
1251 $transpose_out = "transpose_out" . time() . "order1vec";
1252 $transpose_sparsematrix->writeToFile($transpose_out);
1253 }
1254  
1255  
1256 ###########################################################################
1257  
1258 # =========================
1259 # OUTPUT SECTION
1260 # =========================
1261  
1262 # =====================
1263 # Creating KEY file
1264 # =====================
1265  
1266 # DO NOT GENERATE A KEY FILE IN --transpose MODE
1267  
1268 # KEY file is automatically created by the program
1269 # and preserves the instance ids and sense tags of the
1270 # SVAL-2 instances
1271  
1272 if ($opt_transpose == 0) {
1273 $keyfile="keyfile" . time() . ".key";
1274 if(-e $keyfile)
1275 {
1276 print STDERR "ERROR($0):
1277 System generated KEY file <$keyfile> should not already exist.\n";
1278 exit 1;
1279 }
1280  
1281 open(KEY,">$keyfile") || die "ERROR($0):
1282 Error(code=$!) in opening system generated KEY file <$keyfile>\n";
1283  
1284 foreach $instance (@instances)
1285 {
1286 print KEY "<instance id=\"$instance\"\/> ";
1287 foreach $sense (sort keys %{$key_table{$instance}})
1288 {
1289 print KEY "<sense id=\"$sense\"\/> ";
1290 }
1291 print KEY "\n";
1292 }
1293  
1294 close KEY;
1295 }
1296  
1297 # =========================
1298 # Printing output vectors
1299 # =========================
1300  
1301 # printing KEY name when --showkey is ON
1302 if(defined $opt_showkey)
1303 {
1304 print "<keyfile name=\"$keyfile\"\/>\n";
1305 undef $opt_showkey;
1306 }
1307  
1308 # first line for sparse vectors shows
1309 # N M NNZ
1310 # while the first line in dense vectors shows
1311 # N M
1312  
1313 # where N = number of vectors = Number of instances in SVAL2
1314 # M = number of dimensions = Number of features in FEATURE
1315 # NNZ = total number of non-zero entries in sparse vectors
1316  
1317 # Additionally, we also need to consider if the the --transpose was on,
1318 # in which case N and M are swapped. But this file is already created
1319 # in the Math::SparseMatrix transpose code called above. So in that case
1320 # we simply open that file and print it at STDOUT
1321  
1322 if (!defined $opt_dense && $opt_transpose != 0) {
1323 # transpose and sparse
1324 open (TRANS_OUT, "< $transpose_out") or die "ERROR($0):
1325 Error (code=$!) while opening internal file <$transpose_out>\n";
1326 while (<TRANS_OUT>) {
1327 print;
1328 }
1329 close TRANS_OUT;
1330 } else {
1331  
1332 if ($opt_transpose != 0) {
1333 # transpose and dense (since transpose and sparse would have
1334 # been the "if" condition above)
1335 print "$cols " . scalar(@instances);
1336 } else {
1337 # non-transpose and (sparse/dense)
1338 print scalar(@instances) . " $cols";
1339 }
1340  
1341 if(!defined $opt_dense)
1342 {
1343 print " $nnz";
1344 }
1345 print "\n";
1346  
1347 # this is followed by the actual context vectors
1348 if(-e $mod_tempfile)
1349 {
1350 open(TEMP,$mod_tempfile) || die "ERROR($0):
1351 Error(code=$!) in opening internal temporary file <$tempfile>\n";
1352 }
1353 else
1354 {
1355 open(TEMP,$tempfile) || die "ERROR($0):
1356 Error(code=$!) in opening internal temporary file <$tempfile>\n";
1357 }
1358  
1359 while(<TEMP>)
1360 {
1361 print;
1362 }
1363 close TEMP;
1364 }
1365  
1366 # deleting TEMP as the program is successfully finished
1367 unlink $tempfile;
1368 if(-e $mod_tempfile)
1369 {
1370 unlink $mod_tempfile;
1371 }
1372 if (defined $transpose_in && -e $transpose_in)
1373 {
1374 unlink $transpose_in;
1375 }
1376 if (defined $transpose_out && -e $transpose_out)
1377 {
1378 unlink $transpose_out;
1379 }
1380  
1381 undef $opt_binary;
1382  
1383 # ==========================
1384 # Creating Cluto files
1385 # ==========================
1386  
1387 # REMEMBER: if --transpose is specified, then row and column labels get
1388 # interchanged
1389  
1390 # writing rlabel file
1391 if(defined $opt_rlabel)
1392 {
1393 $rlabel=$opt_rlabel;
1394 if(-e $rlabel)
1395 {
1396 print STDERR "Warning($0):
1397 Row label file <$rlabel> already exists, overwrite (y/n)? ";
1398 $ans=<STDIN>;
1399 }
1400 if(!-e $rlabel || $ans=~/Y|y/)
1401 {
1402 open(RLAB,">$rlabel") || die "Error($0):
1403 Error(code=$!) in opening the Row Label file <$rlabel>\n";
1404 if ($opt_transpose == 0) {
1405 # printing rlabels
1406 foreach $instance (@instances)
1407 {
1408 print RLAB "$instance\n";
1409 }
1410 } else {
1411 # printing column labels as row labels during transpose
1412 if (!defined $opt_dense) {
1413 # in sparse mode, we need to check for dropping
1414 # column labels for empty columns
1415 for ($index=1; $index <= @clabels; $index++)
1416 {
1417 if ($col[$index] > 0) {
1418 print RLAB $clabels[$index-1] . "\n";
1419 }
1420 }
1421 } else {
1422 # in dense mode, output all column labels
1423 for ($index=1; $index <= @clabels; $index++)
1424 {
1425 print RLAB $clabels[$index-1] . "\n";
1426 }
1427 }
1428 }
1429 close RLAB;
1430 }
1431 }
1432  
1433 # writing rclass file
1434 if(defined $opt_rclass)
1435 {
1436 $rclass=$opt_rclass;
1437 if(-e $rclass)
1438 {
1439 print STDERR "Warning($0):
1440 Class label file <$rclass> already exists, overwrite (y/n)? ";
1441 $ans=<STDIN>;
1442 }
1443 if(!-e $rclass || $ans=~/Y|y/)
1444 {
1445 open(RCL,">$rclass") || die "Error($0):
1446 Error(code=$!) in opening the Class Label file <$rclass>\n";
1447 # printing rclasses
1448 foreach $instance (@instances)
1449 {
1450 @senses=sort keys %{$key_table{$instance}};
1451 if(scalar(@senses) > 1)
1452 {
1453 print STDERR "ERROR($0):
1454 Instance <$instance> can not have multiple senses in RCLASSFILE.\n";
1455 exit 1;
1456 }
1457 print RCL "$senses[0]\n";
1458 }
1459 close RCL;
1460 }
1461 }
1462  
1463 # writing clabel file
1464 if(defined $opt_clabel)
1465 {
1466 $clabel=$opt_clabel;
1467 if(-e $clabel)
1468 {
1469 print STDERR "Warning($0):
1470 Column label file <$clabel> already exists, overwrite (y/n)? ";
1471 $ans=<STDIN>;
1472 }
1473 if(!-e $clabel || $ans=~/Y|y/)
1474 {
1475 open(CLAB,">$clabel") || die "Error($0):
1476 Error(code=$!) in opening the Column Label file <$clabel>\n";
1477 if ($opt_transpose == 0) {
1478 # printing column labels
1479 if (!defined $opt_dense) {
1480 # in sparse mode, we need to check for dropping
1481 # column labels for empty columns
1482 for ($index=1; $index <= @clabels; $index++)
1483 {
1484 if ($col[$index] > 0) {
1485 print CLAB $clabels[$index-1] . "\n";
1486 }
1487 }
1488 } else {
1489 # in dense mode, output all column labels
1490 for ($index=1; $index <= @clabels; $index++)
1491 {
1492 print CLAB $clabels[$index-1] . "\n";
1493 }
1494 }
1495 } else {
1496 # printing rlabels as column labels during transpose
1497 # check for empty contexts, and skip them in the output
1498 if (!defined $opt_dense) {
1499 # number of instances might reduce in sparse representation
1500 # in --transpose option
1501 for ($i = 0; $i < @instances; $i++)
1502 {
1503 if ($nonempty_instances[$i] == 1) {
1504 print CLAB "$instances[$i]\n";
1505 }
1506 }
1507 } else {
1508 for ($i = 0; $i < @instances; $i++)
1509 {
1510 print CLAB "$instances[$i]\n";
1511 }
1512 }
1513 }
1514 close CLAB;
1515 }
1516 }
1517  
1518 # writing testregex file
1519 if(defined $opt_testregex)
1520 {
1521 $testregex=$opt_testregex;
1522 if(-e $testregex)
1523 {
1524 print STDERR "Warning($0):
1525 Test Regex file <$testregex> already exists, overwrite (y/n)? ";
1526 $ans=<STDIN>;
1527 }
1528 if(!-e $testregex || $ans=~/Y|y/)
1529 {
1530 open(TESTREGEX,">$testregex") || die "Error($0):
1531 Error(code=$!) in opening the Test Regex file <$testregex>\n";
1532 # printing regexes
1533 if (!defined $opt_dense) {
1534 # in sparse mode, we need to check for dropping
1535 # regexes for empty columns
1536 for ($index=1; $index <= @features; $index++)
1537 {
1538 if ($col[$index] > 0) {
1539 print TESTREGEX "/$features[$index-1]/" . " \@name=$clabels[$index-1]\n";
1540 }
1541 }
1542 } else {
1543 # in dense mode, output all column labels
1544 for ($index=1; $index <= @features; $index++)
1545 {
1546 print TESTREGEX "/$features[$index-1]/" . " \@name=$clabels[$index-1]\n";
1547 }
1548 }
1549 close TESTREGEX;
1550 }
1551 }
1552  
1553 ##############################################################################
1554  
1555 # ==========================
1556 # SUBROUTINE SECTION
1557 # ==========================
1558  
1559 #-----------------------------------------------------------------------------
1560 #show minimal usage message
1561 sub showminimal()
1562 {
1563 print "Usage: order1vec.pl [OPTIONS] SVAL2 FEATURE_REGEX";
1564 print "\nTYPE order1vec.pl --help for help\n";
1565 }
1566  
1567 #-----------------------------------------------------------------------------
1568 #show help
1569 sub showhelp()
1570 {
1571 print "Usage: order1vec.pl [OPTIONS] SVAL2 FEATURE_REGEX
1572  
1573 Displays the first order context vectors of the instances in the given SVAL2
1574 file.
1575  
1576 SVAL2
1577 A tokenized, preprocessed and well formatted Senseval-2 instance file.
1578  
1579 FEATURE_REGEX
1580
1581 A file containing Perl regular expressions for features as created
1582 by nsp2regex.pl.
1583  
1584 OPTIONS:
1585  
1586 --binary
1587 Displays binary context vectors that show mere presence or absence of
1588 features in the contexts. By default, frequency vectors are displayed.
1589  
1590 --dense
1591 Displays dense context vectors. By default, context vectors will have
1592 sparse format.
1593  
1594 --rlabel RLABELFILE
1595 Writes row labels (instance ids) to the RLABELFILE which can be given
1596 to vcluster's --rlabelfile option.
1597
1598 --rclass RCLASSFILE
1599 Writes sense ids to the RCLASSFILE which can be given to vcluster's
1600 --rclassfile option.
1601  
1602 This option cannot be specified when --transpose is specified.
1603  
1604 --clabel CLABELFILE
1605 Writes column labels (features) to the CLABELFILE which can be given
1606 to vcluster's --clabelfile option.
1607  
1608 --transpose
1609 Creates feature vectors instead of the default context vectors. The
1610 output is a Latent Semantic Analysis style feature-by-context matrix,
1611 instead of the default context-by-feature matrix that is native to
1612 SenseClusters. As a result, the contents of the RLABELFILE and
1613 CLABELFILE are swapped, i.e. the list of features is output to the
1614 RLABELFILE and the list of contexts is output to the CLABELFILE.
1615  
1616 --testregex TEST_REGEX
1617  
1618 Creates a TEST_REGEX file containing only those regular expressions
1619 from the input FEATURE_REGEX file that matched at least once in the
1620 input SVAL2 file. This list can be different from the original list
1621 in FEATURE_REGEX when different training data has been used to
1622 identify features or when a different scope has been used for
1623 training and test data creation.
1624  
1625 This option is required when the --transpose option is specified.
1626  
1627 --showkey
1628 Displays the system generated KEY file name on the first line.
1629  
1630 This option cannot be specified when --transpose is specified.
1631  
1632 --target TARGET_REGEX
1633 Specify a file containing Perl regex/s that define the target word
1634 in SVAL2. By default, target.regex is assumed to exist in current
1635 directory.
1636  
1637 --extarget
1638 Excludes the target word from features if the target word as
1639 specified by --target or default target.regex, is listed in the
1640 FEATURE_REGEX file.
1641  
1642 Other Options:
1643  
1644 --help
1645 Displays this message.
1646  
1647 --version
1648 Displays the version information.
1649  
1650 Type 'perldoc order1vec.pl' to view detailed documentation of order1vec.\n";
1651 }
1652  
1653 #------------------------------------------------------------------------------
1654 #version information
1655 sub showversion()
1656 {
1657 print '$Id: order1vec.pl,v 1.48 2008/03/30 04:40:58 tpederse Exp $';
1658 print "\nConvert Senseval-2 contexts into first order feature vectors\n";
1659  
1660 # print "\nCopyright (c) 2002-2006, Ted Pedersen, Amruta Purandare, Anagha Kulkarni, & Mahesh Joshi\n";
1661 # print "order1vec.pl - Version 0.08\n";
1662 # print "Displays the first order context vectors.\n";
1663 # print "Date of Last Update: 03/04/2005\n";
1664 }
1665  
1666 #############################################################################
1667