Algorithm-SlopeOne
view release on metacpan or search on metacpan
freqs
Ratings count matrix.
METHODS
clear
Reset the instance.
add($userprefs)
Update matrices with user preference data, accepts a HashRef or an
ArrayRef of HashRefs:
$s->predict({ StarWars => 5, LOTR => 5, StarTrek => 3, Prometheus => 1 });
$s->predict({ StarWars => 3, StarTrek => 5, Prometheus => 4 });
$s->predict([
{ IronMan => 4, Avengers => 5, XMen => 3 },
{ XMen => 5, DarkKnight => 5, SpiderMan => 3 },
]);
predict($userprefs)
Recommend new items given known item ratings.
$s->predict({ StarWars => 5, LOTR => 5, Prometheus => 1 });
TODO
Implement Non-Weighted and Bi-Polar Slope One schemes.
REFERENCES
lib/Algorithm/SlopeOne.pm view on Meta::CPAN
for (qw(diffs freqs)) {
delete $self->{$_};
$self->{$_} = {};
}
return $self;
}
sub add {
my ($self, $userprefs) = @_;
my $type = ref $userprefs;
if ($type eq q(HASH)) {
$userprefs = [ $userprefs ];
} elsif ($type eq q(ARRAY)) {
} else {
confess q(Expects a HashRef or an ArrayRef of HashRefs);
}
for my $ratings (@{$userprefs}) {
for my $item1 (keys %{$ratings}) {
for my $item2 (keys %{$ratings}) {
$self->freqs->{$item1}{$item2} ++;
$self->diffs->{$item1}{$item2} += $ratings->{$item1} - $ratings->{$item2};
}
}
}
return $self;
}
sub predict {
my ($self, $userprefs) = @_;
confess q(Expects a HashRef)
unless q(HASH) eq ref $userprefs;
my (%preds, %freqs);
while (my ($item, $rating) = each %{$userprefs}) {
while (my ($diffitem, $diffratings) = each %{$self->diffs}) {
my $freq = $self->freqs->{$diffitem}{$item};
next unless defined $freq;
$preds{$diffitem} += $diffratings->{$item} + ($freq * $rating);
$freqs{$diffitem} += $freq;
}
}
return {
map { $_ => $preds{$_} / $freqs{$_} }
grep { not exists $userprefs->{$_} }
keys %preds
};
}
1;
__END__
=pod
lib/Algorithm/SlopeOne.pm view on Meta::CPAN
=head2 freqs
Ratings count matrix.
=head1 METHODS
=head2 clear
Reset the instance.
=head2 add($userprefs)
Update matrices with user preference data, accepts a HashRef or an ArrayRef of HashRefs:
$s->predict({ StarWars => 5, LOTR => 5, StarTrek => 3, Prometheus => 1 });
$s->predict({ StarWars => 3, StarTrek => 5, Prometheus => 4 });
$s->predict([
{ IronMan => 4, Avengers => 5, XMen => 3 },
{ XMen => 5, DarkKnight => 5, SpiderMan => 3 },
]);
=head2 predict($userprefs)
Recommend new items given known item ratings.
$s->predict({ StarWars => 5, LOTR => 5, Prometheus => 1 });
=for Pod::Coverage new
=head1 TODO
Implement I<Non-Weighted> and I<Bi-Polar Slope One> schemes.
( run in 1.179 second using v1.01-cache-2.11-cpan-8f98c5d2c55 )