blib/lib/HTML/ListScraper.pm

filter: Statementstmt 86.0% 154 / 179 Branchbran 58.7% 27 / 46 Conditioncond 66.6% 4 / 6 MC/DCmcdc 25.0% 1 / 4 Subroutinesub 88.4% 23 / 26 Podpod 100% 12 / 12 totaltotal 80.9% 221 / 273 CC 34 SCAR 17.3
CC
34
Cov
80%
get_known_sequence
30.0
_make_approx_seq
6.1
shapeless
6.0
CRAP 5.6
« blib/lib/HTML/ListScraper/Sweep.pm ↑ Summary blib/lib/HTML/ListScraper/Book.pm »
1 package HTML::ListScraper;
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 HTML::Parser;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 10 ▶use Class::Generate qw(class);
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 10 ▶use HTML::ListScraper::Vat;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 10 ▶use HTML::ListScraper::Book;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 10 ▶use HTML::ListScraper::Sweep;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 10 ▶use vars qw(@ISA);
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 10 class 'HTML::ListScraper::Sequence' => {
16 len => { type => '$', required => 1, readonly => 1 },
17 instances => { type => '@', required => 1 }
18 };
19  
20 10 class 'HTML::ListScraper::Instance' => {
21 start => { type => '$', required => 1, readonly => 1 },
22 match => { type => '$', required => 1, readonly => 1 },
23 score => { type => '$', required => 0, readonly => 1 },
24 tags => { type => '@', required => 1 }
25 };
26  
27 10 @ISA = qw(HTML::Parser);
28  
29 10 our $VERSION = '0.08';
30  
31 11 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
32 11 my $class = shift;
33  
34 11 my $self = $class->SUPER::new(@_);
35 11 bless $self, $class;
36  
37 11 $self->{book} = HTML::ListScraper::Book->new();
38 11 $self->{min_count} = 2;
39  
40 11 $self->handler('start' => 'on_start', 'self, tagname, attr');
41 11 $self->handler('text' => 'on_text', 'self, dtext');
42 11 $self->handler('end' => 'on_end', 'self, tagname');
43  
44 11 return $self;
45 }
46  
47 1 ▶sub min_count {
Subroutine min_count: called CC=3 SCAR=11.8
Pod documented
48 1 my $self = shift;
49  
50 1 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 1 0 1
51 1 ▶ if ($_[0] < 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_[0] < 2) 0 1 1
52 0 die "minimal sequence count must be at least 2";
53 }
54  
55 1 $self->{min_count} = $_[0];
56 }
57  
58 1 return $self->{min_count};
59 }
60  
61 0 ▶sub shapeless {
Subroutine shapeless: not called CC=2 SCAR=17.9
Pod documented
62 0 my $self = shift;
63  
64 0 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
65 0 $self->{book}->shapeless($_[0]);
66 }
67  
68 0 return $self->{book}->shapeless;
69 }
70  
71 20 ▶sub is_unclosed_tag {
Subroutine is_unclosed_tag: called CC=1 SCAR=0.0
Pod documented
72 20 my ($self, $name) = @_;
73  
74 20 return $self->{book}->is_unclosed_tag($name);
75 }
76  
77 0 ▶sub get_all_tags {
Subroutine get_all_tags: not called CC=1 SCAR=6.9
Pod documented
78 0 my $self = shift;
79  
80 0 return $self->{book}->get_all_tags();
81 }
82  
83 5 ▶sub get_sequences {
Subroutine get_sequences: called CC=3 SCAR=11.1
Pod documented
84 5 my $self = shift;
85  
86 5 my @sequences;
87 5 my $vat = HTML::ListScraper::Vat->new($self->{book}, $self->{min_count});
88 5 my $foam = $vat->create_sequence;
89 5 ▶ if ($foam) {
Branch: 1 branch
BranchTrueFalseTotal
if ($foam) 5 0 5
90 5 foreach my $handle ($foam->get_sequences) {
91 5 my $occ = $foam->get_occurence($handle);
92 5 push @sequences, $self->_make_sequence($occ);
93 }
94 }
95  
96 return sort {
97 0 $a->len <=> $b->len;
98 } @sequences;
99 }
100  
101 7 ▶sub find_sequences {
Subroutine find_sequences: called CC=3 SCAR=11.1
Pod documented
102 7 my $self = shift;
103  
104 7 my @sequences;
105 7 my $vat = HTML::ListScraper::Vat->new($self->{book}, $self->{min_count});
106 7 my $foam = $vat->create_sequence;
107 7 ▶ if ($foam) {
Branch: 1 branch
BranchTrueFalseTotal
if ($foam) 7 0 7
108 7 foreach my $sign ($foam->get_sequences) {
109 7 my $occ = $foam->get_occurence($sign);
110 7 push @sequences, $self->_make_approx_seq($sign, $occ);
111 }
112 }
113  
114 return sort {
115 0 $a->len <=> $b->len;
116 } @sequences;
117 }
118  
119 5 ▶sub _get_known_occ {
Subroutine _get_known_occ: called CC=3 SCAR=11.0
120 5 my ($self, $needle) = @_;
121  
122 5 my $len = length($needle);
123  
124 5 my $haystack = join '', $self->{book}->get_internal_sequence;
125  
126 5 my $occ = undef;
127 5 my $pos = index($haystack, $needle);
128 5 ▶ while ($pos >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pos >= 0) 26 5 31
129 26 ▶ if (!defined($occ)) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $occ) { } 3 23 26
130 3 $occ = HTML::ListScraper::Occurence->new($len, $pos);
131 } else {
132 23 $occ->append_pos($pos);
133 }
134  
135 26 $pos = index($haystack, $needle, $pos + $len);
136 }
137  
138 5 return $occ;
139 }
140  
141 0 ▶sub get_known_sequence {
Subroutine get_known_sequence: not called CC=5 SCAR=34.0
Pod documented
142 0 my $self = shift;
143  
144 0 my $len = scalar(@_);
145 0 ▶ if ($len < 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($len < 2) 0 0 0
146 0 die "need at least 2 items to look for";
147 }
148  
149 0 my $needle = '';
150 0 foreach (@_) {
151 0 my $internal = $self->{book}->get_internal_name($_);
152 0 ▶ if (!defined($internal)) { # sequence not found if item not found
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $internal) 0 0 0
153 0 return undef;
154 }
155  
156 0 $needle .= $internal;
157 }
158  
159 0 my $occ = $self->_get_known_occ($needle);
160 0 ▶ return !defined($occ) ? undef : $self->_make_sequence($occ);
Branch: 1 branch
BranchTrueFalseTotal
!defined($occ) ? : 0 0 0
161 }
162  
163 5 ▶sub find_known_sequence {
Subroutine find_known_sequence: called CC=4 SCAR=13.9
Pod documented
164 5 my $self = shift;
165  
166 5 my $len = scalar(@_);
167  
168 5 ▶ if ($len < 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($len < 2) 0 5 5
169 0 die "need at least 2 items to look for";
170 }
171  
172 5 my @tags = @_;
173  
174 5 my $sign = '';
175 5 foreach (@tags) {
176 105 my $iname = $self->{book}->intern_name($_);
177 105 $sign .= $iname;
178 }
179  
180 5 my $occ = $self->_get_known_occ($sign);
181 5 ▶ if ($occ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($occ) { } 3 2 5
182 3 return $self->_make_approx_seq($sign, $occ);
183 } else {
184 2 return $self->_make_whole_seq($sign);
185 }
186 }
187  
188 10 ▶sub _make_approx_seq {
Subroutine _make_approx_seq: called CC=6 SCAR=18.1
189 10 my ($self, $sign, $occ) = @_;
190  
191 10 my $len = $occ->len;
192 10 my $edge;
193 my @instances;
194 10 foreach my $pos ($occ->positions) {
195 90 my $gap;
196 90 ▶ if (!defined($edge)) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $edge) { } 10 80 90
elsif ($pos >= $edge + $len) { } 80 0 80
197 10 $gap = 0;
198 } elsif ($pos >= $edge + $len) {
199 80 $gap = $edge + $len;
200 }
201  
202 90 ▶ if (defined($gap)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $gap) 90 0 90
203 90 push @instances,
204 $self->_make_approx_inst($sign, $gap, $pos);
205  
206 90 my @tags = $self->{book}->get_tags($pos, $len);
207 90 push @instances,
208 HTML::ListScraper::Instance->new(start => $pos,
209 match => 'exact', tags => \@tags);
210 90 $edge = $pos;
211 }
212 }
213  
214 10 ▶ if (!defined($edge)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $edge) 0 10 10
215 0 die "no occurence";
216 }
217  
218 10 my $iseq = $self->{book}->get_internal_sequence;
219 10 my $end = scalar(@$iseq);
220 10 push @instances,
221 $self->_make_approx_inst($sign, $edge + $len, $end);
222  
223 10 return HTML::ListScraper::Sequence->new(len => $len,
224 instances => \@instances);
225 }
226  
227 2 ▶sub _make_whole_seq {
Subroutine _make_whole_seq: called CC=2 SCAR=7.0
228 2 my ($self, $sign) = @_;
229  
230 2 my $iseq = $self->{book}->get_internal_sequence;
231 2 my $end = scalar(@$iseq);
232 2 my @instances = $self->_make_approx_inst($sign, 0, $end);
233  
234 2 my $seq = undef;
235 2 ▶ if (scalar(@instances)) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @instances) 2 0 2
236 2 $seq = HTML::ListScraper::Sequence->new(len => length($sign),
237 instances => \@instances);
238 }
239  
240 2 return $seq;
241 }
242  
243 102 ▶sub _make_approx_inst {
Subroutine _make_approx_inst: called CC=4 SCAR=13.9
244 102 my ($self, $sign, $begin, $end) = @_;
245  
246 102 my $size = $end - $begin;
247 102 ▶ if ($size < 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($size < 2) 74 28 102
248 74 return ();
249 }
250  
251 my $sweep = HTML::ListScraper::Sweep->new(
252 28 book => $self->{book}, sign => $sign,
253 begin => $begin, end => $end);
254 28 my $dust = $sweep->create_dust;
255 28 my @instances;
256 28 foreach my $align ($dust->get_alignments) {
257 44 my @tags;
258 44 foreach my $pos ($align->positions) {
259 776 my $t = $self->{book}->get_tag($pos);
260 776 push @tags, $t;
261 }
262  
263 44 my $start = $tags[0]->index;
264 44 push @instances,
265 HTML::ListScraper::Instance->new(start => $start,
266 match => 'approx', score => $align->score,
267 tags => \@tags);
268 }
269  
270 28 return @instances;
271 }
272  
273 5 ▶sub _make_sequence {
Subroutine _make_sequence: called CC=4 SCAR=14.0
274 5 my ($self, $occ) = @_;
275  
276 5 my $len = $occ->len;
277 5 my $edge;
278 my @instances;
279 5 foreach my $pos ($occ->positions) {
280 54 ▶ if (!defined($edge) || ($pos >= $edge + $len)) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $edge or $pos >= $edge + $len) 54 0 54
Condition66%2/3
not defined $edge or $pos >= $edge + $len
l!l&&r!l&&!r
5490
MC/DC0%0/2
not defined $edge or $pos >= $edge + $len
not defined $edge $pos >= $edge + $len
Truth table66%2/3
not(A) or B
A= not defined $edgeB= $pos >= $edge + $len
ABresult
1X1
011
000
281 54 my @tags = $self->{book}->get_tags($pos, $len);
282 54 push @instances,
283 HTML::ListScraper::Instance->new(start => $pos,
284 match => 'exact', tags => \@tags);
285 54 $edge = $pos;
286 }
287 }
288  
289 5 return HTML::ListScraper::Sequence->new(len => $len,
290 instances => \@instances);
291 }
292  
293 5300 ▶sub _is_tag {
Subroutine _is_tag: called CC=1 SCAR=0.0
294 5300 my ($self, $tag) = @_;
295  
296 5300 return $tag =~ m/^[a-z0-9-:]+$/i;
297 }
298  
299 2823 ▶sub on_start {
Subroutine on_start: called CC=5 SCAR=16.1
Pod documented
300 2823 my ($self, $rtag, $attr) = @_;
301  
302 2823 my $tag = $rtag;
303 2823 $tag =~ s/\s*\/$//;
304  
305 2823 ▶ if (!$self->_is_tag($tag)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->_is_tag($tag)) 2 2821 2823
306 2 $self->{book}->append_text($tag);
307 2 return;
308 }
309  
310 2821 ▶ if (exists($attr->{href}) && $attr->{href}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $attr->{'href'} and $attr->{'href'}) { } 630 2191 2821
Condition66%2/3
exists $attr->{"href"} and $attr->{"href"}
!ll&&!rl&&r
21910630
MC/DC50%1/2
exists $attr->{"href"} and $attr->{"href"}
exists $attr->{"href"} $attr->{"href"}
Truth table66%2/3
A and B
A= exists $attr->{"href"}B= $attr->{"href"}
ABresult
0X0
100
111
311 630 $self->{book}->push_link($tag, $attr->{href});
312 } else {
313 2191 $self->{book}->push_item($tag);
314 }
315  
316 2821 ▶ if ($tag ne $rtag) { # empty tag - close it
Branch: 1 branch
BranchTrueFalseTotal
if ($tag ne $rtag) 1 2820 2821
317 1 $self->on_end($tag);
318 }
319 }
320  
321 3602 ▶sub on_text {
Subroutine on_text: called CC=1 SCAR=0.0
Pod documented
322 3602 my ($self, $text) = @_;
323  
324 3602 $self->{book}->append_text($text);
325 }
326  
327 2477 ▶sub on_end {
Subroutine on_end: called CC=2 SCAR=8.4
Pod documented
328 2477 my ($self, $tag) = @_;
329  
330 2477 ▶ if (!$self->_is_tag($tag)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->_is_tag($tag)) 0 2477 2477
331 0 $self->{book}->append_text($tag);
332 0 return;
333 }
334  
335 2477 $self->{book}->push_item("/$tag");
336 }
337  
338 10 1;
339  
340 __END__
« blib/lib/HTML/ListScraper/Sweep.pm ↑ Summary blib/lib/HTML/ListScraper/Book.pm »