| 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
Condition100%2/3
ref $class || $class
MC/DC100%0/2
ref $class || $class
ref $class
$class
Truth table100%2/3
A || B
A= ref $classB= $class
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 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
Condition100%3/3
$width and $width =~ /^[1-9][0-9]*$/x
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
| |||||||||||||||||||||||||||||||||
| 47 | 157 | ▶ | $word ||= ''; | ||||||||||||||||||||||||||||||
|
Condition100%2/2
$word ||= ""
MC/DC100%1/1
$word ||= ""
$word
Truth table100%2/2
A
A= $word
| |||||||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||||||
| 49 | 157 | ▶ | return ($word) unless ($width <= length($word)); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 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
Condition100%2/2
scalar @$tokens1 or scalar @$tokens2
MC/DC100%1/1
scalar @$tokens1 or scalar @$tokens2
scalar @$tokens1
Truth table100%2/2
A
A= scalar @$tokens1
| |||||||||||||||||||||||||||||||||
| 58 | 100 | ▶ | return 0 unless (scalar @$tokens1 && scalar @$tokens2 ); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%2/2
scalar @$tokens1 and scalar @$tokens2
MC/DC100%1/1
scalar @$tokens1 and scalar @$tokens2
scalar @$tokens1
Truth table100%2/2
A
A= scalar @$tokens1
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 77 | 92 | ▶ | $intersection += ( $bag2{$_} < $bag1{$_}) ? $bag2{$_} : $bag1{$_}; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 89 | } | ||||||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||||||
| 91 | 6 | 1; | |||||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||||||
| 93 | __END__ | ||||||||||||||||||||||||||||||||