Skip to content

Commit 56cbf60

Browse files
committed
added collaborative filtering methods for music recommendation'
1 parent 45535e8 commit 56cbf60

12 files changed

Lines changed: 589 additions & 70 deletions

src/common/Utilities.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ License, or (at your option) any later version.
4646
public class Utilities {
4747

4848
public static int REC_LIMIT = 20;
49+
public static boolean FILTER_OWN = false;
4950

5051
private final static String REV_START = "<rev xml:space=\"preserve\">";
5152
private final static String REV_END = "</rev>";
@@ -334,6 +335,27 @@ public static List<Set<Integer>> getUserResourceLists(List<Bookmark> userLines)
334335
}
335336
return userLists;
336337
}
338+
339+
public static List<Map<Integer, Integer>> getUserResourceMaps(List<Bookmark> userLines) {
340+
List<Map<Integer, Integer>> userLists = new ArrayList<Map<Integer, Integer>>();
341+
for (Bookmark data : userLines) {
342+
int userID = data.getUserID();
343+
Map<Integer, Integer> resList = null;
344+
if (userID >= userLists.size()) {
345+
resList = new LinkedHashMap<Integer, Integer>();
346+
userLists.add(resList);
347+
} else {
348+
resList = userLists.get(userID);
349+
}
350+
Integer resVal = resList.get(data.getResourceID());
351+
if (resVal == null) {
352+
resList.put(data.getResourceID(), 1);
353+
} else {
354+
resList.put(data.getResourceID(), resVal + 1);
355+
}
356+
}
357+
return userLists;
358+
}
337359

338360
public static List<Map<Integer, Double>> getRelativeTagMaps(List<Bookmark> userLines, boolean resource) {
339361
List<Map<Integer, Integer>> maps = (resource ? getResMaps(userLines) : getUserMaps(userLines));

src/engine/BaseLevelLearningEngine.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,8 @@ public void loadFile(String path, String filename) throws Exception {
6161
Map<String, Map<Integer, Double>> resMaps = new HashMap<>();
6262
Map<String, Map<Integer, Double>> resCounts = new HashMap<>();
6363

64-
List<Map<Integer, Double>> userRecencies = BLLCalculator.getArtifactMaps(reader, reader.getBookmarks(), null, false, new ArrayList<Long>(), new ArrayList<Double>(), 0.5, true, null);
64+
List<Map<Integer, Double>> userRecencies = BLLCalculator.getArtifactMaps(reader, reader.getBookmarks(), null, false,
65+
new ArrayList<Long>(), new ArrayList<Double>(), 0.5, true, null, true);
6566
List<Map<Integer, Double>> userFrequencies = Utilities.getRelativeTagMaps(reader.getBookmarks(), false);
6667
int i = 0;
6768
for (Map<Integer, Double> map : userRecencies) {
@@ -71,7 +72,8 @@ public void loadFile(String path, String filename) throws Exception {
7172
for (Map<Integer, Double> map : userFrequencies) {
7273
userCounts.put(reader.getUsers().get(i++), map);
7374
}
74-
List<Map<Integer, Double>> resRecencies = BLLCalculator.getArtifactMaps(reader, reader.getBookmarks(), null, true, new ArrayList<Long>(), new ArrayList<Double>(), 0.0, true, null);
75+
List<Map<Integer, Double>> resRecencies = BLLCalculator.getArtifactMaps(reader, reader.getBookmarks(), null, true, new ArrayList<Long>(),
76+
new ArrayList<Double>(), 0.0, true, null, true);
7577
List<Map<Integer, Double>> resFrequencies = Utilities.getRelativeTagMaps(reader.getBookmarks(), true);
7678
i = 0;
7779
for (Map<Integer, Double> map : resRecencies) {

src/itemrecommendations/CIRTTCalculator.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ public CIRTTCalculator(BookmarkReader reader, int trainSize, int sampleSize, Sim
9696
this.userTopics = Utilities.getUniqueTopicMaps(this.trainList, false);//Utilities.getRelativeTopicMaps(this.trainList, false);
9797
this.allUsers = Utilities.getAllEntities(this.trainList, false);
9898
if (this.bll) {
99-
this.bllValues = BLLCalculator.getArtifactMaps(reader, this.trainList, this.testList, false, new ArrayList<Long>(), new ArrayList<Double>(), 0.5, true, null);
99+
this.bllValues = BLLCalculator.getArtifactMaps(reader, this.trainList, this.testList, false, new ArrayList<Long>(), new ArrayList<Double>(), 0.5, true, null, true);
100100
}
101101

102102
/*if (this.features == Features.ENTITIES) {

src/processing/BLLCalculator.java

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ public BLLCalculator(BookmarkReader reader, int trainSize, double dVal, int beta
7979
this.userDenoms = new ArrayList<Double>();
8080
this.userTimestamps = new ArrayList<Long>();
8181
//if (this.userBased) {
82-
this.userMaps = getArtifactMaps(reader, this.trainList, testList, false, this.userTimestamps, this.userDenoms, this.dVal, true, lambda);
82+
this.userMaps = getArtifactMaps(reader, this.trainList, testList, false, this.userTimestamps, this.userDenoms, this.dVal, true, lambda, true);
8383
this.userCounts = Utilities.getRelativeTagMaps(this.trainList, false);
8484
this.resCounts = Utilities.getRelativeTagMaps(this.trainList, true);
8585
if (cType != CalculationType.NONE) {
@@ -89,7 +89,7 @@ public BLLCalculator(BookmarkReader reader, int trainSize, double dVal, int beta
8989
this.resDenoms = new ArrayList<Double>();
9090
this.resTimestamps = new ArrayList<Long>();
9191
//if (this.resBased) {
92-
this.resMaps = getArtifactMaps(reader, this.trainList, testList, true, this.resTimestamps, this.resDenoms, this.dVal, true, null);
92+
this.resMaps = getArtifactMaps(reader, this.trainList, testList, true, this.resTimestamps, this.resDenoms, this.dVal, true, null, true);
9393
//}
9494
}
9595

@@ -226,7 +226,7 @@ public Map<Integer, Double> getRankedTagList(int userID, int resID, boolean sort
226226

227227
// Basis activations values for each user
228228
public static List<Map<Integer, Double>> getArtifactMaps(BookmarkReader reader, List<Bookmark> userLines, List<Bookmark> testLines, boolean resource,
229-
List<Long> timestampList, List<Double> denomList, double dVal, boolean normalize, Double lambda) {
229+
List<Long> timestampList, List<Double> denomList, double dVal, boolean normalize, Double lambda, boolean calcOnTags) {
230230

231231
List<Map<Integer, Double>> maps = new ArrayList<Map<Integer, Double>>();
232232
for (Bookmark data : userLines) {
@@ -247,14 +247,14 @@ public static List<Map<Integer, Double>> getArtifactMaps(BookmarkReader reader,
247247
}
248248
timestampList.add(baselineTimestamp);
249249
if (baselineTimestamp != -1) {
250-
maps.add(addActValue(data, new LinkedHashMap<Integer, Double>(), baselineTimestamp, resource, dVal, lambda));
250+
maps.add(addActValue(data, new LinkedHashMap<Integer, Double>(), baselineTimestamp, resource, dVal, lambda, calcOnTags));
251251
} else {
252252
maps.add(null);
253253
}
254254
} else {
255255
baselineTimestamp = timestampList.get(refID);
256256
if (baselineTimestamp != -1) {
257-
addActValue(data, maps.get(refID), baselineTimestamp, resource, dVal, lambda);
257+
addActValue(data, maps.get(refID), baselineTimestamp, resource, dVal, lambda, calcOnTags);
258258
}
259259
}
260260
}
@@ -288,7 +288,7 @@ public static List<Map<Integer, Double>> getArtifactMaps(BookmarkReader reader,
288288
public static Map<Integer, Double> getSortedArtifactMapForUser(int userID, BookmarkReader reader, List<Bookmark> userLines, List<Bookmark> testLines, boolean resource,
289289
List<Long> timestampList, List<Double> denomList, double dVal, boolean normalize) {
290290

291-
List<Map<Integer, Double>> artifactMaps = getArtifactMaps(reader, userLines, testLines, resource, timestampList, denomList, dVal, normalize, null);
291+
List<Map<Integer, Double>> artifactMaps = getArtifactMaps(reader, userLines, testLines, resource, timestampList, denomList, dVal, normalize, null, true);
292292
if (artifactMaps != null && userID < artifactMaps.size()) {
293293
Map<Integer, Double> sortedResultMap = new TreeMap<Integer, Double>(new DoubleMapComparator(artifactMaps.get(userID)));
294294
sortedResultMap.putAll(artifactMaps.get(userID));
@@ -301,7 +301,7 @@ public static Map<Integer, Double> getCollectiveArtifactMap(BookmarkReader reade
301301
List<Long> timestampList, List<Double> denomList, double dVal, boolean normalize) {
302302

303303
Map<Integer, Double> collectiveArtifactMap = new LinkedHashMap<Integer, Double>();
304-
List<Map<Integer, Double>> artifactMaps = getArtifactMaps(reader, userLines, testLines, resource, timestampList, denomList, dVal, normalize, null);
304+
List<Map<Integer, Double>> artifactMaps = getArtifactMaps(reader, userLines, testLines, resource, timestampList, denomList, dVal, normalize, null, true);
305305
for (Map<Integer, Double> map : artifactMaps) {
306306
for (Map.Entry<Integer, Double> entry : map.entrySet()) {
307307
Double val = collectiveArtifactMap.get(entry.getKey());
@@ -314,7 +314,8 @@ public static Map<Integer, Double> getCollectiveArtifactMap(BookmarkReader reade
314314
return sortedResultMap;
315315
}
316316

317-
private static Map<Integer, Double> addActValue(Bookmark data, Map<Integer, Double> actValues, long baselineTimestamp, boolean resource, double dVal, Double lambda) {
317+
private static Map<Integer, Double> addActValue(Bookmark data, Map<Integer, Double> actValues, long baselineTimestamp,
318+
boolean resource, double dVal, Double lambda, boolean calcOnTags) {
318319
if (!data.getTimestamp().isEmpty()) {
319320
Double newAct = 0.0;
320321
if (resource) {
@@ -331,7 +332,8 @@ private static Map<Integer, Double> addActValue(Bookmark data, Map<Integer, Doub
331332
}
332333
//}
333334
}
334-
for (Integer value : data.getTags()) {
335+
List<Integer> entities = calcOnTags ? data.getTags() : data.getCategories();
336+
for (Integer value : entities) {
335337
Double oldAct = actValues.get(value);
336338
if (!newAct.isInfinite() && !newAct.isNaN()) {
337339
actValues.put(value, (oldAct != null ? oldAct + newAct : newAct));

src/processing/GIRPTMCalculator.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ public Map<Integer, Double> getRankedTagList(int userID, int resID) {
6868
sortedResultMap.putAll(resultMap);
6969
int count = 0;
7070
for (Map.Entry<Integer, Double> entry : sortedResultMap.entrySet()) {
71-
if (count++ < 10) {
71+
if (count++ < Utilities.REC_LIMIT) {
7272
returnMap.put(entry.getKey(), entry.getValue());
7373
} else {
7474
break;

src/processing/MPCalculator.java

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ License, or (at your option) any later version.
3535
import common.MemoryThread;
3636
import common.PerformanceMeasurement;
3737
import common.Utilities;
38+
import engine.EngineUtils;
3839
import file.PredictionFileWriter;
3940
import file.BookmarkReader;
4041

@@ -96,6 +97,31 @@ private static List<int[]> getPopularTags(BookmarkReader reader, int sampleSize,
9697
return tags;
9798
}
9899

100+
private static List<int[]> getPopularTagsFiltered(BookmarkReader reader, int trainSize, int sampleSize, int limit) {
101+
System.out.println("MP: Filter own entities");
102+
103+
List<int[]> tags = new ArrayList<int[]>();
104+
int[] tagIDs = getPopularTagList(reader, reader.getTags().size());
105+
List<Map<Integer, Integer>> userMaps = Utilities.getUserMaps(reader.getBookmarks().subList(0, trainSize));
106+
107+
for (int j = trainSize; j < trainSize + sampleSize; j++) {
108+
Map<Integer, Integer> filterTags = userMaps.get(reader.getBookmarks().get(j).getUserID());
109+
List<Integer> returnTags = new ArrayList<Integer>();
110+
for (int popTag: tagIDs) {
111+
if (returnTags.size() < limit) {
112+
if (!filterTags.containsKey(popTag)) {
113+
returnTags.add(popTag);
114+
}
115+
} else {
116+
break;
117+
}
118+
}
119+
tags.add(Ints.toArray(returnTags));
120+
}
121+
122+
return tags;
123+
}
124+
99125
// public statics --------------------------------------------------------------------------------------------
100126
public static BookmarkReader predictPopularTags(String filename, int trainSize, int sampleSize, boolean mp) {
101127
Timer timerThread = new Timer();
@@ -107,7 +133,11 @@ public static BookmarkReader predictPopularTags(String filename, int trainSize,
107133

108134
List<int[]> values = null;
109135
if (mp) {
110-
values = getPopularTags(reader, sampleSize, Utilities.REC_LIMIT);
136+
if (!Utilities.FILTER_OWN) {
137+
values = getPopularTags(reader, sampleSize, Utilities.REC_LIMIT);
138+
} else {
139+
values = getPopularTagsFiltered(reader, trainSize, sampleSize, Utilities.REC_LIMIT);
140+
}
111141
} else {
112142
values = getPerfectTags(reader, sampleSize, Utilities.REC_LIMIT);
113143
}

src/processing/ThreeLTCalculator.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public ThreeLTCalculator(BookmarkReader reader, int trainSize, int dValue, int b
5454
this.bookmarkBLL = bookmarkBLL;
5555
this.cType = cType;
5656

57-
this.resMaps = BLLCalculator.getArtifactMaps(reader, this.trainList, null, true, new ArrayList<Long>(), new ArrayList<Double>(), 0, true, null);
57+
this.resMaps = BLLCalculator.getArtifactMaps(reader, this.trainList, null, true, new ArrayList<Long>(), new ArrayList<Double>(), 0, true, null, true);
5858
this.userCounts = Utilities.getRelativeTagMaps(this.trainList, false);
5959
this.resCounts = Utilities.getRelativeTagMaps(this.trainList, true);
6060
if (this.cType == CalculationType.USER_TO_RESOURCE) {

src/processing/hashtag/HashtagRecommendationEngine.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ private void initBLLAndFreqMaps(int trainSize, double dIndividual, Double lambda
141141
List<Bookmark> trainList = this.reader.getBookmarks().subList(0, this.trainSize);
142142
List<Bookmark> testList = this.reader.getBookmarks().subList(this.trainSize, this.reader.getBookmarks().size());
143143
this.resultMapPersonalBLLAllUsers = BLLCalculator.getArtifactMaps(reader, trainList, testList, false,
144-
new ArrayList<Long>(), new ArrayList<Double>(), dIndividual, true, lambdaIndividual);
144+
new ArrayList<Long>(), new ArrayList<Double>(), dIndividual, true, lambdaIndividual, true);
145145
this.resultMapPersonalFreqAllUsers = Utilities
146146
.getNormalizedMaps(this.reader.getBookmarks().subList(0, trainSize), false);
147147
}

0 commit comments

Comments
 (0)