blib/lib/HTML/ListScraper/Vat.pm

filter: Statementstmt 98.8% 87 / 88 Branchbran 90.9% 20 / 22 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 12 / 12 Podpod 0.0% 0 / 2 totaltotal 95.9% 119 / 124 CC 16 SCAR 12.4
CC
16
Cov
97%
_grow_map
6.0
_fill_pair_map
4.0
_skim_map
4.0
CRAP 3.4
« blib/lib/HTML/ListScraper/Foam.pm ↑ Summary blib/lib/HTML/ListScraper/Occurence.pm »
1 package HTML::ListScraper::Vat;
2  
3 10 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 10 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 10 ▶use Class::Generate;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 10 ▶use HTML::ListScraper::Foam;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 10 ▶use HTML::ListScraper::Occurence;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 10 my $epsilon = 0.0001;
11  
12 12 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod undocumented
13 12 my ($class, $book, $min_count) = @_;
14 12 my $self = { book => $book, min_count => $min_count };
15 12 $self->{seq} = $book->get_internal_sequence;
16 12 bless $self, $class;
17  
18 12 return $self;
19 }
20  
21 12 ▶sub create_sequence {
Subroutine create_sequence: called CC=3 SCAR=11.0
Pod undocumented
22 12 my $self = shift;
23  
24 12 my $count = scalar(@{$self->{seq}});
25 12 ▶ if ($count < $self->{min_count}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($count < $self->{"min_count"}) 0 12 12
26 0 return undef; # if we had a verbose mode, we would warn here
27 }
28  
29 12 $self->{foam} = HTML::ListScraper::Foam->new($self->{book});
30  
31 12 $self->{map} = { };
32 12 $self->_fill_pair_map;
33 12 ▶ while ($self->_has_cand) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_has_cand) 1100 12 1112
34 1100 $self->_prune_map;
35 1100 $self->_skim_map;
36 1100 $self->_grow_map;
37 }
38  
39 12 return $self->{foam};
40 }
41  
42 1112 ▶sub _has_cand {
Subroutine _has_cand: called CC=1 SCAR=0.0
43 1112 my $self = shift;
44  
45 1112 return scalar(keys %{$self->{map}});
46 }
47  
48 12 ▶sub _fill_pair_map {
Subroutine _fill_pair_map: called CC=4 SCAR=13.9
49 12 my $self = shift;
50  
51 12 $self->{max_spread} = 1;
52 12 $self->{length} = 2;
53  
54 12 my $count = scalar(@{$self->{seq}});
55  
56 12 my $first = $self->{seq}->[0];
57 12 my $i = 1;
58 12 ▶ while ($i < $count) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $count) 5721 12 5733
59 5721 my $second = $self->{seq}->[$i];
60 5721 my $seq = $first . $second;
61 5721 ▶ if (!exists($self->{map}->{$seq})) {
Branch: 1 branch
BranchTrueFalseTotal
if (not exists $self->{'map'}{$seq}) { } 994 4727 5721
62 994 $self->{map}->{$seq} =
63 HTML::ListScraper::Occurence->new(2, $i - 1);
64 } else {
65 4727 my $spread = $self->{map}->{$seq}->append_pos($i - 1);
66 4727 ▶ if ($spread > $self->{max_spread}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($spread > $self->{"max_spread"}) 636 4091 4727
67 636 $self->{max_spread} = $spread;
68 }
69 }
70  
71 5721 $first = $second;
72 5721 ++$i;
73 }
74 }
75  
76 1100 ▶sub _prune_map {
Subroutine _prune_map: called CC=3 SCAR=11.0
77 1100 my $self = shift;
78  
79 1100 my @seq = keys %{$self->{map}};
80 1100 foreach my $seq (@seq) {
81 84190 my $n = $self->{map}->{$seq}->spread;
82 84190 ▶ if ($n < $self->{min_count}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n < $self->{"min_count"}) 4085 80105 84190
83 4085 delete $self->{map}->{$seq};
84 }
85 }
86 }
87  
88 1100 ▶sub _skim_map {
Subroutine _skim_map: called CC=4 SCAR=13.9
89 1100 my $self = shift;
90  
91 1100 foreach my $seq (keys %{$self->{map}}) {
92 18474 my $occ = $self->{map}->{$seq};
93 18474 ▶ if ($occ->spread == $self->{max_spread}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($occ->spread == $self->{"max_spread"}) 3638 14836 18474
94 3638 ▶ if (!$self->{foam}->store($seq, $occ)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"foam"}->store($seq, $occ)) 784 2854 3638
95 # the best isn't good enough
96 784 return;
97 }
98 }
99 }
100 }
101  
102 1100 ▶sub _grow_map {
Subroutine _grow_map: called CC=6 SCAR=17.9
103 1100 my $self = shift;
104  
105 1100 my $max_spread = 1;
106 1100 my $length = $self->{length} + 1;
107 1100 my $map = { };
108 1100 foreach my $seq (keys %{$self->{map}}) {
109 80105 my $occ = $self->{map}->{$seq};
110 80105 foreach my $pos ($occ->positions) {
111 396593 ▶ if ($pos > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pos > 0) 396593 0 396593
112 396593 my $npos = $pos - 1;
113 396593 my $nseq = $self->{seq}->[$npos] . $seq;
114  
115 396593 ▶ if (!exists($map->{$nseq})) {
Branch: 1 branch
BranchTrueFalseTotal
if (not exists $map->{$nseq}) { } 83196 313397 396593
116 83196 $map->{$nseq} =
117 HTML::ListScraper::Occurence->new($length, $npos);
118 } else {
119 313397 my $spread = $map->{$nseq}->append_pos($npos);
120 313397 ▶ if ($spread > $max_spread) {
Branch: 1 branch
BranchTrueFalseTotal
if ($spread > $max_spread) 5672 307725 313397
121 5672 $max_spread = $spread;
122 }
123 }
124 }
125 }
126 }
127  
128 1100 $self->{map} = $map;
129 1100 $self->{length} = $length;
130 1100 $self->{max_spread} = $max_spread;
131 }
132  
133 10 1;
« blib/lib/HTML/ListScraper/Foam.pm ↑ Summary blib/lib/HTML/ListScraper/Occurence.pm »