| 1 | #!/usr/bin/perl -w | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 6 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 6 | ▶ | use vars qw($VERSION); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | #<? read_starfish_conf(); echo "\$VERSION = $ModuleVersion;"; !>#+ | ||||||||||||||||||||||||||||
| 6 | 6 | $VERSION = 2.007;#- | |||||||||||||||||||||||||||
| 7 | #+ | ||||||||||||||||||||||||||||
| 8 | 6 | $VERSION = 2.007; | |||||||||||||||||||||||||||
| 9 | #- | ||||||||||||||||||||||||||||
| 10 | # $Revision: 1.26 $ | ||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | 6 | ▶ | use Text::Ngrams; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | 6 | ▶ | use Getopt::Long; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | 6 | my ($help, $version, $orderby, $onlyfirst, $limit, $spartan); | |||||||||||||||||||||||||||
| 16 | 6 | my $n = 3; | |||||||||||||||||||||||||||
| 17 | 6 | my $type = 'character'; | |||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | 0 | ▶ | sub help { | ||||||||||||||||||||||||||
|
Subroutine
help: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 20 | 0 | 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 | 0 | exit(1); | |||||||||||||||||||||||||||
| 45 | } | ||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | 6 | my ($opt_normalize); | |||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | 6 | ▶ | help() | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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 | 6 | ▶ | help() if $n < 1 || int($n) != $n; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$n < 1 or int $n != $n
MC/DC0%0/2
$n < 1 or int $n != $n
$n < 1
int $n != $n
Truth table33%1/3
A or B
A= $n < 1B= int $n != $n
| |||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | 0 | ▶ | sub version { | ||||||||||||||||||||||||||
|
Subroutine
version: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 64 | 0 | print $VERSION, "\n"; | |||||||||||||||||||||||||||
| 65 | 0 | exit(1); | |||||||||||||||||||||||||||
| 66 | } | ||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | 6 | ▶ | help() if $help; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 69 | 6 | ▶ | version() if $version; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | 6 | my %params = ( windowsize=>$n, type=>$type); | |||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | 0 | ▶ | if (defined($limit) and ($limit > 0)) { $params{'limit'} = $limit } | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $limit and $limit > 0
MC/DC0%0/2
defined $limit and $limit > 0
defined $limit
$limit > 0
Truth table33%1/3
A and B
A= defined $limitB= $limit > 0
| |||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | 6 | my $ng = Text::Ngrams->new( %params ); | |||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | 6 | ▶ | if ($#ARGV > -1) { $ng->process_files(@ARGV) } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 78 | 0 | else { $ng->process_files(\*STDIN) } | |||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | 6 | %params = ( 'out' => \*STDOUT ); | |||||||||||||||||||||||||||
| 81 | 6 | ▶ | if (defined($orderby) and $orderby) { $params{'orderby'} = $orderby } | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $orderby and $orderby
MC/DC0%0/2
defined $orderby and $orderby
defined $orderby
$orderby
Truth table33%1/3
A and B
A= defined $orderbyB= $orderby
| |||||||||||||||||||||||||||||
| 82 | 6 | ▶ | if (defined($onlyfirst) and $onlyfirst>0) { $params{'onlyfirst'} = $onlyfirst } | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $onlyfirst and $onlyfirst > 0
MC/DC50%1/2
defined $onlyfirst and $onlyfirst > 0
defined $onlyfirst
$onlyfirst > 0
Truth table66%2/3
A and B
A= defined $onlyfirstB= $onlyfirst > 0
| |||||||||||||||||||||||||||||
| 83 | 6 | ▶ | if ($opt_normalize) { $params{'normalize'} = $opt_normalize } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 84 | 6 | ▶ | if ($spartan) { $params{'spartan'} = $spartan } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | 6 | print $ng->to_string( %params ); | |||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | 6 | exit(0); | |||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | __END__ | ||||||||||||||||||||||||||||