| 1 | #!/usr/local/bin/perl -w | ||
| 2 | |||
| 3 | =head1 NAME | ||
| 4 | |||
| 5 | reduce-count.pl - Reduce size of feature space by removing words not in evaluation data | ||
| 6 | |||
| 7 | =head1 SYNOPSIS | ||
| 8 | |||
| 9 | reduce-count.pl [OPTIONS] BIGRAM UNIGRAM | ||
| 10 | |||
| 11 | The features found in training data are defined in a bigram file 'bigram': | ||
| 12 | |||
| 13 | cat bigram | ||
| 14 | |||
| 15 | Output => | ||
| 16 | |||
| 17 | 1491 | ||
| 18 | at<>least<>3 7 3 | ||
| 19 | co<>occurrences<>3 5 3 | ||
| 20 | be<>a<>3 13 25 | ||
| 21 | General<>Public<>3 3 3 | ||
| 22 | of<>test<>3 41 9 | ||
| 23 | file<>bigfile<>3 26 6 | ||
| 24 | given<>set<>3 7 5 | ||
| 25 | |||
| 26 | The unigrams that occur in the evaluation data are defined in a file 'unigram' : | ||
| 27 | |||
| 28 | cat unigram | ||
| 29 | |||
| 30 | Output => | ||
| 31 | |||
| 32 | at<> | ||
| 33 | be<> | ||
| 34 | test<> | ||
| 35 | |||
| 36 | Now remove any bigram that does not contain at least one of the words in the unigram | ||
| 37 | file: | ||
| 38 | |||
| 39 | reduce-count.pl cout uni | ||
| 40 | |||
| 41 | Output => | ||
| 42 | |||
| 43 | 1491 | ||
| 44 | at<>least<>3 7 3 | ||
| 45 | be<>a<>3 13 25 | ||
| 46 | of<>test<>3 41 9 | ||
| 47 | |||
| 48 | Type C<reduce-count.pl> for a quick summary of options | ||
| 49 | |||
| 50 | =head1 DESCRIPTION | ||
| 51 | |||
| 52 | This program removes all bigrams from the given BIGRAM file that do not | ||
| 53 | include at least one constituent word from the UNIGRAM file. Note that | ||
| 54 | this can also be applied on a co-occurrence file. | ||
| 55 | |||
| 56 | The intent of this in SenseClusters is to allow a user to significantly | ||
| 57 | reduce the number of features in a BIGRAM file by including only those | ||
| 58 | that contain at least one word from the test data. In this use case, a | ||
| 59 | user would have a BIGRAM file of features found in training data, and | ||
| 60 | would also have a UNIGRAM file from a set of test data. The intuition | ||
| 61 | behind this is that we know that a bigram made up of words that do not | ||
| 62 | occur in the test data will not be needed for creating representations of | ||
| 63 | the test data for clustering. | ||
| 64 | |||
| 65 | Note that this program DOES NOT adjust the any of the counts as found in | ||
| 66 | the BIGRAM file. Thus, the intent of this program is to make it possible | ||
| 67 | to reduce the number of features that must be searched through during | ||
| 68 | feature matching, for example. However, it is not intended to adjust the | ||
| 69 | sample size or counts of bigrams. The counts remain the same, so any | ||
| 70 | conclusions drawn from statistic.pl, for example, are not affected by this | ||
| 71 | program. | ||
| 72 | |||
| 73 | =head1 INPUT | ||
| 74 | |||
| 75 | =head2 Required Arguments: | ||
| 76 | |||
| 77 | =head3 BIGRAM | ||
| 78 | |||
| 79 | Should be a bigram file created by NSP programs, count.pl, statistic.pl | ||
| 80 | or combig.pl. Each line containing a word pair (bigram or co-occurrences) | ||
| 81 | should show either of the following forms: | ||
| 82 | |||
| 83 | word1<>word2<>n11 n1p np1 (if created by count or combig) | ||
| 84 | |||
| 85 | word1<>word2<>rank score n11 n1p np1 (if created by statistic) | ||
| 86 | |||
| 87 | Any line that is not formatted like above is simply displayed as it is | ||
| 88 | assuming that its printed by the --extended option in NSP. | ||
| 89 | |||
| 90 | =head3 UNIGRAM | ||
| 91 | |||
| 92 | Should be a unigram output of NSP. Each line in the UNIGRAM file should show either of | ||
| 93 | the following forms: | ||
| 94 | |||
| 95 | word<> | ||
| 96 | |||
| 97 | word<>n | ||
| 98 | |||
| 99 | where n is the frequency count of the word. | ||
| 100 | |||
| 101 | =head2 Optional Arguments: | ||
| 102 | |||
| 103 | =head3 --help | ||
| 104 | |||
| 105 | Displays the summary of command line options. | ||
| 106 | |||
| 107 | =head3 --version | ||
| 108 | |||
| 109 | Displays the version information. | ||
| 110 | |||
| 111 | =head1 OUTPUT | ||
| 112 | |||
| 113 | reduce-count.pl displays all lines in the given BIGRAM file except those that | ||
| 114 | are formatted as follows: | ||
| 115 | |||
| 116 | word1<>word2<>n11 n1p np1 | ||
| 117 | |||
| 118 | word1<>word2<>rank score n11 n1p np1 | ||
| 119 | |||
| 120 | and neither word1 nor word2 are listed in the given UNIGRAM file. | ||
| 121 | |||
| 122 | =head1 SYSTEM REQUIREMENTS | ||
| 123 | |||
| 124 | =over | ||
| 125 | =item Ngram Statistics Package - L<http://search.cpan.org/dist/Test-NSP> | ||
| 126 | =back | ||
| 127 | |||
| 128 | =head1 BUGS | ||
| 129 | |||
| 130 | This program is very conservative in what it removes from a given set of input | ||
| 131 | bigrams. Just because a unigram occurs in the test set of data does not mean that a | ||
| 132 | bigram that contains it must occur. So reduce-count.pl very likely leaves in place | ||
| 133 | some bigrams that do not occur in a given set of test data. However, it can be applied | ||
| 134 | to bigrams, co-occurrence and target-co-occurrences equally well, since it is only | ||
| 135 | looking for a unigram and not an exact match between bigrams (where order matters). | ||
| 136 | |||
| 137 | It must also be remembered that this program was originally intended for use with | ||
| 138 | huge-count.pl, a program from the Ngram Statistics Package that calculates count | ||
| 139 | information on very large corpora. So if you have 1,000,000 different bigrams, | ||
| 140 | then removing all of those that don't contain a given set of unigrams drawn from a | ||
| 141 | much smaller sample of test data will make a very large difference. | ||
| 142 | |||
| 143 | It seems like it should be possible to be more aggressive and find, for example, the | ||
| 144 | intersection of the bigrams found in training and test data, and reset the features to | ||
| 145 | that. | ||
| 146 | |||
| 147 | It also seems like it might be useful to reduce the number of unigram features in the | ||
| 148 | same way. | ||
| 149 | |||
| 150 | This might be replaceable with a simple program that finds the intersection of | ||
| 151 | features observed in training data and those that actually exist in the test data. | ||
| 152 | |||
| 153 | =head1 AUTHORS | ||
| 154 | |||
| 155 | Amruta Purandare, University of Pittsburgh | ||
| 156 | |||
| 157 | Ted Pedersen, University of Minnesota, Duluth | ||
| 158 | tpederse at d.umn.edu | ||
| 159 | |||
| 160 | =head1 COPYRIGHT | ||
| 161 | |||
| 162 | Copyright (c) 2002-2008, Amruta Purandare and Ted Pedersen | ||
| 163 | |||
| 164 | This program is free software; you can redistribute it and/or modify it under | ||
| 165 | the terms of the GNU General Public License as published by the Free Software | ||
| 166 | Foundation; either version 2 of the License, or (at your option) any later | ||
| 167 | version. | ||
| 168 | |||
| 169 | This program is distributed in the hope that it will be useful, but WITHOUT | ||
| 170 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS | ||
| 171 | FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. | ||
| 172 | |||
| 173 | You should have received a copy of the GNU General Public License along with | ||
| 174 | this program; if not, write to | ||
| 175 | |||
| 176 | The Free Software Foundation, Inc., | ||
| 177 | 59 Temple Place - Suite 330, | ||
| 178 | Boston, MA 02111-1307, USA. | ||
| 179 | |||
| 180 | =cut | ||
| 181 | |||
| 182 | ############################################################################### | ||
| 183 | |||
| 184 | # THE CODE STARTS HERE | ||
| 185 | |||
| 186 | #$0 contains the program name along with | ||
| 187 | #the complete path. Extract just the program | ||
| 188 | #name and use in error messages | ||
| 189 | $0=~s/.*\/(.+)/$1/; | ||
| 190 | |||
| 191 | ############################################################################### | ||
| 192 | |||
| 193 | # ================================ | ||
| 194 | # COMMAND LINE OPTIONS AND USAGE | ||
| 195 | # ================================ | ||
| 196 | |||
| 197 | # command line options | ||
| 198 | use Getopt::Long; | ||
| 199 | GetOptions ("help","version"); | ||
| 200 | # show help option | ||
| 201 | if(defined $opt_help) | ||
| 202 | { | ||
| 203 | $opt_help=1; | ||
| 204 | &showhelp(); | ||
| 205 | exit; | ||
| 206 | } | ||
| 207 | |||
| 208 | # show version information | ||
| 209 | if(defined $opt_version) | ||
| 210 | { | ||
| 211 | $opt_version=1; | ||
| 212 | &showversion(); | ||
| 213 | exit; | ||
| 214 | } | ||
| 215 | |||
| 216 | # show minimal usage message if fewer arguments | ||
| 217 | if($#ARGV<1) | ||
| 218 | { | ||
| 219 | &showminimal(); | ||
| 220 | exit; | ||
| 221 | } | ||
| 222 | |||
| 223 | ############################################################################# | ||
| 224 | |||
| 225 | # ================================ | ||
| 226 | # INITIALIZATION AND INPUT | ||
| 227 | # ================================ | ||
| 228 | |||
| 229 | #accept the BIGRAM file name | ||
| 230 | $bigfile=$ARGV[0]; | ||
| 231 | if(!-e $bigfile) | ||
| 232 | { | ||
| 233 | print STDERR "ERROR($0): | ||
| 234 | BIGRAM file <$bigfile> doesn't exist...\n"; | ||
| 235 | exit; | ||
| 236 | } | ||
| 237 | open(BIG,$bigfile) || die "Error($0): | ||
| 238 | Error(code=$!) in opening BIGRAM file <$bigfile>.\n"; | ||
| 239 | |||
| 240 | #accept the UNIGRAM file name | ||
| 241 | $unifile=$ARGV[1]; | ||
| 242 | if(!-e $unifile) | ||
| 243 | { | ||
| 244 | print STDERR "ERROR($0): | ||
| 245 | UNIGRAM file <$unifile> doesn't exist...\n"; | ||
| 246 | exit; | ||
| 247 | } | ||
| 248 | open(UNI,$unifile) || die "Error($0): | ||
| 249 | Error(code=$!) in opening UNIGRAM file <$unifile>.\n"; | ||
| 250 | |||
| 251 | ############################################################################## | ||
| 252 | |||
| 253 | # read the UNIGRAM file and store the list of unigrams | ||
| 254 | # we assume that the UNIGRAM file is small and loadable | ||
| 255 | |||
| 256 | $line_num=0; | ||
| 257 | while(<UNI>) | ||
| 258 | { | ||
| 259 | $line_num++; | ||
| 260 | # checking if valid unigram file | ||
| 261 | if($line_num==1 && /^\d+\s*$/) | ||
| 262 | { | ||
| 263 | next; | ||
| 264 | } | ||
| 265 | $uni_string=$_; | ||
| 266 | $diamonds=0; | ||
| 267 | while(/<>/) | ||
| 268 | { | ||
| 269 | $_=$'; | ||
| 270 | $diamonds++; | ||
| 271 | } | ||
| 272 | if($diamonds != 1) | ||
| 273 | { | ||
| 274 | print STDERR "ERROR($0): | ||
| 275 | UNIGRAM file $unifile is not a valid UNIGRAM output of NSP at line | ||
| 276 | $line_num.\n"; | ||
| 277 | exit 1; | ||
| 278 | } | ||
| 279 | if($uni_string=~/^(.*)<>\d*\s*$/) | ||
| 280 | { | ||
| 281 | $unigram{$1}=1; | ||
| 282 | } | ||
| 283 | } | ||
| 284 | |||
| 285 | # reading the BIGRAM file and printing bigrams that have | ||
| 286 | # at least one of the words present in the UNIGRAM | ||
| 287 | |||
| 288 | $line_num=0; | ||
| 289 | while(<BIG>) | ||
| 290 | { | ||
| 291 | if(/<>/) | ||
| 292 | { | ||
| 293 | if(/^(.*)<>(.*)<>\d+\s+\d+\s+\d+\s*$/ || /^(.*)<>(.*)<>\d+\s+-?\d*\.?\d+\s+\d+\s+\d+\s+\d+\s*$/) | ||
| 294 | { | ||
| 295 | if(!defined $unigram{$1} && !defined $unigram{$2}) | ||
| 296 | { | ||
| 297 | next; | ||
| 298 | } | ||
| 299 | } | ||
| 300 | else | ||
| 301 | { | ||
| 302 | print STDERR "ERROR($0): | ||
| 303 | BIGRAM file $bigfile is not a valid bigram output of NSP at line $line_num.\n"; | ||
| 304 | exit 1; | ||
| 305 | } | ||
| 306 | } | ||
| 307 | print; | ||
| 308 | } | ||
| 309 | |||
| 310 | ############################################################################## | ||
| 311 | |||
| 312 | # ========================== | ||
| 313 | # SUBROUTINE SECTION | ||
| 314 | # ========================== | ||
| 315 | |||
| 316 | #----------------------------------------------------------------------------- | ||
| 317 | #show minimal usage message | ||
| 318 | sub showminimal() | ||
| 319 | { | ||
| 320 | print "Usage: reduce-count.pl [OPTIONS] BIGRAM UNIGRAM"; | ||
| 321 | print "\nTYPE reduce-count.pl --help for help\n"; | ||
| 322 | } | ||
| 323 | |||
| 324 | #----------------------------------------------------------------------------- | ||
| 325 | #show help | ||
| 326 | sub showhelp() | ||
| 327 | { | ||
| 328 | print "Usage: reduce-count.pl [OPTIONS] BIGRAM UNIGRAM | ||
| 329 | |||
| 330 | Reduces the number of entries in the given BIGRAM file by removing those | ||
| 331 | bigrams where both words do not appear in the UNIGRAM file. | ||
| 332 | |||
| 333 | BIGRAM | ||
| 334 | Bigram output of NSP showing the bigram counts or statistical | ||
| 335 | scores. This can also be a file of co-occurrences from combig.pl | ||
| 336 | |||
| 337 | UNIGRAM | ||
| 338 | Unigram output of NSP showing the unigrams with their optional | ||
| 339 | frequency counts. | ||
| 340 | |||
| 341 | OPTIONS: | ||
| 342 | |||
| 343 | --help | ||
| 344 | Displays this message. | ||
| 345 | --version | ||
| 346 | Displays the version information. | ||
| 347 | Type 'perldoc reduce-count.pl' to view the detailed documentation of | ||
| 348 | reduce-count.\n"; | ||
| 349 | } | ||
| 350 | |||
| 351 | #------------------------------------------------------------------------------ | ||
| 352 | #version information | ||
| 353 | sub showversion() | ||
| 354 | { | ||
| 355 | print '$Id: reduce-count.pl,v 1.13 2008/03/30 04:19:01 tpederse Exp $'; | ||
| 356 | print "\nRemove features that don't occur in evaluation data\n"; | ||
| 357 | ## print "\nCopyright (c) 2002-2006, Ted Pedersen & Amruta Purandare\n"; | ||
| 358 | ### print "Date of Last Update: 05/25/2004\n"; | ||
| 359 | } | ||
| 360 | |||
| 361 | ############################################################################# | ||
| 362 |