ExtractContent.pm

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