| 1 | # -*- Perl -*- | ||||||||||||||
| 2 | # | ||||||||||||||
| 3 | # randomly generates outputs from a given pattern | ||||||||||||||
| 4 | |||||||||||||||
| 5 | package Lingua::Awkwords; | ||||||||||||||
| 6 | |||||||||||||||
| 7 | 2 | ▶ | use strict; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 8 | 2 | ▶ | use warnings; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 9 | |||||||||||||||
| 10 | 2 | require Exporter; | |||||||||||||
| 11 | 2 | our @ISA = qw(Exporter); | |||||||||||||
| 12 | 2 | our @EXPORT_OK = qw/&percentize &set_filter &weights2str &weights_from/; | |||||||||||||
| 13 | |||||||||||||||
| 14 | 2 | ▶ | use Carp qw(croak); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 15 | 2 | ▶ | use Lingua::Awkwords::Parser; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 16 | 2 | ▶ | use Moo; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 17 | 2 | ▶ | use namespace::clean; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 18 | |||||||||||||||
| 19 | 2 | our $VERSION = '0.10'; | |||||||||||||
| 20 | |||||||||||||||
| 21 | has pattern => ( | ||||||||||||||
| 22 | is => 'rw', | ||||||||||||||
| 23 | 3 | ▶ | trigger => sub { | ||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||
| 24 | 3 | my ($self, $pat) = @_; | |||||||||||||
| 25 | 3 | $self->_set_tree(Lingua::Awkwords::Parser->new->from_string($pat)); | |||||||||||||
| 26 | }, | ||||||||||||||
| 27 | 2 | ); | |||||||||||||
| 28 | 2 | has tree => (is => 'rwp'); | |||||||||||||
| 29 | |||||||||||||||
| 30 | ######################################################################## | ||||||||||||||
| 31 | # | ||||||||||||||
| 32 | # FUNCTIONS | ||||||||||||||
| 33 | # | ||||||||||||||
| 34 | # TODO these probably should go in a ::Util module? | ||||||||||||||
| 35 | |||||||||||||||
| 36 | # utility routine that makes percentages of the presumably numeric | ||||||||||||||
| 37 | # values of the given hash reference | ||||||||||||||
| 38 | 1 | ▶ | sub percentize { | ||||||||||||
|
Subroutine
percentize: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||||||
| 39 | 1 | my ($href) = @_; | |||||||||||||
| 40 | 1 | my $sum = 0; | |||||||||||||
| 41 | 1 | my $min = ~0; | |||||||||||||
| 42 | 1 | for my $v (values %$href) { | |||||||||||||
| 43 | 4 | $sum += $v; | |||||||||||||
| 44 | 4 | ▶ | $min = $v if $v < $min; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 45 | } | ||||||||||||||
| 46 | 1 | ▶ | croak "sum of values cannot be 0" if $sum == 0; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 47 | 1 | for my $v (values %$href) { | |||||||||||||
| 48 | 4 | $v = $v / $sum * 100; | |||||||||||||
| 49 | } | ||||||||||||||
| 50 | } | ||||||||||||||
| 51 | |||||||||||||||
| 52 | # utility routine for use with ->walk | ||||||||||||||
| 53 | 3 | ▶ | sub set_filter { | ||||||||||||
|
Subroutine
set_filter: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||
| 54 | 3 | my $filter = shift; | |||||||||||||
| 55 | 19 | ▶ | return sub { | ||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||
| 56 | 19 | my $self = shift; | |||||||||||||
| 57 | 19 | ▶ | $self->filter_with($filter) if $self->can('filter_with'); | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 58 | 3 | }; | |||||||||||||
| 59 | } | ||||||||||||||
| 60 | |||||||||||||||
| 61 | 1 | ▶ | sub weights2str { | ||||||||||||
|
Subroutine
weights2str: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||
| 62 | 1 | my ($hr) = @_; | |||||||||||||
| 63 | return join '/', | ||||||||||||||
| 64 | 1 | ▶ | map { $hr->{$_} == 1 ? $_ : join('*', $_, $hr->{$_}) } sort keys %$hr; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 65 | } | ||||||||||||||
| 66 | |||||||||||||||
| 67 | 3 | ▶ | sub weights_from { | ||||||||||||
|
Subroutine
weights_from: called CC=8 SCAR=20.8
Pod
documented
| |||||||||||||||
| 68 | 3 | my ($input) = @_; | |||||||||||||
| 69 | |||||||||||||||
| 70 | 3 | my $type = ref $input; | |||||||||||||
| 71 | 3 | my $fh; | |||||||||||||
| 72 | |||||||||||||||
| 73 | 3 | ▶ | if ($type eq '') { | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 74 | 2 | open $fh, '<', \$input; | |||||||||||||
| 75 | } elsif ($type eq 'GLOB') { | ||||||||||||||
| 76 | 1 | $fh = $input; | |||||||||||||
| 77 | } else { | ||||||||||||||
| 78 | 0 | croak "unknown input type"; | |||||||||||||
| 79 | } | ||||||||||||||
| 80 | |||||||||||||||
| 81 | 3 | my (%first, %mid, %last, %all); | |||||||||||||
| 82 | |||||||||||||||
| 83 | 3 | ▶ | while (readline $fh) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 84 | 3 | chomp; | |||||||||||||
| 85 | LOOP: { | ||||||||||||||
| 86 | 3 | ▶ | redo LOOP if /\G\s+/cg; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 87 | # various \b{...} forms detailed in perlrebackslash may be | ||||||||||||||
| 88 | # better for word boundaries though require perl 5.22 or up | ||||||||||||||
| 89 | 72 | ▶ | if (m/\G\b(.)/cg) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 90 | 19 | $first{$1}++; | |||||||||||||
| 91 | 19 | $all{$1}++; | |||||||||||||
| 92 | 19 | redo LOOP; | |||||||||||||
| 93 | } | ||||||||||||||
| 94 | 53 | ▶ | if (m/\G(.)\b/cg) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 95 | 19 | $last{$1}++; | |||||||||||||
| 96 | 19 | $all{$1}++; | |||||||||||||
| 97 | 19 | redo LOOP; | |||||||||||||
| 98 | } | ||||||||||||||
| 99 | 34 | ▶ | if (m/\G\B(.)/cg) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 100 | 31 | $mid{$1}++; | |||||||||||||
| 101 | 31 | $all{$1}++; | |||||||||||||
| 102 | 31 | redo LOOP; | |||||||||||||
| 103 | } | ||||||||||||||
| 104 | } | ||||||||||||||
| 105 | } | ||||||||||||||
| 106 | |||||||||||||||
| 107 | 3 | return \%first, \%mid, \%last, \%all; | |||||||||||||
| 108 | } | ||||||||||||||
| 109 | |||||||||||||||
| 110 | ######################################################################## | ||||||||||||||
| 111 | # | ||||||||||||||
| 112 | # METHODS | ||||||||||||||
| 113 | |||||||||||||||
| 114 | # avoids need to say | ||||||||||||||
| 115 | # use Lingua::Awkwords::Parser; | ||||||||||||||
| 116 | # ... = Lingua::Awkwords::Parser->new->from_string(q{ ... | ||||||||||||||
| 117 | # in the calling code | ||||||||||||||
| 118 | 2 | ▶ | sub parse_string { | ||||||||||||
|
Subroutine
parse_string: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||
| 119 | 2 | my ($self_or_class, $str) = @_; | |||||||||||||
| 120 | 2 | return Lingua::Awkwords::Parser->new->from_string($str); | |||||||||||||
| 121 | } | ||||||||||||||
| 122 | |||||||||||||||
| 123 | 3 | ▶ | sub render { | ||||||||||||
|
Subroutine
render: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||
| 124 | 3 | my ($self) = @_; | |||||||||||||
| 125 | 3 | my $tree = $self->tree; | |||||||||||||
| 126 | 3 | ▶ | croak "no pattern supplied" if !defined $tree; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 127 | 2 | return $tree->render; | |||||||||||||
| 128 | } | ||||||||||||||
| 129 | |||||||||||||||
| 130 | 3 | ▶ | sub walk { | ||||||||||||
|
Subroutine
walk: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||
| 131 | 3 | my ($self, $callback) = @_; | |||||||||||||
| 132 | 3 | my $tree = $self->tree; | |||||||||||||
| 133 | 3 | ▶ | croak "no pattern supplied" if !defined $tree; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 134 | 2 | $tree->walk($callback); | |||||||||||||
| 135 | 2 | return; | |||||||||||||
| 136 | } | ||||||||||||||
| 137 | |||||||||||||||
| 138 | 2 | 1; | |||||||||||||
| 139 | __END__ | ||||||||||||||