script/discriminate.pl untested

filter: Statementstmt 0.0% 0 / 938 Branchbran 0.0% 0 / 586 Conditioncond 0.0% 0 / 234 MC/DCmcdc 0.0% 0 / 135 Subroutinesub 0.0% 0 / 6 Podpod 0.0% 0 / 4 totaltotal 0.0% 0 / 1903 CC 7 SCAR 35.1
CC
7
Cov
0%
svd
56.0
showminimal
2.0
showhelp
2.0
CRAP 33.5
1 #!/usr/local/bin/perl -w
2  
3 =head1 NAME
4  
5 discriminate.pl Wrapper program to run SenseClusters in a single command
6  
7 =head1 SYNOPSIS
8  
9 Discriminates among the given text instances based on their contextual
10 similarities.
11  
12 =head1 USAGE
13  
14 discriminate.pl [OPTIONS] TEST
15  
16 =head1 INPUT
17  
18 =head2 Required Arguments:
19  
20 =head3 TEST
21  
22 Senseval-2 formatted TEST instance file that contains the instances
23 to be clustered.
24  
25 =head2 Optional Arguments:
26  
27 =head3 DATA OPTIONS :
28  
29 =head4 --training TRAIN
30  
31 Training file in plain text format that can be used to select features.
32 If this is not specified, features are selected from the given TEST file.
33  
34 =head4 --split N
35  
36 Splits the given TEST file into two portions, N% for the use as the TRAIN
37 data and (100-N)% as the TEST data. The value for N is a percentage and
38 should be an integer between 1 to 99 (inclusive). The instances from the
39 original TEST file are not picked or split in any particular order but are
40 randomly split into the two portions of TRAIN and TEST data while maintaining
41 the ratio of N/(100-N).
42  
43 Note: This option cannot be used when --training option is also used.
44  
45 =head4 --token TOKEN
46  
47 A file containing Perl regex/s that define the tokenization scheme in TRAIN
48 and TEST files. If --token is not specified, default token regex file
49 token.regex is searched in the current directory.
50  
51 =head4 --target TARGET
52  
53 A file containing Perl regex/s for identifying the target word. A sample
54 target.regex file containing regex:
55  
56 /<head>\w+</head>/
57  
58 is provided with this distribution. If --target is not specified, default
59 target regex file target.regex is searched in the current directory.
60 If this file doesn't exist, target.regex is automatically created by finding
61 all instances of <head> tags from the TEST data. If there are no instances
62 of <head> tags in TEST, the given data is assumed to be global and target
63 word is not searched in either TRAIN or TEST.
64  
65 Note: --target cannot be specified with headless input data
66 i.e. test file without head/target word(s).
67  
68 =head4 --prefix PRE
69  
70 Specify a prefix to be used in all output file names. e.g. context vector
71 file will have name 'PRE.vectors', features file will have name 'PRE.features'
72 and so on ... By default, a random prefix is created using the time stamp.
73  
74 =head4 --format f16.XX
75  
76 The default format for floating point numbers is f16.06. This means that
77 there is room for 6 digits to the right of the decimal, and 9 to the
78 left. You may change XX to any value between 0 and 15, however, the
79 format must remain 16 spaces long due to formatting requirements of SVDPACKC.
80  
81 =head4 --wordclust
82  
83 Discriminates and clusters each word based upon its direct and indirect
84 co-occurrence with other words (when used without the --lsa switch) or
85 clusters words or features based upon their occurrences in different contexts
86 (when used with the --lsa switch).
87  
88 Note: 1. Separate (--training) TRAIN data should not be used with word
89 clustering.
90 2. Starting with Version 0.93, word clustering is no longer
91 restricted to using only headless data. However, options
92 specific to headed data such as --scope_test and target
93 co-occurrence features (see below) cannot be used.
94  
95 =head4 --lsa
96  
97 Uses Latent Semantic Analysis (LSA) style representation for clustering
98 features or contexts. LSA representation is the transpose of
99 the context-by-feature matrix created using the native SenseClusters
100 order1 context representation.
101  
102 This option can be used only in the following two combinations of
103 the --context and the --wordclust options:
104  
105 =over
106  
107 =item 1. --context o1 --wordclust --lsa
108  
109  
110 Performs feature clustering by grouping together features based on the
111 contexts that they occur in. Features can be unigrams, bigrams or
112 co-occurrences. Feature vectors are the rows of the transposed
113 context-by-feature representation created by order1vec.pl.
114  
115 =item 2. --context o2 --lsa
116  
117  
118 Performs context clustering by creating context vectors by averaging the
119 feature vectors from the transposed context-by-feature representation of
120 order1vec.pl.
121  
122 =back
123  
124 =head3 FEATURE OPTIONS :
125  
126 =head4 --feature TYPE
127  
128 Specify the feature type to be used for representing contexts.
129 Possible options for feature type with first order context representation:
130  
131 bi - bigrams [default]
132 tco - target co-occurrences
133 co - co-occurrences
134 uni - unigrams
135  
136 Possible options for feature type with second order context representation:
137  
138 bi - bigrams [default]
139 co - co-occurrences
140 tco - target co-occurrences
141  
142 Note: --tco (target co-occurrences) cannot be used with headless
143 data i.e. test/train file without head/target word(s).
144  
145 =head4 --scope_train S1
146  
147 Limits the scope of the training contexts to S1 words around (on both
148 sides of) the TARGET word. Thus, it allows selection of local features.
149 If --scope_train is used, each training instance is expected to include
150 the target word as specified by the --target option or default target.regex.
151  
152 Note: --scope_train cannot be used with headless data i.e. train files
153 without head/target word(s).
154  
155 =head4 --scope_test S2
156  
157 Limits the scope of the test contexts to S2 words around (on both sides of)
158 the TARGET word. Thus, it allows to match and use local features in the
159 context vectors.
160  
161 Note: --scope_test cannot be used with headless data i.e. test files
162 without head/target word(s).
163  
164 =head4 --stop STOPFILE
165  
166 A file of Perl regexes that define the stop list of words to be excluded from
167 the features.
168  
169 STOPFILE could be specified with two modes -
170  
171 AND mode - declared by including '@stop.mode=AND' on the first line of the
172 STOPFILE.
173 - ignores word pairs in which both words are stop words.
174  
175 OR mode - declared by including '@stop.mode=OR' on the first line of the
176 STOPFILE.
177 - ignores word pairs in which either word is a stop word.
178  
179 Both modes exclude stop words from unigram features.
180  
181 Default is OR mode.
182  
183 =head4 --remove F
184  
185 Removes features that occur less than F times in the training corpus.
186  
187 =head4 --window W
188  
189 Specifies the window size for bigram/co-occurrence features. Pairs of words
190 that co-occur within the specified window from each other (window W allows at
191 most W-2 intervening words) will form the bigram/co-occurrence features.
192  
193 Default window size is 2 which allows only consecutive word pairs.
194  
195 Not applicable to unigram features.
196  
197 =head4 --stat STAT
198  
199 Bigrams and co-occurrences can be selected based on their statistical scores
200 of association as specified by this option. If --vector = o2 and
201 --stat is used, word association matrix will use the scores computed by the
202 specified statistical test instead of simple joint frequency counts of the
203 word pairs.
204  
205 Available tests of association are :
206  
207 dice - Dice Coefficient
208 ll - Log Likelihood Ratio
209 odds - Odds Ratio
210 phi - Phi Coefficient
211 pmi - Point-Wise Mutual Information
212 tmi - True Mutual Information
213 x2 - Chi-Squared Test
214 tscore - T-Score
215 leftFisher - Left Fisher's Test
216 rightFisher - Right Fisher's Test
217  
218 By default, features are selected and represented using their frequency
219 counts.
220  
221 =head4 --stat_rank N
222  
223 Word pairs ranking below N when arranged in descending order of their test
224 scores are ignored.
225  
226 --stat_rank has no effect unless --stat is specified.
227  
228 =head4 --stat_score S
229  
230 Selects word pairs with scores greater than S after performing the selected
231 test of association. Score could be any real number that will give reasonable
232 number of features for the requested test.
233  
234 --stat_score has no effect unless --stat is specified.
235  
236 =head3 VECTOR OPTIONS :
237  
238 =head4 --context ORD
239  
240 Specifies the context representation to be used. Set ORD to 'o1' to use
241 1st order context vectors, and to 'o2' to select 2nd order context vectors.
242 Default context representation is o2.
243  
244 =head4 --binary
245  
246 Creates binary feature and context vectors. By default, feature vectors
247 show the joint frequency scores of the associated word pairs while the
248 context vectors show the average of the feature vectors of words that occur
249 in the context. With --binary turned ON, feature vectors show mere presence or
250 absence of the particular word pair (co-occurrence/bigram) in TRAIN,
251 while the context vectors will represent a binary 'OR' operation on the
252 corresponding vectors of contextual features.
253  
254 =head3 SVD OPTIONS :
255  
256 =head4 --svd
257  
258 Reduces the feature space dimensions by performing Singular Value Decomposition
259 (SVD). By default, all feature dimensions are retained.
260  
261 =head4 --k K
262  
263 Reduces the dimensions of the feature space to K. Default K = 300
264  
265 =head4 --rf RF
266  
267 Specifies the scaling factor for reducing feature space dimensions such that
268 feature space with N dimensions is reduced down to N/RF. Default RF = 4.
269 RF should be an integer greater than 1.
270  
271 If both --k and --rf are specified, dimensions are reduced to min(k,N/RF).
272  
273 Note: If the reduced dimensions ( min(k,N/RF) ) turn-out to be less than
274 or equal to 10 then svd is not performed.
275  
276 =head4 --iter I
277  
278 Specifies the number of iterations of SVD. Recommended value is 3 times
279 the desired K.
280  
281 =head3 CLUSTER-STOPPING OPTIONS:
282  
283 =head4 --cluststop CS
284  
285 Specifies the cluster stopping measure to be used to predict the number
286 the number of clusters.
287  
288 The possible option values:
289 pk1 - Use PK1 measure [ PK1[m] = (crfun[m] - mean(crfun[1...deltaM]))/std(crfun[1...deltaM])) ]
290 pk2 - Use PK2 measure [ PK2[m] = (crfun[m]/crfun[m-1]) ]
291 pk3 - Use PK3 measure [ PK3[m] = ((2 * crfun[m])/(crfun[m-1] + crfun[m+1])) ]
292 gap - Use Adapted Gap Statistic.
293 pk - Use all the PK measures.
294 all - Use all the four cluster stopping measures.
295  
296 More about these measures can be found in the documentation of
297 Toolkit/clusterstop/clusterstopping.pl
298  
299 NOTE: Options --cluststop and --clusters (described under Clustering options) cannot be used together.
300  
301 =head4 --delta INT
302  
303 NOTE: Delta value can only be a positive integer value.
304  
305 Specify 0 to stop the iterating clustering process when two consecutive crfun values
306 are exactly equal. This is the default setting when the crfun values are integer/whole numbers.
307  
308 Specify non-zero positive integer to stop the iterating clustering process when the difference
309 between two consecutive crfun values is less than or equal to this value. However, note that the
310 integer value specified is internally shifted to capture the difference in the least significant
311 digit of the crfun values when these crfun values are fractional.
312 For example:
313 For crfun = 1.23e-02 & delta = 1 will be transformed to 0.0001
314 For crfun = 2.45e-01 & delta = 5 will be transformed to 0.005
315 The default delta value when the crfun values are fractional is 1.
316  
317 However if the crfun values are integer/whole numbers (exponent >= 2) then the specified delta
318 value is internally shifted only until the least significant digit in the scientific notation.
319 For example:
320 For crfun = 1.23e+04 & delta = 2 will be transformed to 200
321 For crfun = 2.45e+02 & delta = 5 will be transformed to 5
322 For crfun = 1.44e+03 & delta = 1 will be transformed to 10
323  
324 =head4 --threspk1 NUM
325  
326 Specifies the threshold value that should be used by the PK1 measure to predict the k value.
327 Default = -0.7
328  
329 NOTE: This option should be used only when --cluststop option is also used
330 with option value of "all" or "pk1".
331  
332 =head3 CLUSTER-STOPPING: ADAPTED GAP STATISTIC OPTIONS:
333  
334 =head4 --B NUM
335  
336 The number of replicates/references to be generated.
337 Default: 1
338  
339 =head4 --typeref TYP
340  
341 Specifies whether to generate B replicates from a reference or to generate
342 B references.
343  
344 The possible option values:
345 rep - replicates [Default]
346 ref - references
347  
348 =head4 --percentage NUM
349  
350 Specifies the percentage confidence to be reported in the log file.
351 Since Gap Statistic uses parametric bootstrap method for reference distribution
352 generation, it is critical to understand the interval around the sample mean that
353 could contain the population ("true") mean and with what certainty.
354 Default: 90
355  
356 =head4 --seed NUM
357  
358 The seed to be used with the random number generator.
359 Default: No seed is set.
360  
361 =head3 CLUSTERING OPTIONS :
362  
363 =head4 --clusters N
364  
365 Specifies number of clusters to be created. Default is set to 2.
366  
367 =head4 --space SPACE
368  
369 Specifies whether clustering is to be performed in vector or similarity space.
370 Set the value of SPACE to 'vector' to perform clustering in vector space i.e.
371 to cluster the context vectors directly. To cluster in similarity space
372 by explicitly finding the pair-wise similarities among the contexts,
373 set SPACE to 'similarity'.
374  
375 By default, clustering is performed in vector space.
376  
377 =head4 --clmethod CL
378  
379 Specifies the clustering method.
380  
381 Possible option values are :
382  
383 rb - Repeated Bisections [Default]
384 rbr - Repeated Bisections for by k-way refinement
385 direct - Direct k-way clustering
386 agglo - Agglomerative clustering
387 graph - Graph partitioning-based clustering
388 bagglo - Partitional biased Agglomerative clustering
389  
390 For large amount of data, 'rb', 'rbr' or 'direct' are recommended.
391  
392 =head4 --crfun CR
393  
394 Selects the criteria function for Clustering. The meanings of these criteria
395 functions are explained in Cluto's manual.
396  
397 The possible values are:
398  
399 i1 - I1 Criterion function
400 i2 - I2 Criterion function [default for partitional]
401 e1 - E1 Criterion function
402 g1 - G1 Criterion function
403 g1p - G1' Criterion function
404 h1 - H1 Criterion function
405 h2 - H2 Criterion function
406 slink - Single link merging scheme
407 wslink - Single link merging scheme weighted w.r.t. cluster sim
408 clink - Complete link merging scheme
409 wclink - Complete link merging scheme weighted w.r.t. cluster sim
410 upgma - Group average merging scheme [default for agglomerative]
411  
412 Note that for cluster stopping, i1, i2, e1, h1 and h2 criterion functions
413 can only be used. If a crfun other than these is selected then cluster
414 stopping uses the default crfun (i2) while the final clustering of contexts
415 is performed using the crfun specified.
416  
417 =head4 --sim SIM
418  
419 Specifies the similarity measure to be used for either vector or similarity
420 space clustering.
421  
422 When --space = vector (or default), possible values of SIM are :
423  
424 cos - Cosine [default]
425 corr - Correlation Coefficient
426 dist - Euclidean distance
427 jacc - Extended Jaccard Coefficient
428  
429 When --space = similarity and --binary is ON, possible values of SIM are -
430  
431 cos - Cosine [default]
432 mat - Match
433 jac - Jaccard
434 ovr - Overlap
435 dic - Dice
436  
437 Otherwise, only cosine measure is available and is default.
438  
439 The following table summarizes availability of similarity measures
440 for 2 clustering approaches - vector(vcl) and similarity(scl) and
441 on 2 different types of context vectors - binary Vs frequency
442  
443 vcl+bin vcl+freq scl+bin scl+freq
444 cos Y Y Y Y
445 mat N N Y N
446 jacc Y Y Y N
447 dice N N Y N
448 ovr N N Y N
449 dist Y Y N N
450 corr Y Y N N
451  
452 The reasons are purely implementation issues and in future, we plan to support
453 more consistent measures across these combinations.
454  
455 =head4 --rowmodel RMOD
456  
457 The option is used to specify the model to be used to scale every
458 column of each row. (For further details please refer Cluto manual)
459  
460 The possible values for RMOD -
461 none - no scaling is performed (default setting)
462 maxtf - post scaling the values are between 0.5 and 1.0
463 sqrt - square-root of actual values
464 log - log of actual values
465  
466 =head4 --colmodel CMOD
467  
468 The option is used to specify the model to be used to (globally) scale each
469 column across all rows. (For further details please refer Cluto manual)
470  
471 The possible values for CMOD -
472 none - no scaling is performed (default setting)
473 idf - scaling according to inverse-document-frequency
474  
475 =head3 LABELING OPTIONS :
476  
477 Note: Labeling options cannot be used with word-clustering (--wordclust).
478  
479 =head4 --label_stop LABEL_STOPFILE
480  
481 A file of Perl regexes that define the stop list of words to be
482 excluded from the features.
483  
484 LABEL_STOPFILE could be specified with two modes -
485  
486 AND mode - declared by including '@stop.mode=AND' on the first line of the
487 LABEL_STOPFILE
488 - ignores word pairs in which both words are stop words.
489  
490 OR mode - declared by including '@stop.mode=OR' on the first line of the
491 LABEL_STOPFILE
492 - ignores word pairs in which either word is a stop word.
493  
494 Default is OR.
495  
496 =head4 --label_ngram LABEL_NGRAM
497  
498 Specifies the value of n in 'n-gram' for the feature selection.
499 The supported values for n are 2, 3 and 4.
500  
501 Default value is 2 i.e. bigram.
502  
503 =head4 --label_remove LABEL_N
504  
505 Removes ngrams that occur less than LABEL_N times.
506  
507 =head4 --label_window LABEL_W
508  
509 Specifies the window size for bigrams. Pairs of words that co-occur
510 within the specified window from each other (window LABEL_W allows at most
511 LABEL_W-2 intervening words) will form the bigram features.
512 Default window size is 2 which allows only consecutive word pairs.
513  
514 =head4 --label_stat LABEL_STAT
515  
516 Specifies the statistical scores of association.
517  
518 Available tests of association are :
519  
520 dice - Dice Coefficient
521 ll - Log Likelihood Ratio
522 odds - Odds Ratio
523 phi - Phi Coefficient
524 pmi - Point-Wise Mutual Information
525 tmi - True Mutual Information
526 x2 - Chi-Squared Test
527 tscore - T-Score
528 leftFisher - Left Fisher's Test
529 rightFisher - Right Fisher's Test
530  
531 =head4 --label_rank LABEL_R
532  
533 Word pairs ranking below LABEL_R when arranged in descending order of
534 their test scores are ignored.
535  
536 =head3 Other Options :
537  
538 =head4 --eval
539  
540 Evaluates clustering performance by computing precision and recall for maximally
541 accurate assignment of sense tags to clusters. Maximal Assignment is when
542 clusters are given sense labels such that maximum number of instances will be
543 attached with their true sense tags.
544  
545 TEST instances tagged with multiple senses are automatically attached with the
546 single sense-tag that is the most frequent among the attached tags.
547  
548 Note: This option can be used only if the answer tags are provided in the TEST file.
549  
550 =head4 --rank_filter R
551  
552 Allows to remove low frequency senses during evaluation. This will
553 remove the senses that rank below R when senses in TEST are arranged
554 in the descending order of their frequencies. In other words, it
555 selects top R most frequent senses. An instance will be removed if
556 it has all sense tags below rank R.
557  
558 =head4 --percent_filter P
559  
560 Allows to remove low frequency senses based on their percentage
561 frequencies. This will remove senses whose frequency is below P%
562 in the TEST data.
563  
564 If rank or percent filters are specified, they are applied after removing
565 the multiple sense tags.
566  
567 =head4 --help
568  
569 Displays the quick summary of program options.
570  
571 =head4 --version
572  
573 Displays the version information.
574  
575 =head4 --verbose
576  
577 Displays to STDERR the current program status.
578  
579 =head4 --showargs
580  
581 Displays to STDOUT values of compulsory and required parameters.
582 [NOT SUPPORTED IN THIS VERSION]
583  
584 =head1 OUTPUT
585  
586 discriminate.pl creates several output files. The discrimination of contexts
587 performed by discriminate.pl, (i.e., a cluster assigned to each context) is given
588 by the file $PREFIX.clusters if the number of clusters was set manually, otherwise
589 by the file $PREFIX.clusters.$CLUSTSTOP where the $CLUSTSTOP specifies the cluster
590 stopping measure that was used to predict the number of clusters.
591  
592 In addition, discriminate.pl also creates following files:
593  
594 NOTE: If a cluster stopping measure was used then it is indicated in the names of
595 several output files by appending the cluster stopping measure name with the
596 file name. Represented below as filename[.$CLUSTSTOP]
597  
598 =over
599  
600 =item * $PREFIX.clusters_context[.$CLUSTSTOP] - File containing all the input instances grouped by the cluster-id assigned to them.
601  
602 =item * $PREFIX[.$CLUSTSTOP].cluster.CLUSTERID - All the identified clusters and their instances are separated into different files. The filenames end with the cluster-id. e.g.: File containing instances of cluster 0 will be named as $PREFIX.cluster.0
603  
604 =item * $PREFIX.report[.$CLUSTSTOP] - Confusion table if --eval is ON
605  
606 =item * $PREFIX.cluster_labels[.$CLUSTSTOP] - List of labels (word-pairs) assigned to each cluster.
607  
608 =item * $PREFIX[.$CLUSTSTOP].dendogram.ps - Dendograms + some information.
609  
610 =item * $PREFIX.features - Features file
611  
612 =item * $PREFIX.regex - File containing regular expressions for identifying
613 the features listed in $PREFIX.features file.
614  
615 =item * $PREFIX.testregex - File containing only those regular expressions from
616 the $PREFIX.regex file above, which match at least once in the test contexts,
617 only created in second order context clustering mode (SC native as well as LSA)
618 and LSA feature clustering mode
619  
620 =item * $PREFIX.wordvec - Word Vectors if --context = o2
621  
622 =item * $PREFIX.vectors - Context Vectors
623  
624 =item * $PREFIX.rlabel - Row Labels of $PREFIX.vectors
625  
626 =item * $PREFIX.clabel - Column Labels of $PREFIX.vectors
627  
628 =item * $PREFIX.rclass - Class Ids of $PREFIX.vectors if --eval is ON
629  
630 =item * $PREFIX.cluster_solution[.$CLUSTSTOP] - Cluster ids of $PREFIX.vectors
631  
632 =item * $PREFIX.cluster_output[.$CLUSTSTOP] - Clustering program output
633  
634 =back
635  
636 =head3 Cluster Stopping related output files:
637  
638 =over
639  
640 =item * $PREFIX.pk1 - crfun[k] values, delta values, PK1[k] values and predicted k value
641  
642 =item * $PREFIX.pk2 - crfun[k] values, delta values, PK2[k] values and predicted k value
643  
644 =item * $PREFIX.pk3 - crfun[k] values, delta values, PK3[k] values and predicted k value
645  
646 =item * $PREFIX.gap - crfun[k] values, delta values and predicted k value
647  
648 =item * $PREFIX.gap.log - Gap(k), Obs(crfun(k)), Exp(crfun(k)) values etc.
649  
650 =back
651  
652 =head3 The following files are created to facilitate creation of plots, if needed:
653  
654 =over
655  
656 =item * $PREFIX.cr.dat - value-pairs :- k-value crfun-value
657  
658 =item * $PREFIX.pk1.dat - value-pairs :- k-value PK1[k] value
659  
660 =item * $PREFIX.pk2.dat - value-pairs :- k-value PK2[k] value
661  
662 =item * $PREFIX.pk3.dat - value-pairs :- k-value PK3[k] value
663  
664 =item * $PREFIX.gap.dat - value-pairs :- k-value Gap[k] value
665  
666 =item * $PREFIX.exp.dat - value-pairs :- k-value Exp(crfun[k]) value
667  
668 =back
669  
670 =head1 AUTHORS
671  
672 Ted Pedersen, University of Minnesota, Duluth
673 tpederse at d.umn.edu
674  
675 Amruta Purandare, University of Pittsburgh
676  
677 Anagha Kulkarni, Carnegie-Mellon University
678  
679 Mahesh Joshi, Carnegie-Mellon Unversity
680  
681 =head1 COPYRIGHT
682  
683 Copyright (c) 2002-2008, Ted Pedersen, Amruta Purandare, Anagha Kulkarni, Mahesh Joshi
684  
685 This program is free software; you can redistribute it and/or modify it under
686 the terms of the GNU General Public License as published by the Free Software
687 Foundation; either version 2 of the License, or (at your option) any later
688 version.
689  
690 This program is distributed in the hope that it will be useful, but WITHOUT
691 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
692 FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
693  
694 You should have received a copy of the GNU General Public License along with
695 this program; if not, write to
696  
697 The Free Software Foundation, Inc.,
698 59 Temple Place - Suite 330,
699 Boston, MA 02111-1307, USA.
700  
701 =cut
702  
703 ###############################################################################
704  
705 # THE CODE STARTS HERE
706  
707 #$0 contains the program name along with
708 #the complete path. Extract just the program
709 #name and use in error messages
710 $0=~s/.*\/(.+)/$1/;
711  
712 ###############################################################################
713  
714 # ================================
715 # COMMAND LINE OPTIONS AND USAGE
716 # ================================
717  
718 use Math::SparseMatrix;
719  
720 # use the following perl module for command line options parsing
721 # Do not allow abbreviations of options i.e. options have to be spelled out completely.
722 use Getopt::Long qw(:config no_auto_abbrev);
723  
724 # command line options
725 # catch, abort and print the message for unknown options specified
726 eval(GetOptions ("help","version","training=s","token=s","target=s","stop=s","feature=s","remove=i","window=i","scope_train=i","scope_test=i","stat=s","stat_rank=i","stat_score=f","context=s","binary","svd","k=i","rf=i","iter=i","clusters=i","space=s","clmethod=s","crfun=s","sim=s","eval","verbose","showargs","prefix=s","format=s","rank_filter=i","percent_filter=f","label_ngram=n","label_window=i","label_stop=s","label_remove=i","label_stat=s","label_rank=i","wordclust","split=i","rowmodel=s","colmodel=s","cluststop=s","threspk1=f","delta=i","B=i","typeref=s","percentage=i","seed=i", "lsa")) or die("Please check the above mentioned option(s).\n");
727  
728 # show help option
729 if(defined $opt_help)
730 {
731 $opt_help=1;
732 &showhelp();
733 exit;
734 }
735  
736 # show version information
737 if(defined $opt_version)
738 {
739 $opt_version=1;
740 &showversion();
741 exit;
742 }
743  
744 # show minimal usage message if no arguments
745 if($#ARGV<0)
746 {
747 &showminimal();
748 exit 1;
749 }
750  
751 #############################################################################
752  
753 # ================================
754 # INITIALIZATION AND INPUT
755 # ================================
756  
757 # Note on ERROR message conventions - error and warning messages from
758 # discriminate.pl should go to STDERR, and should be intended 1 tab.
759 # Error messages from Toolkit programs should be indented 2 tabs.
760 # TDP August, 2006
761  
762 # ----------
763 # Testfile
764 # ----------
765 if(!defined $ARGV[0])
766 {
767 print STDERR "ERROR($0):
768 Please specify the TEST file name...\n";
769 exit 1;
770 }
771 $testfile=$ARGV[0];
772 if(!-e $testfile)
773 {
774 print STDERR "ERROR($0):
775 Could not locate the TEST file <$testfile>\n";
776 exit 1;
777 }
778  
779 # ---------------
780 # Tokenfile
781 # ---------------
782  
783 if(defined $opt_token)
784 {
785 $token=$opt_token;
786 }
787 else
788 {
789 $token="token.regex";
790 }
791  
792 if(!-e $token)
793 {
794 print STDERR "ERROR($0):
795 Could not locate the TOKEN file <$token>\n";
796 exit 1;
797 }
798 elsif(-z $token)
799 {
800 print STDERR "ERROR($0):
801 TOKEN file <$token> is empty.\n";
802 exit 1;
803 }
804  
805 # ---------------
806 # Targetfile
807 # ---------------
808  
809 my $target = "";
810  
811 if(defined $opt_target)
812 {
813 $target=$opt_target;
814 if(!-e $target)
815 {
816 print STDERR "ERROR($0):
817 Could not locate the TARGET file <$target>\n";
818 exit 1;
819 }
820 }
821 else
822 {
823 $target="target.regex";
824 # this will automatically create the target.regex file
825 # in the current dir.
826 if(!-e $target)
827 {
828 $status=system("maketarget.pl -head $testfile");
829 die "Error while running maketarget.pl on <$testfile>\n" unless $status==0;
830 }
831 }
832  
833 # --------------
834 # Prefix
835 # --------------
836  
837 if(defined $opt_prefix)
838 {
839 $prefix=$opt_prefix;
840 }
841 else
842 {
843 $prefix="expr" . time();
844 }
845  
846 # --------------
847 # Format
848 # --------------
849  
850 if(defined $opt_format)
851 {
852 if ($opt_format !~/^(f16.\d\d)/)
853 {
854 print STDERR "ERROR($0):
855 --format must be of the form f16.XX, where 0 <= XX < 16,
856 not $opt_format\n";
857 exit 1;
858 }
859 else
860 {
861 $format=$opt_format; ## format is defined, has valid form
862 $format =~ /^f16.(\d\d)/;
863 $prec = $1; # precision
864 }
865 }
866 else
867 {
868 $format = "f16.06"; ## format is not defined, use default
869 $prec = 6;
870 }
871  
872 # --------------
873 # SVD options
874 # --------------
875  
876 if(!defined $opt_k)
877 {
878 $opt_k=300;
879 }
880 if(!defined $opt_rf)
881 {
882 $opt_rf=10;
883 }
884  
885 # default feature
886 if(!defined $opt_feature)
887 {
888 $opt_feature = "bi";
889 }
890  
891 # initialize the variable for default number of clusters
892 $default_clusters = 2;
893  
894 # --------------
895 # Error checks
896 # --------------
897  
898 if(defined $opt_space)
899 {
900 if($opt_space !~/^(vector|simil)/)
901 {
902 print STDERR "ERROR($0):
903 --space should be either 'vector' or 'similarity'.\n";
904 exit 1;
905 }
906 }
907  
908 if($opt_feature !~/^(bi(gram)?|co(occur|c)?|uni(gram)?|tco(occur|c)?)/)
909 {
910 print STDERR "ERROR($0):
911 Specified Feature type --$opt_feature is not supported.\n";
912 exit 1;
913 }
914  
915 if($opt_feature=~/^uni(gram)?/ && !defined $opt_lsa && (!defined $opt_context || $opt_context =~ /o2|order2/))
916 {
917 print STDERR "ERROR($0):
918 --feature cannot be $opt_feature when --context is o2,
919 unless --lsa is also specified\n";
920 exit 1;
921 }
922  
923 if(defined $opt_split && ($opt_split >=100 || $opt_split <= 0))
924 {
925 print STDERR "ERROR($0):
926 The N value for the --split option should be between 1 to 99\n";
927 exit 1;
928 }
929  
930  
931 # Option validations for Word Clustering and headless input data.
932  
933 # Find the type (headed/headless) of the Test and Train data
934 # and then handle the following cases:
935 # Case 1: train headless / test headed
936 # Case 2: train headless / test headless
937 # Case 3: train headed / test headed
938 # Case 4: train headed / test headless
939  
940 my $TestType = 0; # By default headed
941 my $TrainType = 0; # By default headed
942  
943 # check the Test data for <head> tag
944 open (INP,$testfile) || die "Error($0):
945 Error(code=$!) in opening <$testfile> file.\n";
946  
947 # read the complete file in single instruction instead of reading line by line.
948 my $temp_delimiter = $/;
949 $/ = undef;
950  
951 my $inp_str = <INP>;
952  
953 $/ = $temp_delimiter;
954 close INP;
955
956 # If the --eval option specified Then check if answer tags present
957 if(defined $opt_eval)
958 {
959 if($inp_str !~ m/<answer/)
960 {
961 print STDERR "ERROR($0):
962 The --eval option cannot be used with unlabeled data.
963 In other words, the experiment cannot be evaluated if
964 the input Senseval2 file does not contain the answer tags.\n";
965 exit 1;
966 }
967 }
968  
969 if($inp_str !~ m/<head>.+<\/head>/i)
970 {
971 $TestType = 1; # headless
972 }
973  
974 # when separate training data specified
975 if(defined $opt_training)
976 {
977 # training data cannot be provided word clustering.
978 if(defined $opt_wordclust)
979 {
980 if (defined $opt_lsa)
981 {
982 print STDERR "ERROR($0):
983 --training option cannot be used with feature-clustering.\n";
984 }
985 else
986 {
987 print STDERR "ERROR($0):
988 --training option cannot be used with word-clustering.\n";
989 }
990 exit 1;
991 }
992  
993 # check if the training file exists
994 if(!-e $opt_training)
995 {
996 print STDERR "ERROR($0):
997 Could not locate the TRAIN file <$opt_training>\n";
998 exit 1;
999 }
1000  
1001 # check if the training file is a text file.
1002 if(!-T $opt_training)
1003 {
1004 print STDERR "ERROR($0):
1005 Training file has to be a plain text file.
1006 The provided file is not a text file. \n";
1007 exit 1;
1008 }
1009
1010 open (INP,$opt_training) || die "Error($0):
1011 Error(code=$!) in opening <$opt_training> file.\n";
1012
1013 # read the complete file in single instruction instead of reading line by line.
1014 my $temp_delimiter = $/;
1015 $/ = undef;
1016
1017 my $inp_str = <INP>;
1018  
1019 $/ = $temp_delimiter;
1020 close INP;
1021  
1022 # check if the training file is senseval2 formatted file - if yes quit.
1023 if($inp_str =~ m/<corpus/i && $inp_str =~ m/<lexelt/i && $inp_str =~ m/<instance/i && $inp_str =~ m/<context/i)
1024 {
1025 print STDERR "ERROR($0):
1026 Training file has to be a plain text file.
1027 The provided file is a Senseval2 formatted file. \n";
1028 exit 1;
1029 }
1030  
1031 # check if the training file is html formatted file - if yes quit.
1032 if($inp_str =~ m/<html/i)
1033 {
1034 print STDERR "ERROR($0):
1035 Training file has to be a plain text file.
1036 The provided file is a html formatted file. \n";
1037 exit 1;
1038 }
1039  
1040 # check if the training file is xml formatted file - if yes quit.
1041 if($inp_str =~ m/<\?xml/i)
1042 {
1043 print STDERR "ERROR($0):
1044 Training file has to be a plain text file.
1045 The provided file is an xml formatted file. \n";
1046 exit 1;
1047 }
1048  
1049 # checks the Train data for <head> tag
1050 if($inp_str !~ m/<head>.+<\/head>/i)
1051 {
1052 $TrainType = 1;
1053 }
1054 }
1055 else # Test data to be used as Train data thus $TrainType = $TestType
1056 {
1057 $TrainType = $TestType;
1058 }
1059  
1060 # scope cannot be used with headless training data
1061 if (defined $opt_scope_train && $TrainType == 1) {
1062 print STDERR "ERROR($0):
1063 --scope_train option cannot be used
1064 when the Train data is headless.\n";
1065 exit 1;
1066 }
1067  
1068 # scope cannot be used with headless test data, or when word clustering
1069 # is requested
1070 if (defined $opt_scope_test && ($TestType == 1 || defined $opt_wordclust)) {
1071 print STDERR "ERROR($0):
1072 --scope_test option cannot be used when the Test data
1073 is headless or when word clustering is requested.\n";
1074 exit 1;
1075 }
1076  
1077 # word-clustering is treated as headless type of clustering thus
1078 # 1. check for target co-occurrence
1079 # 2. target option
1080 if(defined $opt_wordclust)
1081 {
1082 # we do no allow tco as the feature type, headed data is allowed but
1083 # <head>...</head> is simply a normal token in this case
1084 if(defined $opt_feature && $opt_feature eq "tco")
1085 {
1086 print STDERR "ERROR($0):
1087 target co-occurrences (tco) cannot be used as the feature
1088 type with word-clustering.\n";
1089 exit 1;
1090 }
1091  
1092 # headless case which cannot allow target file option
1093 if(defined $opt_target)
1094 {
1095 print STDERR "ERROR($0):
1096 --target option cannot be used with word-clustering.\n";
1097 exit 1;
1098 }
1099 }
1100  
1101 # --lsa cannot be used in o1 context representation, unless word clustering is specified
1102 if (defined $opt_lsa) {
1103 if($opt_context =~ /o1|order1/ && !defined $opt_wordclust) {
1104 print STDERR "ERROR($0):
1105 --lsa option cannot be used with --context o2
1106 without specifying the --wordclust option\n";
1107 exit 1;
1108 }
1109 if((!defined $opt_context || $opt_context =~ /o2|order2/) && defined $opt_wordclust) {
1110 print STDERR "ERROR($0):
1111 --lsa option can be used either with \"--context o2\"
1112 (the default) or with \"--context o1 --wordclust\" options,
1113 but not with \"--context o2 --wordclust\".\n";
1114 exit 1;
1115 }
1116 }
1117  
1118 # Case 1: train headless / test headed
1119 if($TrainType == 1 && $TestType == 0)
1120 {
1121 # headed case which cannot allow tco as the feature type
1122 if(defined $opt_feature && $opt_feature eq "tco")
1123 {
1124 print STDERR "ERROR($0):
1125 target co-occurrences (tco) cannot be used as the feature
1126 type when the Test/Train data is headless.\n";
1127 exit 1;
1128 }
1129 }
1130  
1131 # Case 2: train headless / test headless And
1132 # Case 4: train headed / test headless
1133 if(($TrainType == 1 && $TestType == 1) || ($TrainType == 0 && $TestType == 1))
1134 {
1135 # headless case which cannot allow target file option
1136 if(defined $opt_target)
1137 {
1138 print STDERR "ERROR($0):
1139 --target option cannot be used with headless clustering.\n";
1140 exit 1;
1141 }
1142  
1143 # headless case which cannot allow tco as the feature type
1144 if(defined $opt_feature && $opt_feature eq "tco")
1145 {
1146 print STDERR "ERROR($0):
1147 target co-occurrences (tco) cannot be used as the feature type
1148 when the Test/Train data is headless.\n";
1149 exit 1;
1150 }
1151 }
1152  
1153 # Case 3: train headed / test headed
1154 # No Special error checks required
1155  
1156 # Check if Test and Train specified by user and --split option is also used
1157 if(defined $opt_training && defined $opt_split)
1158 {
1159 print STDERR "ERROR($0):
1160 Cannot use --split option to split the input data
1161 into Test and Train portions if separate Training data
1162 (--training) is alredy specified.\n";
1163 exit 1;
1164 }
1165  
1166 # if space is vector and clmethod is graph then only can
1167 # jacc and dist similarity measures be used.
1168 if((!defined $opt_space || $opt_space eq "vector") && (!defined $opt_clmethod || $opt_clmethod ne "graph") && defined $opt_sim && ($opt_sim eq "dist" || $opt_sim eq "jacc"))
1169 {
1170 print STDERR "ERROR($0):
1171 Similarity Measures (--sim) Euclidean distance and Jaccard can
1172 only be used if Clustering Method(--clmethod graph) is Graph
1173 and Clustering Space (--space vector) is Vector.\n";
1174 exit 1;
1175 }
1176 if(defined $opt_space && $opt_space eq "similarity" && !defined $opt_binary && defined $opt_sim && $opt_sim ne "cos")
1177 {
1178 print STDERR "ERROR($0):
1179 Only Cosine Similarity Measure (--sim cos) is a valid option
1180 if Clustering space is similarity (--space similarity)
1181 and --binary option is not ON.\n";
1182 exit 1;
1183 }
1184 if(defined $opt_space && $opt_space eq "similarity" && defined $opt_clmethod && $opt_clmethod eq "bagglo")
1185 {
1186 print STDERR "ERROR($0):
1187 Partitional biased Agglomerative clustering (--clmethod bagglo)
1188 available only for vector space.\n";
1189 exit 1;
1190 }
1191 if(defined $opt_clmethod && $opt_clmethod ne "agglo" && defined $opt_crfun && ($opt_crfun eq "slink" || $opt_crfun eq "wslink" || $opt_crfun eq "clink" || $opt_crfun eq "wclink" || $opt_crfun eq "upgma"))
1192 {
1193 print STDERR "ERROR($0):
1194 $opt_crfun Criterion Function (--crfun $opt_crfun) valid only if
1195 Clustering Method is agglomerative (--clmethod agglo). \n";
1196 exit 1;
1197 }
1198 # Error Checks for the rowmodel and colmodel options of Cluto
1199 if(defined $opt_rowmodel && $opt_rowmodel !~/^(none|maxtf|sqrt|log)$/)
1200 {
1201 print STDERR "ERROR($0):
1202 Specified rowmodel value: $opt_rowmodel is not supported.\n";
1203 exit 1;
1204 }
1205 if(defined $opt_space && $opt_space eq "similarity" && defined $opt_rowmodel)
1206 {
1207 print STDERR "ERROR($0):
1208 --rowmodel option can be used only in vector space. \n";
1209 exit 1;
1210 }
1211 if(defined $opt_colmodel && $opt_colmodel !~/^(none|idf)$/)
1212 {
1213 print STDERR "ERROR($0):
1214 Specified colmodel value: $opt_colmodel is not supported.\n";
1215 exit 1;
1216 }
1217 if(defined $opt_space && $opt_space eq "similarity" && defined $opt_colmodel)
1218 {
1219 print STDERR "ERROR($0):
1220 --colmodel option can be used only in vector space. \n";
1221 exit 1;
1222 }
1223  
1224 # cluster stopping related initializations and error checks
1225 # if neither #clusters nor cluster-stopping measure specified
1226 if(!defined $opt_clusters && !defined $opt_cluststop)
1227 {
1228 $opt_clusters = $default_clusters;
1229 }
1230  
1231 if(defined $opt_clusters && defined $opt_cluststop)
1232 {
1233 print STDERR "ERROR($0):
1234 --clusters and --cluststop options cannot be used together. \n";
1235 exit 1;
1236 }
1237  
1238 if(defined $opt_cluststop && $opt_cluststop !~ /^(all|pk|pk1|pk2|pk3|gap)$/i)
1239 {
1240 print STDERR "ERROR($0):
1241 $opt_cluststop not a valid option value for --cluststop. \n";
1242 exit 1;
1243 }
1244  
1245 if(!defined $opt_cluststop && defined $opt_threspk1)
1246 {
1247 print STDERR "ERROR($0):
1248 --threspk1 option can be used only when using --cluststop option. \n";
1249 exit 1;
1250 }
1251  
1252 if(!defined $opt_cluststop && defined $opt_delta)
1253 {
1254 print STDERR "ERROR($0):
1255 --delta option can be used only when using --cluststop option. \n";
1256 exit 1;
1257 }
1258  
1259 if(defined $opt_typeref && $opt_typeref !~ /^(rep|ref)$/i)
1260 {
1261 print STDERR "ERROR($0):
1262 $opt_typeref not a valid option value for --typeref. \n";
1263 exit 1;
1264 }
1265  
1266 if(defined $opt_percentage && ($opt_percentage < 0 || $opt_percentage > 100))
1267 {
1268 print STDERR "ERROR($0):
1269 The value for --percentage must be in the range [0,100] (inclusive).\n";
1270 exit 1;
1271 }
1272  
1273 ##############################################################################
1274  
1275 # -------------------------
1276 # Preprocessing
1277 # -------------------------
1278  
1279 if(defined $opt_verbose)
1280 {
1281 print STDERR "Preprocessing the input data ...\n";
1282 }
1283  
1284 # if TEST contains actual sense tags,
1285 # filter TEST to remove multiple
1286 # senses / instance
1287 if(defined $opt_eval)
1288 {
1289 # removing multiple senses of TEST instances
1290  
1291 $test_report="$prefix.test_report";
1292 $status=system("frequency.pl $testfile > $test_report");
1293 die "Error while running frequency.pl on <$testfile>\n" unless $status==0;
1294  
1295 $status=system("filter.pl --percent 0 --nomulti $testfile $test_report > $testfile.nomulti");
1296 die "Error while running filter.pl on <$testfile>\n" unless $status==0;
1297  
1298 # applying filters now
1299 if(defined $opt_rank_filter || defined $opt_percent_filter)
1300 {
1301 if(defined $opt_verbose)
1302 {
1303 print STDERR "Removing Low Frequency Senses from TEST ...\n";
1304 }
1305 if(defined $opt_rank_filter)
1306 {
1307 $filter_string="--rank $opt_rank_filter ";
1308 }
1309 else
1310 {
1311 $filter_string="--percent $opt_percent_filter ";
1312 }
1313 $status=system("filter.pl $filter_string $testfile.nomulti $test_report > $testfile.filtered");
1314 die "Error while running filter.pl on <$testfile.nomulti>\n" unless $status==0;
1315 $testfile="$testfile.filtered";
1316 }
1317 else
1318 {
1319 $testfile="$testfile.nomulti";
1320 }
1321 }
1322  
1323 if(defined $opt_training)
1324 {
1325 $train_plain=$opt_training;
1326  
1327 $tmp_testfile = "$testfile.pro";
1328  
1329 $status = system("preprocess.pl --token $token --removeNotToken --xml $tmp_testfile --nocount $testfile");
1330 die "Error in running preprocess.pl on <$testfile>\n" unless $status==0;
1331 $testfile = $tmp_testfile;
1332 }
1333 else
1334 {
1335 if(defined $opt_split)
1336 {
1337 # convert test in sval2 to plain, process the test file and also split the data
1338 $train_plain="$prefix.train_plain";
1339 $tmp_testfile = "$testfile.pro";
1340
1341 $status = system("preprocess.pl --token $token --removeNotToken --xml $tmp_testfile --count $train_plain --split $opt_split $testfile");
1342 die "Error in running preprocess.pl on <$testfile>\n" unless $status==0;
1343  
1344 # delete the unnecessary file that get created by preprocessor.pl when used with the split option
1345 unlink "$tmp_testfile-training","$train_plain-test";
1346  
1347 # use the appropriate test and train file henceforth
1348 $testfile = "$tmp_testfile-test";
1349 $train_plain = "$train_plain-training";
1350  
1351 $train_sval2=$testfile;
1352 }
1353 else
1354 {
1355 # convert test in sval2 to plain and also clean the test file
1356 $train_plain="$prefix.train_plain";
1357 $tmp_testfile = "$testfile.pro";
1358
1359 $status = system("preprocess.pl --token $token --removeNotToken --xml $tmp_testfile --count $train_plain $testfile");
1360 die "Error in running preprocess.pl on <$testfile>\n" unless $status==0;
1361
1362 # use the clean test file henceforth
1363 $testfile = $tmp_testfile;
1364
1365 $train_sval2=$testfile;
1366 }
1367 }
1368  
1369 ############################################
1370 # Localizing the Context Scope in Training
1371 ############################################
1372  
1373 if(defined $opt_scope_train)
1374 {
1375 if(defined $opt_verbose)
1376 {
1377 print STDERR "Localizing the Context Scope in TRAIN ...\n";
1378 }
1379  
1380 if(!defined $train_sval2)
1381 {
1382 # converting training data to sval2 format
1383 $train_sval2="$prefix.train_sval2";
1384 $status=system("text2sval.pl $train_plain > $train_sval2");
1385 die "Could not run text2sval.pl on <$train_plain>\n" unless $status==0;
1386 }
1387 # running windower
1388 $train_context="$prefix.train_context";
1389 if(defined $opt_target)
1390 {
1391 $status=system("windower.pl --plain --target $target --token $token $train_sval2 $opt_scope_train > $train_context");
1392 die "Error while running windower.pl on <$train_sval2>\n" unless $status ==0;
1393 }
1394 else
1395 {
1396 $status=system("windower.pl --plain --token $token $train_sval2 $opt_scope_train > $train_context");
1397 die "Error while running windower.pl on <$train_sval2>\n" unless $status ==0;
1398 }
1399 $train=$train_context;
1400 }
1401 else
1402 {
1403 $train=$train_plain;
1404 }
1405  
1406 ######################
1407 # Selecting Features
1408 ######################
1409  
1410 if($opt_feature =~ /^uni(gram)?/)
1411 {
1412 if(defined $opt_verbose)
1413 {
1414 print STDERR "Computing Unigram Counts ...\n";
1415 }
1416 $unigrams="$prefix.unigrams";
1417 $count_string="";
1418  
1419 if(defined $opt_remove)
1420 {
1421 $count_string="--remove $opt_remove ";
1422 }
1423  
1424 if(defined $opt_stop)
1425 {
1426 $count_string.="--stop $opt_stop ";
1427 }
1428  
1429 $status=system("count.pl --ngram 1 --newLine --token $token $count_string $unigrams $train");
1430 die "Error while running count.pl with --ngram 1 on <$train>\n" unless $status==0;
1431  
1432 }
1433 else
1434 {
1435 ###########################
1436 # Computing Bigram Counts
1437 ###########################
1438  
1439 if(defined $opt_verbose)
1440 {
1441 print STDERR "Computing Bigram Counts ...\n";
1442 }
1443  
1444 $bigrams="$prefix.bigrams";
1445 $count_string="";
1446  
1447 if(defined $opt_remove)
1448 {
1449 $count_string="--remove $opt_remove ";
1450 }
1451 if(defined $opt_window)
1452 {
1453 $count_string.="--window $opt_window ";
1454 }
1455 if(defined $opt_stop)
1456 {
1457 $count_string.="--stop $opt_stop ";
1458 }
1459  
1460 $status=system("count.pl --extended --newLine --token $token $count_string $bigrams $train");
1461 die "Error while running count.pl on <$train>\n" unless $status==0;
1462  
1463 ###################
1464 # Combining Counts
1465 ###################
1466  
1467 if($opt_feature =~/^(co(occur|c)?|tco(occur|c)?)/)
1468 {
1469 if(defined $opt_verbose)
1470 {
1471 print STDERR "Combining Bigrams into Co-occurrence pairs ...\n";
1472 }
1473
1474 # check the number of bigram features present
1475 open (INP,"<$bigrams") || die "Error($0):
1476 Error(code=$!) in opening <$bigrams> file\n";
1477
1478 my $feat_cnt = 0;
1479 while(<INP>)
1480 {
1481 # skip the header
1482 if(/^@/)
1483 {
1484 next;
1485 }
1486  
1487 # capture the count
1488 if(/^(\d+)/)
1489 {
1490 $feat_cnt = $1;
1491 last;
1492 }
1493 }
1494
1495 if(!$feat_cnt)
1496 {
1497 if($opt_feature =~/^tco(occur|c)?/)
1498 {
1499 print STDERR "ERROR($0):
1500 0 FEATURES found in the <$bigrams> file.
1501 This will lead to 0 co-occurrence features and 0 target
1502 co-occurrence features. Therefore aborting the experiment.\n";
1503 }
1504 else
1505 {
1506 print STDERR "ERROR($0):
1507 0 FEATURES found in the <$bigrams> file.
1508 This will lead to 0 co-occurrence features. Therefore aborting
1509 the experiment.\n";
1510 }
1511  
1512 exit 1;
1513 }
1514  
1515 $pairs="$prefix.cocs";
1516 $status=system("combig.pl $bigrams > $pairs");
1517 die "Error while running combig.pl on <$bigrams>\n" unless $status==0;
1518
1519 if($opt_feature =~ /^tco(occur|c)?/) # target co-occurrences
1520 {
1521 if(defined $opt_verbose)
1522 {
1523 print STDERR "Finding Target Co-occurrences ...\n";
1524 }
1525
1526 # select the target co-occurrences from the *.cocs file
1527 $target_pairs = "$prefix.target_cocs";
1528
1529 open (INP,"<$pairs") || die "Error($0):
1530 Error(code=$!) in opening <$pairs> file\n";
1531
1532 open (OUT,">$target_pairs") || die "Error($0):
1533 Error(code=$!) in opening <$target_pairs> file.\n";
1534
1535 # select the word pairs with target word and write to a temp file
1536 # keep the count of number of such target word-pairs selected
1537
1538 # extract the total number of features from the cocs file
1539 # usually the first number in the file.
1540 $total_feat = 0;
1541 do
1542 {
1543 $sent = <INP>;
1544 if($sent =~ m/^\s*(\d+)\s*$/)
1545 {
1546 $total_feat = $1;
1547 }
1548 } until($total_feat != 0);
1549 # write the total number of features on the first line of the output file
1550 print OUT "$total_feat\n";
1551
1552 while(<INP>)
1553 {
1554 # find and write out the target co-occurrences to the output file
1555 if(m/<head>.+<\/head>/i)
1556 {
1557 print OUT;
1558 }
1559 }
1560 close INP;
1561 close OUT;
1562
1563 $pairs=$target_pairs;
1564 }
1565
1566 }
1567 else
1568 {
1569 $pairs=$bigrams;
1570 }
1571  
1572 ######################
1573 # Running Statistic
1574 ######################
1575  
1576 if(defined $opt_stat)
1577 {
1578 if(defined $opt_verbose)
1579 {
1580 print STDERR "Performing Statistics on Word Pairs ...\n";
1581 }
1582  
1583 $statistic="$prefix.statistic";
1584  
1585 $stat_string="";
1586 if(defined $opt_stat_rank)
1587 {
1588 $stat_string.="--rank $opt_stat_rank ";
1589 }
1590 if(defined $opt_stat_score)
1591 {
1592 $stat_string.="--score $opt_stat_score ";
1593 }
1594  
1595 # included statistic.pl's --precision option if format option specified
1596 $stat_string .= " --precision $prec ";
1597  
1598 $stat_string.="$opt_stat ";
1599  
1600 $status=system("statistic.pl $stat_string $statistic $pairs");
1601 die "Error while running statistic.pl on <$pairs>\n" unless $status ==0;
1602  
1603 $scores=$statistic;
1604 }
1605 else
1606 {
1607 $scores=$pairs;
1608 }
1609 }
1610  
1611 #############################
1612 # Creating Context Vectors
1613 #############################
1614  
1615 $vectors="$prefix.vectors";
1616  
1617 # -------------------------
1618 # defining context scope
1619 # -------------------------
1620  
1621 if(defined $opt_scope_test)
1622 {
1623 if(defined $opt_verbose)
1624 {
1625 print STDERR "Localizing the Context Scope in TEST ...\n";
1626 }
1627
1628 $test_context="$prefix.test_context";
1629 if(defined $opt_target)
1630 {
1631 $status=system("windower.pl --token $token --target $target $testfile $opt_scope_test > $test_context");
1632 die "Error while running windower.pl on <$testfile>\n" unless $status==0;
1633 }
1634 else
1635 {
1636 $status=system("windower.pl --token $token $testfile $opt_scope_test > $test_context");
1637 die "Error while running windower.pl on <$testfile>\n" unless $status==0;
1638 }
1639 }
1640 else
1641 {
1642 $test_context=$testfile;
1643 }
1644  
1645 $rlabel="$prefix.rlabel";
1646 if(defined $opt_eval)
1647 {
1648 $rclass="$prefix.rclass";
1649 $rclass_string="--rclass $rclass";
1650 }
1651 else
1652 {
1653 $rclass_string="";
1654 }
1655 $clabel="$prefix.clabel";
1656  
1657 # turned ON if svd defined and actually applied
1658 my $svd_flag = 0;
1659  
1660 # default context representation is order2
1661 if(!defined $opt_context || $opt_context =~/o2|order2/)
1662 {
1663
1664 # do not rename any feature file to .features file yet, since
1665 # wordvec.pl produces a new .features file
1666 # just decide for now which is the features file
1667 if ($opt_feature =~ /^uni(gram)?/) {
1668 $featuresfile = $unigrams;
1669 } else {
1670 $featuresfile = $scores;
1671 }
1672 # check if atleast 10 feature present in the features file.
1673 open(FEAT,$featuresfile) || die "Error($0):
1674 Error(code=$!) while opening the feature file <$featuresfile>\n";
1675  
1676 # read the complete file in single instruction instead of reading line by line.
1677 my $temp_delimiter = $/;
1678 $/ = undef;
1679
1680 my $inp_str = <FEAT>;
1681
1682 $/ = $temp_delimiter;
1683 close FEAT;
1684
1685 my $feat_cnt = 0;
1686 while($inp_str =~ m/<>.*\n/g && $feat_cnt < 10)
1687 {
1688 $feat_cnt++;
1689 }
1690  
1691 if($feat_cnt < 10)
1692 {
1693 print STDERR "ERROR($0):
1694 Only $feat_cnt FEATURES found in the <$scores> file.
1695 At least 10 FEATURES required to proceed with context
1696 representation.\n";
1697 exit 1;
1698 }
1699  
1700 if (defined $opt_lsa) {
1701 # we will get feature vectors from a feature-by-context matrix
1702 # the extension is maintained to be .wordvec to be
1703 # consistent with the web interface interpretation as of now
1704 $featvec="$prefix.wordvec";
1705 $features = "$prefix.features";
1706 # move the appropriate feature output file as the .features file
1707 if ($opt_feature =~ /^uni(gram)?/) {
1708 $status = system("mv $unigrams $features");
1709 die "Error while moving <$unigrams> file to <$features>\n" unless $status==0;
1710 } else {
1711 $status = system("mv $scores $features");
1712 die "Error while moving <$scores> file to <$features>\n" unless $status==0;
1713 }
1714  
1715 # -----------------------
1716 # finding feature regexs
1717 # -----------------------
1718  
1719 if(defined $opt_verbose)
1720 {
1721 print STDERR "Finding Feature Regex/s ...\n";
1722 }
1723  
1724 $feature_regex="$prefix.regex";
1725 $status=system("nsp2regex.pl $features > $feature_regex");
1726 die "Error while running nsp2regex.pl on <$features>\n" unless $status==0;
1727  
1728 if(defined $opt_verbose)
1729 {
1730 print STDERR "Building First Order Vectors for LSA...\n";
1731 }
1732  
1733 # we are doing context clustering in lsa fashion
1734 # binary requested
1735 if(defined $opt_binary)
1736 {
1737 $binary="--binary";
1738 }
1739 else
1740 {
1741 $binary="";
1742 }
1743  
1744 $o1_presvd="$prefix.o1_presvd";
1745 # do not generate the .rclass file and the .rlabel / .clabel files
1746 # generate the .testregex file which corresponds to the features
1747 # identified in the test data, this needs to be passed to
1748 # order2vec.pl later
1749 # Also specify --transpose option, for getting a feature-by-context
1750 # representation
1751 $testregex = "$prefix.testregex";
1752  
1753 $status=system("order1vec.pl --transpose --testregex $testregex $binary $test_context $feature_regex > $o1_presvd");
1754 die "Error while running order1vec.pl on <$test_context>\n" unless $status==0;
1755  
1756 # the keyfile produced by order1vec.pl should be removed, since later
1757 # order2vec.pl will create another one
1758 unlink <keyfile*.key>;
1759
1760 # set input file for svd
1761 $svdinput = $o1_presvd;
1762 # set an output file name for svd
1763 $postsvdvectors = $featvec;
1764
1765 } else {
1766 # we are doing either context clustering or word clustering in SC fashion
1767  
1768 if(defined $opt_verbose)
1769 {
1770 print STDERR "Building Word Vectors ...\n";
1771 }
1772  
1773 $wordvec="$prefix.wordvec";
1774 # creating word vectors from scores file
1775 $wordvec_presvd="$prefix.wordvec_presvd";
1776 $features = "$prefix.features";
1777 $dims="$prefix.dims";
1778  
1779 $wordvec_string="--feats $features --dims $dims ";
1780 if($opt_feature=~/^co(occur|c)?|tco(occur|c)?/)
1781 {
1782 $wordvec_string.="--wordorder nocare ";
1783 }
1784 else
1785 {
1786 $wordvec_string.="--wordorder follow ";
1787 }
1788 if(defined $opt_binary)
1789 {
1790 $wordvec_string.="--binary ";
1791 }
1792 $status=system("wordvec.pl --format $format $wordvec_string $scores > $wordvec_presvd");
1793 die "ERROR($0): Error while running wordvec.pl\n" unless $status==0;
1794  
1795 # set input file for svd
1796 $svdinput = $wordvec_presvd;
1797 # set an output file name for svd
1798 $postsvdvectors = $wordvec;
1799 }
1800  
1801 # SVD
1802 if(defined $opt_svd)
1803 {
1804 # Check if performing svd will reduce the number of features i.e. number of columns
1805 # less than or equal to 10, if so do not perform svd
1806  
1807 open(INSVD,$svdinput) || die "Error($0):
1808 Error(code=$!) in opening Matrix file <$svdinput>\n";
1809  
1810 # line1 in Matrix file should either show the
1811 # <keyfile> tag or #rows #cols #nnz
1812 $line1=<INSVD>;
1813  
1814 if($line1=~/keyfile/)
1815 {
1816 $line1=<IN>;
1817 }
1818  
1819 if($line1=~/^\s*(\d+)\s+(\d+)\s+(\d+)\s*$/)
1820 {
1821 $rows=$1;
1822 $cols=$2;
1823 $nnz1=$3;
1824 }
1825 else
1826 {
1827 print STDERR "ERROR($0):
1828 Line $line1 in Matrix file <$svdinput> should show #rows #cols #nnz\n";
1829 exit 1;
1830 }
1831
1832 close INSVD;
1833  
1834 $flag_svd = 0;
1835 $maxprs=$opt_k > ($cols/$opt_rf) ? int($cols/$opt_rf) : $opt_k;
1836 if($maxprs >= 10)
1837 {
1838 if(defined $opt_verbose)
1839 {
1840 print STDERR "Performing SVD ...\n";
1841 }
1842 $svd_flag = 1;
1843 # calling svd(input,output)
1844 svd($svdinput, $postsvdvectors);
1845 $flag_svd = 1;
1846 }
1847 else
1848 {
1849 print STDERR "WARNING($0):
1850 SVD could not be performed on SVDINPUT <$svdinput>
1851 because svd with reduction factor = $opt_k and scaling
1852 factor = $opt_rf would reduce the resultant number of
1853 features to = $maxprs, computed via (min($opt_k, $cols/$opt_rf)).
1854 The minimum number of features required for representing
1855 the contexts is 10\n";
1856  
1857 $status=system("mv $svdinput $postsvdvectors");
1858 die "Error while creating <$postsvdvectors> file.\n" unless $status==0;
1859 }
1860 }
1861 else
1862 {
1863 $status=system("mv $svdinput $postsvdvectors");
1864 die "Error while creating <$postsvdvectors> file.\n" unless $status==0;
1865 }
1866  
1867 # If word clustering (synonym finding) do not create context vectors but
1868 # instead pass the word vectors to the clustering stage.
1869  
1870 if(defined $opt_wordclust)
1871 {
1872 $status=system("mv $wordvec $vectors");
1873 die "Error while creating <$vectors> file.\n" unless $status==0;
1874  
1875 $status=system("mv $features $rlabel");
1876 die "Error while creating <$rlabel> file.\n" unless $status==0;
1877
1878 $status=system("mv $dims $clabel");
1879 die "Error while creating <$clabel>\n" unless $status==0;
1880 }
1881 else
1882 {
1883 # --------------------------
1884 # Creating Context Vectors
1885 # --------------------------
1886  
1887 if (!defined $opt_lsa) {
1888 # only in native SC order2 context clustering mode, generate
1889 # a regex file from the output of wordvec.pl. we don't do
1890 # this immediately after calling wordvec.pl above as that
1891 # will be unnecessarily created in SC word clustering mode
1892
1893 # generate a .testregex file from the $features file created by
1894 # wordvec.pl
1895 $testregex = "$prefix.testregex";
1896 $status=system("nsp2regex.pl $features > $testregex");
1897 die "Error while running nsp2regex.pl on <$features>\n" unless $status==0;
1898 }
1899  
1900 if(defined $opt_verbose)
1901 {
1902 print STDERR "Building 2nd Order Context Vectors ...\n";
1903 }
1904 $context_string="--rlabel $rlabel ";
1905 if(defined $opt_svd && $flag_svd == 1)
1906 {
1907 $context_string.="--dense ";
1908 }
1909 if(defined $opt_binary)
1910 {
1911 $context_string.="--binary ";
1912 }
1913 $status=system("order2vec.pl --format $format $context_string $rclass_string $test_context $postsvdvectors $testregex > $vectors");
1914 die "Error while running order2vec.pl on <$test_context>\n" unless $status==0;
1915 }
1916 }
1917 # requested context type is order1
1918 else
1919 {
1920 $features="$prefix.features";
1921 if($opt_feature=~/^uni(gram)?/)
1922 {
1923 $status=system("mv $unigrams $features");
1924 die "Error while creating Unigram Feature file <$features>\n" unless $status==0;
1925 }
1926 else
1927 {
1928 $status=system("mv $scores $features");
1929 die "Error while creating Bigram Feature file <$features>\n" unless $status==0;
1930 }
1931 # else # target co-occurrences
1932 # {
1933 # if(defined $opt_verbose)
1934 # {
1935 # print STDERR "Finding Target Co-occurrences ...\n";
1936 # }
1937 # # run kocos to find co-occurrences from scores file
1938 # $status=system("kocos.pl --order 1 --regex $target $scores > $features");
1939 # die "Error while running kocos.pl on $scores.\n" unless $status==0;
1940 # }
1941  
1942 # check if atleast 10 feature present in the features file.
1943 open(FEAT,$features) || die "Error($0):
1944 Error(code=$!) while opening the feature file <$features>\n";
1945  
1946 # read the complete file in single instruction instead of reading line by line.
1947 my $temp_delimiter = $/;
1948 $/ = undef;
1949
1950 my $inp_str = <FEAT>;
1951
1952 $/ = $temp_delimiter;
1953 close FEAT;
1954
1955 my $feat_cnt = 0;
1956 while($inp_str =~ m/<>.*\n/g && $feat_cnt < 10)
1957 {
1958 $feat_cnt++;
1959 }
1960  
1961 if($feat_cnt < 10)
1962 {
1963 print STDERR "ERROR($0):
1964 Only $feat_cnt FEATURES found in the <$scores> file.
1965 At least 10 FEATURES required to proceed with context
1966 representation.\n";
1967 exit 1;
1968 }
1969  
1970 # -----------------------
1971 # finding feature regexs
1972 # -----------------------
1973  
1974 if(defined $opt_verbose)
1975 {
1976 print STDERR "Finding Feature Regex/s ...\n";
1977 }
1978  
1979 $feature_regex="$prefix.regex";
1980 $status=system("nsp2regex.pl $features > $feature_regex");
1981 die "Error while running nsp2regex.pl on <$features>\n" unless $status==0;
1982  
1983 # -------------------------
1984 # creating context vectors
1985 # -------------------------
1986  
1987 if(defined $opt_verbose)
1988 {
1989 print STDERR "Building 1st Order Context Vectors ...\n";
1990 }
1991  
1992 # binary requested
1993 if(defined $opt_binary)
1994 {
1995 $binary="--binary";
1996 }
1997 else
1998 {
1999 $binary="";
2000 }
2001  
2002 $o1_presvd="$prefix.o1_presvd";
2003  
2004 if (defined $opt_lsa) {
2005 # do not create .rclass file and .clabel file in word / feature
2006 # clustering
2007 # create the .rlabel file and specify --transpose option to get
2008 # feature-by-context output
2009 # MJ - 06/30/2006
2010 # we also need to specify --testregex option with --transpose,
2011 # although we don't use it in LSA feature clustering.
2012 $testregex = "$prefix.testregex";
2013 $status=system("order1vec.pl --transpose --testregex $testregex --rlabel $rlabel $binary $test_context $feature_regex > $o1_presvd");
2014 } else {
2015 # print STDERR "order1vec.pl $binary --rlabel $rlabel $rclass_string --clabel $clabel $test_context $feature_regex > $o1_presvd\n";
2016 $status=system("order1vec.pl $binary --rlabel $rlabel $rclass_string --clabel $clabel $test_context $feature_regex > $o1_presvd");
2017 }
2018 die "ERROR ($0):
2019 Error (code=$!) while running order1vec.pl on <$test_context>\n" unless $status==0;
2020  
2021 $svdinput = $o1_presvd;
2022  
2023 # SVD
2024 if(defined $opt_svd)
2025 {
2026 # Check if performing svd will reduce the number of features i.e. number of columns
2027 # less than or equal to 10, if so do not perform svd
2028 open(INSVD,$svdinput) || die "Error($0):
2029 Error(code=$!) in opening Matrix file <$svdinput>\n";
2030  
2031 # line1 in Matrix file should either show the
2032 # <keyfile> tag or #rows #cols #nnz
2033 $line1=<INSVD>;
2034  
2035 if($line1=~/keyfile/)
2036 {
2037 $line1=<IN>;
2038 }
2039  
2040 if($line1=~/^\s*(\d+)\s+(\d+)\s+(\d+)\s*$/)
2041 {
2042 $rows=$1;
2043 $cols=$2;
2044 $nnz1=$3;
2045 }
2046 else
2047 {
2048 print STDERR "ERROR($0):
2049 Line $line1 in Matrix file <$svdinput> should show #rows #cols #nnz\n";
2050 exit 1;
2051 }
2052
2053 close INSVD;
2054  
2055 $maxprs=$opt_k > ($cols/$opt_rf) ? int($cols/$opt_rf) : $opt_k;
2056 if($maxprs >= 10)
2057 {
2058 if(defined $opt_verbose)
2059 {
2060 print STDERR "Performing SVD ...\n";
2061 }
2062 $svd_flag = 1;
2063 # calling svd function
2064 svd($svdinput,$vectors);
2065 }
2066 else
2067 {
2068 print STDERR "WARNING($0):
2069 SVD could not be performed on SVDINPUT <$svdinput>
2070 because svd with reduction factor = $opt_k and scaling
2071 factor = $opt_rf would reduce the resultant number of
2072 features to = $maxprs, computed via (min($opt_k, $cols/$opt_rf)).
2073 The minimum number of features required for representing
2074 the contexts is 10\n";
2075  
2076 $status=system("mv $svdinput $vectors");
2077 die "Error while creating file <$vectors>\n" unless $status==0;
2078 }
2079 }
2080 else
2081 {
2082 $status=system("mv $svdinput $vectors");
2083 die "Error while creating file <$vectors>\n" unless $status==0;
2084 }
2085 }
2086  
2087 ##############
2088 # Clustering
2089 ##############
2090  
2091 # cluster stopping param string
2092 $cluststop_str = "";
2093  
2094 # params common to both vcluster and scluster
2095 $cluster_str ="--rlabelfile $rlabel ";
2096 if(defined $opt_clmethod)
2097 {
2098 $cluster_str .="--clmethod $opt_clmethod ";
2099  
2100 if($opt_clmethod =~ /^(rb|rbr|direct|agglo|bagglo)$/i)
2101 {
2102 $cluststop_str .="--clmethod $opt_clmethod ";
2103 }
2104 else
2105 {
2106 $cluststop_str .="--clmethod rb ";
2107 }
2108 }
2109 if(defined $opt_crfun)
2110 {
2111 $cluster_str .="--crfun $opt_crfun ";
2112  
2113 if($opt_crfun =~ /^(i1|i2|h1|h2|e1)$/i)
2114 {
2115 $cluststop_str .="--crfun $opt_crfun ";
2116 }
2117 else
2118 {
2119 $cluststop_str .="--crfun i2 ";
2120 }
2121 }
2122  
2123 # cluster in vector space
2124 if(!defined $opt_space || $opt_space =~/^vector$/)
2125 {
2126 if(defined $opt_verbose)
2127 {
2128 print STDERR "Clustering in Vector Space ...\n";
2129 }
2130
2131 # build the string of params for vcluster
2132 $vclus_str = $cluster_str;
2133  
2134 if(defined $opt_sim)
2135 {
2136 $vclus_str .= "--sim $opt_sim ";
2137  
2138 if($opt_sim =~ /^(cos|corr)$/i)
2139 {
2140 $cluststop_str .= "--sim $opt_sim ";
2141 }
2142 else
2143 {
2144 $cluststop_str .= "--sim cos ";
2145 }
2146  
2147 if($opt_sim =~ /^co/)
2148 {
2149 $vclus_str .="--showfeatures ";
2150 }
2151 }
2152  
2153 $clabel_str = "";
2154 if (-f $clabel) {
2155 $clabel_str = "--clabel $clabel";
2156 }
2157 $vclus_str .="--nfeatures 10 $clabel_str ";
2158  
2159 # row scaling option
2160 if(defined $opt_rowmodel)
2161 {
2162 $vclus_str .= "--rowmodel $opt_rowmodel ";
2163 $cluststop_str .= "--rowmodel $opt_rowmodel ";
2164 }
2165 else
2166 {
2167 $vclus_str .= "--rowmodel none ";
2168 $cluststop_str .= "--rowmodel none ";
2169 }
2170  
2171 # column scaling option
2172 if(defined $opt_colmodel)
2173 {
2174 $vclus_str .= "--colmodel $opt_colmodel ";
2175 $cluststop_str .= "--colmodel $opt_colmodel ";
2176 }
2177 else
2178 {
2179 $vclus_str .= "--colmodel none ";
2180 $cluststop_str .= "--colmodel none ";
2181 }
2182  
2183 # cluster stopping
2184 if(defined $opt_cluststop)
2185 {
2186 $cluststop = $opt_cluststop;
2187  
2188 if(defined $opt_verbose)
2189 {
2190 print STDERR "Finding Number of Clusters with Cluster Stopping...\n";
2191 }
2192  
2193 if(defined $opt_threspk1)
2194 {
2195 $cluststop_str .= "--threspk1 $opt_threspk1 ";
2196 }
2197
2198 if(defined $opt_delta)
2199 {
2200 $cluststop_str .= "--delta $opt_delta ";
2201 }
2202 if(defined $opt_B)
2203 {
2204 $cluststop_str .= "--B $opt_B ";
2205 }
2206 if(defined $opt_typeref)
2207 {
2208 $cluststop_str .= "--typeref $opt_typeref ";
2209 }
2210 if(defined $opt_percentage)
2211 {
2212 $cluststop_str .= "--percentage $opt_percentage ";
2213 }
2214 if(defined $opt_seed)
2215 {
2216 $cluststop_str .= "--seed $opt_seed ";
2217 }
2218  
2219 $cluststop_str .= "--space vector --measure $opt_cluststop --precision $prec ";
2220
2221 $status = system("clusterstopping.pl --prefix $prefix $cluststop_str $vectors >& $prefix.predictions");
2222 # error handling for clusterstopping.pl
2223 if ($status != 0)
2224 {
2225 my $tmp = uc $opt_cluststop;
2226  
2227 # if predictions file not created fall-back to using the default #clusters
2228 if(!-e "$prefix.predictions")
2229 {
2230 print STDERR "WARNING($0):
2231 Could not locate the PREDICTIONS <$prefix.predictions>
2232 file which indicates that the cluster-stopping measure
2233 $tmp failed to predict the optimal number of clusters
2234 for the VECTORS <$vectors> file.
2235 Proceeding with the default number of clusters of $default_clusters\n\n";
2236  
2237 # default number of clusters
2238 $opt_clusters = $default_clusters;
2239 }
2240 else
2241 {
2242 # if predictions file exists then print out the error message present in the file
2243 # and fall-back to using the default #clusters
2244 open (TFP,"$prefix.predictions");
2245 $errstr = "";
2246 while(<TFP>)
2247 {
2248 $errstr .= $_;
2249 }
2250 print STDERR "WARNING($0):
2251 $errstr
2252 The cluster-stopping measure $tmp failed to predict the
2253 optimal number of clusters for <$vectors>
2254 Proceeding with the default number of clusters of $default_clusters\n\n";
2255
2256 # default #clusters
2257 $opt_clusters = $default_clusters;
2258 }
2259  
2260 # undefine cluster-stopping option to indicate that the #clusters being used is not
2261 # predicted by the measures but is set manually to the default value.
2262 $opt_cluststop = undef;
2263  
2264 # proceed with the default #clusters
2265 $num_k = 0;
2266 $predict[$num_k] = $opt_clusters;
2267
2268 $cluster_solution ="$prefix.cluster_solution";
2269 $cluster_output ="$prefix.cluster_output";
2270 $vclus_str .="--clustfile $cluster_solution ";
2271
2272 # running vcluster
2273 # use the -showtree option only if the #clusters is greater than 1
2274 if($opt_clusters > 1)
2275 {
2276 my $tmp_fig_str = "--showtree --plotclusters $prefix.dendogram.ps --plotformat ps ";
2277 system("vcluster $vclus_str $rclass_string $tmp_fig_str $vectors $opt_clusters > $cluster_output");
2278 }
2279 else
2280 {
2281 system("vcluster $vclus_str $rclass_string $vectors $opt_clusters > $cluster_output");
2282 }
2283 }
2284 else # If clusterstopping.pl ran successfully.
2285 {
2286 open (TFP,"$prefix.predictions") || die "Error($0):
2287 Error(code=$!) in opening <$prefix.predictions> file.\n";
2288
2289 $num_k = 0;
2290 while(<TFP>)
2291 {
2292 chomp;
2293 $predict[$num_k++] = $_;
2294 }
2295 $num_k--;
2296 close TFP;
2297
2298 $i = 0;
2299 while($i <= $num_k)
2300 {
2301 $opt_clusters = $predict[$i];
2302
2303 if($cluststop ne "all" && $cluststop ne "pk")
2304 {
2305 $cluster_solution ="$prefix.cluster_solution.$cluststop";
2306 $cluster_output ="$prefix.cluster_output.$cluststop";
2307 $dendo_file = "$prefix.$cluststop.dendogram.ps";
2308 }
2309 else
2310 {
2311 if($i == 0)
2312 {
2313 $cluster_solution ="$prefix.cluster_solution.pk1";
2314 $cluster_output ="$prefix.cluster_output.pk1";
2315 $dendo_file = "$prefix.pk1.dendogram.ps";
2316 }
2317 elsif($i == 1)
2318 {
2319 $cluster_solution ="$prefix.cluster_solution.pk2";
2320 $cluster_output ="$prefix.cluster_output.pk2";
2321 $dendo_file = "$prefix.pk2.dendogram.ps";
2322 }
2323 elsif($i == 2)
2324 {
2325 $cluster_solution ="$prefix.cluster_solution.pk3";
2326 $cluster_output ="$prefix.cluster_output.pk3";
2327 $dendo_file = "$prefix.pk3.dendogram.ps";
2328 }
2329 elsif($i == 3)
2330 {
2331 $cluster_solution ="$prefix.cluster_solution.gap";
2332 $cluster_output ="$prefix.cluster_output.gap";
2333 $dendo_file = "$prefix.gap.dendogram.ps";
2334 }
2335 }
2336
2337 $update_str ="--clustfile $cluster_solution ";
2338
2339 # running vcluster
2340 # use the -showtree option only if the #clusters is greater than 1
2341 if($opt_clusters > 1)
2342 {
2343 my $tmp_fig_str = "--showtree --plotclusters $dendo_file --plotformat ps ";
2344 system("vcluster $vclus_str $update_str $rclass_string $tmp_fig_str $vectors $opt_clusters > $cluster_output");
2345 }
2346 else
2347 {
2348 system("vcluster $vclus_str $update_str $rclass_string $vectors $opt_clusters > $cluster_output");
2349 }
2350  
2351 $i++;
2352 }
2353 }
2354 }
2355 else # if not using cluster stopping measures
2356 {
2357 $num_k = 0;
2358 $predict[$num_k] = $opt_clusters;
2359  
2360 $cluster_solution ="$prefix.cluster_solution";
2361 $cluster_output ="$prefix.cluster_output";
2362 $vclus_str .="--clustfile $cluster_solution ";
2363  
2364 # running vcluster
2365 # use the -showtree option only if the #clusters is greater than 1
2366 if($opt_clusters > 1)
2367 {
2368 my $tmp_fig_str = "--showtree --plotclusters $prefix.dendogram.ps --plotformat ps ";
2369 system("vcluster $vclus_str $rclass_string $tmp_fig_str $vectors $opt_clusters > $cluster_output");
2370 }
2371 else
2372 {
2373 system("vcluster $vclus_str $rclass_string $vectors $opt_clusters > $cluster_output");
2374 }
2375 }
2376 }
2377 else # cluster in similarity space
2378 {
2379 if(defined $opt_verbose)
2380 {
2381 print STDERR "Building Similarity Matrix ...\n";
2382 }
2383  
2384 # creating similarity matrix
2385 $simat="$prefix.simat";
2386
2387 my $simat_string = " ";
2388  
2389 if(defined $opt_svd && $svd_flag == 1)
2390 {
2391 $simat_string ="--dense ";
2392 }
2393  
2394 if(defined $opt_binary)
2395 {
2396 if(defined $opt_sim)
2397 {
2398 $simat_string .="--measure $opt_sim ";
2399 }
2400 $sim_program ="bitsimat.pl";
2401 }
2402 else
2403 {
2404 $sim_program ="simat.pl";
2405 }
2406  
2407 $status=system("$sim_program --format $format $simat_string $vectors > $simat");
2408 die "Error while running $sim_program\n" unless $status==0;
2409  
2410 if(defined $opt_verbose)
2411 {
2412 print STDERR "Clustering in Similarity Space ...\n";
2413 }
2414  
2415 # build the string of params for scluster
2416 $sclus_str = $cluster_str;
2417  
2418 # cluster stopping
2419 if(defined $opt_cluststop)
2420 {
2421 $cluststop = $opt_cluststop;
2422  
2423 if(defined $opt_verbose)
2424 {
2425 print STDERR "Finding Number of Clusters with Cluster Stopping...\n";
2426 }
2427  
2428 if(defined $opt_threspk1)
2429 {
2430 $cluststop_str .= "--threspk1 $opt_threspk1 ";
2431 }
2432
2433 if(defined $opt_delta)
2434 {
2435 $cluststop_str .= "--delta $opt_delta ";
2436 }
2437 if(defined $opt_B)
2438 {
2439 $cluststop_str .= "--B $opt_B ";
2440 }
2441 if(defined $opt_typeref)
2442 {
2443 $cluststop_str .= "--typeref $opt_typeref ";
2444 }
2445 if(defined $opt_percentage)
2446 {
2447 $cluststop_str .= "--percentage $opt_percentage ";
2448 }
2449 if(defined $opt_seed)
2450 {
2451 $cluststop_str .= "--seed $opt_seed ";
2452 }
2453
2454 $cluststop_str .= "--space similarity --measure $opt_cluststop --precision $prec ";
2455
2456 $status = system("clusterstopping.pl --prefix $prefix $cluststop_str $simat >& $prefix.predictions");
2457 # error handling for clusterstopping.pl
2458 # If clusterstopping.pl returned an error code
2459 if ($status != 0)
2460 {
2461 my $tmp = uc $opt_cluststop;
2462  
2463 # if predictions file not created fall-back to using the default #clusters
2464 if(!-e "$prefix.predictions")
2465 {
2466 print STDERR "WARNING($0):
2467 Could not locate the PREDICTIONS <$prefix.predictions>
2468 file which indicates that the cluster-stopping measure
2469 $tmp failed to predict the optimal number of clusters
2470 for the VECTORS <$vectors> file.
2471 Proceeding with the default number of clusters of $default_clusters\n\n";
2472  
2473 # default #clusters
2474 $opt_clusters = $default_clusters;
2475 }
2476 else
2477 {
2478 # if predictions file exists then print out the error message present in the file
2479 # and fall-back to using the default #clusters
2480 open (TFP,"$prefix.predictions");
2481 $errstr = "";
2482 while(<TFP>)
2483 {
2484 $errstr .= $_;
2485 }
2486 print STDERR "WARNING($0):
2487 $errstr
2488 The cluster-stopping measure $tmp failed to predict the
2489 optimal number of clusters for the given data.
2490 Proceeding with the default number of clusters of $default_clusters\n\n";
2491
2492 # default #clusters
2493 $opt_clusters = $default_clusters;
2494 }
2495  
2496 # undefine cluster-stopping option to indicate that the #clusters being used is not
2497 # predicted by the measures but is set manually to the default value.
2498 $opt_cluststop = undef;
2499  
2500 # proceed with the default #clusters
2501 $num_k = 0;
2502 $predict[$num_k] = $opt_clusters;
2503
2504 $cluster_solution ="$prefix.cluster_solution";
2505 $cluster_output ="$prefix.cluster_output";
2506
2507 $sclus_str .="--clustfile $cluster_solution ";
2508
2509 # running scluster
2510 # use the -showtree option only if the #clusters is greater than 1
2511 if($opt_clusters > 1)
2512 {
2513 my $tmp_fig_str = "--showtree --plotsclusters $prefix.dendogram.ps --plotformat ps ";
2514 system("scluster $sclus_str $rclass_string $tmp_fig_str $simat $opt_clusters > $cluster_output");
2515 }
2516 else
2517 {
2518 system("scluster $sclus_str $rclass_string $simat $opt_clusters > $cluster_output");
2519 }
2520 }
2521 else # If clusterstopping.pl ran successfully.
2522 {
2523 open (TFP,"$prefix.predictions") || die "Error($0):
2524 Error(code=$!) in opening <$prefix.predictions> file.\n";
2525
2526 $num_k = 0;
2527 while(<TFP>)
2528 {
2529 chomp;
2530 $predict[$num_k++] = $_;
2531 }
2532 $num_k--;
2533 close TFP;
2534
2535 $i = 0;
2536 while($i <= $num_k)
2537 {
2538 $opt_clusters = $predict[$i];
2539
2540 if($cluststop ne "all" && $cluststop ne "pk")
2541 {
2542 $cluster_solution ="$prefix.cluster_solution.$cluststop";
2543 $cluster_output ="$prefix.cluster_output.$cluststop";
2544 $dendo_file = "$prefix.$cluststop.dendogram.ps";
2545 }
2546 else
2547 {
2548 if($i == 0)
2549 {
2550 $cluster_solution ="$prefix.cluster_solution.pk1";
2551 $cluster_output ="$prefix.cluster_output.pk1";
2552 $dendo_file = "$prefix.pk1.dendogram.ps";
2553 }
2554 elsif($i == 1)
2555 {
2556 $cluster_solution ="$prefix.cluster_solution.pk2";
2557 $cluster_output ="$prefix.cluster_output.pk2";
2558 $dendo_file = "$prefix.pk2.dendogram.ps";
2559 }
2560 elsif($i == 2)
2561 {
2562 $cluster_solution ="$prefix.cluster_solution.pk3";
2563 $cluster_output ="$prefix.cluster_output.pk3";
2564 $dendo_file = "$prefix.pk3.dendogram.ps";
2565 }
2566 elsif($i == 3)
2567 {
2568 $cluster_solution ="$prefix.cluster_solution.gap";
2569 $cluster_output ="$prefix.cluster_output.gap";
2570 $dendo_file = "$prefix.gap.dendogram.ps";
2571 }
2572 }
2573
2574 $update_str ="--clustfile $cluster_solution ";
2575
2576 # running scluster
2577 # use the -showtree option only if the #clusters is greater than 1
2578 if($opt_clusters > 1)
2579 {
2580 my $tmp_fig_str = "--showtree --plotsclusters $dendo_file --plotformat ps ";
2581 system("scluster $sclus_str $update_str $rclass_string $tmp_fig_str $simat $opt_clusters > $cluster_output");
2582 }
2583 else
2584 {
2585 system("scluster $sclus_str $update_str $rclass_string $simat $opt_clusters > $cluster_output");
2586 }
2587 $i++;
2588 }
2589 }
2590 }
2591 else # if not using cluster stopping measures
2592 {
2593 $num_k = 0;
2594 $predict[$num_k] = $opt_clusters;
2595  
2596 $cluster_solution ="$prefix.cluster_solution";
2597 $cluster_output ="$prefix.cluster_output";
2598  
2599 $sclus_str .="--clustfile $cluster_solution ";
2600  
2601 # running scluster
2602 # use the -showtree option only if the #clusters is greater than 1
2603 if($opt_clusters > 1)
2604 {
2605 my $tmp_fig_str = "--showtree --plotsclusters $prefix.dendogram.ps --plotformat ps ";
2606 system("scluster $sclus_str $rclass_string $tmp_fig_str $simat $opt_clusters > $cluster_output");
2607 }
2608 else
2609 {
2610 system("scluster $sclus_str $rclass_string $simat $opt_clusters > $cluster_output");
2611 }
2612 }
2613 }
2614  
2615 #*********************
2616  
2617 # formatting clustering solution, show instances in each cluster
2618 $i = 0;
2619 while($i <= $num_k)
2620 {
2621 if(defined $opt_cluststop)
2622 {
2623 if($cluststop ne "all" && $cluststop ne "pk")
2624 {
2625 $clusters="$prefix.clusters.$cluststop";
2626 $cluster_solution = "$prefix.cluster_solution.$cluststop";
2627 $clusters_context = "$prefix.clusters_context.$cluststop";
2628 }
2629 else
2630 {
2631 if($i == 0)
2632 {
2633 $clusters="$prefix.clusters.pk1";
2634 $cluster_solution = "$prefix.cluster_solution.pk1";
2635 $clusters_context = "$prefix.clusters_context.pk1";
2636 }
2637 elsif($i == 1)
2638 {
2639 $clusters="$prefix.clusters.pk2";
2640 $cluster_solution = "$prefix.cluster_solution.pk2";
2641 $clusters_context = "$prefix.clusters_context.pk2";
2642 }
2643 elsif($i == 2)
2644 {
2645 $clusters="$prefix.clusters.pk3";
2646 $cluster_solution = "$prefix.cluster_solution.pk3";
2647 $clusters_context = "$prefix.clusters_context.pk3";
2648 }
2649 elsif($i == 3)
2650 {
2651 $clusters="$prefix.clusters.gap";
2652 $cluster_solution = "$prefix.cluster_solution.gap";
2653 $clusters_context = "$prefix.clusters_context.gap";
2654 }
2655 }
2656 }
2657 else # No. of Clusters: Set Manually
2658 {
2659 $clusters="$prefix.clusters";
2660 $cluster_solution = "$prefix.cluster_solution";
2661 $clusters_context = "$prefix.clusters_context";
2662 }
2663  
2664 if(defined $opt_wordclust)
2665 {
2666 $status=system("format_clusters.pl $cluster_solution $rlabel > $clusters");
2667 die "Error while formatting clusters.\n" unless $status==0;
2668 }
2669 else
2670 {
2671 $status=system("format_clusters.pl $cluster_solution $rlabel --senseval2 $testfile > $clusters");
2672 die "Error while formatting clusters.\n" unless $status==0;
2673  
2674 # execute the format_clusters.pl with --context option and use this file to label the clusters.
2675 $status=system("format_clusters.pl $cluster_solution $rlabel --context $testfile > $clusters_context");
2676 die "Error while running format_clusters.pl $cluster_solution $rlabel --context $testfile > $clusters_context\n" unless $status==0;
2677 }
2678  
2679 $i++;
2680 }
2681  
2682 if(!defined $opt_wordclust)
2683 {
2684  
2685 # create the parameter string for clusterlabeling.pl
2686 if(defined $opt_verbose)
2687 {
2688 print STDERR "Creating Cluster Labels ...\n";
2689 }
2690  
2691 $cluslabel_str = " --token $token ";
2692
2693 if(defined $opt_label_window)
2694 {
2695 $cluslabel_str .= " --window $opt_label_window ";
2696 }
2697
2698 if(defined $opt_label_ngram)
2699 {
2700 if($opt_label_ngram < 2 || $opt_label_ngram > 4)
2701 {
2702 print STDERR "\n ERROR($0):
2703 Labeling mechanism only support bigrams, trigrams and 4-grams for feature selection..\n";
2704 exit 1;
2705 }
2706  
2707 $cluslabel_str .= " --ngram $opt_label_ngram ";
2708 }
2709
2710 if(defined $opt_label_stop)
2711 {
2712 $cluslabel_str .= " --stop $opt_label_stop ";
2713 }
2714
2715 if(defined $opt_label_remove)
2716 {
2717 $cluslabel_str .= " --remove $opt_label_remove ";
2718 }
2719
2720 if(defined $opt_label_stat)
2721 {
2722 $cluslabel_str .= " --stat $opt_label_stat ";
2723 }
2724
2725 if(defined $opt_label_rank)
2726 {
2727 $cluslabel_str .= " --rank $opt_label_rank ";
2728 }
2729  
2730 $i = 0;
2731 while($i <= $num_k)
2732 {
2733 if(defined $opt_cluststop)
2734 {
2735 if($cluststop ne "all" && $cluststop ne "pk")
2736 {
2737 $clusters_context = "$prefix.clusters_context.$cluststop";
2738 $cluster_labels = "$prefix.cluster_labels.$cluststop";
2739 $param_str = $cluslabel_str . "--prefix $prefix.$cluststop ";
2740 }
2741 else
2742 {
2743 if($i == 0)
2744 {
2745 $clusters_context = "$prefix.clusters_context.pk1";
2746 $cluster_labels = "$prefix.cluster_labels.pk1";
2747 $param_str = $cluslabel_str . "--prefix $prefix.pk1 ";
2748 }
2749 elsif($i == 1)
2750 {
2751 $clusters_context = "$prefix.clusters_context.pk2";
2752 $cluster_labels = "$prefix.cluster_labels.pk2";
2753 $param_str = $cluslabel_str . "--prefix $prefix.pk2 ";
2754 }
2755 elsif($i == 2)
2756 {
2757 $clusters_context = "$prefix.clusters_context.pk3";
2758 $cluster_labels = "$prefix.cluster_labels.pk3";
2759 $param_str = $cluslabel_str . "--prefix $prefix.pk3 ";
2760 }
2761 elsif($i == 3)
2762 {
2763 $clusters_context = "$prefix.clusters_context.gap";
2764 $cluster_labels = "$prefix.cluster_labels.gap";
2765 $param_str = $cluslabel_str . "--prefix $prefix.gap ";
2766 }
2767 }
2768 }
2769 else # No. of Clusters: Set Manually
2770 {
2771 $clusters_context = "$prefix.clusters_context";
2772 $cluster_labels = "$prefix.cluster_labels";
2773 $param_str = $cluslabel_str . "--prefix $prefix ";
2774 }
2775  
2776 # execute the cluster labeling program
2777 $status=system("clusterlabeling.pl $param_str $clusters_context > $cluster_labels");
2778 die "Error while running clusterlabeling.pl $param_str $clusters_context > $cluster_labels\n" unless $status==0;
2779  
2780 $i++;
2781 }
2782 }
2783  
2784 ################
2785 # Evaluation
2786 ################
2787  
2788 if(defined $opt_eval)
2789 {
2790 if(defined $opt_verbose)
2791 {
2792 print STDERR "Evaluating ...\n";
2793 }
2794  
2795 $i = 0;
2796 while($i <= $num_k)
2797 {
2798 if(defined $opt_cluststop)
2799 {
2800 if($cluststop ne "all" && $cluststop ne "pk")
2801 {
2802 $prelabel="$prefix.prelabel.$cluststop";
2803 $label="$prefix.label.$cluststop";
2804 $report="$prefix.report.$cluststop";
2805 $cluster_solution ="$prefix.cluster_solution.$cluststop";
2806 }
2807 else
2808 {
2809 if($i == 0)
2810 {
2811 $prelabel="$prefix.prelabel.pk1";
2812 $label="$prefix.label.pk1";
2813 $report="$prefix.report.pk1";
2814 $cluster_solution ="$prefix.cluster_solution.pk1";
2815 }
2816 elsif($i == 1)
2817 {
2818 $prelabel="$prefix.prelabel.pk2";
2819 $label="$prefix.label.pk2";
2820 $report="$prefix.report.pk2";
2821 $cluster_solution ="$prefix.cluster_solution.pk2";
2822 }
2823 elsif($i == 2)
2824 {
2825 $prelabel="$prefix.prelabel.pk3";
2826 $label="$prefix.label.pk3";
2827 $report="$prefix.report.pk3";
2828 $cluster_solution ="$prefix.cluster_solution.pk3";
2829 }
2830 elsif($i == 3)
2831 {
2832 $prelabel="$prefix.prelabel.gap";
2833 $label="$prefix.label.gap";
2834 $report="$prefix.report.gap";
2835 $cluster_solution ="$prefix.cluster_solution.gap";
2836 }
2837 }
2838 }
2839 else # No. of Clusters: Set Manually
2840 {
2841 $prelabel="$prefix.prelabel";
2842 $label="$prefix.label";
2843 $report="$prefix.report";
2844 $cluster_solution ="$prefix.cluster_solution";
2845 }
2846  
2847 $status=system("cluto2label.pl $cluster_solution keyfile*.key > $prelabel");
2848 die "Error while running cluto2label.pl\n" unless $status==0;
2849  
2850 $status=system("label.pl $prelabel > $label");
2851 die "Error while running label.pl\n" unless $status==0;
2852  
2853 $status=system("report.pl $label $prelabel > $report");
2854 die "Error while running report.pl\n" unless $status==0;
2855  
2856 $i++;
2857 }
2858  
2859 $status=system("mv keyfile*.key $prefix.key");
2860 die "Error while creating the KEY file.\n" unless $status==0;
2861 }
2862  
2863 ##################
2864 # Printing Output
2865 ##################
2866  
2867 if(defined $opt_cluststop)
2868 {
2869 if($opt_cluststop eq "all")
2870 {
2871 $predict_measure[0] = "PK1 measure";
2872 $predict_measure[1] = "PK2 measure";
2873 $predict_measure[2] = "PK3 measure";
2874 $predict_measure[3] = "Adapted Gap Statistic";
2875 }
2876 elsif($opt_cluststop eq "pk")
2877 {
2878 $predict_measure[0] = "PK1 measure";
2879 $predict_measure[1] = "PK2 measure";
2880 $predict_measure[2] = "PK3 measure";
2881 }
2882 else
2883 {
2884 $predict_measure[0] = uc $opt_cluststop;
2885 $predict_measure[0] .= " measure";
2886 }
2887 }
2888 else
2889 {
2890 $predict_measure[0] = "Set manually";
2891 }
2892  
2893 $i = 0;
2894 while($i <= $num_k)
2895 {
2896 print "\n=================================================================\n";
2897 print "Output when #clusters = $predict[$i] ($predict_measure[$i])\n";
2898 print "=================================================================\n";
2899  
2900 if(defined $opt_cluststop)
2901 {
2902 if($cluststop ne "all" && $cluststop ne "pk")
2903 {
2904 $cluster_output ="$prefix.cluster_output.$cluststop";
2905 $status=system("cat $cluster_output");
2906 die "Error while displaying the cluster results.\n" unless $status==0;
2907
2908 if(defined $opt_eval)
2909 {
2910 $report = "$prefix.report.$cluststop";
2911 $status=system("cat $report");
2912 die "Error while displaying the report file.\n" unless $status==0;
2913 }
2914
2915 $clusters="$prefix.clusters.$cluststop";
2916 print "\nClusters of given contexts can be found in file: <$clusters>\n\n";
2917 }
2918 else
2919 {
2920 if($i == 0)
2921 {
2922 $cluster_output ="$prefix.cluster_output.pk1";
2923 $status=system("cat $cluster_output");
2924 die "Error while displaying the cluster results.\n" unless $status==0;
2925
2926 if(defined $opt_eval)
2927 {
2928 $report = "$prefix.report.pk1";
2929 $status=system("cat $report");
2930 die "Error while displaying the report file.\n" unless $status==0;
2931 }
2932
2933 $clusters="$prefix.clusters.pk1";
2934 print "\nClusters of given contexts can be found in file: $clusters\n\n";
2935 }
2936 elsif($i == 1)
2937 {
2938 $cluster_output ="$prefix.cluster_output.pk2";
2939 $status=system("cat $cluster_output");
2940 die "Error while displaying the cluster results.\n" unless $status==0;
2941
2942 if(defined $opt_eval)
2943 {
2944 $report = "$prefix.report.pk2";
2945 $status=system("cat $report");
2946 die "Error while displaying the report file.\n" unless $status==0;
2947 }
2948
2949 $clusters="$prefix.clusters.pk2";
2950 print "\nClusters of given contexts can be found in file: $clusters\n\n";
2951 }
2952 elsif($i == 2)
2953 {
2954 $cluster_output ="$prefix.cluster_output.pk3";
2955 $status=system("cat $cluster_output");
2956 die "Error while displaying the cluster results.\n" unless $status==0;
2957
2958 if(defined $opt_eval)
2959 {
2960 $report = "$prefix.report.pk3";
2961 $status=system("cat $report");
2962 die "Error while displaying the report file.\n" unless $status==0;
2963 }
2964
2965 $clusters="$prefix.clusters.pk3";
2966 print "\nClusters of given contexts can be found in file: $clusters\n\n";
2967 }
2968 elsif($i == 3)
2969 {
2970 $cluster_output ="$prefix.cluster_output.gap";
2971 $status=system("cat $cluster_output");
2972 die "Error while displaying the cluster results.\n" unless $status==0;
2973
2974 if(defined $opt_eval)
2975 {
2976 $report = "$prefix.report.gap";
2977 $status=system("cat $report");
2978 die "Error while displaying the report file.\n" unless $status==0;
2979 }
2980
2981 $clusters="$prefix.clusters.gap";
2982 print "\nClusters of given contexts can be found in file: $clusters\n\n";
2983 }
2984 }
2985 }
2986 else # No. of Clusters: Set Manually
2987 {
2988 $cluster_output ="$prefix.cluster_output";
2989 $status=system("cat $cluster_output");
2990 die "Error while displaying the cluster results.\n" unless $status==0;
2991
2992 if(defined $opt_eval)
2993 {
2994 $report = "$prefix.report";
2995 $status=system("cat $report");
2996 die "Error while displaying the report file.\n" unless $status==0;
2997 }
2998
2999 $clusters="$prefix.clusters";
3000 print "\nClusters of given contexts can be found in file: $clusters\n\n";
3001 }
3002  
3003 $i++;
3004 }
3005  
3006 ##############################################################################
3007  
3008 # ==========================
3009 # SUBROUTINE SECTION
3010 # ==========================
3011  
3012 sub svd
3013 {
3014 ($svdin,$svdout)=@_;
3015 # converting input to harwell-boeing format
3016 $svd_string="";
3017 if(defined $opt_k)
3018 {
3019 $svd_string="--k $opt_k ";
3020 }
3021 if(defined $opt_rf)
3022 {
3023 $svd_string.="--rf $opt_rf ";
3024 }
3025 if(defined $opt_iter)
3026 {
3027 $svd_string.="--iter $opt_iter ";
3028 }
3029  
3030 $numform = "5$format"; ## numform is 5f16.XX
3031  
3032 $status=system("mat2harbo.pl --numform $numform --param $svd_string $svdin > matrix");
3033 die "Error while running mat2harbo.pl on <$svdin>\n" unless $status==0;
3034  
3035 system("las2");
3036  
3037 $harbomat="$prefix.harbomat";
3038 $status=system("mv matrix $harbomat");
3039  
3040 die "Error in creating <$harbomat>\n" unless $status==0;
3041  
3042 # reconstruction
3043 $status=system("svdpackout.pl --rowonly --format $format lav2 lao2 > $svdout");
3044 die "Error while running svdpackout.pl\n" unless $status==0;
3045 }
3046  
3047 #-----------------------------------------------------------------------------
3048 #show minimal usage message
3049 sub showminimal()
3050 {
3051 print "Usage: discriminate.pl [OPTIONS] TEST";
3052 print "\nTYPE discriminate.pl --help for help\n";
3053 }
3054  
3055 #-----------------------------------------------------------------------------
3056 #show help
3057 sub showhelp()
3058 {
3059 print "Usage: discriminate.pl [OPTIONS] TEST
3060  
3061 Wrapper program for SenseClusters' Toolkit. Discriminates among the
3062 given text instances based on their contextual similarities.
3063  
3064 TEST
3065 Senseval-2 formatted TEST instance file containing the instances
3066 to be clustered.
3067  
3068 OPTIONS:
3069  
3070 --training TRAIN
3071 Specify the training file in plain text format. Instances from this
3072 file are used for selecting features. If --training is not specified,
3073 features are selected from the same TEST file.
3074  
3075 --split N
3076 Splits the given TEST file into two portions, N% for the use as the
3077 TRAIN data and (100-N)% as the TEST data. The value for N is a
3078 percentage and should be an integer between 1 to 99 (inclusive).
3079 The instances from the original TEST file are not picked or split
3080 in any particular order but are randomly split into the two portions
3081 of TRAIN and TEST data while maintaining the ratio of N/(100-N).
3082  
3083 Note: This option cannot be used when --training option is also used.
3084  
3085 --token TOKEN
3086 Specify a file containing Perl regex/s that define the tokenization
3087 scheme in TRAIN and TEST files. By default, token.regex is searched
3088 in the current directory.
3089  
3090 --target TARGET
3091 Specify a file containing Perl regex/s that identify the target word/s
3092 whose senses are to be discriminated.
3093  
3094 If --target is not specified, target.regex file is searched in the
3095 current directory. If this file doesn't exist, target.regex is
3096 automatically created by searching the <head> tags in the TEST data.
3097 If no <head> tags are found in TEST, TEST is assumed to be global.
3098  
3099 Note: --target cannot be specified with headless input data
3100 i.e. test file without head/target word(s).
3101  
3102 --prefix PRE
3103 Specify the prefix to be used for output filenames.
3104  
3105 --format f16.XX
3106 The default format for floating point numbers is f16.06. This means
3107 that there is room for 6 digits to the left of the decimal,
3108 and 9 to the right. You may change XX to any value between 0
3109 and 15, however, the format must remain 16 spaces long due to
3110 formatting requirements of SVDPACKC.
3111  
3112 --wordclust
3113  
3114 Discriminates and clusters each word based upon its direct and indirect
3115 co-occurrence with other words (when used without the --lsa switch) or
3116 clusters words or features based upon their occurrences in different
3117 contexts (when used with the --lsa switch).
3118  
3119 Note: 1. Separate (--training) TRAIN data should not be used with word
3120 clustering.
3121 2. Starting with Version 0.93, word clustering is no longer
3122 restricted to using only headless data. However, options
3123 specific to headed data such as --scope_test and target
3124 co-occurrence features (see below) cannot be used.
3125  
3126 --lsa
3127  
3128 Uses Latent Semantic Analysis (LSA) style representation for clustering
3129 features or contexts. LSA representation is the transpose of
3130 the context-by-feature matrix created using the native SenseClusters
3131 order1 context representation.
3132  
3133 This option can be used only in the following two combinations of
3134 the --context and the --wordclust options:
3135  
3136 1. --context o1 --wordclust --lsa
3137  
3138 Performs feature clustering by grouping together features based on the
3139 contexts that they occur in. Features can be unigrams, bigrams or
3140 co-occurrences. Feature vectors are the rows of the transposed
3141 context-by-feature representation created by order1vec.pl.
3142  
3143 2. --context o2 --lsa
3144  
3145 Performs context clustering by creating context vectors by averaging the
3146 feature vectors from the transposed context-by-feature representation of
3147 order1vec.pl.
3148  
3149 Feature Options :
3150  
3151 --feature TYPE
3152 Specify the feature type to be used for representing contexts.
3153 Possible options for feature type with first order context
3154 representation:
3155  
3156 bi - bigrams [default]
3157 tco - target co-occurrences
3158 co - co-occurrences
3159 uni - unigrams
3160  
3161 Possible options for feature type with second order context
3162 representation:
3163  
3164 bi - bigrams [default]
3165 co - co-occurrences
3166 tco - target co-occurrences
3167  
3168 Note: --tco (target co-occurrences) cannot be used with headless
3169 data i.e. test/train file without head/target word(s).
3170  
3171 --scope_train S1
3172 Context in TRAIN instances is limited to include only S1 words on the
3173 left and right of the TARGET word. Use --scope_train only if every
3174 training instance contains the TARGET word.
3175  
3176 Note: --scope_train cannot be used with headless data i.e. train file
3177 without head/target word(s).
3178  
3179 --scope_test S2
3180 Context in TEST instances is limited to include only S2 words on the
3181 left and right of the TARGET word. Use --scope_test only if every
3182 test instance contains the TARGET word.
3183  
3184 Note: --scope_test cannot be used with headless data i.e. test file
3185 without head/target word(s).
3186  
3187 --remove F
3188 Features occurring less than F number of times are removed from the
3189 feature set.
3190  
3191 --window W
3192 Sets the window size for bigram and co-occurrence features. Words
3193 occurring within W positions from each other (i.e. at most W-2
3194 intervening words) form bigrams/co-occurrences.
3195  
3196 --stop STOPFILE
3197 Specify a file of Perl regex/s that define a stop list of words to be
3198 excluded from the features.
3199  
3200 --stat Stat
3201 Performs the specified statistical test of association on bigrams/
3202 co-occurrences. The test scores can be used to filter insignificant
3203 pairs or in the feature vector representations.
3204  
3205 The possible values of STAT are -
3206  
3207 dice - Dice Coefficient
3208 ll - Log Likelihood Ratio
3209 odds - Odds Ratio
3210 phi - Phi Coefficient
3211 pmi - Point-Wise Mutual Information
3212 tmi - True Mutual Information
3213 x2 - Chi-Squared Test
3214 tscore - T-Score
3215 leftFisher - Left Fisher's Test
3216 rightFisher - Right Fisher's Test
3217  
3218 --stat_rank R
3219  
3220 Word pairs ranking below R when arranged in descending order of
3221 their test scores are ignored.
3222  
3223 --stat_rank will be ignored unless --stat option is specified.
3224  
3225 --stat_score S
3226 Specify the score cutoff value to select pairs with test scores
3227 greater than S.
3228  
3229 --stat_score will be ignored unless option --stat is specified.
3230  
3231 Vector Options :
3232  
3233 --context ORD
3234 Specify the context representation to be used to represent the TEST
3235 instances. Set ORD to 'o1' to use 1st order context vectors and to
3236 'o2' to use 2nd order context vectors. Default context representation
3237 is o2.
3238  
3239 --binary
3240 Creates binary feature and context vectors. By default, the frequency
3241 scores are retained by these vectors.
3242  
3243 SVD Options :
3244  
3245 --svd
3246 Performs Singular Value Decomposition to reduce the feature space
3247 dimensions.
3248  
3249 --k K
3250 Reduces dimensions of the feature space to K. Default is 300.
3251  
3252 --rf RF
3253 Specifies the reduction factor such that feature space with N
3254 dimensions is reduced down to N/RF (RF >= 1). Default RF=10.
3255  
3256 --iter I
3257 Specifies the number of SVD iterations. Recommended value is (3 x K)
3258  
3259 Cluster-Stopping Options:
3260  
3261 --cluststop CS
3262 Specify the cluster stopping measure to be used to predict the number
3263 the number of clusters.
3264  
3265 The possible option values:
3266  
3267 pk1 - Use PK1 measure
3268 [PK1[m] = (crfun[m] - mean(crfun[1...deltaM]))/std(crfun[1...deltaM]))]
3269 pk2 - Use PK2 measure
3270 [PK2[m] = (crfun[m]/crfun[m-1])]
3271 pk3 - Use PK3 measure
3272 [PK3[m] = ((2 * crfun[m])/(crfun[m-1] + crfun[m+1]))]
3273 gap - Use Adapted Gap Statistic.
3274 pk - Use all the PK measures.
3275 all - Use all the four cluster stopping measures.
3276
3277 More about these measures can be found in the documentation of
3278 Toolkit/clusterstop/clusterstopping.pl
3279  
3280 NOTE: Options --clusters and --cluststop cannot be used together.
3281  
3282 --delta INT
3283 NOTE: Delta value can only be a positive integer value.
3284  
3285 Specify 0 to stop the iterating clustering process when two consecutive
3286 crfun values are exactly equal. This is the default setting when the
3287 crfun values are integer/whole numbers.
3288  
3289 Specify non-zero positive integer to stop the iterating clustering
3290 process when the difference between two consecutive crfun values
3291 is less than or equal to this value. However, note that the integer
3292 value specified is internally shifted to capture the difference in
3293 the least significant digit of the crfun values when these crfun
3294 values are fractional.
3295 For example:
3296 For crfun = 1.23e-02 & delta = 1 will be transformed to 0.0001
3297 For crfun = 2.45e-01 & delta = 5 will be transformed to 0.005
3298 The default delta value when the crfun values are fractional is 1.
3299  
3300 However if the crfun values are integer/whole numbers (exponent >= 2)
3301 then the specified delta value is internally shifted only until the
3302 least significant digit in the scientific notation.
3303 For example:
3304 For crfun = 1.23e+04 & delta = 2 will be transformed to 200
3305 For crfun = 2.45e+02 & delta = 5 will be transformed to 5
3306 For crfun = 1.44e+03 & delta = 1 will be transformed to 10
3307  
3308 --threspk1 NUM
3309 The threshold value that should be used by the PK1 measure to predict
3310 the k value.
3311 Default = -0.7
3312  
3313 NOTE: This option should be used only when --cluststop option is also
3314 used with option value of \"all\" or \"pk1\".
3315  
3316 Cluster-Stopping: Adapted Gap Statistic Options:
3317  
3318 --B NUM
3319 The number of replicates/references to be generated.
3320 Default: 1
3321  
3322 --typeref TYP
3323 Specifies whether to generate B replicates from a reference or to
3324 generate B references.
3325  
3326 The possible option values:
3327 rep - replicates [Default]
3328 ref - references
3329  
3330 --percentage NUM
3331 Specifies the percentage confidence to be reported in the log file.
3332 Since Gap Statistic uses parametric bootstrap method for reference
3333 distribution generation, it is critical to understand the interval
3334 around the sample mean that could contain the population (\"true\")
3335 mean and with what certainty.
3336 Default: 90
3337  
3338 --seed NUM
3339 The seed to be used with the random number generator.
3340 Default: No seed is set.
3341  
3342 Clustering Options :
3343  
3344 --clusters C
3345 Specify the number of clusters to be created. Default is 2.
3346  
3347 --space SPACE
3348 Specifies whether clustering is to be performed in vector or similarity
3349 space. Set SPACE to 'vector' to cluster context vectors directly in
3350 vector space OR to 'similarity' to compose a similarity matrix and
3351 cluster instances in similarity space. Default SPACE is vector.
3352  
3353 --clmethod CL
3354 Specifies the clustering method.
3355  
3356 Possible option values are :
3357 rb - Repeated Bisections [Default]
3358 rbr - Repeated Bisections for by k-way refinement
3359 direct - Direct k-way clustering
3360 agglo - Agglomerative clustering
3361 graph - Graph partitioning-based clustering
3362 bagglo - Partitional biased Agglomerative clustering
3363  
3364 --crfun CR
3365 Selects the criteria function for Clustering. The meanings of these
3366 criteria functions is explained in Cluto's manual.
3367  
3368 The possible values are :
3369 i1 - I1 Criterion function
3370 i2 - I2 Criterion function [default for partitional]
3371 e1 - E1 Criterion function
3372 g1 - G1 Criterion function
3373 g1p - G1' Criterion function
3374 h1 - H1 Criterion function
3375 h2 - H2 Criterion function
3376 slink - Single link merging scheme
3377 wslink - Single link merging scheme weighted w.r.t. cluster sim
3378 clink - Complete link merging scheme
3379 wclink - Complete link merging scheme weighted w.r.t. cluster sim
3380 upgma - Group average merging scheme [default for agglomerative]
3381  
3382 Note that for cluster stopping, i1, i2, e1, h1 and h2 criterion
3383 functions can only be used. If a crfun other than these is selected
3384 then cluster stopping uses the default crfun (i2) while the final
3385 clustering of contexts is performed using the crfun specified.
3386  
3387 --sim SIM
3388 Specifies the similarity measure to be used during clustering.
3389 When --space is vector, possible option values of SIM are :
3390  
3391 cos - Cosine Coefficient [default]
3392 corr - Correlation Coefficient
3393 dist - Euclidean distance
3394 jacc - Extended Jaccard Coeeficient
3395  
3396 When --space is similarity and --binary is ON,
3397 possible values of SIM are :
3398  
3399 cos - Cosine Coefficient [default]
3400 mat - Match Coefficient
3401 jac - Jaccard Coefficient
3402 ovr - Overlap Coefficient
3403 dic - Dice Coefficient
3404  
3405 Otherwise, only cosine coefficient is available and is default.
3406  
3407 --rowmodel RMOD
3408 The option is used to specify the model to be used to scale every
3409 column of each row. (For further details please refer Cluto manual)
3410  
3411 The possible values for RMOD -
3412 none - no scaling is performed (default setting)
3413 maxtf - post scaling the values are between 0.5 and 1.0
3414 sqrt - square-root of actual values
3415 log - log of actual values
3416  
3417 --colmodel CMOD
3418 The option is used to specify the model to be used to (globally)
3419 scale each column across all rows. (For further details please refer
3420 Cluto manual)
3421  
3422 The possible values for CMOD -
3423 none - no scaling is performed (default setting)
3424 idf - scaling according to inverse-document-frequency
3425  
3426 Labeling Options :
3427  
3428 Note: Labeling options cannot be used with word-clustering
3429 (--wordclust).
3430  
3431 --label_stop LABEL_STOPFILE
3432 A file of Perl regexes that define the stop list of words to be
3433 excluded from the labels.
3434  
3435 --label_ngram LABEL_NGRAM
3436 Specifies the value of n in 'n-gram' for the feature selection.
3437 The supported values for n are 2, 3 and 4.
3438
3439 Default value is 2.
3440  
3441 --label_remove LABEL_N
3442 Removes ngrams that occur less than LABEL_N times.
3443  
3444 --label_window LABEL_W
3445 Specifies the window size for bigrams. Pairs of words that co-occur
3446 within the specified window from each other (window LABEL_W allows at
3447 most LABEL_W-2 intervening words) will form the bigram features.
3448 Default window size is 2 which allows only consecutive word pairs.
3449  
3450 --label_stat LABEL_STAT
3451 Specifies the statistical scores of association.
3452  
3453 Available tests of association are :
3454  
3455 dice - Dice Coefficient
3456 ll - Log Likelihood Ratio
3457 odds - Odds Ratio
3458 phi - Phi Coefficient
3459 pmi - Point-Wise Mutual Information
3460 tmi - True Mutual Information
3461 x2 - Chi-Squared Test
3462 tscore - T-Score
3463 leftFisher - Left Fisher's Test
3464 rightFisher - Right Fisher's Test
3465  
3466 --label_rank LABEL_R
3467 Features ranking below LABEL_R when arranged in descending order of
3468 their test scores are ignored.
3469  
3470 Other Options :
3471  
3472 --eval
3473 Evaluates clustering performace by comparing results against correct
3474 answer keys.
3475  
3476 Note: This option can be used only if the answer tags are provided
3477 in the TEST file.
3478  
3479 --rank_filter R
3480 Allows to remove low frequency senses during evaluation. This will
3481 remove the senses that rank below R when senses in TEST are arranged
3482 in the descending order of their frequencies. In other words, it
3483 selects top R most frequent senses. An instance will be removed if
3484 it has all sense tags below rank R.
3485  
3486 --percent_filter P
3487 Allows to remove low frequency senses based on their percentage
3488 frequencies. This will remove senses whose frequency is below P%
3489 in the TEST data.
3490  
3491 --showargs
3492 Displays to STDOUT values of compulsory and optional arguments.
3493 [NOT SUPPORTED IN THIS VERSION]
3494  
3495 --verbose
3496 Displays to STDERR the current program status.
3497  
3498 --help
3499 Displays this message.
3500  
3501 --version
3502 Displays the version information.
3503  
3504 Type 'perldoc discriminate.pl' for more detailed information.\n";
3505 }
3506  
3507 #------------------------------------------------------------------------------
3508 #version information
3509 sub showversion()
3510 {
3511 print '$Id: discriminate.pl,v 1.108 2013/06/26 01:09:24 jhaxx030 Exp $';
3512 print "\nDriver to Run SenseClusters\n";
3513 ## print "\nCopyright (c) 2002-2006, Ted Pedersen, Amruta Purandare, Anagha Kulkarni, & Mahesh Joshi\n";
3514 ## print "Date of Last Update: 07/30/2006\n";
3515 }
3516  
3517 #############################################################################
3518