Skip to content

Commit

Permalink
added MP engine
Browse files Browse the repository at this point in the history
  • Loading branch information
domkowald committed Aug 14, 2015
1 parent 4d1affc commit 485f620
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 9 deletions.
49 changes: 49 additions & 0 deletions src/engine/MostPopularCollectiveEngine.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package engine;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import file.BookmarkReader;

public class MostPopularCollectiveEngine implements EngineInterface {

private BookmarkReader reader;
private final Map<String, Double> collectiveTags = new LinkedHashMap<String, Double>();

public void loadFile(String filename) throws Exception {
BookmarkReader reader = EngineUtils.getSortedBookmarkReader(filename);
Map<Integer, Double> collectiveTags = EngineUtils.calcTopEntities(reader, EntityType.TAG);

// map to strings
Map<String, Double> collectiveTagNames = new LinkedHashMap<String, Double>();
for (Map.Entry<Integer, Double> tag : collectiveTags.entrySet()) {
collectiveTagNames.put(reader.getTags().get(tag.getKey()), tag.getValue());
}

resetStructures(reader, collectiveTagNames);
}

@Override
public synchronized Map<String, Double> getEntitiesWithLikelihood(String user, String resource, List<String> topics, Integer count, Boolean filterOwnEntities, Algorithm algorithm, EntityType type) {
if (count == null || count.doubleValue() < 1) {
count = 10;
}

Map<String, Double> returnMap = new LinkedHashMap<String, Double>();
for (Map.Entry<String, Double> entry : collectiveTags.entrySet()) {
if (returnMap.size() < count.intValue()) {
returnMap.put(entry.getKey(), entry.getValue());
} else {
break;
}
}
return returnMap;
}

private synchronized void resetStructures(BookmarkReader reader, Map<String, Double> collectiveTags) {
this.reader = reader;
this.collectiveTags.clear();
this.collectiveTags.putAll(collectiveTags);
}
}
17 changes: 12 additions & 5 deletions src/engine/TagRecommenderEvalEngine.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,14 @@ License, or (at your option) any later version.

public class TagRecommenderEvalEngine implements EngineInterface {

//private EngineInterface lmEngine;
private EngineInterface mpEngine;
private EngineInterface bllEngine;
private EngineInterface threelEngine;
//private Random random;
private BufferedWriter bw;

public TagRecommenderEvalEngine() {
//this.lmEngine = null;
this.mpEngine = null;
this.bllEngine = null;
this.threelEngine = null;
//this.random = new Random();
Expand All @@ -55,8 +55,8 @@ public TagRecommenderEvalEngine() {
@Override
public void loadFile(String filename) throws Exception {
//this.lmEngine = null;
this.bllEngine = null;
this.threelEngine = null;
//this.bllEngine = null;
//this.threelEngine = null;

/* old
BookmarkReader reader = new BookmarkReader(0, false);
Expand All @@ -79,6 +79,8 @@ public void loadFile(String filename) throws Exception {
this.bllEngine = new BaseLevelLearningCollectiveEngine();
this.bllEngine.loadFile(filename);
//}
this.mpEngine = new MostPopularCollectiveEngine();
this.mpEngine.loadFile(filename);
}

@Override
Expand Down Expand Up @@ -113,7 +115,7 @@ public synchronized Map<String, Double> getEntitiesWithLikelihood(String user, S
algorithmString = "3Lcoll";
}
}
} else {
} else if (algorithm == Algorithm.BLLcoll || algorithm == Algorithm.BLL) {
if (this.bllEngine != null) {
returnMap = this.bllEngine.getEntitiesWithLikelihood(user, resource, topics, count, filterOwnEntities, algorithm, type);
if (algorithm == Algorithm.BLL) {
Expand All @@ -122,6 +124,11 @@ public synchronized Map<String, Double> getEntitiesWithLikelihood(String user, S
algorithmString = "BLLcoll";
}
}
} else {
if (this.mpEngine != null) {
returnMap = this.mpEngine.getEntitiesWithLikelihood(user, resource, topics, count, filterOwnEntities, algorithm, type);
algorithmString = "MP";
}
}
if (this.bw != null) {
try {
Expand Down
9 changes: 5 additions & 4 deletions src/test/Pipeline.java
Original file line number Diff line number Diff line change
Expand Up @@ -500,10 +500,11 @@ private static void startKnowBrainTest(String path) {
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));
System.out.println("Tags for user: " + recEngine.getEntitiesWithLikelihood("14", null, Arrays.asList("t3"), 10, false, Algorithm.BLLcoll, null));
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));
System.out.println("Tags for user: " + recEngine.getEntitiesWithLikelihood("14", null, Arrays.asList("t3"), 10, false, Algorithm.THREEL, null));
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));
System.out.println("Tags for user: " + recEngine.getEntitiesWithLikelihood("14", null, Arrays.asList("t3"), 10, false, Algorithm.BLL, null));
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));
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));
//System.out.println("Tags for user: " + recEngine.getEntitiesWithLikelihood("14", null, Arrays.asList("t3"), 10, false, Algorithm.THREEL, null));
//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));
//System.out.println("Tags for user: " + recEngine.getEntitiesWithLikelihood("14", null, Arrays.asList("t3"), 10, false, Algorithm.BLL, null));
//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));
}

// Helpers
Expand Down
Binary file modified tagrec.jar
Binary file not shown.

0 comments on commit 485f620

Please sign in to comment.