-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmining.go
277 lines (198 loc) · 5.5 KB
/
mining.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
package main
import (
"fmt"
"math"
_ "github.com/mattn/go-sqlite3"
"github.com/emirpasic/gods/maps/hashmap"
)
type User struct {
Id int;
Books *hashmap.Map;
WantToRead *hashmap.Map;
kNNUser []User;
kNNDist []float64;
};
var users []User;
var averageRatingByBookId [10001]float64;
func setupUserRecommendData() {
toRead := make([][]int, 54000);
toReads, err := db.Query(`select book_id, user_id from to_read where 1`);
checkError(err);
for toReads.Next() {
var BookId int;
var UserId int;
err = toReads.Scan(&BookId, &UserId);
checkError(err);
toRead[UserId] = append(toRead[UserId], BookId);
}
toReads.Close()
userRows, err := db.Query(`SELECT DISTINCT user_id FROM ratings WHERE 1`);
checkError(err);
for userRows.Next() {
var user User;
user.Books = hashmap.New();
user.WantToRead = hashmap.New();
err = userRows.Scan(&user.Id);
checkError(err);
booksToRead := toRead[user.Id];
for _, BookId := range booksToRead {
user.WantToRead.Put(BookId, true);
user.Books.Put(BookId, true);
}
users = append(users, user);
}
fmt.Println("user count: ", len(users));
userRows.Close();
avgRows, err := db.Query(`SELECT book_id, average_rating FROM books WHERE 1`);
checkError(err);
for avgRows.Next() {
var BookId int;
var Rating float64;
err = avgRows.Scan(&BookId, &Rating);
checkError(err);
averageRatingByBookId[BookId] = Rating;
}
avgRows.Close();
}
func generateRecommendation(likedBookIds []int, numberOfBooks int) []int{
var recommendations []int; //return array
//Active User
user1 := User{-1, nil, nil, nil, nil};
user1.Id = -1;
user1.WantToRead = hashmap.New();
defer user1.WantToRead.Clear();
for _, BookId := range likedBookIds {
user1.WantToRead.Put(BookId, true);
}
fmt.Println("user likes: ", user1.WantToRead.Size());
N := 20;
for i := 0; i < N; i++ {
emptyUser := User{-1, nil, nil, nil, nil};
user1.kNNUser = append(user1.kNNUser, emptyUser);
user1.kNNDist = append(user1.kNNDist, -1);
}
user1BookCount := user1.WantToRead.Size();
if(user1BookCount == 0){
return recommendations;
}
for _, user2 := range users {
user2BookCount := user2.Books.Size();
intersectingBookCount := 0;
for _, bookId := range user1.WantToRead.Keys() {
_, wantsToRead := user2.Books.Get(bookId);
if(wantsToRead){
intersectingBookCount++;
}
}
user2ToReadCount := user2.WantToRead.Size();
user1ToReadCount := user1.WantToRead.Size();
intersectingToReadCount := 0;
for _, bookId := range user1.WantToRead.Keys() {
_, wantsToRead := user2.WantToRead.Get(bookId);
if(wantsToRead){
intersectingToReadCount++;
}
}
BooksJaccardIndex := float64(intersectingBookCount) / float64((user1BookCount + user2BookCount) - intersectingBookCount);
ToReadJaccardIndex := float64(intersectingToReadCount) / float64((user1ToReadCount + user2ToReadCount) - intersectingToReadCount);
similarity := ToReadJaccardIndex + BooksJaccardIndex;
if(math.IsNaN(similarity)){
fmt.Println("NaN");
continue;
}
for i := 0; i < N; i++ {
if(user1.kNNUser[i].Id == -1 || similarity > user1.kNNDist[i]){
user1.kNNUser[i] = user2;
user1.kNNDist[i] = similarity;
break;
}
}
}
var bestRatings []float64;
var bestRatingBooks []int;
var maxBestRatings = numberOfBooks;
for i:=0;i<maxBestRatings;i++{
bestRatings = append(bestRatings, -1);
bestRatingBooks = append(bestRatingBooks, -1);
}
var recBookLen int = 0;
var recBooks []int;
var recBookRatingSum []float64;
var recBookRatingCount []float64;
ratings := hashmap.New(); //use this for ratings as we pull them from each closest user
for i, user := range user1.kNNUser {
ratingsRow, err := db.Query(`select rating, book_id from ratings where user_id = ?`, user.Id);
checkError(err);
for ratingsRow.Next() {
var bookId int;
var rating float64;
err = ratingsRow.Scan(&rating, &bookId);
checkError(err);
ratings.Put(bookId, rating);
}
ratingsRow.Close();
for _, id := range user.Books.Keys() {
BookId := id.(int);
skipBook := false;
for _, userbook := range user1.WantToRead.Keys() {
if(BookId == userbook){
skipBook = true;
break;
}
}
if(skipBook){
break;
}
recIndex := -1;
for i:=0; i < recBookLen; i++ {
if(recBooks[i] == BookId){
recIndex = i;
break;
}
}
if(recIndex == -1){
//check it doesn't exist in users liked list
recBooks = append(recBooks, BookId);
recBookRatingSum = append(recBookRatingSum, 0);
recBookRatingCount = append(recBookRatingCount, 0);
recIndex = len(recBooks)-1;
}
dist := user1.kNNDist[i];
r, hasRating := ratings.Get(BookId);
if(!hasRating){
continue;
}
rating := r.(float64);
recBookRatingSum[recIndex] += rating * dist;
recBookRatingCount[recIndex] += dist;
}
ratings.Clear();
}
for a:=0; a < len(recBooks); a++ {
BookId := recBooks[a];
prediction := (recBookRatingSum[a] / recBookRatingCount[a]);
for i:=0; i < maxBestRatings; i++ {
if(bestRatings[i] == -1 || prediction > bestRatings[i]){
alreadyRecommended := false;
for j:=0;j<maxBestRatings;j++{
if(bestRatingBooks[j] == BookId){
alreadyRecommended = true;
break;
}
}
if(!alreadyRecommended){
bestRatings[i] = prediction;
bestRatingBooks[i] = BookId;
}
break;
}
}
}
for i, bookId := range bestRatingBooks {
if(bookId == -1 || bestRatings[i] == -1){
continue;
}
recommendations = append(recommendations, bookId);
}
return recommendations;
}