blib/lib/HTML/ExtractContent.pm

filter: Statementstmt 87.6% 114 / 130 Branchbran 62.0% 31 / 50 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 18 / 18 Podpod 100% 4 / 4 totaltotal 82.6% 167 / 202 CC 29 SCAR 24.0
CC
29
Cov
81%
extract
20.0
_is_linklist
17.4
_extract_title
4.0
CRAP 11.1
↑ Summary blib/lib/HTML/ExtractContent/Util.pm »
1 package HTML::ExtractContent;
2 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 # core
7 2 ▶use List::Util qw(reduce);
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 # cpan
10 use Class::Accessor::Lite (
11 2 rw => [qw(opt content)],
12 2 ▶);
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 # lib
15 2 ▶use HTML::ExtractContent::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 2 our $VERSION = '0.11';
18  
19 sub new {
20 1 ▶ my ($class, $opt) = @_;
Subroutine new: called CC=1 SCAR=0.0
Pod documented
21 1 my $self = bless {}, $class;
22 $self->{opt} = {
23 1 threshold => 60, # threhold for score of clusters
24 min_length => 30, # minimum length of blocks
25 decay_factor => 0.75, # decay factor for block scores
26 no_body_factor => 0.72,
27 continuous_factor => 1.62, # continuous factor for block scores
28 punctuation_weight => 10, # score weight for punctuations
29 punctuations => qr/(?:[。、.,!?]|\.[^A-Za-z0-9]|,[^0-9]|!|\?)/is,
30 waste_expressions => qr/Copyright|All\s*Rights?\s*Reserved?/is,
31 # characteristic keywords including footer
32 affiliate_expressions =>
33 qr/amazon[a-z0-9\.\/\-\?&]+-22/is,
34 block_separator => qr/<\/?(?:div|center|td)[^>]*>|<p\s*[^>]*class\s*=\s*["']?(?:posted|plugin-\w+)['"]?[^>]*>/is,
35 # nocontent => qr/<\/frameset>|<meta\s+http-equiv\s*=\s*["']?refresh['"]?[^>]*url/is,
36 nocontent => qr/<\/frameset>/is,
37 min_nolink => 8,
38 nolist_ratio => 0.2,
39 debug => 0
40 };
41 $self->{pattern} = {
42 1 a => qr/<a\s[^>]*>.*?<\/a\s*>/is,
43 href => qr/<a\s+href\s*=\s*(['"]?)(?:[^"'\s]+)\1/is,
44 list => qr/<(ul|dl|ol)(.+)<\/\1>/is,
45 li => qr/(?:<li[^>]*>|<dd[^>]*>)/is,
46 title => qr/<title[^>]*>(.*?)<\/title\s*>/is,
47 headline => qr/(<h\d\s*>\s*(.*?)\s*<\/h\d\s*>)/is,
48 head => qr/<head[^>]*>.*?<\/head\s*>/is,
49 comment => qr{ (?:
50 <!-- .*? --> |
51 # remove invisible elements
52 < ( [\w:.-]+ ) \s [^>]*? style \s* = [^>]*? \b
53 (?: display \s* : \s* none | visibility \s* : \s* hidden )
54 \b [^>]* > .*? </ \1 \s* >
55 ) }xis,
56 special => qr/<![A-Za-z].*?>/is,
57 useless => [
58 qr/<(script|style|select|noscript)[^>]*>.*?<\/\1\s*>/is,
59 qr/<div\s[^>]*(?:id|class)\s*=\s*['"]?\S*(?:more|menu|side|navi)\S*["']?[^>]*>/is,
60 ],
61 };
62 1 return bless $self, $class;
63 }
64  
65 sub as_text {
66 2 ▶ my $self = shift;
Subroutine as_text: called CC=1 SCAR=0.0
Pod documented
67 2 return to_text $self->content;
68 }
69  
70 sub as_html {
71 2 ▶ my $self = shift;
Subroutine as_html: called CC=1 SCAR=0.0
Pod documented
72 2 return $self->content;
73 }
74  
75 sub extract {
76 2 ▶ my $self = shift;;
Subroutine extract: called CC=19 SCAR=29.9
Pod documented
77 2 $self->content(shift);
78 2 ▶ if ($self->content =~ $self->opt->{nocontent}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->content =~ /$self->opt->{"nocontent"}/) 0 2 2
79 # frameset or redirect
80 0 $self->content('');
81 0 return $self;
82 }
83 2 $self->_extract_title;
84 2 $self->_eliminate_head;
85  
86 2 $self->_eliminate_useless_symbols;
87 2 $self->_eliminate_useless_tags;
88  
89 2 my ($factor, $continuous);
90 2 $factor = $continuous = 1.0;
91 2 my $body = '';
92 2 my $score = 0;
93 2 my $best = {
94 content => "",
95 score => 0,
96 };
97 2 my @list = split $self->opt->{block_separator}, $self->content;
98 2 my $flag = 0;
99 2 for my $block (@list) {
100 44 $block = strip $block;
101 44 ▶ next unless decode $block;
Branch: 1 branch
BranchTrueFalseTotal
unless decode($block) 31 13 44
102 13 ▶ $continuous /= $self->opt->{continuous_factor} if length $body;
Branch: 1 branch
BranchTrueFalseTotal
if length $body 8 5 13
103  
104 # ignore link list block
105 13 my $nolink = $self->_eliminate_links($block);
106 13 my $nolinklen = length $nolink;
107 13 ▶ next if $nolinklen < $self->opt->{min_length};
Branch: 1 branch
BranchTrueFalseTotal
if $nolinklen < $self->opt->{"min_length"} 4 9 13
108  
109 # score
110 9 my $c = $self->_score($nolink, $factor);
111 9 $factor *= $self->opt->{decay_factor};
112  
113 # anti-scoring factors
114 9 my $no_body_rate = $self->_no_body_rate($block);
115  
116 9 $c *= ($self->opt->{no_body_factor} ** $no_body_rate);
117 9 my $c1 = $c * $continuous;
118  
119 # cluster scoring
120 9 ▶ if ($c1 > $self->opt->{threshold}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($c1 > $self->opt->{'threshold'}) { } 2 7 9
elsif ($c > $self->opt->{'threshold'}) { } 1 6 7
121 2 $flag = 1;
122 print "\n---- continue $c*$continuous=$c1 $nolinklen\n\n$block\n"
123 2 ▶ if $self->opt->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $self->opt->{"debug"} 0 2 2
124 2 $body .= $block . "\n";
125 2 $score += $c1;
126 2 $continuous = $self->opt->{continuous_factor};
127 } elsif ($c > $self->opt->{threshold}) {
128 1 $flag = 1;
129 1 ▶ print "\n---- end of cluster: $score\n" if $self->opt->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $self->opt->{"debug"} 0 1 1
130 1 ▶ if ($score > $best->{score}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($score > $best->{"score"}) 1 0 1
131 1 ▶ print "!!!! best: score=$score\n" if $self->opt->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $self->opt->{"debug"} 0 1 1
132 1 $best = {
133 content => $body,
134 score => $score,
135 };
136 }
137 1 ▶ print "\n" if $self->opt->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $self->opt->{"debug"} 0 1 1
138 1 $body = $block . "\n";
139 1 $score = $c;
140 1 $continuous = $self->opt->{continuous_factor};
141 print "\n---- continue $c*$continuous=$c1 $nolinklen\n\n$block\n"
142 1 ▶ if $self->opt->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $self->opt->{"debug"} 0 1 1
143 } else {
144 6 ▶ $factor /= $self->opt->{decay_factor} if !$flag;
Branch: 1 branch
BranchTrueFalseTotal
unless $flag 3 3 6
145 print "\n>> reject $c*$continuous=$c1 $nolinklen\n$block\n",
146 6 ▶ "<< reject\n" if $self->opt->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $self->opt->{"debug"} 0 6 6
147 }
148 }
149 2 ▶ print "\n---- end of cluster: $score\n" if $self->opt->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $self->opt->{"debug"} 0 2 2
150 2 ▶ if ($best->{score} < $score) {
Branch: 1 branch
BranchTrueFalseTotal
if ($best->{"score"} < $score) 1 1 2
151 1 ▶ print "!!!! best: score=$score\n" if $self->opt->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $self->opt->{"debug"} 0 1 1
152 1 $best = {
153 content =>$body,
154 score => $score,
155 };
156 }
157 2 $self->content($best->{content});
158  
159 2 return $self;
160 }
161  
162 sub _score {
163 9 ▶ my ($self, $nolink, $factor) = @_;
Subroutine _score: called CC=1 SCAR=0.0
164 return ((length $nolink)
165 + (match_count $nolink, $self->opt->{punctuations})
166 * $self->opt->{punctuation_weight})
167 9 * $factor;
168 }
169  
170 sub _no_body_rate {
171 9 ▶ my ($self, $block) = @_;
Subroutine _no_body_rate: called CC=1 SCAR=0.0
172 return (match_count $block,$self->opt->{waste_expressions})
173 9 + (match_count $block,$self->opt->{affiliate_expressions})/2.0;
174 }
175  
176 sub _extract_title {
177 2 ▶ my $self = shift;
Subroutine _extract_title: called CC=4 SCAR=14.0
178 2 my $title;
179 2 ▶ if ($self->content =~ $self->{pattern}->{title}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->content =~ /$self->{"pattern"}{"title"}/) 2 0 2
180 2 $title = strip (strip_tags $1);
181 2 ▶ if (length $title) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $title) 2 0 2
182 2 my $pat = $self->{pattern}->{headline};
183 2 $self->{content} =~ s/$pat/
184 4 ▶ (index $title, strip(strip_tags($2))) >= 0 ? "<div>$2<\/div>" : "$1"/igse;
Branch: 1 branch
BranchTrueFalseTotal
index($title, strip(strip_tags($2))) != -1 ? : 2 2 4
185 }
186 }
187 }
188  
189 sub _eliminate_head {
190 2 ▶ my $self = shift;
Subroutine _eliminate_head: called CC=1 SCAR=0.0
191 2 my $pat = $self->{pattern}->{head};
192 2 $self->{content} =~ s/$pat//is;
193 }
194  
195 sub _eliminate_useless_symbols {
196 2 ▶ my $self = shift;
Subroutine _eliminate_useless_symbols: called CC=1 SCAR=0.0
197 2 my $comment = $self->{pattern}->{comment};
198 2 my $special = $self->{pattern}->{special};
199 2 $self->{content} =~ s/$comment//igs;
200 2 $self->{content} =~ s/$special//igs;
201 }
202  
203 sub _eliminate_useless_tags {
204 2 ▶ my $self = shift;
Subroutine _eliminate_useless_tags: called CC=2 SCAR=6.9
205 2 my @useless = @{$self->{pattern}->{useless}};
206 2 for my $pat (@useless) {
207 4 $self->{content} =~ s/$pat//igs;
208 }
209 }
210  
211 sub _eliminate_links {
212 13 ▶ my ($self, $block) = @_;
Subroutine _eliminate_links: called CC=3 SCAR=11.2
213 13 my $count = match_count $block, $self->{pattern}->{a};
214 13 my $nolink = to_text (eliminate_forms (eliminate_links $block));
215 13 ▶ return '' if length $nolink < $self->opt->{min_nolink} * $count;
Branch: 1 branch
BranchTrueFalseTotal
if length $nolink < $self->opt->{"min_nolink"} * $count 0 13 13
216 13 ▶ return '' if $self->_is_linklist($block);
Branch: 1 branch
BranchTrueFalseTotal
if $self->_is_linklist($block) 0 13 13
217 13 return $nolink;
218 }
219  
220 sub _is_linklist {
221 13 ▶ my ($self, $block) = @_;
Subroutine _is_linklist: called CC=5 SCAR=28.6
222 13 my $listpat = $self->{pattern}->{list};
223 13 ▶ if ($block =~ $listpat) {
Branch: 1 branch
BranchTrueFalseTotal
if ($block =~ /$listpat/) 0 13 13
224 0 my $list = $2;
225 0 my @fragments = split($listpat, $block, 2);
226 0 my $nolist = $list;
227 0 $nolist =~ s/$listpat//igs;
228 0 $nolist = to_text(join($nolist, @fragments));
229 0 my @listitems = split $self->{pattern}->{li}, $list;
230 0 shift @listitems;
231 0 my $rate = 0;
232 0 for my $li (@listitems) {
233 0 ▶ $rate++ if $li =~ $self->{pattern}->{href};
Branch: 1 branch
BranchTrueFalseTotal
if $li =~ /$self->{"pattern"}{"href"}/ 0 0 0
234 }
235 0 ▶ $rate = 1.0 * $rate / ($#listitems+1) if $#listitems+1;
Branch: 1 branch
BranchTrueFalseTotal
if $#listitems + 1 0 0 0
236 0 $list = to_text $list;
237 0 my $limit = ($self->opt->{nolist_ratio}*$rate)
238 * ($rate * (length $list));
239 0 return length $nolist < $limit;
240 }
241 13 return 0;
242 }
243  
244 2 1;
245 __END__
↑ Summary blib/lib/HTML/ExtractContent/Util.pm »