| 1 | # eww - it's a Base.pm | ||||||||||||||||||||||||||||||||||||||||||
| 2 | package Algorithm::MarkovChain::Base; | ||||||||||||||||||||||||||||||||||||||||||
| 3 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 4 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 5 | 1 | ▶ | use Carp; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||||||||||||
| 7 | 1 | ▶ | use fields qw(seperator _symbols _recover_symbols _start_states); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||||||||||||||||
| 9 | 1 | ▶ | sub new { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=2 SCAR=7.1
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 10 | 1 | my $invocant = shift; | |||||||||||||||||||||||||||||||||||||||||
| 11 | 1 | my %args = @_; | |||||||||||||||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||||||||||||||||
| 13 | 1 | ▶ | my $class = ref $invocant || $invocant; | ||||||||||||||||||||||||||||||||||||||||
|
Condition33%1/3
ref $invocant || $invocant
MC/DC0%0/2
ref $invocant || $invocant
ref $invocant
$invocant
Truth table33%1/3
A || B
A= ref $invocantB= $invocant
| |||||||||||||||||||||||||||||||||||||||||||
| 14 | 1 | my Algorithm::MarkovChain::Base $self = fields::new($class); | |||||||||||||||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||||||||||||||||
| 16 | 1 | $self->{seperator} = $;; | |||||||||||||||||||||||||||||||||||||||||
| 17 | 1 | $self->{_symbols} = {}; | |||||||||||||||||||||||||||||||||||||||||
| 18 | 1 | $self->{_recover_symbols} = $args{recover_symbols}; | |||||||||||||||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||||||||||||||||
| 20 | 1 | return $self; | |||||||||||||||||||||||||||||||||||||||||
| 21 | } | ||||||||||||||||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||||||||||||||||
| 24 | 1 | ▶ | sub seed { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
seed: called CC=8 SCAR=21.3
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 25 | 1 | my Algorithm::MarkovChain::Base $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 26 | 1 | my %args = @_; | |||||||||||||||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||||||||||||||||
| 28 | 1 | local $; = $self->{seperator}; | |||||||||||||||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||||||||||||||||
| 30 | 1 | ▶ | croak 'seed: no symbols' unless $args{symbols}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 31 | 1 | ▶ | croak 'seed: bad symbols' unless ref($args{symbols}) eq 'ARRAY'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||||||||||||||||
| 33 | 1 | ▶ | my $longest = $args{longest} || 4; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$args{'longest'} || 4
MC/DC0%0/1
$args{'longest'} || 4
$args{'longest'}
Truth table50%1/2
A
A= $args{'longest'}
| |||||||||||||||||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||||||||||||||||
| 35 | 1 | our @symbols; | |||||||||||||||||||||||||||||||||||||||||
| 36 | 1 | *symbols = $args{symbols}; | |||||||||||||||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||||||||||||||||
| 38 | 1 | push @{ $self->{_start_states} }, $symbols[0]; | |||||||||||||||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||||||||||||||||
| 40 | 1 | ▶ | if ($self->{_recover_symbols}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 41 | 0 | $self->{_symbols}{$_} = $_ for @symbols; | |||||||||||||||||||||||||||||||||||||||||
| 42 | } | ||||||||||||||||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||||||||||||||||
| 44 | 1 | for my $length (1..$longest) { | |||||||||||||||||||||||||||||||||||||||||
| 45 | 4 | ▶ | for (my $i = 0; ($i + $length) < @symbols; $i++) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 46 | 1 | my $link = join($;, @symbols[$i..$i + $length - 1]); | |||||||||||||||||||||||||||||||||||||||||
| 47 | 1 | $self->increment_seen($link, $symbols[$i + $length]); | |||||||||||||||||||||||||||||||||||||||||
| 48 | } | ||||||||||||||||||||||||||||||||||||||||||
| 49 | } | ||||||||||||||||||||||||||||||||||||||||||
| 50 | } | ||||||||||||||||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||||||||||||||||
| 53 | 1 | ▶ | sub spew { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
spew: called CC=20 SCAR=35.8
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 54 | 1 | my Algorithm::MarkovChain::Base $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 55 | 1 | my %args = @_; | |||||||||||||||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||||||||||||||||
| 57 | 1 | local $; = $self->{seperator}; | |||||||||||||||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||||||||||||||||
| 59 | 1 | ▶ | my $longest_sequence = $self->longest_sequence() | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 60 | or croak "don't appear to be seeded"; | ||||||||||||||||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||||||||||||||||
| 62 | 1 | ▶ | my $length = $args{length} || 30; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$args{'length'} || 30
MC/DC0%0/1
$args{'length'} || 30
$args{'length'}
Truth table50%1/2
A
A= $args{'length'}
| |||||||||||||||||||||||||||||||||||||||||||
| 63 | 1 | ▶ | my $subchain = $args{longest_subchain} || $length; | ||||||||||||||||||||||||||||||||||||||||
|
Condition33%1/3
$args{'longest_subchain'} || $length
MC/DC0%0/2
$args{'longest_subchain'} || $length
$args{'longest_subchain'}
$length
Truth table33%1/3
A || B
A= $args{'longest_subchain'}B= $length
| |||||||||||||||||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||||||||||||||||
| 65 | 1 | my @fin; # final chain | |||||||||||||||||||||||||||||||||||||||||
| 66 | my @sub; # current sub-chain | ||||||||||||||||||||||||||||||||||||||||||
| 67 | 1 | ▶ | if ($args{complete} && ref $args{complete} eq 'ARRAY') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$args{"complete"} and ref $args{"complete"} eq "ARRAY"
MC/DC0%0/2
$args{"complete"} and ref $args{"complete"} eq "ARRAY"
$args{"complete"}
ref $args{"complete"} eq "ARRAY"
Truth table33%1/3
A and B
A= $args{"complete"}B= ref $args{"complete"} eq "ARRAY"
| |||||||||||||||||||||||||||||||||||||||||||
| 68 | 1 | @sub = @{ $args{complete} }; | |||||||||||||||||||||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||||||||||||||||
| 71 | 1 | ▶ | while (@fin < $length) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 72 | 2 | ▶ | if (@sub && (!$self->sequence_known($sub[-1]) || (@sub > $subchain))) { # we've gone terminal | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%4/6
@sub and !$self->sequence_known($sub[-1]) || @sub > $subchain
not $self->sequence_known($sub[-1]) or @sub > $subchain
MC/DC33%1/3
@sub and !$self->sequence_known($sub[-1]) || @sub > $subchain
@sub
not $self->sequence_known($sub[-1])
@sub > $subchain
Truth table50%2/4
A and not(B) or C
A= @subB= not $self->sequence_known($sub[-1])C= @sub > $subchain
| |||||||||||||||||||||||||||||||||||||||||||
| 73 | 1 | push @fin, @sub; | |||||||||||||||||||||||||||||||||||||||||
| 74 | 1 | @sub = (); | |||||||||||||||||||||||||||||||||||||||||
| 75 | 1 | ▶ | next if $args{force_length}; # ignore stop_at_terminal | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 76 | 1 | ▶ | last if $args{stop_at_terminal}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 77 | } | ||||||||||||||||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||||||||||||||||
| 79 | 1 | ▶ | unless (@sub) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 80 | 0 | ▶ | if ($args{strict_start}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 81 | 0 | our @starts; | |||||||||||||||||||||||||||||||||||||||||
| 82 | 0 | *starts = $self->{_start_states}; | |||||||||||||||||||||||||||||||||||||||||
| 83 | 0 | @sub = $starts[rand $#starts]; | |||||||||||||||||||||||||||||||||||||||||
| 84 | } | ||||||||||||||||||||||||||||||||||||||||||
| 85 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 86 | 0 | @sub = split $;, $self->random_sequence(); | |||||||||||||||||||||||||||||||||||||||||
| 87 | } | ||||||||||||||||||||||||||||||||||||||||||
| 88 | } | ||||||||||||||||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||||||||||||||||
| 90 | 1 | my $consider = 1; | |||||||||||||||||||||||||||||||||||||||||
| 91 | 1 | ▶ | if (@sub > 1) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 92 | 0 | $consider = int rand ($longest_sequence - 1); | |||||||||||||||||||||||||||||||||||||||||
| 93 | } | ||||||||||||||||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||||||||||||||||
| 95 | 1 | my $start = join($;, @sub[-$consider..-1]); | |||||||||||||||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||||||||||||||||
| 97 | 1 | ▶ | next unless $self->sequence_known($start); # loop if we missed | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||||||||||||||||
| 99 | 1 | my $cprob; | |||||||||||||||||||||||||||||||||||||||||
| 100 | 1 | my $target = rand; | |||||||||||||||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||||||||||||||||
| 102 | 1 | my %options = $self->get_options($start); | |||||||||||||||||||||||||||||||||||||||||
| 103 | 1 | for my $word (keys %options) { | |||||||||||||||||||||||||||||||||||||||||
| 104 | 1 | $cprob += $options{$word}; | |||||||||||||||||||||||||||||||||||||||||
| 105 | 1 | ▶ | if ($cprob >= $target) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 106 | 1 | push @sub, $word; | |||||||||||||||||||||||||||||||||||||||||
| 107 | 1 | last; | |||||||||||||||||||||||||||||||||||||||||
| 108 | } | ||||||||||||||||||||||||||||||||||||||||||
| 109 | } | ||||||||||||||||||||||||||||||||||||||||||
| 110 | } | ||||||||||||||||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||||||||||||||||
| 112 | $#fin = $length | ||||||||||||||||||||||||||||||||||||||||||
| 113 | 1 | ▶ | if $args{force_length}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||||||||||||||||
| 115 | 0 | @fin = map { $self->{_symbols}{$_} } @fin | |||||||||||||||||||||||||||||||||||||||||
| 116 | 1 | ▶ | if $self->{_recover_symbols}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||||||||||||||||
| 118 | 1 | return @fin; | |||||||||||||||||||||||||||||||||||||||||
| 119 | } | ||||||||||||||||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||||||||||||||||
| 122 | 0 | ▶ | sub increment_seen { croak "virtual method call" } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
increment_seen: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 123 | 0 | ▶ | sub get_options { croak "virtual method call" } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
get_options: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 124 | 0 | ▶ | sub longest_sequence { croak "virtual method call" } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
longest_sequence: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 125 | 0 | ▶ | sub sequence_known { croak "virtual method call" } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
sequence_known: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 126 | 0 | ▶ | sub random_sequence { croak "virtual method call" } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
random_sequence: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||||||||||||||||
| 129 | 1 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 130 | __END__ | ||||||||||||||||||||||||||||||||||||||||||