| 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
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 62 | 79 | my @round = $self->_make_presentable; | |||||||||||||||||||||||||||||||
| 63 | 79 | ▶ | if ((scalar(@round) >= 2) && | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
scalar @round >= 2 and scalar @round > scalar(@{$self->{"needle"};}) / 2
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
| |||||||||||||||||||||||||||||||||
| 64 | 77 | (scalar(@round) > (scalar(@{$self->{needle}}) / 2))) { | |||||||||||||||||||||||||||||||
| 65 | 44 | ▶ | if (scalar(@round) < $found_count) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 103 | 1078 | my ($i, $j) = @{$arg->{align}}; | |||||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||||||
| 105 | 1078 | ▶ | if ($self->{haystack}->[$i] eq $self->{needle}->[$j]) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 136 | 533 | push @stack, $cur_tag; | |||||||||||||||||||||||||||||||
| 137 | } else { | ||||||||||||||||||||||||||||||||
| 138 | 535 | my $skip = 0; | |||||||||||||||||||||||||||||||
| 139 | 535 | ▶ | while (!$skip && scalar(@stack) && | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%5/5
not $skip and scalar @stack and $stack[scalar(@stack) - 1]->name ne $stem
not $skip and scalar @stack
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
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 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
Condition100%2/2
not $skip and scalar @stack
MC/DC100%1/1
not $skip and scalar @stack
not $skip
Truth table100%2/2
not(A)
A= not $skip
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 159 | 32 | my $top_tag = pop @stack; | |||||||||||||||||||||||||||||||
| 160 | 32 | ▶ | if ($self->{book}->is_unclosed_tag($top_tag->name)) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 161 | 10 | $core{$top_tag->index} = 1; | |||||||||||||||||||||||||||||||
| 162 | } | ||||||||||||||||||||||||||||||||
| 163 | } | ||||||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||||||
| 165 | 79 | return sort { $a <=> $b; } keys %core; | |||||||||||||||||||||||||||||||
| 166 | } | ||||||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||||||
| 168 | 10 | 1; | |||||||||||||||||||||||||||||||