|
| 1 | +/* |
| 2 | + TagRecommender: |
| 3 | + A framework to implement and evaluate algorithms for the recommendation |
| 4 | + of tags. |
| 5 | + Copyright (C) 2013 Dominik Kowald |
| 6 | + |
| 7 | + This program is free software: you can redistribute it and/or modify |
| 8 | + it under the terms of the GNU Affero General Public License as |
| 9 | + published by the Free Software Foundation, either version 3 of the |
| 10 | + License, or (at your option) any later version. |
| 11 | + |
| 12 | + This program is distributed in the hope that it will be useful, |
| 13 | + but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | + GNU Affero General Public License for more details. |
| 16 | + |
| 17 | + You should have received a copy of the GNU Affero General Public License |
| 18 | + along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 19 | + */ |
| 20 | + |
| 21 | +package engine; |
| 22 | + |
| 23 | +import java.io.BufferedWriter; |
| 24 | +import java.io.File; |
| 25 | +import java.io.FileWriter; |
| 26 | +import java.io.IOException; |
| 27 | +import java.util.Date; |
| 28 | +import java.util.List; |
| 29 | +import java.util.Map; |
| 30 | +import java.util.Random; |
| 31 | + |
| 32 | +import file.BookmarkReader; |
| 33 | + |
| 34 | +public class TagRecommenderEvalEngine implements EngineInterface { |
| 35 | + |
| 36 | + private EngineInterface lmEngine; |
| 37 | + private EngineInterface bllEngine; |
| 38 | + private EngineInterface threelEngine; |
| 39 | + |
| 40 | + private Random random; |
| 41 | + private BufferedWriter bw; |
| 42 | + |
| 43 | + public TagRecommenderEvalEngine() { |
| 44 | + this.lmEngine = null; |
| 45 | + this.bllEngine = null; |
| 46 | + this.threelEngine = null; |
| 47 | + this.bw = null; |
| 48 | + this.random = new Random(); |
| 49 | + |
| 50 | + try { |
| 51 | + FileWriter writer = new FileWriter(new File("./data/tagrec_log.txt"), true); |
| 52 | + this.bw = new BufferedWriter(writer); |
| 53 | + } catch (Exception e) { |
| 54 | + e.printStackTrace(); |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + @Override |
| 59 | + public void loadFile(String filename) throws Exception { |
| 60 | + this.lmEngine = null; |
| 61 | + this.bllEngine = null; |
| 62 | + this.threelEngine = null; |
| 63 | + |
| 64 | + BookmarkReader reader = new BookmarkReader(0, false); |
| 65 | + reader.readFile(filename); |
| 66 | + if (reader.getCategories().size() > 0) { |
| 67 | + this.threelEngine = new ThreeLayersEngine(); |
| 68 | + this.threelEngine.loadFile(filename); |
| 69 | + } |
| 70 | + if (reader.hasTimestamp()) { |
| 71 | + this.bllEngine = new BaseLevelLearningEngine(); |
| 72 | + this.bllEngine.loadFile(filename); |
| 73 | + } |
| 74 | + this.lmEngine = new LanguageModelEngine(); |
| 75 | + this.lmEngine.loadFile(filename); |
| 76 | + } |
| 77 | + |
| 78 | + @Override |
| 79 | + public synchronized Map<String, Double> getEntitiesWithLikelihood(String user, String resource, List<String> topics, Integer count) { |
| 80 | + Map<String, Double> returnMap = null; |
| 81 | + String algorithm = null; |
| 82 | + boolean useCognitiveAlgo = this.random.nextBoolean(); |
| 83 | + |
| 84 | + if (useCognitiveAlgo) { |
| 85 | + if (topics != null && topics.size() > 0 && this.threelEngine != null) { |
| 86 | + algorithm = "3LT"; |
| 87 | + returnMap = this.threelEngine.getEntitiesWithLikelihood(user, resource, topics, count); |
| 88 | + } else if (this.bllEngine != null) { |
| 89 | + algorithm = "BLL"; |
| 90 | + returnMap = this.bllEngine.getEntitiesWithLikelihood(user, resource, topics, count); |
| 91 | + } |
| 92 | + } |
| 93 | + if (!useCognitiveAlgo || returnMap == null) { |
| 94 | + algorithm = "MP"; |
| 95 | + returnMap = this.lmEngine.getEntitiesWithLikelihood(user, resource, topics, count); |
| 96 | + } |
| 97 | + |
| 98 | + if (this.bw != null) { |
| 99 | + try { |
| 100 | + this.bw.write(user + "|" + resource + "|" + topics + "|" + count + "|" + System.currentTimeMillis() + "|" + useCognitiveAlgo + "|" + algorithm + "|" + returnMap.keySet() + "\n"); |
| 101 | + this.bw.flush(); |
| 102 | + } catch (IOException e) { |
| 103 | + e.printStackTrace(); |
| 104 | + } |
| 105 | + } |
| 106 | + return returnMap; |
| 107 | + } |
| 108 | +} |
0 commit comments