| 1 | #!/usr/local/bin/perl | ||
| 2 | #! /usr/bin/perl | ||
| 3 | |||
| 4 | ##---------------------------------------------------------------------------## | ||
| 5 | ## Author: Hugo WL ter Doest terdoest@cs.utwente.nl | ||
| 6 | ## Description: Wrapper around Statistics::MaxEntropy | ||
| 7 | ## | ||
| 8 | ##---------------------------------------------------------------------------## | ||
| 9 | ## Copyright (C) 1998, 1999 Hugo WL ter Doest terdoest@cs.utwente.nl | ||
| 10 | ## | ||
| 11 | ## This program is free software; you can redistribute it and/or modify | ||
| 12 | ## it under the terms of the GNU General Public License as published by | ||
| 13 | ## the Free Software Foundation; either version 2 of the License, or | ||
| 14 | ## (at your option) any later version. | ||
| 15 | ## | ||
| 16 | ## This program is distributed in the hope that it will be useful, | ||
| 17 | ## but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 18 | ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| 19 | ## GNU General Public License for more details. | ||
| 20 | ## | ||
| 21 | ## You should have received a copy of the GNU General Public License | ||
| 22 | ## along with this program; if not, write to the Free Software | ||
| 23 | ## Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. | ||
| 24 | ##---------------------------------------------------------------------------## | ||
| 25 | |||
| 26 | |||
| 27 | |||
| 28 | ##---------------------------------------------------------------------------## | ||
| 29 | ## Require libraries | ||
| 30 | ##---------------------------------------------------------------------------## | ||
| 31 | use strict; | ||
| 32 | use diagnostics -verbose; | ||
| 33 | use Getopt::Long; | ||
| 34 | use Statistics::MaxEntropy qw($debug | ||
| 35 | $KL_max_it | ||
| 36 | $NEWTON_max_it | ||
| 37 | $KL_min | ||
| 38 | $NEWTON_min | ||
| 39 | $SAMPLE_size | ||
| 40 | ); | ||
| 41 | use Statistics::Candidates; | ||
| 42 | |||
| 43 | use vars qw($PROG | ||
| 44 | $VERSION | ||
| 45 | $help | ||
| 46 | $events | ||
| 47 | $candidates | ||
| 48 | $i_dump_file | ||
| 49 | $o_dump_file | ||
| 50 | $events_file | ||
| 51 | $new_events_file | ||
| 52 | $parameters_file | ||
| 53 | $parameters_with_names_file | ||
| 54 | $new_parameters_file | ||
| 55 | $candidates_file | ||
| 56 | $new_candidates_file | ||
| 57 | $nr_to_add | ||
| 58 | $integer | ||
| 59 | $GIS | ||
| 60 | $IIS | ||
| 61 | $MC | ||
| 62 | $CORPUS | ||
| 63 | $ENUM); | ||
| 64 | |||
| 65 | |||
| 66 | |||
| 67 | ##---------------------------------------------------------------------------## | ||
| 68 | ## Globals | ||
| 69 | ##---------------------------------------------------------------------------## | ||
| 70 | ($PROG = $0) =~ s/.*\///; | ||
| 71 | $VERSION = "0.2"; | ||
| 72 | |||
| 73 | |||
| 74 | ##---------------------------------------------------------------------------## | ||
| 75 | ## Routines | ||
| 76 | ##---------------------------------------------------------------------------## | ||
| 77 | |||
| 78 | # parse command line and read some files | ||
| 79 | sub prologue { | ||
| 80 | my($integerstring); | ||
| 81 | |||
| 82 | print "$0 $VERSION\n"; | ||
| 83 | GetOptions("help!", \$help, | ||
| 84 | "debug!" , \$debug, | ||
| 85 | "i_events=s" , \$events_file, | ||
| 86 | "i_parameters=s" , \$parameters_file, | ||
| 87 | "i_candidates=s" , \$candidates_file, | ||
| 88 | "o_events=s" , \$new_events_file, | ||
| 89 | "o_candidates=s" , \$new_candidates_file, | ||
| 90 | "o_parameters=s" , \$new_parameters_file, | ||
| 91 | "special=s", \$parameters_with_names_file, | ||
| 92 | "i_dump=s", \$i_dump_file, | ||
| 93 | "o_dump=s", \$o_dump_file, | ||
| 94 | "integer!", \$integer, | ||
| 95 | "KL_max_it=i", \$KL_max_it, | ||
| 96 | "NEWTON_max_it=i", \$NEWTON_max_it, | ||
| 97 | "KL_min=f", \$KL_min, | ||
| 98 | "NEWTON_min=f", \$NEWTON_min, | ||
| 99 | "nr_to_add=i", \$nr_to_add, | ||
| 100 | "GIS!", \$GIS, | ||
| 101 | "IIS!", \$IIS, | ||
| 102 | "MC!", \$MC, | ||
| 103 | "CORPUS!", \$CORPUS, | ||
| 104 | "ENUM!", \$ENUM, | ||
| 105 | "SAMPLE=i", \$SAMPLE_size | ||
| 106 | ); | ||
| 107 | |||
| 108 | if ($help) { | ||
| 109 | usage(); | ||
| 110 | } | ||
| 111 | |||
| 112 | $integerstring = "binary"; | ||
| 113 | if ($integer) { | ||
| 114 | $integerstring = "integer"; | ||
| 115 | } | ||
| 116 | |||
| 117 | if ($events_file) { | ||
| 118 | $events = Statistics::MaxEntropy->new($integerstring, $events_file); | ||
| 119 | } | ||
| 120 | elsif ($i_dump_file) { | ||
| 121 | $events = Statistics::MaxEntropy->undump($i_dump_file); | ||
| 122 | } | ||
| 123 | if ($parameters_file) { | ||
| 124 | $events->read_parameters($parameters_file); | ||
| 125 | } | ||
| 126 | if ($candidates_file) { | ||
| 127 | $candidates = Statistics::Candidates->new($candidates_file); | ||
| 128 | } | ||
| 129 | } | ||
| 130 | |||
| 131 | |||
| 132 | sub usage { | ||
| 133 | print STDOUT <<EndOfUsage; | ||
| 134 | $main::PROG -- a wrapper around Statistics::MaxEntropy for easy use | ||
| 135 | |||
| 136 | ME.wrapper.pl --help | ||
| 137 | --debug | ||
| 138 | --i_events <filename> | ||
| 139 | --i_candidates <filename> | ||
| 140 | --i_dump <filename> | ||
| 141 | --o_events <filename> | ||
| 142 | --o_candidates <filename> | ||
| 143 | --o_parameters <filename> | ||
| 144 | --special <filename> | ||
| 145 | --o_dump <filename> | ||
| 146 | --integer | ||
| 147 | --KL_max_it <integer> | ||
| 148 | --NEWTON_max_it <integer> | ||
| 149 | --KL_min <float> | ||
| 150 | --NEWTON_min <float> | ||
| 151 | --nr_to_add <integer> | ||
| 152 | --SAMPLE <integer> | ||
| 153 | --GIS | ||
| 154 | --IIS | ||
| 155 | --MC | ||
| 156 | --CORPUS | ||
| 157 | --ENUM | ||
| 158 | |||
| 159 | Version: $main::VERSION | ||
| 160 | Copyright (C) 1998 Hugo WL ter Doest, terdoest\@cs.utwente.nl | ||
| 161 | $main::PROG comes with ABSOLUTELY NO WARRANTY and may be copied | ||
| 162 | only under the terms of the GNU General Public License (version 2, or | ||
| 163 | later), which may be found in the distribution. | ||
| 164 | EndOfUsage | ||
| 165 | |||
| 166 | exit 0; | ||
| 167 | } | ||
| 168 | |||
| 169 | |||
| 170 | # run the feature induction algorithm, using either GIS or IIS | ||
| 171 | sub run { | ||
| 172 | my($scale_string, | ||
| 173 | $sampling_string); | ||
| 174 | |||
| 175 | $scale_string = "iis"; # default | ||
| 176 | if ($GIS) { | ||
| 177 | $scale_string = "gis"; | ||
| 178 | } | ||
| 179 | $sampling_string = "corpus"; # default | ||
| 180 | if ($MC) { | ||
| 181 | $sampling_string = "mc"; | ||
| 182 | } | ||
| 183 | if ($CORPUS) { | ||
| 184 | $sampling_string = "corpus"; | ||
| 185 | } | ||
| 186 | if ($ENUM) { | ||
| 187 | $sampling_string = "enum"; | ||
| 188 | } | ||
| 189 | if ($nr_to_add && $candidates) { | ||
| 190 | $events->fi($scale_string, | ||
| 191 | $candidates, | ||
| 192 | $nr_to_add, | ||
| 193 | $sampling_string); | ||
| 194 | } | ||
| 195 | elsif ($events) { | ||
| 196 | $events->scale($sampling_string, $scale_string); | ||
| 197 | } | ||
| 198 | } | ||
| 199 | |||
| 200 | |||
| 201 | # write some information files | ||
| 202 | sub epilogue { | ||
| 203 | if ($events) { | ||
| 204 | if ($parameters_with_names_file) { | ||
| 205 | $events->write_parameters_with_names($parameters_with_names_file); | ||
| 206 | } | ||
| 207 | if ($new_parameters_file) { | ||
| 208 | $events->write_parameters($new_parameters_file); | ||
| 209 | } | ||
| 210 | if ($new_events_file) { | ||
| 211 | $events->write($new_events_file); | ||
| 212 | } | ||
| 213 | if ($o_dump_file) { | ||
| 214 | $events->dump($o_dump_file); | ||
| 215 | } | ||
| 216 | } | ||
| 217 | if ($candidates) { | ||
| 218 | if ($new_candidates_file) { | ||
| 219 | $candidates->write($new_candidates_file); | ||
| 220 | } | ||
| 221 | } | ||
| 222 | } | ||
| 223 | |||
| 224 | |||
| 225 | ##------------[MAIN]---------------------------------------------------------## | ||
| 226 | |||
| 227 | prologue(); | ||
| 228 | run(); | ||
| 229 | epilogue(); | ||
| 230 | |||
| 231 | ##------------[MAIN]---------------------------------------------------------## | ||
| 232 | |||
| 233 | |||
| 234 | # Below is the stub of documentation for your module. You better edit it! | ||
| 235 | |||
| 236 | =head1 NAME | ||
| 237 | |||
| 238 | ME.wrapper.pl - a wrapper around C<Statistics::MaxEntropy> and C<Statistics::Candidates> | ||
| 239 | |||
| 240 | |||
| 241 | =head1 SYNOPSIS | ||
| 242 | |||
| 243 | ME.wrapper.pl --help | ||
| 244 | --debug | ||
| 245 | --i_events <filename> | ||
| 246 | --i_candidates <filename> | ||
| 247 | --i_dump <filename> | ||
| 248 | --o_events <filename> | ||
| 249 | --o_candidates <filename> | ||
| 250 | --o_parameters <filename> | ||
| 251 | --special <filename> | ||
| 252 | --o_dump <filename> | ||
| 253 | --integer | ||
| 254 | --KL_max_it <integer> | ||
| 255 | --NEWTON_max_it <integer> | ||
| 256 | --KL_min <float> | ||
| 257 | --NEWTON_min <float> | ||
| 258 | --nr_to_add <integer> | ||
| 259 | --SAMPLE <integer> | ||
| 260 | --GIS | ||
| 261 | --IIS | ||
| 262 | --MC | ||
| 263 | --CORPUS | ||
| 264 | --ENUM | ||
| 265 | |||
| 266 | |||
| 267 | =head1 DESCRIPTION | ||
| 268 | |||
| 269 | C<ME.wrapper.pl> is a command-line interface to | ||
| 270 | C<Statistics::MaxEntropy> and C<Statistics::Candidates>. The wrapper | ||
| 271 | and its command line options provide an easy-to-use and transparent | ||
| 272 | connection to the MaxEntropy modules. Below we explain the meaning of | ||
| 273 | the options. | ||
| 274 | |||
| 275 | |||
| 276 | =head1 COMMAND LINE ARGUMENTS | ||
| 277 | |||
| 278 | We explain the command line options, and state the at which moment | ||
| 279 | they are applied or executed. For this we assume the main program of | ||
| 280 | C<ME.wrapper.pl> to have the following form | ||
| 281 | |||
| 282 | prologue(); | ||
| 283 | run(); | ||
| 284 | epilogue(); | ||
| 285 | |||
| 286 | If both candidates and events are specified, the feature induction | ||
| 287 | algorithm is called. If only events are specified a scaling algorithm | ||
| 288 | is called (GIS by default). | ||
| 289 | |||
| 290 | =over 4 | ||
| 291 | |||
| 292 | =item C<--integer> | ||
| 293 | |||
| 294 | Specifies whether the feature functions should be interpreted as | ||
| 295 | binary or integer functions. | ||
| 296 | |||
| 297 | =item C<--KL_max_it integer> | ||
| 298 | |||
| 299 | (set in prologue) The maximum number of iterations performed by the | ||
| 300 | scaling algorithms. | ||
| 301 | |||
| 302 | =item C<--NEWTON_max_it integer> | ||
| 303 | |||
| 304 | (set in prologue) The maximum number of iteration in Newton's method | ||
| 305 | (IIS only). | ||
| 306 | |||
| 307 | =item C<--KL_min integer> | ||
| 308 | |||
| 309 | (set in prologue) The minimum difference in Kullback-Leibler | ||
| 310 | divergence that a new scale iteration should bring. Otherwise Scaling | ||
| 311 | is stopped. | ||
| 312 | |||
| 313 | =item C<--NEWTON_min float> | ||
| 314 | |||
| 315 | (set in prologue) The minimum difference between the new x and the old | ||
| 316 | x in Newton's method (IIS only). | ||
| 317 | |||
| 318 | =item C<--nr_to_add integer> | ||
| 319 | |||
| 320 | (used in run) Passed to the feature induction algorithm (if | ||
| 321 | called). It states the number of candidates that should be added. | ||
| 322 | |||
| 323 | =item C<--SAMPLE integer> | ||
| 324 | |||
| 325 | (used in run) Passed to the feature induction algorithm (if | ||
| 326 | called). It determines the size of the Monte Carlo sample. Only makes | ||
| 327 | sense if C<--MC> is set. | ||
| 328 | |||
| 329 | =item C<--GIS> | ||
| 330 | |||
| 331 | (used in run) Sets the scaling algorithm to to Generalised Iterative | ||
| 332 | Scaling. | ||
| 333 | |||
| 334 | =item C<--IIS> | ||
| 335 | |||
| 336 | (used in run) Sets the scaling algorithm to Improved Iterative | ||
| 337 | Scaling. | ||
| 338 | |||
| 339 | =item C<--MC> | ||
| 340 | |||
| 341 | (used in run) Sets the sampling method to Monte Carlo. See also the | ||
| 342 | C<--SAMPLE> option. | ||
| 343 | |||
| 344 | =item C<--CORPUS> | ||
| 345 | |||
| 346 | (used in run) Tells the scaling algorithm to consider the event space | ||
| 347 | a good sample (risky: overtraining). | ||
| 348 | |||
| 349 | =item C<--ENUM> | ||
| 350 | |||
| 351 | (used in run) For scaling the complete event space (all bitvectors) | ||
| 352 | should be enumerated. This is done in memory, so beware! | ||
| 353 | |||
| 354 | =item C<--help> | ||
| 355 | |||
| 356 | (done in prologue) Exits after showing the name of the program, and | ||
| 357 | the list of command line options. | ||
| 358 | |||
| 359 | =item C<--debug> | ||
| 360 | |||
| 361 | (set in prologue) Tells the C<MaxEntropy> and C<Candidates> modules to | ||
| 362 | output a lot of text. | ||
| 363 | |||
| 364 | =item C<--i_events filename> | ||
| 365 | |||
| 366 | (done in prologue) The events are read from <filename>. | ||
| 367 | |||
| 368 | =item C<--i_candidates filename> | ||
| 369 | |||
| 370 | (done in prologue) The candidates are read from <filename>. | ||
| 371 | |||
| 372 | =item C<--i_dump filename> | ||
| 373 | |||
| 374 | (done in prologue) An event space read from the dump in | ||
| 375 | <filename>. This option overrules C<--i_events> option. | ||
| 376 | |||
| 377 | =item C<--o_events filename> | ||
| 378 | |||
| 379 | (done in epilogue) The events (including candidates that were added) | ||
| 380 | are written to C<filename>. | ||
| 381 | |||
| 382 | =item C<--o_candidates filename> | ||
| 383 | |||
| 384 | (done in epilogue) The candidates (if present) are written to | ||
| 385 | C<filename>. Only candidates that were not added to the event space | ||
| 386 | are written. | ||
| 387 | |||
| 388 | =item C<--o_parameters filename> | ||
| 389 | |||
| 390 | (done in epilogue) The parameters are written to C<filename>. | ||
| 391 | |||
| 392 | =item C<--special filename> | ||
| 393 | |||
| 394 | (done in epilogue) The parameters are written to C<filename> in a | ||
| 395 | B<special> format I like. | ||
| 396 | |||
| 397 | =item C<--o_dump filename> | ||
| 398 | |||
| 399 | (done in epilogue) The event space is dumped to C<filename>. It can | ||
| 400 | be read in again using C<--i_dump> (the next time you use | ||
| 401 | C<ME.wrapper.pl>). | ||
| 402 | |||
| 403 | =back | ||
| 404 | |||
| 405 | |||
| 406 | =head1 BUGS | ||
| 407 | |||
| 408 | Options C<--MC>, C<--CORPUS>, C<--ENUM> should be put under one | ||
| 409 | argument that has a parameter, for instance C<--sample_type [corpus, | ||
| 410 | enum, mc]>. | ||
| 411 | |||
| 412 | |||
| 413 | =head1 SEE ALSO | ||
| 414 | |||
| 415 | L<perl(1)>, L<Statistics::SparseVector(3)> | ||
| 416 | L<Statistics::Candidates(3)>, L<Statistics::MaxEntropy(3)>. | ||
| 417 | |||
| 418 | |||
| 419 | =head1 VERSION | ||
| 420 | |||
| 421 | Version 0.2. | ||
| 422 | |||
| 423 | |||
| 424 | =head1 AUTHOR | ||
| 425 | |||
| 426 | =begin roff | ||
| 427 | |||
| 428 | Hugo WL ter Doest, terdoest@cs.utwente.nl | ||
| 429 | |||
| 430 | =end roff | ||
| 431 | |||
| 432 | =begin latex | ||
| 433 | |||
| 434 | Hugo WL ter Doest, \texttt{terdoest\symbol{'100}cs.utwente.nl} | ||
| 435 | |||
| 436 | =end latex | ||
| 437 | |||
| 438 | |||
| 439 | =head1 COPYRIGHT | ||
| 440 | |||
| 441 | =begin roff | ||
| 442 | |||
| 443 | Copyright (C) 1998, 1999 Hugo WL ter Doest, terdoest@cs.utwente.nl | ||
| 444 | Univ. of Twente, Dept. of Comp. Sc., Parlevink Research, Enschede, | ||
| 445 | The Netherlands. | ||
| 446 | |||
| 447 | =end roff | ||
| 448 | |||
| 449 | =begin latex | ||
| 450 | |||
| 451 | \copyright 1998, 1999 Hugo WL ter Doest, \texttt{terdoest\symbol{'100}cs.utwente.nl} | ||
| 452 | Univ. of Twente, Dept. of Comp. Sc., Parlevink Research, Enschede, | ||
| 453 | The Netherlands. | ||
| 454 | |||
| 455 | =end latex | ||
| 456 | |||
| 457 | C<ME.wrapper.pl> comes with ABSOLUTELY NO WARRANTY and may be copied | ||
| 458 | only under the terms of the GNU Library General Public License (version 2, or | ||
| 459 | later), which may be found in the distribution. | ||
| 460 | |||
| 461 | =cut |