diff --git a/src/engine/MostPopularCollectiveEngine.java b/src/engine/MostPopularCollectiveEngine.java new file mode 100644 index 0000000..3178c5c --- /dev/null +++ b/src/engine/MostPopularCollectiveEngine.java @@ -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 collectiveTags = new LinkedHashMap(); + + public void loadFile(String filename) throws Exception { + BookmarkReader reader = EngineUtils.getSortedBookmarkReader(filename); + Map collectiveTags = EngineUtils.calcTopEntities(reader, EntityType.TAG); + + // map to strings + Map collectiveTagNames = new LinkedHashMap(); + for (Map.Entry tag : collectiveTags.entrySet()) { + collectiveTagNames.put(reader.getTags().get(tag.getKey()), tag.getValue()); + } + + resetStructures(reader, collectiveTagNames); + } + + @Override + public synchronized Map getEntitiesWithLikelihood(String user, String resource, List topics, Integer count, Boolean filterOwnEntities, Algorithm algorithm, EntityType type) { + if (count == null || count.doubleValue() < 1) { + count = 10; + } + + Map returnMap = new LinkedHashMap(); + for (Map.Entry 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 collectiveTags) { + this.reader = reader; + this.collectiveTags.clear(); + this.collectiveTags.putAll(collectiveTags); + } +} diff --git a/src/engine/TagRecommenderEvalEngine.java b/src/engine/TagRecommenderEvalEngine.java index e0636bc..9d4a089 100644 --- a/src/engine/TagRecommenderEvalEngine.java +++ b/src/engine/TagRecommenderEvalEngine.java @@ -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(); @@ -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); @@ -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 @@ -113,7 +115,7 @@ public synchronized Map 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) { @@ -122,6 +124,11 @@ public synchronized Map 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 { diff --git a/src/test/Pipeline.java b/src/test/Pipeline.java index d13029b..6431bd9 100644 --- a/src/test/Pipeline.java +++ b/src/test/Pipeline.java @@ -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 diff --git a/tagrec.jar b/tagrec.jar index fc31cbb..91f7cf2 100644 Binary files a/tagrec.jar and b/tagrec.jar differ