line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Text::Spintax::RenderNode; |
2
|
|
|
|
|
|
|
|
3
|
2
|
|
|
2
|
|
11
|
use strict; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
246
|
|
4
|
2
|
|
|
2
|
|
13
|
use warnings FATAL => 'all'; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
1525
|
|
5
|
|
|
|
|
|
|
|
6
|
1
|
50
|
|
1
|
0
|
5
|
sub parent { scalar @_ == 2 and $_[0]->{parent} = $_[1]; return $_[0]->{parent} } |
|
1
|
|
|
|
|
5
|
|
7
|
305
|
100
|
|
305
|
0
|
714
|
sub children { scalar @_ == 2 and $_[0]->{children} = $_[1]; return $_[0]->{children} } |
|
305
|
|
|
|
|
862
|
|
8
|
359
|
50
|
|
359
|
0
|
855
|
sub weight { scalar @_ == 2 and $_[0]->{weight} = $_[1]; return $_[0]->{weight} } |
|
359
|
|
|
|
|
1166
|
|
9
|
303
|
50
|
|
303
|
0
|
605
|
sub text { scalar @_ == 2 and $_[0]->{text} = $_[1]; return $_[0]->{text} } |
|
303
|
|
|
|
|
1689
|
|
10
|
811
|
50
|
|
811
|
0
|
1823
|
sub type { scalar @_ == 2 and $_[0]->{type} = $_[1]; return $_[0]->{type} } |
|
811
|
|
|
|
|
2640
|
|
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
=head1 SUBROUTINES/METHODS |
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
=cut |
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
sub new { |
17
|
7
|
|
|
7
|
0
|
14
|
my $class = shift; |
18
|
7
|
|
|
|
|
35
|
my %init = @_; |
19
|
7
|
|
|
|
|
27
|
my $self = bless \%init, $class; |
20
|
7
|
|
|
|
|
23
|
return $self; |
21
|
|
|
|
|
|
|
} |
22
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
=head2 render |
24
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
Generates a text string from all the possible variations. Uses weights to determine how likely each possible string is to be rendered. |
26
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
=cut |
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
sub render { |
30
|
505
|
|
|
505
|
1
|
1666
|
my $self = shift; |
31
|
505
|
100
|
|
|
|
815
|
if ($self->type eq "text") { |
|
|
100
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
32
|
303
|
|
|
|
|
566
|
return $self->text; |
33
|
|
|
|
|
|
|
} |
34
|
|
|
|
|
|
|
elsif ($self->type eq "spin") { |
35
|
101
|
|
|
|
|
124
|
my $total = 0; |
36
|
101
|
|
|
|
|
102
|
foreach my $child (@{$self->children}) { |
|
101
|
|
|
|
|
253
|
|
37
|
202
|
|
|
|
|
356
|
$total += $child->weight; |
38
|
|
|
|
|
|
|
} |
39
|
101
|
|
|
|
|
342
|
my $rand = rand $total; |
40
|
101
|
|
|
|
|
110
|
foreach my $child (@{$self->children}) { |
|
101
|
|
|
|
|
296
|
|
41
|
157
|
|
|
|
|
316
|
$rand -= $child->weight; |
42
|
157
|
100
|
|
|
|
507
|
$rand <= 0 and return $child->render; |
43
|
|
|
|
|
|
|
} |
44
|
|
|
|
|
|
|
} |
45
|
|
|
|
|
|
|
elsif ($self->type eq "sequence") { |
46
|
101
|
|
|
|
|
111
|
return join "", map { $_->render } @{$self->children}; |
|
303
|
|
|
|
|
617
|
|
|
101
|
|
|
|
|
281
|
|
47
|
|
|
|
|
|
|
} |
48
|
|
|
|
|
|
|
else { |
49
|
0
|
|
|
|
|
|
die "invalid type"; |
50
|
|
|
|
|
|
|
} |
51
|
|
|
|
|
|
|
} |
52
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
sub equal_path_weight { |
54
|
0
|
|
|
0
|
0
|
|
my $self = shift; |
55
|
0
|
|
|
|
|
|
$self->weight($self->num_paths); |
56
|
0
|
0
|
|
|
|
|
foreach my $child ($self->children ? @{$self->children} : ()) { |
|
0
|
|
|
|
|
|
|
57
|
0
|
|
|
|
|
|
$child->equal_path_weight; |
58
|
|
|
|
|
|
|
} |
59
|
|
|
|
|
|
|
} |
60
|
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
=head2 num_paths |
62
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
Returns the number of possible strings that could be generated from this node. Combinatorially speaking, children of a sequence node multiply and children of a spin node add. |
64
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
"{a|b|c}" has 1+1+1=3 possibilities: a, b, c |
66
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
"{a|b} {c|d}" has 2*2=4 possibilities: a c, a d, b c, b d |
68
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
=cut |
70
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
sub num_paths { |
72
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
73
|
0
|
0
|
|
|
|
|
if ($self->type eq "spin") { |
74
|
0
|
|
|
|
|
|
my $num_paths = 0; |
75
|
0
|
0
|
|
|
|
|
foreach my $child ($self->children ? @{$self->children} : ()) { |
|
0
|
|
|
|
|
|
|
76
|
0
|
|
|
|
|
|
$num_paths += $child->num_paths; |
77
|
|
|
|
|
|
|
} |
78
|
0
|
|
|
|
|
|
return $num_paths; |
79
|
|
|
|
|
|
|
} |
80
|
|
|
|
|
|
|
else { |
81
|
0
|
|
|
|
|
|
my $num_paths = 1; |
82
|
0
|
0
|
|
|
|
|
foreach my $child ($self->children ? @{$self->children} : ()) { |
|
0
|
|
|
|
|
|
|
83
|
0
|
|
|
|
|
|
$num_paths *= $child->num_paths; |
84
|
|
|
|
|
|
|
} |
85
|
0
|
|
|
|
|
|
return $num_paths; |
86
|
|
|
|
|
|
|
} |
87
|
|
|
|
|
|
|
} |
88
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
1; |