-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRecommendation.java
139 lines (131 loc) · 4.9 KB
/
Recommendation.java
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
import java.io.File;
import java.io.FileNotFoundException;
//import java.io.IOException;
import java.util.ArrayList;
//import java.util.Comparator;
import java.util.HashMap;
//import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
//import java.util.Set;
//import java.util.stream.Collectors;
import java.util.Collections;
public class Recommendation {
Map<Long, HashMap<String, Integer>> userData;
//Map<Long, HashMap<String, Integer>> allData;
public Recommendation(String filePath) {
userData = new HashMap<>();
//allData = new HashMap<>();
this.parseCsvFile(filePath);
}
private void parseCsvFile(String csvFilePath) {
try {
Scanner s = new Scanner(new File(csvFilePath));
String[] lst;
if (s.hasNextLine()) {
s.nextLine();
}
while (s.hasNextLine()) {
lst = s.nextLine().split(",");
long userID = Long.parseLong(lst[0]);
String artist = lst[1];
int minutes = Integer.parseInt(lst[3]);
HashMap<String, Integer> newData;
if (this.userData.containsKey(userID)) {
newData = this.userData.get(userID);
if (newData.containsKey(artist)) {
minutes += newData.get(artist);
}
} else {
newData = new HashMap<String, Integer>();
}
newData.put(artist, minutes);
this.userData.put(userID, newData);
//this.allData.put(userID, newData);
}
s.close();
for (long key : this.userData.keySet()) {
this.sortArtist(key);
}
} catch (FileNotFoundException e) {
/*exp*/
}
}
private void sortArtist(long key) {
HashMap<String, Integer> keySongs = this.userData.get(key);
ArrayList<Integer> minutes = new ArrayList<Integer>(keySongs.values());
//System.out.println(keySongs.keySet());
Collections.sort(minutes);
Collections.reverse(minutes);
HashMap<String, Integer> artists = new HashMap<String, Integer>();
for (Integer minute : minutes) {
for (String artist : keySongs.keySet()) {
if (keySongs.get(artist) == minute) {
//System.out.println(artist);
artists.put(artist, minute);
//i++;
}
if (artists.size() == 5) {
this.userData.put(key, artists);
/*
for (String art : artists.keySet()) {
System.out.println(art);
}
*/
return;
}
}
}
/*
for (String art : artists.keySet()) {
System.out.println(art);
}
*/
this.userData.put(key, artists);
}
public String[] recommendNewArtists(List<String> artistList) {
ArrayList<Long> users = new ArrayList<>(this.userData.keySet());
ArrayList<Double> similarity = new ArrayList<>();
for (long key : users) {
int cup = 0;
int cap = 0;
for (String artistA : artistList) {
for (String artistB : this.userData.get(key).keySet()) {
if (artistA.equals(artistB)) {
cap++;
}
}
}
cup = artistList.size() + this.userData.get(key).keySet().size() - cap;
//System.out.println(key + ":" + (cap + 0.0) / cup);
similarity.add((cap + 0.0) / cup);
}
int i = 0;
ArrayList<String> tops = new ArrayList<String>();
while (i < 3 && users.size() > 0) {
int idx = 0;
for (int j = 0; j < users.size(); j++) {
if (similarity.get(j) > similarity.get(idx)) {
idx = j;
}
}
//System.out.println(users.get(idx));
for (String artists : this.userData.get(users.get(idx)).keySet()) {
if (!tops.contains(artists) && !artistList.contains(artists)) {
//System.out.println(artists + " - added");
tops.add(artists);
}
}
users.remove(users.get(idx));
similarity.remove(similarity.get(idx));
i++;
}
String[] recommend = new String[tops.size()];
for (int idx = 0; idx < recommend.length; idx++) {
recommend[idx] = tops.get(idx);
//System.out.println(recommend[idx]);
}
return recommend;
}
}