blib/lib/HTML/ListScraper/Sweep.pm

filter: Statementstmt 96.3% 105 / 109 Branchbran 94.4% 34 / 36 Conditioncond 100% 10 / 10 MC/DCmcdc 100% 5 / 5 Subroutinesub 100% 10 / 10 Podpod 0.0% 0 / 2 totaltotal 95.3% 164 / 172 CC 25 SCAR 19.8
CC
25
Cov
96%
_make_presentable
12.0
create_dust
8.0
_on_align
5.0
CRAP 7.2
« scrape ↑ Summary blib/lib/HTML/ListScraper.pm »
1 package HTML::ListScraper::Sweep;
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 Algorithm::NeedlemanWunsch;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 10 ▶use HTML::ListScraper::Dust;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 10 my $match_score = 2;
10 10 my $mismatch_score = -4;
11 10 my $gap_open_penalty = -2;
12 10 my $gap_extend_penalty = -1;
13  
14 225851 ▶sub _score_sub {
Subroutine _score_sub: called CC=2 SCAR=6.9
15 225851 my ($a, $b) = @_;
16  
17 225851 ▶ return ($a eq $b) ? $match_score : $mismatch_score;
Branch: 1 branch
BranchTrueFalseTotal
$a eq $b ? : 14442 211409 225851
18 }
19  
20 88 ▶sub new {
Subroutine new: called CC=2 SCAR=7.0
Pod undocumented
21 88 my $class = shift;
22 88 my $self = { @_ };
23  
24 88 my $iseq = $self->{book}->get_internal_sequence;
25 88 my $first = $self->{begin};
26 88 my $last = $self->{end} - 1;
27 # warn "sweeping $first .. $last\n";
28 88 my @seq = @$iseq[$first .. $last];
29 88 $self->{haystack} = \@seq;
30  
31 88 my @sign = split //, $self->{sign};
32 88 ▶ if (scalar(@sign) < 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @sign < 2) 0 88 88
33 0 die "sequence signature too short";
34 }
35  
36 88 $self->{needle} = \@sign;
37  
38 88 bless $self, $class;
39  
40 88 return $self;
41 }
42  
43 88 ▶sub create_dust {
Subroutine create_dust: called CC=8 SCAR=20.8
Pod undocumented
44 88 my $self = shift;
45  
46 88 $self->{dust} = HTML::ListScraper::Dust->new();
47  
48 88 ▶ if (scalar(@{$self->{haystack}}) > 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @{$self->{"haystack"};} > 2) 83 5 88
49 83 my $matcher = Algorithm::NeedlemanWunsch->new(\&_score_sub);
50 83 $matcher->gap_open_penalty($gap_open_penalty);
51 83 $matcher->gap_extend_penalty($gap_extend_penalty);
52 83 $matcher->local(1);
53  
54 83 $self->{found} = [ ];
55  
56 my $score = $matcher->align($self->{haystack},
57 $self->{needle},
58 83 ▶ { select_align => sub { $self->_on_align($_[0]); } });
Subroutine __ANON__: called CC=1 SCAR=0.0
59  
60 83 my $found_count = scalar(@{$self->{found}});
61 83 ▶ if ($found_count >= 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($found_count >= 2) 79 4 83
62 79 my @round = $self->_make_presentable;
63 79 ▶ if ((scalar(@round) >= 2) &&
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @round >= 2 and scalar @round > scalar(@{$self->{"needle"};}) / 2) 44 35 79
Condition100%3/3
scalar @round >= 2 and scalar @round > scalar(@{$self->{"needle"};}) / 2
!ll&&!rl&&r
23344
MC/DC100%2/2
scalar @round >= 2 and scalar @round > scalar(@{$self->{"needle"};}) / 2
scalar @round >= 2 scalar @round > scalar(@{$self->{"needle"};}) / 2
Truth table100%3/3
A and B
A= scalar @round >= 2B= scalar @round > scalar(@{$self->{"needle"};}) / 2
ABresult
0X0
100
111
64 77 (scalar(@round) > (scalar(@{$self->{needle}}) / 2))) {
65 44 ▶ if (scalar(@round) < $found_count) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @round < $found_count) 5 39 44
66 5 $score = undef;
67 }
68  
69 44 my $begin = $self->{begin};
70 44 my $end = $round[0];
71 44 ▶ if ($begin < $end) {
Branch: 1 branch
BranchTrueFalseTotal
if ($begin < $end) 38 6 44
72 my $sweep = HTML::ListScraper::Sweep->new(
73 book => $self->{book}, sign => $self->{sign},
74 38 begin => $begin, end => $end);
75 38 my $dust = $sweep->create_dust;
76 38 my $before = $dust->get_alignments;
77 38 $self->{dust}->add_alignments_before($before);
78 }
79  
80 44 $self->{dust}->add_alignment($score, \@round);
81  
82 44 $begin = $round[-1] + 1;
83 44 $end = $self->{end};
84 44 ▶ if ($begin < $end) {
Branch: 1 branch
BranchTrueFalseTotal
if ($begin < $end) 22 22 44
85 my $sweep = HTML::ListScraper::Sweep->new(
86 book => $self->{book}, sign => $self->{sign},
87 22 begin => $begin, end => $end);
88 22 my $dust = $sweep->create_dust;
89 22 my $after = $dust->get_alignments;
90 22 $self->{dust}->add_alignments_after($after);
91 }
92 }
93 }
94 }
95  
96 88 return $self->{dust};
97 }
98  
99 10925 ▶sub _on_align {
Subroutine _on_align: called CC=5 SCAR=16.1
100 10925 my ($self, $arg) = @_;
101  
102 10925 ▶ if (exists($arg->{align})) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $arg->{"align"}) 1078 9847 10925
103 1078 my ($i, $j) = @{$arg->{align}};
104  
105 1078 ▶ if ($self->{haystack}->[$i] eq $self->{needle}->[$j]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"haystack"}[$i] eq $self->{"needle"}[$j]) 1070 8 1078
106 1070 unshift @{$self->{found}}, $self->{begin} + $i;
107 1070 return 'align';
108 }
109 }
110  
111 9855 foreach (qw(shift_a shift_b)) {
112 10552 ▶ if (exists($arg->{$_})) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $arg->{$_}) 9855 697 10552
113 9855 return $_;
114 }
115 }
116  
117 0 return 'align';
118 }
119  
120 79 ▶sub _make_presentable {
Subroutine _make_presentable: called CC=12 SCAR=24.9
121 79 my $self = shift;
122  
123 79 ▶ if ($self->{book}->shapeless) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"book"}->shapeless) 0 79 79
124 0 return @{$self->{found}};
125 }
126  
127 79 my %core;
128 my @stack;
129 79 foreach (@{$self->{found}}) {
130 1068 my $cur_tag = $self->{book}->get_tag($_);
131 1068 my $name = $cur_tag->name;
132 1068 my $stem = $name;
133 1068 $stem =~ s~^\/~~;
134  
135 1068 ▶ if ($name eq $stem) {
Branch: 1 branch
BranchTrueFalseTotal
if ($name eq $stem) { } 533 535 1068
136 533 push @stack, $cur_tag;
137 } else {
138 535 my $skip = 0;
139 535 ▶ while (!$skip && scalar(@stack) &&
Branch: 1 branch
BranchTrueFalseTotal
if (not $skip and scalar @stack and $stack[scalar(@stack) - 1]->name ne $stem) 68 535 603
Condition100%5/5
not $skip and scalar @stack and $stack[scalar(@stack) - 1]->name ne $stem
!ll&&!rl&&r
7745868
not $skip and scalar @stack
!ll
25578
MC/DC100%2/2
not $skip and scalar @stack and $stack[scalar(@stack) - 1]->name ne $stem
not $skip $stack[scalar(@stack) - 1]->name ne $stem
Truth table100%3/3
not(A) and B
A= not $skipB= $stack[scalar(@stack) - 1]->name ne $stem
ABresult
0X0
100
111
140 ($stack[scalar(@stack) - 1]->name ne $stem)) {
141 68 my $top_tag = $stack[scalar(@stack) - 1];
142 68 ▶ if ($self->{book}->is_unclosed_tag($top_tag->name)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'book'}->is_unclosed_tag($top_tag->name)) { } 43 25 68
143 43 $core{$top_tag->index} = 1;
144 43 pop @stack;
145 } else {
146 25 $skip = 1;
147 }
148 }
149  
150 535 ▶ if (!$skip && scalar(@stack)) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $skip and scalar @stack) 458 77 535
Condition100%2/2
not $skip and scalar @stack
!ll
25510
MC/DC100%1/1
not $skip and scalar @stack
not $skip
Truth table100%2/2
not(A)
A= not $skip
Aresult
00
11
151 458 my $top_tag = pop @stack;
152 458 $core{$top_tag->index} = 1;
153 458 $core{$_} = 1;
154 }
155 }
156 }
157  
158 79 ▶ while (scalar(@stack)) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @stack) 32 79 111
159 32 my $top_tag = pop @stack;
160 32 ▶ if ($self->{book}->is_unclosed_tag($top_tag->name)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"book"}->is_unclosed_tag($top_tag->name)) 10 22 32
161 10 $core{$top_tag->index} = 1;
162 }
163 }
164  
165 79 return sort { $a <=> $b; } keys %core;
166 }
167  
168 10 1;
« scrape ↑ Summary blib/lib/HTML/ListScraper.pm »