blib/lib/Algorithm/SlopeOne.pm

filter: Statementstmt 100% 53 / 53 Branchbran 100% 12 / 12 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 10 / 10 Podpod 83.3% 5 / 6 totaltotal 98.7% 80 / 81 CC 11 SCAR 12.8
CC
11
Cov
100%
add
6.0
predict
5.0
clear
2.0
CRAP 3.6
↑ Summary
1 package Algorithm::SlopeOne;
2 # ABSTRACT: Slope One collaborative filtering for rated resources
3  
4  
5 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 4 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 4 ▶use warnings qw(all);
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 4 ▶use Carp qw(confess);
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 4 our $VERSION = '0.004'; # VERSION
12  
13  
14 2 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod undocumented
15 2 my ($class) = @_;
16 2 return bless {
17 diffs => {},
18 freqs => {},
19 } => $class;
20 }
21  
22  
23 117 ▶sub diffs {
Subroutine diffs: called CC=1 SCAR=0.0
Pod documented
24 117 my ($self) = @_;
25 117 return $self->{diffs};
26 }
27  
28 112 ▶sub freqs {
Subroutine freqs: called CC=1 SCAR=0.0
Pod documented
29 112 my ($self) = @_;
30 112 return $self->{freqs};
31 }
32  
33  
34 1 ▶sub clear {
Subroutine clear: called CC=2 SCAR=6.9
Pod documented
35 1 my ($self) = @_;
36  
37 1 for (qw(diffs freqs)) {
38 2 delete $self->{$_};
39 2 $self->{$_} = {};
40 }
41  
42 1 return $self;
43 }
44  
45  
46 5 ▶sub add {
Subroutine add: called CC=6 SCAR=17.9
Pod documented
47 5 my ($self, $userprefs) = @_;
48  
49 5 my $type = ref $userprefs;
50 5 ▶ if ($type eq q(HASH)) {
Branch: 2 branches
BranchTrueFalseTotal
if ($type eq 'HASH') { } 2 3 5
elsif ($type eq 'ARRAY') { } 2 1 3
51 2 $userprefs = [ $userprefs ];
52 } elsif ($type eq q(ARRAY)) {
53 } else {
54 1 confess q(Expects a HashRef or an ArrayRef of HashRefs);
55 }
56  
57 4 for my $ratings (@{$userprefs}) {
58 8 for my $item1 (keys %{$ratings}) {
59 27 for my $item2 (keys %{$ratings}) {
60 93 $self->freqs->{$item1}{$item2} ++;
61 93 $self->diffs->{$item1}{$item2} += $ratings->{$item1} - $ratings->{$item2};
62 }
63 }
64 }
65  
66 4 return $self;
67 }
68  
69  
70 5 ▶sub predict {
Subroutine predict: called CC=5 SCAR=16.1
Pod documented
71 5 my ($self, $userprefs) = @_;
72  
73 5 ▶ confess q(Expects a HashRef)
Branch: 1 branch
BranchTrueFalseTotal
unless "HASH" eq ref $userprefs 1 4 5
74 unless q(HASH) eq ref $userprefs;
75  
76 4 my (%preds, %freqs);
77 4 ▶ while (my ($item, $rating) = each %{$userprefs}) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$userprefs;}) 5 4 9
78 5 ▶ while (my ($diffitem, $diffratings) = each %{$self->diffs}) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$self->diffs;}) 19 5 24
79 19 my $freq = $self->freqs->{$diffitem}{$item};
80 19 ▶ next unless defined $freq;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $freq 6 13 19
81 13 $preds{$diffitem} += $diffratings->{$item} + ($freq * $rating);
82 13 $freqs{$diffitem} += $freq;
83 }
84 }
85  
86 return {
87 6 map { $_ => $preds{$_} / $freqs{$_} }
88 4 grep { not exists $userprefs->{$_} }
89 keys %preds
90 };
91 }
92  
93  
94 4 1;
95  
96 __END__
↑ Summary