forked from MadhuRam93/Song-Recommendation-System
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRecommend.java
More file actions
239 lines (203 loc) · 7.77 KB
/
Recommend.java
File metadata and controls
239 lines (203 loc) · 7.77 KB
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
package org.myorg;
import java.io.IOException;
import java.io.*;
import java.util.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Enumeration;
import java.util.Vector;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.HashSet;
import java.util.Set;
import java.util.Map;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Map.Entry;
import java.io.FileNotFoundException;
import org.apache.commons.lang.StringUtils;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.conf.Configured;
import org.apache.hadoop.util.Tool;
import org.apache.hadoop.util.ToolRunner;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.io.DoubleWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
public class Recommend extends Configured implements Tool {
public static void main( String[] args) throws Exception {
int res = ToolRunner .run( new Recommend(), args);
System .exit(res);
}
/*run starts*/
public int run( String[] args) throws Exception {
Configuration conf = new Configuration();
Path output_path = new Path(args[2]);
conf.set("Sim_Path", args[1]);
FileSystem hdfs = FileSystem.get(conf);
try {
if(hdfs.exists(output_path)){
hdfs.delete(output_path, true);
}
}
catch (IOException e) {
e.printStackTrace();
}
Job job = Job .getInstance(conf, "Recommend");
job.setJarByClass(Recommend.class);
FileInputFormat.addInputPaths(job, args[0]);
FileOutputFormat.setOutputPath(job, output_path);
job.setMapperClass( Map_Recom .class);
job.setReducerClass( Reduce_Recom .class);
job.setOutputKeyClass( Text .class);
job.setOutputValueClass( Text .class);
int success = job.waitForCompletion( true) ? 0 : 1;
return 0;
}/*run ends */
//Input: test_0.txt
//Output: <UserID SongID,Rating>
public static class Map_Recom extends Mapper<LongWritable , Text , Text , Text > {
public void map( LongWritable offset, Text lineText, Context context)
throws IOException, InterruptedException {
String line = lineText.toString();
String[] parts = StringUtils.split(line);
if(parts.length == 3){
String userID = parts[0];
String songID = parts[1];
String rating = parts[2];
context.write(new Text(userID), new Text(songID + "," + rating));
}
}
}
//Input: < UserID <Listof SongID,Rating>>
//Output: < UserID <List of Recommendations >
public static class Reduce_Recom extends Reducer<Text, Text, Text, Text> {
/*HashMap to store the similarity list and value for songs*/
private Map<Integer, String> ISMap = new HashMap<Integer, String>();/* itemId1 <itemId2 rating>*/
@Override
public void reduce(Text key, Iterable<Text> values, Context context) throws IOException, InterruptedException {
Map<Integer,Integer> UserList = new HashMap<Integer,Integer>();//list of songs user has listened to from train data
Map<Integer,Float> recom = new HashMap<Integer,Float>();
for (Text value : values){
int i = Integer.parseInt(value.toString().split(",")[0].trim());
int r = Integer.parseInt(value.toString().split(",")[1].trim());
UserList.put(i,r);
}
/*For each Song from the list of songs User has liked, add the list of similar songs to recommendation */
for (int itemId : UserList.keySet()) {
int user_song = itemId;
boolean flag = false;
String sim_list = "";
if( ISMap.containsKey(user_song)){
sim_list = ISMap.get(user_song);
flag = true;
}
if(flag){
String[] a = sim_list.split(",");
/*sort the similarity list*/
Arrays.sort(a, new Comparator<String>() {
public int compare(String str1, String str2) {
String substr1 = str1.substring(str1.lastIndexOf("=")+1);
String substr2 = str2.substring(str2.lastIndexOf("=")+1);
return Double.valueOf(substr2).compareTo(Double.valueOf(substr1));}});
for(int i=0; i< a.length ; i++)
{
int song = Integer.parseInt(a[i].split("=")[0]);
float sim = Float.parseFloat(a[i].split("=")[1]);
float p = predictRating(song, UserList);//Predicting the rating for similar song
if( recom.containsKey(song))
{
if( p >= recom.get(song))
recom.put(song,p);
else
recom.put(song,recom.get(song));
}
}
}
recom.remove(user_song);//Remove the songs user has already listed/rated from the recommendation list.
}
Set<Entry<Integer, Float>> set = recom.entrySet();
List<Entry<Integer, Float>> list = new ArrayList<Entry<Integer, Float>>(set);
Collections.sort( list, new Comparator<Map.Entry<Integer, Float>>()
{
public int compare( Map.Entry<Integer, Float> o1, Map.Entry<Integer, Float> o2 )
{
return (o2.getValue()).compareTo( o1.getValue() );
}} );
String recommendations = "";
int limit = 0;
for(Map.Entry<Integer, Float> entry:list){
limit++;
recommendations += Integer.toString(entry.getKey()) + ",";
if( limit > 10)
break;
}
recommendations = recommendations.substring(0, recommendations.length()-1);
context.write(key, new Text(recommendations));
}/*reduce ends*/
/*setup(org.apache.hadoop.mapreduce.Mapper.Context context) Called once at the beginning of the task.*/
@Override
protected void setup(Context context) throws IOException,InterruptedException {
Configuration confR = context.getConfiguration();
String simFileName = confR.get("Sim_Path");
loadSimilarityData(simFileName);
}
/* predict the rating using weighted average */
private float predictRating(int itemId, Map<Integer,Integer> userlist){
float sumSR = 0, sumS = 0;
for (Integer item : userlist.keySet()) {
//System.out.println("ratings >> " +sr);
float similarity=0;
int smallerId = item;
int largerId = itemId;
int rating = userlist.get(item);
System.out.println("id and rating >> " + item+ " , " + rating);
if( itemId < smallerId){
int t = largerId;
largerId = smallerId;
smallerId = t;
}
if (ISMap.containsKey(smallerId)){
String[] t = ISMap.get(smallerId).split(",");
Map<Integer, Float> smap = new HashMap<Integer, Float>();
for( String str:t){
int i = Integer.parseInt(str.split("=")[0]);
float f = Float.parseFloat(str.split("=")[1]);
smap.put(i,f);
}
if(smap.containsKey(largerId))
similarity = smap.get(largerId);
else
similarity = 0; // handle the case where item1 and item2 are not similar
}
sumSR += similarity * rating;
sumS += similarity;
}
float pr = 0;
if(sumS > 0)
pr = sumSR/sumS;
return pr;
}
/* Load data from part-r-00000 into Item-Similarity Map : ISMap */
private void loadSimilarityData( String simFileName) throws FileNotFoundException, IOException {
Path pt=new Path(simFileName);
FileSystem fs = FileSystem.get(new Configuration());
BufferedReader br=new BufferedReader(new InputStreamReader(fs.open(pt)));
String line = null;
while ((line = br.readLine()) != null) {
String[] tokens = line.split("\t");
int item1 = Integer.parseInt(tokens[0]);
ISMap.put(item1, tokens[1]);
}
br.close();
}
}
}