Skip to content

Commit e5e77a7

Browse files
committed
added log functionality to the engine classes
1 parent acf3b93 commit e5e77a7

File tree

5 files changed

+127
-16
lines changed

5 files changed

+127
-16
lines changed

src/engine/EngineUtils.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,8 @@ public static Map<String, Double> calcTopTags(BookmarkReader reader) {
5151
sortedCountMap.putAll(countMap);
5252

5353
for (Map.Entry<Integer, Integer> entry : sortedCountMap.entrySet()) {
54-
tagMap.put(reader.getTags().get(entry.getKey()),
55-
((double) entry.getValue()) / countSum);
54+
double tagValue = countSum != 0 ? ((double) entry.getValue()) / countSum : 0.0;
55+
tagMap.put(reader.getTags().get(entry.getKey()), tagValue);
5656
}
5757

5858
return tagMap;
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
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+
}

src/processing/ThreeLayersCalculator.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -226,12 +226,12 @@ private Map<Integer, Double> getResultMap(List<Bookmark> bookmarks, List<Integer
226226
// normalize and return
227227
double denom = 0.0;
228228
for (Map.Entry<Integer, Double> entry : resultMap.entrySet()) {
229-
Double val = Math.log(entry.getValue());
229+
Double val = Math.log(entry.getValue() + 1.0);
230230
denom += Math.exp(val);
231231
entry.setValue(val);
232232
}
233233
for (Map.Entry<Integer, Double> entry : resultMap.entrySet()) {
234-
entry.setValue(Math.exp(entry.getValue()) / denom);
234+
entry.setValue(denom != 0.0 ? Math.exp(entry.getValue()) / denom : 0.0);
235235
}
236236
return resultMap;
237237
}

src/test/Pipeline.java

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ License, or (at your option) any later version.
5353
import engine.LanguageModelEngine;
5454
import engine.ResourceEngineInterface;
5555
import engine.TagRecommenderEngine;
56+
import engine.TagRecommenderEvalEngine;
5657
import engine.ThreeLayersEngine;
5758
import file.BookmarkReader;
5859
import file.BookmarkSplitter;
@@ -153,6 +154,14 @@ public static void main(String[] args) {
153154

154155
// Engine Testing
155156
/*
157+
final ResourceEngineInterface resrecEngine = new CFResourceRecommenderEngine();
158+
try {
159+
resrecEngine.loadFile(path);
160+
} catch (Exception e2) {
161+
e2.printStackTrace();
162+
}
163+
System.out.println("CF Filter: " + resrecEngine.getEntitiesWithLikelihood(null, null, null, 20, true));
164+
System.out.println("CF -Filter: " + resrecEngine.getEntitiesWithLikelihood(null, null, null, 20, false));
156165
EngineInterface engine = new ThreeLayersEngine();
157166
try {
158167
engine.loadFile("bib_core/bib_sample" + "_1_lda_500_res");
@@ -174,23 +183,17 @@ public static void main(String[] args) {
174183
e3.printStackTrace();
175184
}
176185
System.out.println("LM: " + lmEngine.getEntitiesWithLikelihood("41", "545", null, 10));
177-
EngineInterface tagrecEngine = new TagRecommenderEngine();
186+
EngineInterface tagrecEngine = new TagRecommenderEvalEngine();
178187
try {
179-
tagrecEngine.loadFile("hugo2");
188+
tagrecEngine.loadFile(path + "_lda_500");
180189
} catch (Exception e) {
181190
e.printStackTrace();
182191
}
183-
System.out.println("TagRec with Topics: " + tagrecEngine.getEntitiesWithLikelihood("41", "545", Arrays.asList("ontology", "conference", "tutorial", "web2.0", "rss", "tools"), 10));
184-
System.out.println("TagRec without Topics: " + tagrecEngine.getEntitiesWithLikelihood("41", "545", null, 10));
185-
final ResourceEngineInterface resrecEngine = new CFResourceRecommenderEngine();
186-
try {
187-
resrecEngine.loadFile(path);
188-
} catch (Exception e2) {
189-
e2.printStackTrace();
190-
}
191-
System.out.println("CF Filter: " + resrecEngine.getEntitiesWithLikelihood(null, null, null, 20, true));
192-
System.out.println("CF -Filter: " + resrecEngine.getEntitiesWithLikelihood(null, null, null, 20, false));
192+
System.out.println("TagRec without Topics: " + tagrecEngine.getEntitiesWithLikelihood("0", "13", Arrays.asList("t333"), 10));
193+
System.out.println("TagRec without Topics: " + tagrecEngine.getEntitiesWithLikelihood("0", "13", Arrays.asList("t333"), 10));
194+
System.out.println("TagRec without Topics: " + tagrecEngine.getEntitiesWithLikelihood("0", "13", Arrays.asList("t333"), 10));
193195
*/
196+
194197

195198
// Commandline Arguments
196199
if (args.length < 3) {

tagrec.jar

48.8 KB
Binary file not shown.

0 commit comments

Comments
 (0)