| 1 | package Lingua::EN::Summarize; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 1 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 1 | ▶ | use Exporter; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 1 | ▶ | use Text::Wrap qw(wrap); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 1 | ▶ | use Text::Sentence qw(split_sentences); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 1 | ▶ | use Lingua::EN::Summarize::Filters; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | 1 | ▶ | use vars qw($VERSION @ISA @EXPORT); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | 1 | @ISA = qw(Exporter); | |||||||||||||||||||||||||||
| 13 | 1 | @EXPORT = qw(summarize); | |||||||||||||||||||||||||||
| 14 | 1 | $VERSION = '0.2'; | |||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | 3 | ▶ | sub summarize { | ||||||||||||||||||||||||||
|
Subroutine
summarize: called CC=15 SCAR=27.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 18 | 3 | my ($text, %options) = @_; | |||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | ### This section massages the text into a usable format before summarizing. | ||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | # Run each filter over the text. | ||||||||||||||||||||||||||||
| 23 | 3 | ▶ | return unless $text; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 24 | 3 | ▶ | if ($options{filter}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 25 | my @filters = ref $options{filter} eq 'ARRAY' ? | ||||||||||||||||||||||||||||
| 26 | 0 | ▶ | @{$options{filter}} : $options{filter}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | 1 | foreach (@filters) { | |||||||||||||||||||||||||||
| 29 | 1 | ▶ | no strict 'refs'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | 1 | ▶ | if (ref $_ eq 'CODE') { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 32 | 0 | $text = $_->( $text ); | |||||||||||||||||||||||||||
| 33 | ▶ | } elsif (exists $Lingua::EN::Summarize::Filters::{$_} | |||||||||||||||||||||||||||
|
Condition50%1/2
exists $Lingua::EN::Summarize::Filters::{$_} and *{"Lingua::EN::Summarize::Filters::$_";}{"CODE"}
MC/DC0%0/1
exists $Lingua::EN::Summarize::Filters::{$_} and *{"Lingua::EN::Summarize::Filters::$_";}{"CODE"}
exists $Lingua::EN::Summarize::Filters::{$_}
Truth table50%1/2
A
A= exists $Lingua::EN::Summarize::Filters::{$_}
| |||||||||||||||||||||||||||||
| 34 | 1 | and *{"Lingua::EN::Summarize::Filters::$_"}{CODE}) { | |||||||||||||||||||||||||||
| 35 | 1 | $text = &{"Lingua::EN::Summarize::Filters::$_"}( $text ); | |||||||||||||||||||||||||||
| 36 | } else { | ||||||||||||||||||||||||||||
| 37 | 0 | croak "Unknown text filter \"$_\""; | |||||||||||||||||||||||||||
| 38 | } | ||||||||||||||||||||||||||||
| 39 | } | ||||||||||||||||||||||||||||
| 40 | } | ||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | # Strip whitespace and formatting out of the text. | ||||||||||||||||||||||||||||
| 43 | 3 | $text =~ s/^\s+//; | |||||||||||||||||||||||||||
| 44 | 3 | $text =~ s/\s+/ /sg; | |||||||||||||||||||||||||||
| 45 | 3 | $text =~ s/\s+$//; | |||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | 3 | ▶ | unless (exists $options{maxlength} and $options{maxlength} > 0) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
exists $options{"maxlength"} and $options{"maxlength"} > 0
MC/DC50%1/2
exists $options{"maxlength"} and $options{"maxlength"} > 0
exists $options{"maxlength"}
$options{"maxlength"} > 0
Truth table66%2/3
A and B
A= exists $options{"maxlength"}B= $options{"maxlength"} > 0
| |||||||||||||||||||||||||||||
| 48 | 2 | $options{maxlength} = length( $text ) / 30; | |||||||||||||||||||||||||||
| 49 | } | ||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | ### Here's where the interesting logic happens. | ||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | # First we break it into sentence pieces. Kind of. Sort of. | ||||||||||||||||||||||||||||
| 55 | 3 | my $keywords = "(is|are|was|were|will|have)"; | |||||||||||||||||||||||||||
| 56 | 659 | my @clauses = grep { /\b$keywords\b/i } | |||||||||||||||||||||||||||
| 57 | 3 | map { split /(,|;|--)/ } split_sentences( $text ); | |||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | 3 | my $stopwords = "(and|but|instead)"; | |||||||||||||||||||||||||||
| 60 | 3 | foreach (@clauses) { | |||||||||||||||||||||||||||
| 61 | 170 | s/^\s+//; | |||||||||||||||||||||||||||
| 62 | 170 | s/^$stopwords\s+//i; | |||||||||||||||||||||||||||
| 63 | 170 | $_ = ucfirst; | |||||||||||||||||||||||||||
| 64 | 170 | ▶ | $_ .= ". " unless /[.!?]$/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 65 | } | ||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | # Assemble the resulting phrases into the summary response. | ||||||||||||||||||||||||||||
| 68 | 3 | my $summary = ''; | |||||||||||||||||||||||||||
| 69 | 3 | ▶ | while (@clauses and length $summary < $options{maxlength}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
@clauses and length $summary < $options{"maxlength"}
MC/DC100%2/2
@clauses and length $summary < $options{"maxlength"}
@clauses
length $summary < $options{"maxlength"}
Truth table100%3/3
A and B
A= @clausesB= length $summary < $options{"maxlength"}
| |||||||||||||||||||||||||||||
| 70 | 19 | $summary .= " " . shift @clauses; | |||||||||||||||||||||||||||
| 71 | } | ||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | ### Done! Do any necessary postprocessing before returning. | ||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | # Prettyprint the summary to make it look nice on a terminal, if requested. | ||||||||||||||||||||||||||||
| 77 | 3 | ▶ | if ($options{wrap}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 78 | 0 | $Text::Wrap::columns = $options{wrap}; | |||||||||||||||||||||||||||
| 79 | 0 | $summary = wrap( '', '', $summary ); | |||||||||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | 3 | $summary =~ s/^\s+//mg; | |||||||||||||||||||||||||||
| 83 | 3 | $summary =~ s/[ \t]+/ /g; # if we use \s, that screws up the wrapping | |||||||||||||||||||||||||||
| 84 | 3 | $summary =~ s/\s+$//mg; | |||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | 3 | return $summary; | |||||||||||||||||||||||||||
| 87 | } | ||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | 1 | 1; | |||||||||||||||||||||||||||
| 91 | __END__ | ||||||||||||||||||||||||||||