lib/StupidMarkov.pm

filter: Statementstmt 16.3% 8 / 49 Branchbran 0.0% 0 / 12 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 22.2% 2 / 9 Podpod 100% 6 / 6 totaltotal 19.7% 16 / 81 CC 10 SCAR 26.0
CC
10
Cov
12%
get_next_item
30.0
add_item
12.0
_get_probable_next_item
12.0
CRAP 13.4
↑ Summary script/stupidmarkov-dissociator.pl »
1 #!/usr/bin/perl
2  
3 package StupidMarkov;
4  
5 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 2 our $VERSION = "0.002";
9  
10  
11 0 ▶sub new {
Subroutine new: not called CC=2 SCAR=17.9
Pod documented
12 0 my $proto = shift;
13 0 ▶ my $class = ref($proto) || $proto;
Condition0%0/3
ref $proto || $proto
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $proto || $proto
ref $proto $proto
Truth table0%0/3
A || B
A= ref $protoB= $proto
ABresult
1X1
011
000
14 0 my $self = {
15 "_probabilities" => {},
16 "_prev_item" => undef,
17 "_first_item" => undef,
18 "_item_count" => 0,
19 "_state" => undef,
20 };
21  
22 0 bless $self, $class;
23 }
24  
25 0 ▶sub get_item_count {
Subroutine get_item_count: not called CC=1 SCAR=6.9
Pod documented
26 0 my $self = shift;
27 0 return $self->{"_item_count"};
28 }
29  
30 0 ▶sub get_probabilities {
Subroutine get_probabilities: not called CC=1 SCAR=6.9
Pod documented
31 0 my $self = shift;
32 0 return $self->{"_probabilities"};
33 }
34  
35 0 ▶sub get_state {
Subroutine get_state: not called CC=1 SCAR=6.9
Pod documented
36 0 my $self = shift;
37 0 return $self->{"_state"};
38 }
39  
40 0 ▶sub add_item {
Subroutine add_item: not called CC=3 SCAR=24.8
Pod documented
41 0 my $self = shift;
42 0 my $item = shift;
43 0 my $prev_item = $self->{"_prev_item"};
44 0 my $probabilities = $self->{"_probabilities"};
45  
46 0 ▶ if (defined($prev_item)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $prev_item) { } 0 0 0
47 0 ▶ if (!defined($probabilities->{$prev_item})) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $probabilities->{$prev_item}) { } 0 0 0
48 0 $probabilities->{$prev_item} = { $item => 1 };
49 } else {
50 0 $probabilities->{$prev_item}->{$item}++;
51 }
52 } else {
53 0 $self->{"_first_item"} = $item;
54 }
55  
56 0 $self->{"_prev_item"} = $item;
57 0 $self->{"_item_count"}++;
58 }
59  
60 0 ▶sub _get_probable_next_item {
Subroutine _get_probable_next_item: not called CC=3 SCAR=24.8
61 0 my $self = shift;
62 0 my $state = $self->{"_state"};
63 0 my $probabilities = $self->{"_probabilities"};
64 0 my $items = [];
65  
66 0 foreach my $next_state (keys(%{$probabilities->{$state}})) {
67 0 foreach (0 .. $probabilities->{$state}->{$next_state}) {
68 0 push(@{$items}, $next_state);
69 }
70 }
71  
72 0 return $items->[int(rand(@{$items}))];
73 }
74  
75 0 ▶sub get_next_item {
Subroutine get_next_item: not called CC=5 SCAR=34.0
Pod documented
76 0 my $self = shift;
77 0 my $ext_state = shift;
78 0 my $probabilities = $self->{"_probabilities"};
79  
80 0 ▶ if (defined($ext_state)) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $ext_state) { } 0 0 0
elsif (not defined $self->{'_state'}) { } 0 0 0
81 0 ▶ return undef if (!defined($probabilities->{$ext_state}));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $probabilities->{$ext_state} 0 0 0
82 0 ▶ $self->{"_state"} = $ext_state if (!defined($self->{"_state"}));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"_state"} 0 0 0
83 } elsif (!defined($self->{"_state"})) {
84 0 $self->{"_state"} = $self->{"_first_item"};
85 }
86  
87 0 $self->{"_state"} = $self->_get_probable_next_item();
88 0 return $self->{"_state"};
89 }
90  
91  
92 2 1;
93 __END__
↑ Summary script/stupidmarkov-dissociator.pl »