| 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
| |||||||||||||||
| 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
| |||||||||||||||
| 74 | unless q(HASH) eq ref $userprefs; | ||||||||||||||
| 75 | |||||||||||||||
| 76 | 4 | my (%preds, %freqs); | |||||||||||||
| 77 | 4 | ▶ | while (my ($item, $rating) = each %{$userprefs}) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 78 | 5 | ▶ | while (my ($diffitem, $diffratings) = each %{$self->diffs}) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 79 | 19 | my $freq = $self->freqs->{$diffitem}{$item}; | |||||||||||||
| 80 | 19 | ▶ | next unless defined $freq; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 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__ | ||||||||||||||