| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
1
|
|
|
|
|
|
|
# -*- Perl -*- |
|
2
|
|
|
|
|
|
|
# |
|
3
|
|
|
|
|
|
|
# randomly generates outputs from a given pattern |
|
4
|
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
package Lingua::Awkwords; |
|
6
|
|
|
|
|
|
|
|
|
7
|
2
|
|
|
2
|
|
81858
|
use strict; |
|
|
2
|
|
|
|
|
11
|
|
|
|
2
|
|
|
|
|
47
|
|
|
8
|
2
|
|
|
2
|
|
8
|
use warnings; |
|
|
2
|
|
|
|
|
3
|
|
|
|
2
|
|
|
|
|
147
|
|
|
9
|
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
require Exporter; |
|
11
|
|
|
|
|
|
|
our @ISA = qw(Exporter); |
|
12
|
|
|
|
|
|
|
our @EXPORT_OK = qw/&percentize &set_filter &weights2str &weights_from/; |
|
13
|
|
|
|
|
|
|
|
|
14
|
2
|
|
|
2
|
|
14
|
use Carp qw(croak); |
|
|
2
|
|
|
|
|
4
|
|
|
|
2
|
|
|
|
|
79
|
|
|
15
|
2
|
|
|
2
|
|
250
|
use Lingua::Awkwords::Parser; |
|
|
2
|
|
|
|
|
3
|
|
|
|
2
|
|
|
|
|
50
|
|
|
16
|
2
|
|
|
2
|
|
8
|
use Moo; |
|
|
2
|
|
|
|
|
4
|
|
|
|
2
|
|
|
|
|
7
|
|
|
17
|
2
|
|
|
2
|
|
506
|
use namespace::clean; |
|
|
2
|
|
|
|
|
4
|
|
|
|
2
|
|
|
|
|
10
|
|
|
18
|
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
our $VERSION = '0.07'; |
|
20
|
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
has pattern => ( |
|
22
|
|
|
|
|
|
|
is => 'rw', |
|
23
|
|
|
|
|
|
|
trigger => sub { |
|
24
|
|
|
|
|
|
|
my ( $self, $pat ) = @_; |
|
25
|
|
|
|
|
|
|
$self->_set_tree( Lingua::Awkwords::Parser->new->from_string($pat) ); |
|
26
|
|
|
|
|
|
|
}, |
|
27
|
|
|
|
|
|
|
); |
|
28
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
sub percentize { |
|
39
|
1
|
|
|
1
|
1
|
4683
|
my ($href) = @_; |
|
40
|
1
|
|
|
|
|
2
|
my $sum = 0; |
|
41
|
1
|
|
|
|
|
1
|
my $min = ~0; |
|
42
|
1
|
|
|
|
|
3
|
for my $v ( values %$href ) { |
|
43
|
4
|
|
|
|
|
5
|
$sum += $v; |
|
44
|
4
|
100
|
|
|
|
8
|
$min = $v if $v < $min; |
|
45
|
|
|
|
|
|
|
} |
|
46
|
1
|
50
|
|
|
|
4
|
croak "sum of values cannot be 0" if $sum == 0; |
|
47
|
1
|
|
|
|
|
2
|
for my $v ( values %$href ) { |
|
48
|
4
|
|
|
|
|
8
|
$v = $v / $sum * 100; |
|
49
|
|
|
|
|
|
|
} |
|
50
|
|
|
|
|
|
|
} |
|
51
|
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
# utility routine for use with ->walk |
|
53
|
|
|
|
|
|
|
sub set_filter { |
|
54
|
3
|
|
|
3
|
1
|
316
|
my $filter = shift; |
|
55
|
|
|
|
|
|
|
return sub { |
|
56
|
19
|
|
|
19
|
|
22
|
my $self = shift; |
|
57
|
19
|
100
|
|
|
|
66
|
$self->filter_with($filter) if $self->can('filter_with'); |
|
58
|
3
|
|
|
|
|
16
|
}; |
|
59
|
|
|
|
|
|
|
} |
|
60
|
|
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
sub weights2str { |
|
62
|
1
|
|
|
1
|
1
|
3
|
my ($hr) = @_; |
|
63
|
|
|
|
|
|
|
return join '/', |
|
64
|
1
|
100
|
|
|
|
6
|
map { $hr->{$_} == 1 ? $_ : join( '*', $_, $hr->{$_} ) } sort keys %$hr; |
|
|
9
|
|
|
|
|
26
|
|
|
65
|
|
|
|
|
|
|
} |
|
66
|
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
sub weights_from { |
|
68
|
3
|
|
|
3
|
1
|
6489
|
my ($input) = @_; |
|
69
|
|
|
|
|
|
|
|
|
70
|
3
|
|
|
|
|
6
|
my $type = ref $input; |
|
71
|
3
|
|
|
|
|
3
|
my $fh; |
|
72
|
|
|
|
|
|
|
|
|
73
|
3
|
100
|
|
|
|
11
|
if ( $type eq '' ) { |
|
|
|
50
|
|
|
|
|
|
|
74
|
2
|
|
|
1
|
|
43
|
open $fh, '<', \$input; |
|
|
1
|
|
|
|
|
6
|
|
|
|
1
|
|
|
|
|
3
|
|
|
|
1
|
|
|
|
|
5
|
|
|
75
|
|
|
|
|
|
|
} elsif ( $type eq 'GLOB' ) { |
|
76
|
1
|
|
|
|
|
3
|
$fh = $input; |
|
77
|
|
|
|
|
|
|
} else { |
|
78
|
0
|
|
|
|
|
0
|
croak "unknown input type"; |
|
79
|
|
|
|
|
|
|
} |
|
80
|
|
|
|
|
|
|
|
|
81
|
3
|
|
|
|
|
590
|
my ( %first, %mid, %last, %all ); |
|
82
|
|
|
|
|
|
|
|
|
83
|
3
|
|
|
|
|
29
|
while ( readline $fh ) { |
|
84
|
3
|
|
|
|
|
6
|
chomp; |
|
85
|
|
|
|
|
|
|
LOOP: { |
|
86
|
3
|
100
|
|
|
|
3
|
redo LOOP if /\G\s+/cg; |
|
|
88
|
|
|
|
|
152
|
|
|
87
|
|
|
|
|
|
|
# various \b{...} forms detailed in perlrebackslash may be |
|
88
|
|
|
|
|
|
|
# better for word boundaries though require perl 5.22 or up |
|
89
|
72
|
100
|
|
|
|
114
|
if (m/\G\b(.)/cg) { |
|
90
|
19
|
|
|
|
|
28
|
$first{$1}++; |
|
91
|
19
|
|
|
|
|
27
|
$all{$1}++; |
|
92
|
19
|
|
|
|
|
24
|
redo LOOP; |
|
93
|
|
|
|
|
|
|
} |
|
94
|
53
|
100
|
|
|
|
81
|
if (m/\G(.)\b/cg) { |
|
95
|
19
|
|
|
|
|
25
|
$last{$1}++; |
|
96
|
19
|
|
|
|
|
22
|
$all{$1}++; |
|
97
|
19
|
|
|
|
|
20
|
redo LOOP; |
|
98
|
|
|
|
|
|
|
} |
|
99
|
34
|
100
|
|
|
|
65
|
if (m/\G\B(.)/cg) { |
|
100
|
31
|
|
|
|
|
47
|
$mid{$1}++; |
|
101
|
31
|
|
|
|
|
48
|
$all{$1}++; |
|
102
|
31
|
|
|
|
|
35
|
redo LOOP; |
|
103
|
|
|
|
|
|
|
} |
|
104
|
|
|
|
|
|
|
} |
|
105
|
|
|
|
|
|
|
} |
|
106
|
|
|
|
|
|
|
|
|
107
|
3
|
|
|
|
|
20
|
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
|
|
|
|
|
|
|
sub parse_string { |
|
119
|
2
|
|
|
2
|
1
|
352
|
my ( $self_or_class, $str ) = @_; |
|
120
|
2
|
|
|
|
|
6
|
return Lingua::Awkwords::Parser->new->from_string($str); |
|
121
|
|
|
|
|
|
|
} |
|
122
|
|
|
|
|
|
|
|
|
123
|
|
|
|
|
|
|
sub render { |
|
124
|
3
|
|
|
3
|
1
|
1224
|
my ($self) = @_; |
|
125
|
3
|
|
|
|
|
8
|
my $tree = $self->tree; |
|
126
|
3
|
100
|
|
|
|
22
|
croak "no pattern supplied" if !defined $tree; |
|
127
|
2
|
|
|
|
|
5
|
return $tree->render; |
|
128
|
|
|
|
|
|
|
} |
|
129
|
|
|
|
|
|
|
|
|
130
|
|
|
|
|
|
|
sub walk { |
|
131
|
3
|
|
|
3
|
1
|
822
|
my ( $self, $callback ) = @_; |
|
132
|
3
|
|
|
|
|
8
|
my $tree = $self->tree; |
|
133
|
3
|
100
|
|
|
|
13
|
croak "no pattern supplied" if !defined $tree; |
|
134
|
2
|
|
|
|
|
7
|
$tree->walk($callback); |
|
135
|
2
|
|
|
|
|
3
|
return; |
|
136
|
|
|
|
|
|
|
} |
|
137
|
|
|
|
|
|
|
|
|
138
|
|
|
|
|
|
|
1; |
|
139
|
|
|
|
|
|
|
__END__ |