line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
#!/usr/bin/perl |
2
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
package Math::Preference::SVD; |
4
|
|
|
|
|
|
|
|
5
|
1
|
|
|
1
|
|
28683
|
use strict; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
34
|
|
6
|
1
|
|
|
1
|
|
4
|
use warnings; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
23
|
|
7
|
|
|
|
|
|
|
|
8
|
1
|
|
|
1
|
|
942
|
no AutoLoader; |
|
1
|
|
|
|
|
1807
|
|
|
1
|
|
|
|
|
5
|
|
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
our $VERSION = '0.01'; |
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
require XSLoader; |
13
|
|
|
|
|
|
|
XSLoader::load('Math::Preference::SVD', $VERSION); |
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
sub new { |
16
|
0
|
|
|
0
|
0
|
|
my $class = shift; |
17
|
0
|
|
|
|
|
|
bless { @_ }, $class; |
18
|
|
|
|
|
|
|
} |
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
# sub set_rating { |
21
|
|
|
|
|
|
|
# # doing just one would be tricky... have to figure out if the movie has already been seen and add it if not |
22
|
|
|
|
|
|
|
# # customers add themselves as necessary I think |
23
|
|
|
|
|
|
|
# my $self = shift; |
24
|
|
|
|
|
|
|
# } |
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
sub set_ratings { |
27
|
0
|
|
|
0
|
0
|
|
my $self = shift; |
28
|
0
|
|
|
|
|
|
my $ratings = shift; # array of arrays, each of those having customer_id, movie_id (or, in general, id of the thing rated), rating |
29
|
0
|
|
|
|
|
|
my @ratings = @{ $ratings }; |
|
0
|
|
|
|
|
|
|
30
|
0
|
|
|
|
|
|
my $max_movie_id = 0; |
31
|
0
|
|
|
|
|
|
my $max_customer_id = 0; |
32
|
0
|
|
|
|
|
|
my $max_rating_id = 0; |
33
|
|
|
|
|
|
|
|
34
|
0
|
|
|
|
|
|
@ratings = sort { $a->[1] <=> $b->[1] } @ratings; # sort by movie |
|
0
|
|
|
|
|
|
|
35
|
0
|
|
|
|
|
|
$max_movie_id = $ratings[-1]->[1]; |
36
|
|
|
|
|
|
|
|
37
|
0
|
|
|
|
|
|
for my $rating ( @ratings ) { |
38
|
0
|
0
|
|
|
|
|
die unless ref $rating eq 'ARRAY'; |
39
|
0
|
|
|
|
|
|
(my $custId, my $movieId, my $rating) = @{ $rating }; |
|
0
|
|
|
|
|
|
|
40
|
0
|
0
|
|
|
|
|
defined $custId or die; |
41
|
0
|
0
|
|
|
|
|
defined $movieId or die; |
42
|
0
|
0
|
|
|
|
|
defined $rating or die; |
43
|
0
|
0
|
|
|
|
|
$max_customer_id = $custId if $custId > $max_customer_id; |
44
|
0
|
|
|
|
|
|
$max_rating_id++; |
45
|
|
|
|
|
|
|
} |
46
|
0
|
|
|
|
|
|
warn "totals calced: customers: @{[ $max_customer_id + 1 ]} ratings: $max_rating_id movies: @{[ $max_movie_id + 1 ]}"; |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
47
|
0
|
|
|
|
|
|
Engine($max_customer_id+1, $max_rating_id, $max_movie_id+1); |
48
|
|
|
|
|
|
|
|
49
|
0
|
|
|
|
|
|
my $last_movie_id = -1; |
50
|
0
|
|
|
|
|
|
for my $rating ( @ratings ) { |
51
|
0
|
|
|
|
|
|
(my $custId, my $movieId, my $rating) = @{ $rating }; |
|
0
|
|
|
|
|
|
|
52
|
0
|
0
|
|
|
|
|
if( $last_movie_id != $movieId ) { |
53
|
0
|
|
|
|
|
|
set_Movies($movieId, 0, 0); # RatingCount = 0, RatingSum = 0 |
54
|
0
|
|
|
|
|
|
$last_movie_id = $movieId; |
55
|
|
|
|
|
|
|
} |
56
|
0
|
|
|
|
|
|
set_Ratings($movieId, $custId, $rating); |
57
|
|
|
|
|
|
|
} |
58
|
|
|
|
|
|
|
|
59
|
0
|
|
|
|
|
|
warn "calc metrics..."; |
60
|
|
|
|
|
|
|
|
61
|
0
|
|
|
|
|
|
CalcMetrics(); |
62
|
|
|
|
|
|
|
|
63
|
0
|
|
|
|
|
|
warn "calc features..."; |
64
|
|
|
|
|
|
|
|
65
|
0
|
|
|
|
|
|
CalcFeatures(); |
66
|
|
|
|
|
|
|
|
67
|
0
|
|
|
|
|
|
warn "processed"; |
68
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
} |
70
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
sub predict_rating { |
72
|
0
|
|
|
0
|
0
|
|
my $self = shift; |
73
|
0
|
|
|
|
|
|
my $movieId = shift; |
74
|
0
|
|
|
|
|
|
my $custId = shift; |
75
|
0
|
|
|
|
|
|
PredictRating($movieId, $custId); |
76
|
|
|
|
|
|
|
} |
77
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
1; |
79
|
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
__END__ |