HTML/Summary.pm

filter: Statementstmt 90.5% 115 / 127 Branchbran 71.8% 23 / 32 Conditioncond 83.3% 20 / 24 MC/DCmcdc 71.4% 10 / 14 Subroutinesub 90.0% 18 / 20 Podpod 100% 4 / 4 totaltotal 85.9% 190 / 221 CC 28 SCAR 17.2
CC
28
Cov
86%
option
20.0
__ANON__
9.0
__ANON__
6.1
CRAP 5.6
« Lingua/JA/Jtruncate.pm ↑ Summary Text/Sentence.pm »
1 package HTML::Summary;
2 2 $HTML::Summary::VERSION = '0.022';
3 #==============================================================================
4 #
5 # Start of POD
6 #
7 #==============================================================================
8  
9 =head1 NAME
10  
11 HTML::Summary - generate a summary from a web page
12  
13 =head1 SYNOPSIS
14  
15 use HTML::Summary;
16 use HTML::TreeBuilder;
17
18 my $tree = HTML::TreeBuilder->new;
19 $tree->parse( $document );
20  
21 my $summarizer = HTML::Summary->new(
22 LENGTH => 200,
23 USE_META => 1,
24 );
25  
26 $summary = $summarizer->generate( $tree );
27 $summarizer->option( 'USE_META' => 1 );
28 $length = $summarizer->option( 'LENGTH' );
29 if ( $summarizer->meta_used() ) {
30 # do something
31 }
32  
33 =head1 DESCRIPTION
34  
35 The C<HTML::Summary> module produces summaries from the textual content of
36 web pages. It does so using the location heuristic, which determines the value
37 of a given sentence based on its position and status within the document; for
38 example, headings, section titles and opening paragraph sentences may be
39 favoured over other textual content. A LENGTH option can be used to restrict
40 the length of the summary produced.
41  
42 =head1 CONSTRUCTOR
43  
44 =head2 new( $attr1 => $value1 [, $attr2 => $value2 ] )
45  
46 Possible attributes are:
47  
48 =over 4
49  
50 =item VERBOSE
51  
52 Generate verbose messages to STDERR.
53  
54 =item LENGTH
55  
56 Maximum length of summary (in bytes). Default is 500.
57  
58 =item USE_META
59  
60 Flag to tell summarizer whether to use the content of the C<<META>> tag
61 in the page header, if one is present, instead of generating a summary from the
62 body text. B<Note that> if the USE_META flag is set, this overrides the LENGTH
63 flag - in other words, the summary provided by the C<<META>> tag is
64 returned in full, even if it is greater than LENGTH bytes. Default is 0 (no).
65  
66 =back
67  
68 my $summarizer = HTML::Summary->new(LENGTH => 200);
69  
70 =head1 METHODS
71  
72 =head2 option( )
73  
74 Get / set HTML::Summary configuration options.
75  
76 my $length = $summarizer->option( 'LENGTH' );
77 $summarizer->option( 'USE_META' => 1 );
78  
79 =head2 generate( $tree )
80  
81 Takes an HTML::Element object, and generates a summary from it.
82  
83 my $tree = HTML::TreeBuilder->new;
84 $tree->parse( $document );
85 my $summary = $summarizer->generate( $tree );
86  
87 =head2 meta_used( )
88  
89 Returns 1 if the META tag description was used to generate the summary.
90  
91 if ( $summarizer->meta_used() ) {
92 # do something ...
93 }
94  
95 =head1 SEE ALSO
96  
97 L<HTML::TreeBuilder>,
98 L<Text::Sentence>,
99 L<Lingua::JA::Jcode>,
100 L<Lingua::JA::Jtruncate>.
101  
102 =head1 REPOSITORY
103  
104 L<https://github.com/neilb/HTML-Summary>
105  
106 =head1 AUTHORS
107  
108 This module was originally whipped up by Neil Bowers and Tony Rose.
109 It was then developed and maintained by Ave Wrigley and Tony Rose.
110  
111 Neil Bowers is currently maintaining the HTML-Summary distribution.
112  
113 Neil Bowers E<lt>neilb@cpan.orgE<gt>
114  
115 =head1 COPYRIGHT AND LICENSE
116  
117 Copyright (c) 1997 Canon Research Centre Europe (CRE). All rights reserved.
118  
119 This is free software; you can redistribute it and/or modify it under
120 the same terms as the Perl 5 programming language system itself.
121  
122 =cut
123  
124 #==============================================================================
125 #
126 # End of POD
127 #
128 #==============================================================================
129  
130 #==============================================================================
131 #
132 # Pragmas
133 #
134 #==============================================================================
135  
136 2 require 5.006;
137 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
138 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
139  
140 #==============================================================================
141 #
142 # Modules
143 #
144 #==============================================================================
145  
146 2 ▶use Text::Sentence qw( split_sentences );
Subroutine BEGIN: called CC=1 SCAR=0.0
147 2 ▶use Lingua::JA::Jtruncate qw( jtruncate );
Subroutine BEGIN: called CC=1 SCAR=0.0
148  
149 #==============================================================================
150 #
151 # Constants
152 #
153 #==============================================================================
154  
155 2 ▶use constant IGNORE_TEXT => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
156  
157 #==============================================================================
158 #
159 # Private globals
160 #
161 #==============================================================================
162  
163 2 my $DEFAULT_SCORE = 0;
164  
165 2 my %ELEMENT_SCORES = (
166 'p' => 100,
167 'h1' => 90,
168 'h2' => 80,
169 'h3' => 70,
170 );
171  
172 2 my %DEFAULTS = (
173 'USE_META' => 0,
174 'VERBOSE' => 0,
175 'LENGTH' => 500,
176 );
177  
178 #==============================================================================
179 #
180 # Public methods
181 #
182 #==============================================================================
183  
184 #------------------------------------------------------------------------------
185 #
186 # new - constructor. Configuration through "hash" type arguments, i.e.
187 # my $abs = HTML::Summary->new( VAR1 => 'foo', VAR2 => 'bar' );
188 #
189 #------------------------------------------------------------------------------
190  
191 sub new
192 25 ▶{
Subroutine new: called CC=1 SCAR=0.0
Pod documented
193 25 my $class = shift;
194 25 my $self = bless { }, $class;
195 25 return $self->_initialize( @_ );
196 }
197  
198 #------------------------------------------------------------------------------
199 #
200 # generate - main public interface method to generate a summary
201 #
202 #------------------------------------------------------------------------------
203  
204 sub generate
205 26 ▶{
Subroutine generate: called CC=5 SCAR=16.1
Pod documented
206 26 my $self = shift;
207 26 my $tree = shift;
208  
209 26 my $summary;
210  
211 26 $self->_verbose( 'Generate summary ...' );
212  
213 # check to see if there is a summary already defined in a META tag ...
214  
215 26 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"USE_META"} and $summary = $self->_get_summary_from_meta($tree)) 1 25 26
Condition100%3/3
$self->{"USE_META"} and $summary = $self->_get_summary_from_meta($tree)
!ll&&!rl&&r
2411
MC/DC100%2/2
$self->{"USE_META"} and $summary = $self->_get_summary_from_meta($tree)
$self->{"USE_META"} $summary = $self->_get_summary_from_meta($tree)
Truth table100%3/3
A and B
A= $self->{"USE_META"}B= $summary = $self->_get_summary_from_meta($tree)
ABresult
0X0
100
111
216 $self->{ USE_META } and
217 $summary = $self->_get_summary_from_meta( $tree )
218 )
219 {
220 1 $self->_verbose( "use summary from META tag ..." );
221 1 $self->_verbose( $summary );
222 1 return $summary;
223 }
224  
225 # traverse the HTML tree, building up @summary array
226  
227 25 my @summary = $self->_get_summary( $tree );
228  
229 # sort @summary by score, truncate if it is greater than LENGTH
230 # characters, and the re-sort by original order. Truncate AFTER the LENGTH
231 # has been exceeded, so that last sentence is truncated later by
232 # jtruncate
233  
234 25 @summary = sort { $b->{ score } <=> $a->{ score } } @summary;
235  
236 25 my $tot_length = 0;
237 25 my @truncated = ();
238  
239 25 for ( @summary )
240 {
241 135 push( @truncated, $_ );
242 135 ▶ last if ( $tot_length += $_->{ 'length' } ) > $self->{ LENGTH };
Branch: 1 branch
BranchTrueFalseTotal
if ($tot_length += $_->{"length"}) > $self->{"LENGTH"} 25 110 135
243 }
244 25 @truncated = sort { $a->{ order } <=> $b->{ order } } @truncated;
245  
246 # these whitespaces will push the length over LENGTH, but jtruncate
247 # should take care of this
248  
249 25 $summary = join( ' ', map { $_->{ text } } @truncated );
250 25 $self->_verbose( "truncate the summary to ", $self->{ LENGTH } );
251 25 $summary = jtruncate( $summary, $self->{ LENGTH } );
252 25 return $summary;
253 }
254  
255 #------------------------------------------------------------------------------
256 #
257 # meta_used - tells whether the description from the META tag was used; returns
258 # 1 if it was, 0 if the summary was generated automatically
259 #
260 #------------------------------------------------------------------------------
261  
262 sub meta_used
263 0 ▶{
Subroutine meta_used: not called CC=1 SCAR=6.9
Pod documented
264 0 my $self = shift;
265  
266 0 return $self->{ META_USED };
267 }
268  
269 #------------------------------------------------------------------------------
270 #
271 # option - get / set configuration option
272 #
273 #------------------------------------------------------------------------------
274  
275 sub option
276 0 ▶{
Subroutine option: not called CC=4 SCAR=30.0
Pod documented
277 0 my $self = shift;
278 0 my $option = shift;
279 0 my $val = shift;
280  
281 0 ▶ die "No HTML::Summary option name given" unless defined $option;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $option 0 0 0
282 die "$option is not an HTML::Summary option" unless
283 0 ▶ grep { $_ eq $option } keys %DEFAULTS
Branch: 1 branch
BranchTrueFalseTotal
unless grep {$_ eq $option;} keys %DEFAULTS 0 0 0
284 ;
285  
286 0 ▶ if ( defined $val )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $val) 0 0 0
287 {
288 0 $self->{ $option } = $val;
289 }
290  
291 0 return $self->{ $option } = $val;
292 }
293  
294 #==============================================================================
295 #
296 # Private methods
297 #
298 #==============================================================================
299  
300 #------------------------------------------------------------------------------
301 #
302 # _initialize - supports sub-classing
303 #
304 #------------------------------------------------------------------------------
305  
306 sub _initialize
307 25 ▶{
Subroutine _initialize: called CC=2 SCAR=7.0
308 25 my $self = shift;
309  
310 25 ▶ return undef unless @_ % 2 == 0; # check that config hash has even no.
Branch: 1 branch
BranchTrueFalseTotal
unless @_ % 2 == 0 0 25 25
311 # of elements
312  
313 25 %{ $self } = ( %DEFAULTS, @_ ); # set options from defaults / config.
314 # hash passed as arguments
315  
316 25 return $self;
317 }
318  
319 #------------------------------------------------------------------------------
320 #
321 # _verbose - generate verbose error messages, if the VERBOSE option has been
322 # selected
323 #
324 #------------------------------------------------------------------------------
325  
326 sub _verbose
327 867 ▶{
Subroutine _verbose: called CC=2 SCAR=8.1
328 867 my $self = shift;
329  
330 867 ▶ return unless $self->{ VERBOSE };
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"VERBOSE"} 867 0 867
331 0 print STDERR @_, "\n";
332 }
333  
334 #------------------------------------------------------------------------------
335 #
336 # _get_summary - get sentences from an element to generate the summary from.
337 # Uses lexically scoped array @sentences to build up result from the traversal
338 # callback
339 #
340 #------------------------------------------------------------------------------
341  
342 sub _get_summary
343 25 ▶{
Subroutine _get_summary: called CC=1 SCAR=0.0
344 25 my $self = shift;
345 25 my $tree = shift;
346  
347 25 my @summary = ();
348 470 ▶ my $add_sentence = sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
349 470 my $text = shift;
350 470 my $tag = shift;
351 470 ▶ my $score = shift || $DEFAULT_SCORE;
Condition66%2/3
shift() || $DEFAULT_SCORE
l!l&&r!l&&!r
2600210
MC/DC50%1/2
shift() || $DEFAULT_SCORE
shift() $DEFAULT_SCORE
Truth table66%2/3
A || B
A= shift()B= $DEFAULT_SCORE
ABresult
1X1
011
000
352  
353 470 ▶ return unless $text =~ /\w/;
Branch: 1 branch
BranchTrueFalseTotal
unless $text =~ /\w/ 30 440 470
354  
355 440 $text =~ s!^\s*!!; # remove leading ...
356 440 $text =~ s!\s*$!!; # ... and trailing whitespace
357  
358 440 $summary[ scalar( @summary ) ] = {
359 'text' => $text,
360 'length' => length( $text ),
361 'tag' => $tag,
362 'score' => $score,
363 'order' => scalar( @summary ),
364 };
365 25 };
366 $tree->traverse(
367 1339 ▶ sub {
Subroutine __ANON__: called CC=9 SCAR=22.0
368 1339 my $node = shift;
369 1339 my $flag = shift;
370  
371 1339 ▶ if ( $flag ) # entering node ...
Branch: 1 branch
BranchTrueFalseTotal
if ($flag) 1001 338 1339
372 {
373 1001 my $tag = $node->tag;
374 1001 ▶ return 0 if $tag eq 'head';
Branch: 1 branch
BranchTrueFalseTotal
if $tag eq "head" 25 976 1001
375  
376 # add sentences which either are scoring, or span no other
377 # scoring sentences (and have a score of 0). In this way, all
378 # text is captured, even if it scores 0; the only exception is
379 # something like <BODY>some text <P>foobar</P></BODY>, where
380 # everything but "foobar" will be lost. However, if you have
381 # <BODY><TD>some text</TD><P>foobar</P></BODY> you should get
382 # all the text.
383  
384 976 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if ($ELEMENT_SCORES{$tag} or not _has_scoring_element($node)) 638 338 976
Condition100%3/3
$ELEMENT_SCORES{$tag} or not _has_scoring_element($node)
l!l&&r!l&&!r
458180338
MC/DC100%2/2
$ELEMENT_SCORES{$tag} or not _has_scoring_element($node)
$ELEMENT_SCORES{$tag} not _has_scoring_element($node)
Truth table100%3/3
A or not(B)
A= $ELEMENT_SCORES{$tag}B= not _has_scoring_element($node)
ABresult
1X1
011
000
385 $ELEMENT_SCORES{ $tag } ||
386 ! _has_scoring_element( $node )
387 )
388 {
389 638 my $text = _get_text( $node );
390 638 foreach ( $text ) # alias $_ to $text
391 {
392 # get rid of whitespace (including &nbsp;) from start /
393 # end of $text
394 638 s/^[\s\160]*//;
395 638 s/[\s\160]*$//;
396 # get rid of any spurious tags that have slipped
397 # through the HTML::TreeBuilder
398 638 s!<[^>]+>!!g;
399 }
400  
401 638 ▶ if ( $text =~ /\S/ )
Branch: 1 branch
BranchTrueFalseTotal
if ($text =~ /\S/) 344 294 638
402 {
403 344 ▶ my $score = $ELEMENT_SCORES{ $tag } || $DEFAULT_SCORE;
Condition66%2/3
$ELEMENT_SCORES{$tag} || $DEFAULT_SCORE
l!l&&r!l&&!r
260084
MC/DC50%1/2
$ELEMENT_SCORES{$tag} || $DEFAULT_SCORE
$ELEMENT_SCORES{$tag} $DEFAULT_SCORE
Truth table66%2/3
A || B
A= $ELEMENT_SCORES{$tag}B= $DEFAULT_SCORE
ABresult
1X1
011
000
404  
405 # add all the sentences in the text. Only the first
406 # sentence gets the element score - the rest get the
407 # default score
408  
409 344 $self->_verbose( "TEXT: $text" );
410 344 for my $sentence (
411 split_sentences( $text, $self->{ 'LOCALE' } )
412 )
413 {
414 470 $self->_verbose( "SENTENCE: $text" );
415 470 $add_sentence->( $sentence, $tag, $score );
416 470 $score = $DEFAULT_SCORE;
417 }
418 }
419  
420 # return 0 to avoid getting the same sentence in a scoring
421 # "daughter" element
422  
423 638 return 0;
424 }
425 }
426  
427 # continue traversal ...
428  
429 676 return 1;
430 },
431 25 IGNORE_TEXT
432 );
433 25 return @summary;
434 }
435  
436 #------------------------------------------------------------------------------
437 #
438 # _get_summary_from_meta - check to see if there is already a summary
439 # defined in the META tag in the HEAD
440 #
441 #------------------------------------------------------------------------------
442  
443 sub _get_summary_from_meta
444 2 ▶{
Subroutine _get_summary_from_meta: called CC=2 SCAR=7.0
445 2 my $self = shift;
446 2 my $tree = shift;
447  
448 2 my $summary;
449  
450 $tree->traverse(
451 23 ▶ sub {
Subroutine __ANON__: called CC=6 SCAR=18.1
452 23 my $node = shift;
453 23 my $flag = shift;
454  
455 23 ▶ if ($node->tag eq 'meta'
Branch: 1 branch
BranchTrueFalseTotal
if ($node->tag eq "meta" and defined $node->attr("name") and lc $node->attr("name") eq "description" and defined $node->attr("content")) 1 22 23
Condition77%7/9
$node->tag eq "meta" and defined $node->attr("name") and lc $node->attr("name") eq "description" and defined $node->attr("content")
!ll&&!rl&&r
2201
$node->tag eq "meta" and defined $node->attr("name") and lc $node->attr("name") eq "description"
!ll&&!rl&&r
2201
$node->tag eq "meta" and defined $node->attr("name")
!ll&&!rl&&r
2021
MC/DC50%2/4
$node->tag eq "meta" and defined $node->attr("name") and lc $node->attr("name") eq "description" and defined $node->attr("content")
$node->tag eq "meta" defined $node->attr("name") lc $node->attr("name") eq "description" defined $node->attr("content")
Truth table60%3/5
A and B and C and D
A= $node->tag eq "meta"B= defined $node->attr("name")C= lc $node->attr("name") eq "description"D= defined $node->attr("content")
ABCDresult
0XXX0
10XX0
110X0
11100
11111
456 && defined($node->attr('name'))
457 && lc( $node->attr('name') ) eq 'description'
458 && defined($node->attr('content')))
459 {
460 1 $summary = $node->attr( 'content' );
461 1 ▶ $summary = undef if $summary eq 'content';
Branch: 1 branch
BranchTrueFalseTotal
if $summary eq "content" 0 1 1
462 1 return 0;
463 }
464 22 return 1;
465 },
466 2 IGNORE_TEXT
467 );
468  
469 2 ▶ $self->{ META_USED } = defined( $summary ) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
defined $summary ? : 1 1 2
470 2 return $summary;
471 }
472  
473 #==============================================================================
474 #
475 # Private functions
476 #
477 #==============================================================================
478  
479 #------------------------------------------------------------------------------
480 #
481 # _get_text - get all the text spanned by an element. Uses lexically scoped
482 # variable $html to build up result from the traversal callback
483 #
484 #------------------------------------------------------------------------------
485
486 sub _get_text
487 638 ▶{
Subroutine _get_text: called CC=1 SCAR=0.0
488 638 my $node = shift;
489
490 638 my $html = '';
491 $node->traverse(
492 7356 ▶ sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
493 7356 my $node = shift;
494 7356 ▶ $html .= $node unless ref( $node );
Branch: 1 branch
BranchTrueFalseTotal
unless ref $node 1874 5482 7356
495 7356 return 1;
496 }
497 638 );
498 638 return $html;
499 }
500  
501 #------------------------------------------------------------------------------
502 #
503 # _has_scoring_element - check to see if this element spans any scoring
504 # element. Uses lexically scoped variable $has_scoring_element to build up
505 # result from the traversal callback.
506 #
507 #------------------------------------------------------------------------------
508  
509 sub _has_scoring_element
510 518 ▶{
Subroutine _has_scoring_element: called CC=1 SCAR=0.0
511 518 my $node = shift;
512
513 518 my $has_scoring_element = 0;
514 $node->traverse(
515 34325 ▶ sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
516 34325 my $node = shift;
517 34325 my $tag = $node->tag;
518 34325 ▶ $has_scoring_element ||= $ELEMENT_SCORES{ $tag };
Condition100%3/3
$has_scoring_element ||= $ELEMENT_SCORES{$tag}
l!l&&r!l&&!r
300153383972
MC/DC100%2/2
$has_scoring_element ||= $ELEMENT_SCORES{$tag}
$has_scoring_element $ELEMENT_SCORES{$tag}
Truth table100%3/3
A ||= B
A= $has_scoring_elementB= $ELEMENT_SCORES{$tag}
ABresult
1X1
011
000
519 34325 return 1;
520 },
521 518 IGNORE_TEXT
522 );
523 518 return $has_scoring_element;
524 }
525  
526 #==============================================================================
527 #
528 # Return TRUE
529 #
530 #==============================================================================
531  
532 2 1;
« Lingua/JA/Jtruncate.pm ↑ Summary Text/Sentence.pm »