Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 112 lines (98 sloc) 2.527 kb
b69da71 @ultraist co-occurrence
authored
1 use strict;
2 use warnings;
3 use Repo;
4 use User;
5 use Lang;
6 use Result;
7 use Utils;
8 $|=1;
9
8926b7a @ultraist bagging
authored
10 our @RECOMMENDER = (
2d1c1ed @ultraist ensemble
authored
11 {
12 file => "./results_forkbase.txt",
603a5e9 @ultraist param
authored
13 weight => 2.2,
3b0dd5f @ultraist param
authored
14 K => 30,
a024fb9 @ultraist end
authored
15 score => sub { 1.0 / (1.0 + $_[0]); }
2d1c1ed @ultraist ensemble
authored
16 },
17 {
18 file => "./results_co_occurrence.txt",
ec7900c @ultraist param
authored
19 weight => 1.45,
3b0dd5f @ultraist param
authored
20 K => 30,
0ef8f5f @ultraist param
authored
21 score => sub { 1.0 / (1.0 + $_[0]); }
2d1c1ed @ultraist ensemble
authored
22 },
0a41355 @ultraist param
authored
23 {
24 file => "./results_author.txt",
7130ad3 @ultraist refact
authored
25 weight => 0.60,
3b0dd5f @ultraist param
authored
26 K => 40,
27 score => sub { 1.0 / (1.5 + $_[0]); }
c0e890c @ultraist param
authored
28 },
29 {
30 file => "./results_name.txt",
66a2cee @ultraist param
authored
31 weight => 0.25,
3b0dd5f @ultraist param
authored
32 K => 30,
f7acb6e @ultraist param
authored
33 score => sub { 1.0 / (1.5 + $_[0]); }
34 },
20c0317 @ultraist param
authored
35 {
36 file => "./results_popular.txt",
3d215ad @ultraist name
authored
37 weight => 0.1,
448872b @ultraist param
authored
38 K => 10,
20c0317 @ultraist param
authored
39 score => sub { 1.0 / (1.0 + $_[0]); }
40 }
8926b7a @ultraist bagging
authored
41 );
42
43 sub load_recommender
44 {
45 my $recommender = [];
46
47 foreach my $rec (@RECOMMENDER) {
48 print "loading.. $rec->{file}\r";
c16117c @ultraist name
authored
49 my $result = new Result($rec->{file}, $rec->{K});
8926b7a @ultraist bagging
authored
50 my $weight = $rec->{weight};
2d1c1ed @ultraist ensemble
authored
51 push(@$recommender, { result => $result, weight => $rec->{weight}, K => $rec->{K}, score => $rec->{score} });
8926b7a @ultraist bagging
authored
52 }
53
54 return $recommender;
55 }
b69da71 @ultraist co-occurrence
authored
56
b854ce8 @ultraist refact
authored
57 ensemble_recommender:
b69da71 @ultraist co-occurrence
authored
58 {
63a4db8 @ultraist memo
authored
59 print "$0: loading ..\r";
b69da71 @ultraist co-occurrence
authored
60 my $repo = new Repo("./download/repos.txt");
61 my $lang = new Lang("./download/lang.txt", $repo);
62 my $user = new User("./download/data.txt", $lang);
63 my $test = new Result("./download/test.txt", $lang);
64 my $count = $test->count();
65 my $i = 0;
8926b7a @ultraist bagging
authored
66 my $recommender = load_recommender();
b69da71 @ultraist co-occurrence
authored
67
b854ce8 @ultraist refact
authored
68 open(R, ">results_ensemble.txt") or die $!;
b69da71 @ultraist co-occurrence
authored
69
70 $repo->set_lang($lang);
71 $repo->ranking($user);
72
8926b7a @ultraist bagging
authored
73 print "ok..\n";
74
b69da71 @ultraist co-occurrence
authored
75 foreach my $uid (@{$test->users()}) {
63a4db8 @ultraist memo
authored
76 printf("$0: %.2f%% \r", 100 * $i / $count);
b69da71 @ultraist co-occurrence
authored
77 my @result_tmp;
78 my @result;
8926b7a @ultraist bagging
authored
79 my %reco_repo;
b69da71 @ultraist co-occurrence
authored
80 my $user_repos = $user->repos($uid);
81
8926b7a @ultraist bagging
authored
82 foreach my $reco (@$recommender) {
83 my $repos = $reco->{result}->repos($uid);
2d1c1ed @ultraist ensemble
authored
84 if (!defined($repos)) {
85 next;
86 }
4e01878 @ultraist K
authored
87 for (my $i = 0; $i < $reco->{K} && $i < @$repos; ++$i) {
8926b7a @ultraist bagging
authored
88 if (!exists($reco_repo{$repos->[$i]})) {
f85877e @ultraist param
authored
89 $reco_repo{$repos->[$i]} = 0.0;
8926b7a @ultraist bagging
authored
90 }
f85877e @ultraist param
authored
91 $reco_repo{$repos->[$i]} += &{$reco->{score}}($i) * $reco->{weight};
b69da71 @ultraist co-occurrence
authored
92 }
93 }
8926b7a @ultraist bagging
authored
94 foreach my $rid (keys(%reco_repo)) {
95 push(@result_tmp, { id => $rid, score => $reco_repo{$rid} });
96 }
97
98 @result_tmp = sort { $b->{score} <=> $a->{score} } @result_tmp;
b69da71 @ultraist co-occurrence
authored
99 foreach my $rid (@result_tmp) {
100 if (!Utils::includes($user_repos, $rid->{id})) {
101 push(@result, $rid->{id});
102 push(@$user_repos, $rid->{id});
103 }
104 }
105 print R Result::format($uid, @result);
106 ++$i;
107 }
108 close(R);
109 }
110
1bfe153 @ultraist ranking
authored
111
Something went wrong with that request. Please try again.