| 1 | #!/usr/local/bin/perl -w | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use vars qw($VERSION); | ||
| 5 | #<? read_starfish_conf(); echo "\$VERSION = $ModuleVersion;"; !>#+ | ||
| 6 | $VERSION = 2.007;#- | ||
| 7 | #+ | ||
| 8 | $VERSION = 2.007; | ||
| 9 | #- | ||
| 10 | # $Revision: 1.26 $ | ||
| 11 | |||
| 12 | use Text::Ngrams; | ||
| 13 | use Getopt::Long; | ||
| 14 | |||
| 15 | my ($help, $version, $orderby, $onlyfirst, $limit, $spartan); | ||
| 16 | my $n = 3; | ||
| 17 | my $type = 'character'; | ||
| 18 | |||
| 19 | sub help { | ||
| 20 | print <<EOF; | ||
| 21 | Usage: $0 [options] [files] | ||
| 22 | Compute the ngram frequencies and produce tables to the stdout. | ||
| 23 | Options: | ||
| 24 | --n=N The default is 3-grams. | ||
| 25 | --normalize Produce normalized frequencies (divided by the total | ||
| 26 | number of n-grams of the same size) | ||
| 27 | --type=T The default is character. Other types include: byte, | ||
| 28 | words, utf8, or there can be user-defined types. | ||
| 29 | --limit=N Limit the number of distinct n-grams. | ||
| 30 | BEWARE: Final tables may be inaccurate if limit is used. | ||
| 31 | --help Show this help. | ||
| 32 | --version Show version. | ||
| 33 | --orderby=ARG ARG can be: frequency or ngram. | ||
| 34 | --onlyfirst=N Only first N ngrams are printed for each n. | ||
| 35 | --spartan If specified, only the n-grams of maximal length are | ||
| 36 | printed. | ||
| 37 | |||
| 38 | The options can be shortened to their unique prefixes and | ||
| 39 | the two dashes to one dash. No files means using STDIN. | ||
| 40 | |||
| 41 | NOTE: The documentation of the module Text::Ngrams.pl provides more | ||
| 42 | information. | ||
| 43 | EOF | ||
| 44 | exit(1); | ||
| 45 | } | ||
| 46 | |||
| 47 | my ($opt_normalize); | ||
| 48 | |||
| 49 | help() | ||
| 50 | unless | ||
| 51 | GetOptions('n=i' => \$n, | ||
| 52 | 'normalize' => \$opt_normalize, | ||
| 53 | 'type=s' => \$type, | ||
| 54 | 'limit=i' => \$limit, | ||
| 55 | 'help' => \$help, | ||
| 56 | 'version' => \$version, | ||
| 57 | 'orderby=s' => \$orderby, | ||
| 58 | 'onlyfirst=i' => \$onlyfirst, | ||
| 59 | 'spartan' => \$spartan); | ||
| 60 | |||
| 61 | help() if $n < 1 || int($n) != $n; | ||
| 62 | |||
| 63 | sub version { | ||
| 64 | print $VERSION, "\n"; | ||
| 65 | exit(1); | ||
| 66 | } | ||
| 67 | |||
| 68 | help() if $help; | ||
| 69 | version() if $version; | ||
| 70 | |||
| 71 | my %params = ( windowsize=>$n, type=>$type); | ||
| 72 | |||
| 73 | if (defined($limit) and ($limit > 0)) { $params{'limit'} = $limit } | ||
| 74 | |||
| 75 | my $ng = Text::Ngrams->new( %params ); | ||
| 76 | |||
| 77 | if ($#ARGV > -1) { $ng->process_files(@ARGV) } | ||
| 78 | else { $ng->process_files(\*STDIN) } | ||
| 79 | |||
| 80 | %params = ( 'out' => \*STDOUT ); | ||
| 81 | if (defined($orderby) and $orderby) { $params{'orderby'} = $orderby } | ||
| 82 | if (defined($onlyfirst) and $onlyfirst>0) { $params{'onlyfirst'} = $onlyfirst } | ||
| 83 | if ($opt_normalize) { $params{'normalize'} = $opt_normalize } | ||
| 84 | if ($spartan) { $params{'spartan'} = $spartan } | ||
| 85 | |||
| 86 | print $ng->to_string( %params ); | ||
| 87 | |||
| 88 | exit(0); | ||
| 89 | |||
| 90 | __END__ |