Skip to content

Commit 4aa8bec

Browse files
committed
added collective engines
1 parent 8ef0b4e commit 4aa8bec

11 files changed

Lines changed: 263 additions & 59689 deletions

data/csv/spear/del_sample_lda_24.txt

Lines changed: 0 additions & 59651 deletions
This file was deleted.

data/csv/spear/spear_test_data.txt

Lines changed: 0 additions & 9 deletions
This file was deleted.

data/csv/spear/test_data.txt

Lines changed: 0 additions & 10 deletions
This file was deleted.

src/engine/Algorithm.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,14 @@ public enum Algorithm {
55
BLL,
66
BLLac,
77
BLLacMPr,
8+
BLLcoll,
89
MPu,
910
MPr,
1011
MPur,
1112
THREEL,
1213
THREELT,
1314
THREELTMPr,
15+
THREELcoll,
1416

1517
RESOURCEMP,
1618
RESOURCECF,
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package engine;
2+
3+
import java.util.ArrayList;
4+
import java.util.LinkedHashMap;
5+
import java.util.List;
6+
import java.util.Map;
7+
8+
import processing.BLLCalculator;
9+
import file.BookmarkReader;
10+
11+
public class BaseLevelLearningCollectiveEngine implements EngineInterface {
12+
13+
private BookmarkReader reader = null;
14+
private final Map<String, Double> collectiveTags = new LinkedHashMap<String, Double>();
15+
16+
public void loadFile(String filename) throws Exception {
17+
BookmarkReader reader = EngineUtils.getSortedBookmarkReader(filename);
18+
Map<Integer, Double> collectiveTags = BLLCalculator.getCollectiveArtifactMap(reader, reader.getBookmarks(), null, false, new ArrayList<Long>(), new ArrayList<Double>(), 0.5, true);
19+
20+
// map to strings
21+
Map<String, Double> collectiveTagNames = new LinkedHashMap<String, Double>();
22+
for (Map.Entry<Integer, Double> tag : collectiveTags.entrySet()) {
23+
collectiveTagNames.put(reader.getTags().get(tag.getKey()), tag.getValue());
24+
}
25+
26+
resetStructures(reader, collectiveTagNames);
27+
}
28+
29+
@Override
30+
public synchronized Map<String, Double> getEntitiesWithLikelihood(String user, String resource, List<String> topics, Integer count, Boolean filterOwnEntities, Algorithm algorithm, EntityType type) {
31+
if (count == null || count.doubleValue() < 1) {
32+
count = 10;
33+
}
34+
Map<String, Double> tagMap = new LinkedHashMap<String, Double>();
35+
if (algorithm == null || algorithm == Algorithm.BLLcoll) {
36+
tagMap = this.collectiveTags;
37+
} else {
38+
int userID = this.reader.getUsers().indexOf(user);
39+
if (user != null && userID != -1) {
40+
Map<Integer, Double> userTags = BLLCalculator.getSortedArtifactMapForUser(userID, this.reader, this.reader.getBookmarks(), null, false, new ArrayList<Long>(), new ArrayList<Double>(), 0.5, true);
41+
for (Map.Entry<Integer, Double> tag : userTags.entrySet()) {
42+
tagMap.put(this.reader.getTags().get(tag.getKey()), tag.getValue());
43+
}
44+
}
45+
}
46+
47+
Map<String, Double> returnMap = new LinkedHashMap<String, Double>();
48+
for (Map.Entry<String, Double> entry : tagMap.entrySet()) {
49+
if (returnMap.size() < count.intValue()) {
50+
returnMap.put(entry.getKey(), entry.getValue());
51+
} else {
52+
break;
53+
}
54+
}
55+
return returnMap;
56+
}
57+
58+
private synchronized void resetStructures(BookmarkReader reader, Map<String, Double> collectiveTags) {
59+
this.reader = reader;
60+
this.collectiveTags.clear();
61+
this.collectiveTags.putAll(collectiveTags);
62+
}
63+
}

src/engine/TagRecommenderEvalEngine.java

Lines changed: 45 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -31,16 +31,16 @@ License, or (at your option) any later version.
3131

3232
public class TagRecommenderEvalEngine implements EngineInterface {
3333

34-
private EngineInterface lmEngine;
34+
//private EngineInterface lmEngine;
3535
private EngineInterface bllEngine;
36-
//private EngineInterface threelEngine;
36+
private EngineInterface threelEngine;
3737
//private Random random;
3838
private BufferedWriter bw;
3939

4040
public TagRecommenderEvalEngine() {
41-
this.lmEngine = null;
41+
//this.lmEngine = null;
4242
this.bllEngine = null;
43-
//this.threelEngine = null;
43+
this.threelEngine = null;
4444
//this.random = new Random();
4545
this.bw = null;
4646

@@ -54,29 +54,39 @@ public TagRecommenderEvalEngine() {
5454

5555
@Override
5656
public void loadFile(String filename) throws Exception {
57-
this.lmEngine = null;
57+
//this.lmEngine = null;
5858
this.bllEngine = null;
59-
//this.threelEngine = null;
59+
this.threelEngine = null;
6060

61+
/* old
6162
BookmarkReader reader = new BookmarkReader(0, false);
6263
reader.readFile(filename);
63-
//if (reader.getCategories().size() > 0) {
64-
// this.threelEngine = new ThreeLayersEngine();
65-
// this.threelEngine.loadFile(filename);
66-
//}
64+
if (reader.getCategories().size() > 0) {
65+
this.threelEngine = new ThreeLayersEngine();
66+
this.threelEngine.loadFile(filename);
67+
}
6768
if (reader.hasTimestamp()) {
6869
this.bllEngine = new BaseLevelLearningEngine();
6970
this.bllEngine.loadFile(filename);
7071
}
7172
this.lmEngine = new LanguageModelEngine();
7273
this.lmEngine.loadFile(filename);
74+
*/
75+
//if (filename.contains("group1") || filename.contains("group3")) {
76+
this.threelEngine = new ThreeLayersCollectiveEngine();
77+
this.threelEngine.loadFile(filename);
78+
//} else {
79+
this.bllEngine = new BaseLevelLearningCollectiveEngine();
80+
this.bllEngine.loadFile(filename);
81+
//}
7382
}
7483

7584
@Override
7685
public synchronized Map<String, Double> getEntitiesWithLikelihood(String user, String resource, List<String> topics, Integer count, Boolean filterOwnEntities, Algorithm algorithm, EntityType type) {
7786
Map<String, Double> returnMap = null;
7887
String algorithmString = null;
7988

89+
/* old
8090
if (this.bllEngine != null) {
8191
if (algorithm == null || algorithm == Algorithm.BLLacMPr) {
8292
algorithmString = "BLLacMPr";
@@ -88,13 +98,31 @@ public synchronized Map<String, Double> getEntitiesWithLikelihood(String user, S
8898
if (algorithmString != null) {
8999
returnMap = this.bllEngine.getEntitiesWithLikelihood(user, resource, topics, count, filterOwnEntities, algorithm, type);
90100
}
91-
}
92-
101+
}
93102
if (algorithmString == null) {
94103
algorithmString = "MPur";
95104
returnMap = this.lmEngine.getEntitiesWithLikelihood(user, resource, topics, count, filterOwnEntities, algorithm, type);
96105
}
97-
106+
*/
107+
if (algorithm == null || algorithm == Algorithm.THREELcoll || algorithm == Algorithm.THREEL) {
108+
if (this.threelEngine != null) {
109+
returnMap = this.threelEngine.getEntitiesWithLikelihood(user, resource, topics, count, filterOwnEntities, algorithm, type);
110+
if (algorithm == Algorithm.THREEL) {
111+
algorithmString = "3L";
112+
} else {
113+
algorithmString = "3Lcoll";
114+
}
115+
}
116+
} else {
117+
if (this.bllEngine != null) {
118+
returnMap = this.bllEngine.getEntitiesWithLikelihood(user, resource, topics, count, filterOwnEntities, algorithm, type);
119+
if (algorithm == Algorithm.BLL) {
120+
algorithmString = "BLL";
121+
} else {
122+
algorithmString = "BLLcoll";
123+
}
124+
}
125+
}
98126
if (this.bw != null) {
99127
try {
100128
this.bw.write(user + "|" + resource + "|" + topics + "|" + count + "|" + filterOwnEntities + "|" + System.currentTimeMillis() + "|" + algorithmString + "|" + returnMap.keySet() + "\n");
@@ -105,4 +133,8 @@ public synchronized Map<String, Double> getEntitiesWithLikelihood(String user, S
105133
}
106134
return returnMap;
107135
}
136+
137+
public static boolean getRandomBoolean() {
138+
return Math.random() < 0.5;
139+
}
108140
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package engine;
2+
3+
import java.util.ArrayList;
4+
import java.util.LinkedHashMap;
5+
import java.util.List;
6+
import java.util.Map;
7+
8+
import common.CalculationType;
9+
import processing.BLLCalculator;
10+
import processing.ThreeLTCalculator;
11+
import file.BookmarkReader;
12+
13+
public class ThreeLayersCollectiveEngine implements EngineInterface {
14+
15+
private BookmarkReader reader = null;
16+
private ThreeLTCalculator calculator = null;
17+
18+
public void loadFile(String filename) throws Exception {
19+
BookmarkReader reader = EngineUtils.getSortedBookmarkReader(filename);
20+
ThreeLTCalculator calculator = new ThreeLTCalculator(reader, reader.getBookmarks().size(), 5, 5, true, false, false, CalculationType.NONE);
21+
22+
resetStructures(reader, calculator);
23+
}
24+
25+
@Override
26+
public synchronized Map<String, Double> getEntitiesWithLikelihood(String user, String resource, List<String> topics, Integer count, Boolean filterOwnEntities, Algorithm algorithm, EntityType type) {
27+
if (count == null || count.doubleValue() < 1) {
28+
count = 10;
29+
}
30+
List<Integer> topicIDs = new ArrayList<>();
31+
if (topics != null) {
32+
for (String t : topics) {
33+
int tID = this.reader.getCategories().indexOf(t);
34+
if (tID != -1) {
35+
topicIDs.add(tID);
36+
}
37+
}
38+
}
39+
Map<Integer, Double> tagIDs = null;
40+
if (algorithm == null || algorithm == Algorithm.THREELcoll) {
41+
tagIDs = this.calculator.getCollectiveRankedTagList(topicIDs, System.currentTimeMillis() / 1000.0, count.intValue(), false, false);
42+
} else {
43+
int userID = this.reader.getUsers().indexOf(user);
44+
if (user != null && userID != -1) {
45+
tagIDs = this.calculator.getRankedTagList(userID, -1, topicIDs, System.currentTimeMillis() / 1000.0, count.intValue(), false, false, true);
46+
} else {
47+
new LinkedHashMap<String, Double>();
48+
}
49+
}
50+
51+
// map to strings
52+
Map<String, Double> tagStrings = new LinkedHashMap<String, Double>();
53+
for (Map.Entry<Integer, Double> entry : tagIDs.entrySet()) {
54+
tagStrings.put(this.reader.getTags().get(entry.getKey()), entry.getValue());
55+
}
56+
return tagStrings;
57+
}
58+
59+
private synchronized void resetStructures(BookmarkReader reader, ThreeLTCalculator calculator) {
60+
this.reader = reader;
61+
this.calculator = calculator;
62+
}
63+
}

src/engine/ThreeLayersEngine.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ License, or (at your option) any later version.
3333
import common.CalculationType;
3434
import common.DoubleMapComparator;
3535

36-
// TODO: integrate Tag-Filtering!
3736
// TODO: make it work in online setting! (caching + LDA topic calculation)
3837
public class ThreeLayersEngine implements EngineInterface {
3938

@@ -61,6 +60,7 @@ public synchronized Map<String, Double> getEntitiesWithLikelihood(String user, S
6160
if (filterOwnEntities == null) {
6261
filterOwnEntities = true;
6362
}
63+
List<Integer> filterTags = new ArrayList<Integer>();
6464

6565
Map<Integer, Double> tagIDs = new LinkedHashMap<>();
6666
Map<String, Double> tagMap = new LinkedHashMap<>();
@@ -72,6 +72,7 @@ public synchronized Map<String, Double> getEntitiesWithLikelihood(String user, S
7272
if (user != null) {
7373
userID = this.reader.getUsers().indexOf(user);
7474
}
75+
filterTags = EngineUtils.getFilterTags(filterOwnEntities, this.reader, user, resource, this.calculator.getUserMaps().get(userID));
7576
int resID = -1;
7677
if (resource != null) {
7778
resID = this.reader.getResources().indexOf(resource);
@@ -94,6 +95,8 @@ public synchronized Map<String, Double> getEntitiesWithLikelihood(String user, S
9495
}
9596
}
9697

98+
// TODO: finish filtering
99+
97100
// fill up with MP tags
98101
if (tagIDs.size() < count) {
99102
for (Map.Entry<Integer, Double> t : this.topTags.entrySet()) {

src/processing/BLLCalculator.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,36 @@ public static List<Map<Integer, Double>> getArtifactMaps(BookmarkReader reader,
282282
return maps;
283283
}
284284

285+
286+
public static Map<Integer, Double> getSortedArtifactMapForUser(int userID, BookmarkReader reader, List<Bookmark> userLines, List<Bookmark> testLines, boolean resource,
287+
List<Long> timestampList, List<Double> denomList, double dVal, boolean normalize) {
288+
289+
List<Map<Integer, Double>> artifactMaps = getArtifactMaps(reader, userLines, testLines, resource, timestampList, denomList, dVal, normalize);
290+
if (artifactMaps != null && userID < artifactMaps.size()) {
291+
Map<Integer, Double> sortedResultMap = new TreeMap<Integer, Double>(new DoubleMapComparator(artifactMaps.get(userID)));
292+
sortedResultMap.putAll(artifactMaps.get(userID));
293+
return sortedResultMap;
294+
}
295+
return new LinkedHashMap<Integer, Double>();
296+
}
297+
298+
public static Map<Integer, Double> getCollectiveArtifactMap(BookmarkReader reader, List<Bookmark> userLines, List<Bookmark> testLines, boolean resource,
299+
List<Long> timestampList, List<Double> denomList, double dVal, boolean normalize) {
300+
301+
Map<Integer, Double> collectiveArtifactMap = new LinkedHashMap<Integer, Double>();
302+
List<Map<Integer, Double>> artifactMaps = getArtifactMaps(reader, userLines, testLines, resource, timestampList, denomList, dVal, normalize);
303+
for (Map<Integer, Double> map : artifactMaps) {
304+
for (Map.Entry<Integer, Double> entry : map.entrySet()) {
305+
Double val = collectiveArtifactMap.get(entry.getKey());
306+
collectiveArtifactMap.put(entry.getKey(), val != null ? val.doubleValue() + entry.getValue() : entry.getValue());
307+
}
308+
}
309+
310+
Map<Integer, Double> sortedResultMap = new TreeMap<Integer, Double>(new DoubleMapComparator(collectiveArtifactMap));
311+
sortedResultMap.putAll(collectiveArtifactMap);
312+
return sortedResultMap;
313+
}
314+
285315
private static Map<Integer, Double> addActValue(Bookmark data, Map<Integer, Double> actValues, long baselineTimestamp, boolean resource, double dVal) {
286316
if (!data.getTimestamp().isEmpty()) {
287317
Double newAct = 0.0;

src/processing/ThreeLTCalculator.java

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@ public class ThreeLTCalculator {
3939
private List<Map<Integer, Double>> userCounts;
4040
private List<Map<Integer, Double>> resCounts;
4141

42+
public List<Map<Integer, Double>> getUserMaps() {
43+
return this.userCounts;
44+
}
45+
4246
public ThreeLTCalculator(BookmarkReader reader, int trainSize, int dValue, int beta, boolean userBased, boolean resBased, boolean bookmarkBLL, CalculationType cType) {
4347
this.reader = reader;
4448
this.trainList = this.reader.getBookmarks().subList(0, trainSize);
@@ -172,6 +176,30 @@ public Map<Integer, Double> getRankedTagList(int userID, int resID, List<Integer
172176
}
173177
return returnMap;
174178
}
179+
180+
public Map<Integer, Double> getCollectiveRankedTagList(List<Integer> testCats, double testTimestamp, int limit, boolean tagBLL, boolean topicBLL) {
181+
Map<Integer, Double> collectiveTagMap = new LinkedHashMap<Integer, Double>();
182+
for (int id = 0; id < this.reader.getUsers().size(); id++) {
183+
Map<Integer, Double> tagMap = getRankedTagList(id, -1, testCats, testTimestamp, limit, tagBLL, topicBLL, false);
184+
for (Map.Entry<Integer, Double> entry : tagMap.entrySet()) {
185+
Double val = collectiveTagMap.get(entry.getKey());
186+
collectiveTagMap.put(entry.getKey(), val == null ? entry.getValue() : val.doubleValue() + entry.getValue());
187+
}
188+
}
189+
190+
Map<Integer, Double> sortedResultMap = new TreeMap<Integer, Double>(new DoubleMapComparator(collectiveTagMap));
191+
sortedResultMap.putAll(collectiveTagMap);
192+
Map<Integer, Double> returnMap = new LinkedHashMap<Integer, Double>();
193+
for (Map.Entry<Integer, Double> entry : sortedResultMap.entrySet()) {
194+
if (returnMap.size() < limit) {
195+
returnMap.put(entry.getKey(), entry.getValue());
196+
} else {
197+
break;
198+
}
199+
}
200+
201+
return returnMap;
202+
}
175203

176204
private Map<Integer, Double> getResultMap(List<Bookmark> bookmarks, List<Integer> testCats, Map<Integer, Double> userTagMap, Map<Integer, Double> userCatMap, double testTimestamp, boolean topicBLL) {
177205
Map<Integer, Double> resultMap = new LinkedHashMap<Integer, Double>();
@@ -211,7 +239,10 @@ private Map<Integer, Double> getResultMap(List<Bookmark> bookmarks, List<Integer
211239
// normalize and return
212240
double denom = 0.0;
213241
for (Map.Entry<Integer, Double> entry : resultMap.entrySet()) {
214-
Double val = Math.log(entry.getValue());
242+
double val = 0.0;
243+
if (entry.getValue() != 0.0) {
244+
val = Math.log(entry.getValue());
245+
}
215246
denom += Math.exp(val);
216247
entry.setValue(val);
217248
}

0 commit comments

Comments
 (0)