Skip to content

Commit 485f620

Browse files
committed
added MP engine
1 parent 4d1affc commit 485f620

File tree

4 files changed

+66
-9
lines changed

4 files changed

+66
-9
lines changed
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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+
import file.BookmarkReader;
8+
9+
public class MostPopularCollectiveEngine implements EngineInterface {
10+
11+
private BookmarkReader reader;
12+
private final Map<String, Double> collectiveTags = new LinkedHashMap<String, Double>();
13+
14+
public void loadFile(String filename) throws Exception {
15+
BookmarkReader reader = EngineUtils.getSortedBookmarkReader(filename);
16+
Map<Integer, Double> collectiveTags = EngineUtils.calcTopEntities(reader, EntityType.TAG);
17+
18+
// map to strings
19+
Map<String, Double> collectiveTagNames = new LinkedHashMap<String, Double>();
20+
for (Map.Entry<Integer, Double> tag : collectiveTags.entrySet()) {
21+
collectiveTagNames.put(reader.getTags().get(tag.getKey()), tag.getValue());
22+
}
23+
24+
resetStructures(reader, collectiveTagNames);
25+
}
26+
27+
@Override
28+
public synchronized Map<String, Double> getEntitiesWithLikelihood(String user, String resource, List<String> topics, Integer count, Boolean filterOwnEntities, Algorithm algorithm, EntityType type) {
29+
if (count == null || count.doubleValue() < 1) {
30+
count = 10;
31+
}
32+
33+
Map<String, Double> returnMap = new LinkedHashMap<String, Double>();
34+
for (Map.Entry<String, Double> entry : collectiveTags.entrySet()) {
35+
if (returnMap.size() < count.intValue()) {
36+
returnMap.put(entry.getKey(), entry.getValue());
37+
} else {
38+
break;
39+
}
40+
}
41+
return returnMap;
42+
}
43+
44+
private synchronized void resetStructures(BookmarkReader reader, Map<String, Double> collectiveTags) {
45+
this.reader = reader;
46+
this.collectiveTags.clear();
47+
this.collectiveTags.putAll(collectiveTags);
48+
}
49+
}

src/engine/TagRecommenderEvalEngine.java

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

3232
public class TagRecommenderEvalEngine implements EngineInterface {
3333

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

4040
public TagRecommenderEvalEngine() {
41-
//this.lmEngine = null;
41+
this.mpEngine = null;
4242
this.bllEngine = null;
4343
this.threelEngine = null;
4444
//this.random = new Random();
@@ -55,8 +55,8 @@ public TagRecommenderEvalEngine() {
5555
@Override
5656
public void loadFile(String filename) throws Exception {
5757
//this.lmEngine = null;
58-
this.bllEngine = null;
59-
this.threelEngine = null;
58+
//this.bllEngine = null;
59+
//this.threelEngine = null;
6060

6161
/* old
6262
BookmarkReader reader = new BookmarkReader(0, false);
@@ -79,6 +79,8 @@ public void loadFile(String filename) throws Exception {
7979
this.bllEngine = new BaseLevelLearningCollectiveEngine();
8080
this.bllEngine.loadFile(filename);
8181
//}
82+
this.mpEngine = new MostPopularCollectiveEngine();
83+
this.mpEngine.loadFile(filename);
8284
}
8385

8486
@Override
@@ -113,7 +115,7 @@ public synchronized Map<String, Double> getEntitiesWithLikelihood(String user, S
113115
algorithmString = "3Lcoll";
114116
}
115117
}
116-
} else {
118+
} else if (algorithm == Algorithm.BLLcoll || algorithm == Algorithm.BLL) {
117119
if (this.bllEngine != null) {
118120
returnMap = this.bllEngine.getEntitiesWithLikelihood(user, resource, topics, count, filterOwnEntities, algorithm, type);
119121
if (algorithm == Algorithm.BLL) {
@@ -122,6 +124,11 @@ public synchronized Map<String, Double> getEntitiesWithLikelihood(String user, S
122124
algorithmString = "BLLcoll";
123125
}
124126
}
127+
} else {
128+
if (this.mpEngine != null) {
129+
returnMap = this.mpEngine.getEntitiesWithLikelihood(user, resource, topics, count, filterOwnEntities, algorithm, type);
130+
algorithmString = "MP";
131+
}
125132
}
126133
if (this.bw != null) {
127134
try {

src/test/Pipeline.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -500,10 +500,11 @@ private static void startKnowBrainTest(String path) {
500500
System.out.println("Tags for user: " + recEngine.getEntitiesWithLikelihood("14", null, Arrays.asList("t848","t655","t448","t40","t53","t997","t508","t268"), 10, false, Algorithm.THREELcoll, null));
501501
System.out.println("Tags for user: " + recEngine.getEntitiesWithLikelihood("14", null, Arrays.asList("t3"), 10, false, Algorithm.BLLcoll, null));
502502
System.out.println("Tags for user: " + recEngine.getEntitiesWithLikelihood("14", null, Arrays.asList("t848","t655","t448","t40","t53","t997","t508","t268"), 10, false, Algorithm.BLLcoll, null));
503-
System.out.println("Tags for user: " + recEngine.getEntitiesWithLikelihood("14", null, Arrays.asList("t3"), 10, false, Algorithm.THREEL, null));
504-
System.out.println("Tags for user: " + recEngine.getEntitiesWithLikelihood("14", null, Arrays.asList("t848","t655","t448","t40","t53","t997","t508","t268"), 10, false, Algorithm.THREEL, null));
505-
System.out.println("Tags for user: " + recEngine.getEntitiesWithLikelihood("14", null, Arrays.asList("t3"), 10, false, Algorithm.BLL, null));
506-
System.out.println("Tags for user: " + recEngine.getEntitiesWithLikelihood("14", null, Arrays.asList("t848","t655","t448","t40","t53","t997","t508","t268"), 10, false, Algorithm.BLL, null));
503+
System.out.println("Tags for user: " + recEngine.getEntitiesWithLikelihood("14", null, Arrays.asList("t848","t655","t448","t40","t53","t997","t508","t268"), 10, false, Algorithm.MP, null));
504+
//System.out.println("Tags for user: " + recEngine.getEntitiesWithLikelihood("14", null, Arrays.asList("t3"), 10, false, Algorithm.THREEL, null));
505+
//System.out.println("Tags for user: " + recEngine.getEntitiesWithLikelihood("14", null, Arrays.asList("t848","t655","t448","t40","t53","t997","t508","t268"), 10, false, Algorithm.THREEL, null));
506+
//System.out.println("Tags for user: " + recEngine.getEntitiesWithLikelihood("14", null, Arrays.asList("t3"), 10, false, Algorithm.BLL, null));
507+
//System.out.println("Tags for user: " + recEngine.getEntitiesWithLikelihood("14", null, Arrays.asList("t848","t655","t448","t40","t53","t997","t508","t268"), 10, false, Algorithm.BLL, null));
507508
}
508509

509510
// Helpers

tagrec.jar

-725 KB
Binary file not shown.

0 commit comments

Comments
 (0)