blib/lib/Text/Context/EitherSide.pm

filter: Statementstmt 94.7% 54 / 57 Branchbran 75.0% 12 / 16 Conditioncond 100% 6 / 6 MC/DCmcdc 100% 4 / 4 Subroutinesub 91.6% 11 / 12 Podpod 100% 6 / 6 totaltotal 92.0% 93 / 101 CC 15 SCAR 14.8
CC
15
Cov
91%
as_sparse_list
7.0
context
6.0
as_list
6.0
CRAP 4.4
↑ Summary
1 package Text::Context::EitherSide;
2  
3 2 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 2 our $VERSION = '1.4';
9  
10 2 ▶use base 'Exporter';
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 our @EXPORT_OK = qw(get_context);
12  
13 2 ▶use constant DEFAULT_WORDS => 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 9 ▶sub get_context {
Subroutine get_context: called CC=1 SCAR=0.0
Pod documented
16 9 my ($n, $string, @words) = @_;
17 9 Text::Context::EitherSide->new($string, context => $n)->as_string(@words);
18 }
19  
20 9 ▶sub new {
Subroutine new: called CC=3 SCAR=11.4
Pod documented
21 9 my $class = shift;
22 9 ▶ my $text = shift or carp "No text supplied for context search";
Branch: 1 branch
BranchTrueFalseTotal
my $text = shift() or carp("No text supplied for context search") 9 0 9
23 9 my %args = @_;
24  
25 return bless {
26 9 ▶ n => exists $args{context} ? $args{context} : DEFAULT_WORDS,
Branch: 1 branch
BranchTrueFalseTotal
exists $args{'context'} ? : 9 0 9
27 text => $text
28 }, $class;
29 }
30  
31 0 ▶sub context {
Subroutine context: not called CC=2 SCAR=17.9
Pod documented
32 0 my $self = shift;
33 0 ▶ $self->{n} = shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 0 0
34 0 return $self->{n};
35 }
36  
37 9 ▶sub as_sparse_list {
Subroutine as_sparse_list: called CC=7 SCAR=19.5
Pod documented
38 9 my $self = shift;
39 9 my @words = @_;
40 19 my %keywords = map { lc $_ => 1 }
41 9 map { split /\s+/, $_ } @words; # Decouple phrases
42  
43 # First, split the string into words
44 9 my @split_s = split /\s+/, $self->{text};
45  
46 # Now, locate keywords and "mark" the indices we want.
47 9 my @marks = (undef) x @split_s;
48 9 my $ok = 0;
49 9 for (0 .. $#split_s) {
50 72 my $word = lc $split_s[$_];
51 72 for my $subword (split /\W+/, $word) {
52 69 ▶ if (exists $keywords{$subword}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $keywords{$subword}) 23 46 69
53 23 $ok++;
54  
55 # Mark it and its $n neighbours.
56 $marks[$_] = $split_s[$_]
57 23 ▶ for grep { $_ >= 0 and $_ <= $#split_s }
Condition100%3/3
$_ >= 0 && $_ <= $#split_s
!ll&&!rl&&r
3781
MC/DC100%2/2
$_ >= 0 && $_ <= $#split_s
$_ >= 0 $_ <= $#split_s
Truth table100%3/3
A && B
A= $_ >= 0B= $_ <= $#split_s
ABresult
0X0
100
111
58 $_ - $self->{n} .. $_ + $self->{n};
59 23 last;
60 }
61 }
62 }
63  
64 9 ▶ return $ok ? @marks : ();
Branch: 1 branch
BranchTrueFalseTotal
$ok ? : 8 1 9
65 }
66  
67 9 ▶sub as_list {
Subroutine as_list: called CC=6 SCAR=17.9
Pod documented
68 9 my $self = shift;
69 9 my @sparse = $self->as_sparse_list(@_);
70 9 ▶ return () unless @sparse;
Branch: 1 branch
BranchTrueFalseTotal
unless @sparse 1 8 9
71 8 my @ret;
72 8 for (0 .. $#sparse) {
73 69 ▶ if (defined $sparse[$_]) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $sparse[$_]) { } 46 23 69
74 46 push @ret, $sparse[$_];
75 } else {
76 23 ▶ push @ret, "..." unless @ret and $ret[-1] eq "...";
Branch: 1 branch
BranchTrueFalseTotal
unless @ret and $ret[-1] eq "..." 13 10 23
Condition100%3/3
@ret and $ret[-1] eq "..."
!ll&&!rl&&r
7610
MC/DC100%2/2
@ret and $ret[-1] eq "..."
@ret $ret[-1] eq "..."
Truth table100%3/3
A and B
A= @retB= $ret[-1] eq "..."
ABresult
0X0
100
111
77 }
78 }
79 8 return @ret;
80 }
81  
82 9 ▶sub as_string {
Subroutine as_string: called CC=1 SCAR=0.0
Pod documented
83 9 my $self = shift;
84 9 return join " ", $self->as_list(@_);
85 }
86  
87 2 1;
88 __END__
↑ Summary