Similarity.pm

filter: Statementstmt 100% 55 / 55 Branchbran 100% 24 / 24 Conditioncond 100% 11 / 12 (1 uncoverable) MC/DCmcdc 100% 5 / 7 (2 uncoverable) Subroutinesub 100% 13 / 13 Podpod 100% 8 / 8 totaltotal 100% 116 / 119 (3 uncoverable) CC 21 SCAR 13.6
CC
21
Cov
100%
intersection
6.0
ngrams
5.0
from_tokens
5.0
CRAP 3.9
↑ Summary Similarity/Cosine.pm »
1 package Bag::Similarity;
2  
3 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 6 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 6 ▶use 5.008_005;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 6 our $VERSION = '0.022';
8  
9 6 ▶use Carp 'croak';
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 6 ▶sub new {
Subroutine new: called CC=4 SCAR=13.9
Pod documented
12 6 my $class = shift;
13 # uncoverable condition false
14 6 ▶ bless @_ ? @_ > 1 ? {@_} : {%{$_[0]}} : {}, ref $class || $class;
Branch: 2 branches
BranchTrueFalseTotal
@_ ? : 3 3 6
@_ > 1 ? : 1 2 3
Condition100%2/3
ref $class || $class
l!l&&r!l&&!r
420
MC/DC100%0/2
ref $class || $class
ref $class $class
Truth table100%2/3
A || B
A= ref $classB= $class
ABresult
1X1
011
000
15 }
16  
17 102 ▶sub similarity {
Subroutine similarity: called CC=1 SCAR=0.0
Pod documented
18 102 my ($self, $any1, $any2, $width) = @_;
19  
20 102 return $self->from_tokens(
21 $self->_any($any1,$width),
22 $self->_any($any2,$width)
23 );
24 }
25  
26 218 ▶sub _any {
Subroutine _any: called CC=4 SCAR=13.9
27 218 my ($self, $any, $width) = @_;
28  
29 218 ▶ if (ref($any) eq 'ARRAY') {
Branch: 3 branches
BranchTrueFalseTotal
if (ref $any eq 'ARRAY') { } 58 160 218
elsif (ref $any eq 'HASH') { } 13 147 160
elsif (ref $any) { } 1 146 147
30 58 return $any;
31 }
32 elsif (ref($any) eq 'HASH') {
33 13 return [grep { $any->{$_} } keys %$any];
34 }
35 elsif (ref($any)) {
36 1 return [];
37 }
38 else {
39 146 return [$self->ngrams($any,$width)];
40 }
41 }
42  
43 157 ▶sub ngrams {
Subroutine ngrams: called CC=5 SCAR=16.1
Pod documented
44 157 my ($self, $word, $width) = @_;
45  
46 157 ▶ $width = 1 unless ($width && $width =~ m/^[1-9][0-9]*$/x);
Branch: 1 branch
BranchTrueFalseTotal
unless $width and $width =~ /^[1-9][0-9]*$/x 81 76 157
Condition100%3/3
$width and $width =~ /^[1-9][0-9]*$/x
!ll&&!rl&&r
79276
MC/DC100%2/2
$width and $width =~ /^[1-9][0-9]*$/x
$width $width =~ /^[1-9][0-9]*$/x
Truth table100%3/3
A and B
A= $widthB= $width =~ /^[1-9][0-9]*$/x
ABresult
0X0
100
111
47 157 ▶ $word ||= '';
Condition100%2/2
$word ||= ""
l!l
14017
MC/DC100%1/1
$word ||= ""
$word
Truth table100%2/2
A
A= $word
Aresult
11
00
48  
49 157 ▶ return ($word) unless ($width <= length($word));
Branch: 1 branch
BranchTrueFalseTotal
unless $width <= length $word 19 138 157
50  
51 138 return map {substr $word,$_,$width;} (0..length($word)-$width);
52 }
53  
54 106 ▶sub from_tokens {
Subroutine from_tokens: called CC=5 SCAR=16.1
Pod documented
55 106 my ($self, $tokens1, $tokens2) = @_;
56  
57 106 ▶ return 1 if (!(scalar @$tokens1 || scalar @$tokens2));
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @$tokens1 or scalar @$tokens2 6 100 106
Condition100%2/2
scalar @$tokens1 or scalar @$tokens2
l!l
9412
MC/DC100%1/1
scalar @$tokens1 or scalar @$tokens2
scalar @$tokens1
Truth table100%2/2
A
A= scalar @$tokens1
Aresult
11
00
58 100 ▶ return 0 unless (scalar @$tokens1 && scalar @$tokens2 );
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @$tokens1 and scalar @$tokens2 12 88 100
Condition100%2/2
scalar @$tokens1 and scalar @$tokens2
!ll
694
MC/DC100%1/1
scalar @$tokens1 and scalar @$tokens2
scalar @$tokens1
Truth table100%2/2
A
A= scalar @$tokens1
Aresult
00
11
59  
60 88 return $self->from_bags(
61 $tokens1,
62 $tokens2,
63 );
64 }
65  
66 1 ▶sub from_bags { croak 'Method "from_bags" not implemented in subclass' }
Subroutine from_bags: called CC=1 SCAR=0.0
Pod documented
67  
68 66 ▶sub intersection {
Subroutine intersection: called CC=6 SCAR=17.9
Pod documented
69 66 my ($self, $tokens1, $tokens2) = @_;
70 66 my %bag1;
71 my %bag2;
72 66 $bag1{$_}++ for @{$tokens1};
73 66 $bag2{$_}++ for @{$tokens2};
74 66 my $intersection = 0;
75 66 for (keys %bag1) {
76 196 ▶ next unless (exists $bag2{$_} );
Branch: 1 branch
BranchTrueFalseTotal
unless exists $bag2{$_} 104 92 196
77 92 ▶ $intersection += ( $bag2{$_} < $bag1{$_}) ? $bag2{$_} : $bag1{$_};
Branch: 1 branch
BranchTrueFalseTotal
$bag2{$_} < $bag1{$_} ? : 13 79 92
78 }
79 66 return $intersection;
80 }
81  
82 46 ▶sub combined_length {
Subroutine combined_length: called CC=1 SCAR=0.0
Pod documented
83 46 scalar(@{$_[1]}) + scalar(@{$_[2]});
84 }
85  
86 20 ▶sub min {
Subroutine min: called CC=2 SCAR=6.9
Pod documented
87 20 (scalar(@{$_[1]}) < scalar(@{$_[2]}))
88 20 ▶ ? scalar(@{$_[1]}) : scalar(@{$_[2]});
Branch: 1 branch
BranchTrueFalseTotal
scalar @{$_[1];} < scalar @{$_[2];} ? : 2 18 20
89 }
90  
91 6 1;
92  
93 __END__
↑ Summary Similarity/Cosine.pm »