r67926 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r67925‎ | r67926 | r67927 >
Date:21:24, 12 June 2010
Author:daniel
Status:deferred
Tags:
Comment:
reworked fetchers/caching
Modified paths:
  • /trunk/WikiWord/WikiWord/src/main/java/de/brightbyte/wikiword/disambig/AbstractDisambiguator.java (modified) (history)
  • /trunk/WikiWord/WikiWord/src/main/java/de/brightbyte/wikiword/disambig/CachingFeatureFetcher.java (added) (history)
  • /trunk/WikiWord/WikiWord/src/main/java/de/brightbyte/wikiword/disambig/CachingMeaningFetcher.java (added) (history)
  • /trunk/WikiWord/WikiWord/src/main/java/de/brightbyte/wikiword/disambig/CachingPropertyFetcher.java (added) (history)
  • /trunk/WikiWord/WikiWord/src/main/java/de/brightbyte/wikiword/disambig/CoherenceDisambiguator.java (modified) (history)
  • /trunk/WikiWord/WikiWord/src/main/java/de/brightbyte/wikiword/disambig/FeatureCache.java (deleted) (history)
  • /trunk/WikiWord/WikiWord/src/main/java/de/brightbyte/wikiword/disambig/MeaningCache.java (deleted) (history)
  • /trunk/WikiWord/WikiWord/src/main/java/de/brightbyte/wikiword/disambig/PopularityDisambiguator.java (modified) (history)
  • /trunk/WikiWord/WikiWord/src/main/java/de/brightbyte/wikiword/disambig/PropertyFetcher.java (added) (history)
  • /trunk/WikiWord/WikiWord/src/main/java/de/brightbyte/wikiword/disambig/SlidingCoherenceDisambiguator.java (modified) (history)
  • /trunk/WikiWord/WikiWord/src/main/java/de/brightbyte/wikiword/disambig/StoredPropertyFetcher.java (added) (history)

Diff [purge]

Index: trunk/WikiWord/WikiWord/src/main/java/de/brightbyte/wikiword/disambig/MeaningCache.java
@@ -1,123 +0,0 @@
2 -package de.brightbyte.wikiword.disambig;
3 -
4 -import java.util.ArrayList;
5 -import java.util.Collection;
6 -import java.util.Collections;
7 -import java.util.HashMap;
8 -import java.util.List;
9 -import java.util.Map;
10 -
11 -import de.brightbyte.util.PersistenceException;
12 -import de.brightbyte.wikiword.model.TermReference;
13 -import de.brightbyte.wikiword.model.WikiWordConcept;
14 -
15 -public class MeaningCache<C extends WikiWordConcept> implements MeaningFetcher<C> {
16 -
17 - protected static class Manager<C extends WikiWordConcept> {
18 - protected int maxDepth;
19 -
20 - protected MeaningFetcher<? extends C> root;
21 - protected List<MeaningCache<C>> stack;
22 -
23 - public Manager(MeaningFetcher<? extends C> root, int maxDepth) {
24 - if (root==null) throw new NullPointerException();
25 - this.stack = new ArrayList<MeaningCache<C>>(maxDepth+1);
26 - this.maxDepth = maxDepth;
27 - this.root = root;
28 - }
29 -
30 - private MeaningFetcher<? extends C> getTop() {
31 - if (stack.isEmpty()) return root;
32 - else return stack.get(stack.size()-1);
33 - }
34 -
35 - public synchronized MeaningCache<C> newCache() {
36 - MeaningCache<C> cache = new MeaningCache<C>( getTop() );
37 - stack.add(cache);
38 -
39 - if (stack.size()>maxDepth) {
40 - MeaningCache<C> old = stack.remove(0);
41 - old.dispose();
42 - }
43 - if (!stack.isEmpty()) stack.get(0).setParent(root);
44 -
45 - return cache;
46 - }
47 - }
48 -
49 - protected MeaningFetcher<C> parent;
50 -
51 - protected Map<String, List<? extends C>> cache;
52 -
53 - public MeaningCache(MeaningFetcher<? extends C> parent) {
54 - if (parent==null) throw new NullPointerException();
55 - this.setParent(parent);
56 - this.cache = new HashMap<String, List<? extends C>>();
57 - }
58 -
59 -
60 - public MeaningFetcher<? extends C> getParent() {
61 - return parent;
62 - }
63 -
64 - public void setParent(MeaningFetcher<? extends C> parent) {
65 - if (parent == null) throw new NullPointerException();
66 - if (parent == this) throw new IllegalArgumentException("can't be my own parent");
67 - //TODO: prevent cycles
68 -
69 - this.parent = (MeaningFetcher<C>)(Object)parent; //XXX: ugly scast. generics are a pain.
70 - }
71 -
72 - public void clear() {
73 - cache.clear();
74 - }
75 -
76 -
77 - public List<? extends C> getMeanings(String term) throws PersistenceException {
78 - List<? extends C> meanings = cache.get(term);
79 -
80 - if (meanings==null) {
81 - meanings = parent.getMeanings(term);
82 - cache.put(term, meanings);
83 - }
84 -
85 - return meanings;
86 - }
87 -
88 -
89 - public <X extends TermReference> Map<X, List<? extends C>> getMeanings(Collection<X> terms) throws PersistenceException {
90 - Map<X, List<? extends C>> meanings= new HashMap<X, List<? extends C>>();
91 - List<X> todo = new ArrayList<X>(terms.size());
92 -
93 - for (X t: terms) {
94 - List<? extends C> m = cache.get(t.getTerm());
95 - if (m!=null) {
96 - meanings.put(t, m);
97 - cache.put(t.getTerm(), m);
98 - continue;
99 - } else {
100 - todo.add(t);
101 - }
102 - }
103 -
104 - if (!todo.isEmpty()) {
105 - Map<X, List<? extends C>> parentMeanings = parent.getMeanings(todo); //XXX: ugly cast, generics are a pain
106 - meanings.putAll(parentMeanings);
107 -
108 - for (X t: todo) {
109 - List<? extends C> m = parentMeanings.get(t);
110 - if (m==null) m = Collections.emptyList();
111 - cache.put(t.getTerm(), m);
112 - }
113 - }
114 -
115 - return meanings;
116 - }
117 -
118 - protected void dispose() {
119 - this.cache.clear();
120 - this.cache = null;
121 - this.parent = null;
122 - }
123 -
124 -}
Index: trunk/WikiWord/WikiWord/src/main/java/de/brightbyte/wikiword/disambig/FeatureCache.java
@@ -1,124 +0,0 @@
2 -package de.brightbyte.wikiword.disambig;
3 -
4 -import java.util.ArrayList;
5 -import java.util.Collection;
6 -import java.util.HashMap;
7 -import java.util.List;
8 -import java.util.Map;
9 -
10 -import de.brightbyte.util.PersistenceException;
11 -import de.brightbyte.wikiword.model.ConceptFeatures;
12 -import de.brightbyte.wikiword.model.WikiWordConcept;
13 -
14 -public class FeatureCache<C extends WikiWordConcept, K> implements FeatureFetcher<C, K> {
15 -
16 - protected static class Manager<C extends WikiWordConcept, K> {
17 - protected int maxDepth;
18 -
19 - protected FeatureFetcher<C, K> root;
20 - protected List<FeatureCache<C, K>> stack;
21 -
22 - public Manager(FeatureFetcher<C, K> root, int maxDepth) {
23 - if (root==null) throw new NullPointerException();
24 - this.stack = new ArrayList<FeatureCache<C, K>>(maxDepth+1);
25 - this.maxDepth = maxDepth;
26 - this.root = root;
27 - }
28 -
29 - private FeatureFetcher<C, K> getTop() {
30 - if (stack.isEmpty()) return root;
31 - else return stack.get(stack.size()-1);
32 - }
33 -
34 - public synchronized FeatureCache<C, K> newCache() {
35 - FeatureCache<C, K> cache = new FeatureCache<C, K>( getTop() );
36 - stack.add(cache);
37 -
38 - if (stack.size()>maxDepth) {
39 - FeatureCache<C, K> old = stack.remove(0);
40 - old.dispose();
41 - }
42 -
43 - if (!stack.isEmpty()) stack.get(0).setParent(root);
44 -
45 - return cache;
46 - }
47 -
48 - public boolean getFeaturesAreNormalized() {
49 - return root.getFeaturesAreNormalized();
50 - }
51 - }
52 -
53 - protected FeatureFetcher<C, K> parent;
54 -
55 - protected Map<Integer, ConceptFeatures<C, K>> cache;
56 -
57 - public FeatureCache(FeatureFetcher<C, K> parent) {
58 - if (parent==null) throw new NullPointerException();
59 - this.parent = parent;
60 - this.cache = new HashMap<Integer, ConceptFeatures<C,K>>();
61 - }
62 -
63 - protected void dispose() {
64 - this.cache.clear();
65 - this.cache = null;
66 - this.parent = null;
67 - }
68 -
69 - public ConceptFeatures<C, K> getFeatures(C c)
70 - throws PersistenceException {
71 -
72 - ConceptFeatures<C, K> f = cache.get(c.getId());
73 - if (f!=null) return f;
74 -
75 - f = parent.getFeatures(c);
76 - cache.put(c.getId(), f);
77 -
78 - return f;
79 - }
80 -
81 - public Map<Integer, ConceptFeatures<C, K>> getFeatures(Collection<? extends C> concepts) throws PersistenceException {
82 - Map<Integer, ConceptFeatures<C, K>> features = new HashMap<Integer, ConceptFeatures<C, K>> ();
83 - List<C> todo = new ArrayList<C>(concepts.size());
84 - for (C c: concepts) {
85 - if (c==null) continue;
86 -
87 - ConceptFeatures<C, K> f = cache.get(c.getId());
88 - if (f!=null) {
89 - features.put(c.getId(), f);
90 - continue;
91 - } else {
92 - todo.add(c);
93 - }
94 - }
95 -
96 - if (!todo.isEmpty()) {
97 - Map<Integer, ConceptFeatures<C, K>> parentFeatures = parent.getFeatures(todo);
98 - features.putAll(parentFeatures);
99 - }
100 -
101 - cache.putAll(features);
102 - return features;
103 - }
104 -
105 - public FeatureFetcher<C, K> getParent() {
106 - return parent;
107 - }
108 -
109 - public void setParent(FeatureFetcher<C, K> parent) {
110 - if (parent == null) throw new NullPointerException();
111 - if (parent == this) throw new IllegalArgumentException("can't be my own parent");
112 - //TODO: prevent cycles
113 -
114 - this.parent = parent;
115 - }
116 -
117 - public void clear() {
118 - cache.clear();
119 - }
120 -
121 - public boolean getFeaturesAreNormalized() {
122 - return parent.getFeaturesAreNormalized();
123 - }
124 -
125 -}
Index: trunk/WikiWord/WikiWord/src/main/java/de/brightbyte/wikiword/disambig/CachingPropertyFetcher.java
@@ -0,0 +1,61 @@
 2+package de.brightbyte.wikiword.disambig;
 3+
 4+import java.util.ArrayList;
 5+import java.util.Collection;
 6+import java.util.HashMap;
 7+import java.util.List;
 8+import java.util.Map;
 9+
 10+import de.brightbyte.data.MRUHashMap;
 11+import de.brightbyte.util.PersistenceException;
 12+import de.brightbyte.wikiword.model.ConceptProperties;
 13+import de.brightbyte.wikiword.model.WikiWordConcept;
 14+
 15+public class CachingPropertyFetcher<C extends WikiWordConcept> implements PropertyFetcher<C> {
 16+
 17+ protected PropertyFetcher<C> fetcher;
 18+ protected MRUHashMap<Integer, ConceptProperties<C>> cache;
 19+
 20+ public CachingPropertyFetcher(PropertyFetcher<C> fetcher, int capacity) {
 21+ this.fetcher = fetcher;
 22+ this.cache = new MRUHashMap<Integer, ConceptProperties<C>>(capacity);
 23+ }
 24+
 25+ public ConceptProperties<C> getProperties(C c)
 26+ throws PersistenceException {
 27+
 28+ ConceptProperties<C> f = cache.get(c.getId());
 29+ if (f!=null) return f;
 30+
 31+ f = fetcher.getProperties(c);
 32+ cache.put(c.getId(), f);
 33+
 34+ return f;
 35+ }
 36+
 37+ public Map<Integer, ConceptProperties<C>> getProperties(Collection<? extends C> concepts) throws PersistenceException {
 38+ Map<Integer, ConceptProperties<C>> features = new HashMap<Integer, ConceptProperties<C>> ();
 39+ List<C> todo = new ArrayList<C>(concepts.size());
 40+
 41+ for (C c: concepts) {
 42+ if (c==null) continue;
 43+
 44+ ConceptProperties<C> f = cache.get(c.getId());
 45+ if (f!=null) {
 46+ features.put(c.getId(), f);
 47+ continue;
 48+ } else {
 49+ todo.add(c);
 50+ }
 51+ }
 52+
 53+ if (!todo.isEmpty()) {
 54+ Map<Integer, ConceptProperties<C>> parentProperties = fetcher.getProperties(todo);
 55+ features.putAll(parentProperties);
 56+ }
 57+
 58+ cache.putAll(features);
 59+ return features;
 60+ }
 61+
 62+}
Index: trunk/WikiWord/WikiWord/src/main/java/de/brightbyte/wikiword/disambig/StoredPropertyFetcher.java
@@ -0,0 +1,58 @@
 2+package de.brightbyte.wikiword.disambig;
 3+
 4+import java.util.Collection;
 5+import java.util.Map;
 6+
 7+import de.brightbyte.io.Output;
 8+import de.brightbyte.util.PersistenceException;
 9+import de.brightbyte.wikiword.model.ConceptProperties;
 10+import de.brightbyte.wikiword.model.WikiWordConcept;
 11+import de.brightbyte.wikiword.store.PropertyStore;
 12+
 13+public class StoredPropertyFetcher<C extends WikiWordConcept> implements PropertyFetcher<C> {
 14+ protected PropertyStore<C> store;
 15+ protected Output trace;
 16+ private Collection<String> properties;
 17+
 18+ public StoredPropertyFetcher(PropertyStore<C> store, Collection<String> properties) {
 19+ if (store==null) throw new NullPointerException();
 20+
 21+ this.store = store;
 22+ this.properties = properties;
 23+ }
 24+
 25+ public ConceptProperties<C> getProperties(C c) throws PersistenceException {
 26+ trace("fetching properties for "+c);
 27+
 28+ if (properties == null) return store.getConceptProperties(c.getId());
 29+ else return store.getConceptProperties(c.getId(), properties);
 30+ }
 31+
 32+ public Map<Integer, ConceptProperties<C>> getProperties(Collection<? extends C> concepts) throws PersistenceException {
 33+ trace("fetching properties for "+concepts.size()+" concepts");
 34+
 35+ int[] ids = new int[concepts.size()];
 36+ int i = 0;
 37+ for (C c: concepts) ids[i++] = c.getId();
 38+
 39+ if (properties == null) return store.getConceptsProperties(ids);
 40+ else return store.getConceptsProperties(ids, properties);
 41+ }
 42+
 43+ public Output getTrace() {
 44+ return trace;
 45+ }
 46+
 47+ public void setTrace(Output trace) {
 48+ this.trace = trace;
 49+ }
 50+
 51+ protected void trace(String msg) {
 52+ if (trace!=null) trace.println(msg);
 53+ }
 54+
 55+ public boolean getPropertiesAreNormalized() {
 56+ return true;
 57+ }
 58+
 59+}
Index: trunk/WikiWord/WikiWord/src/main/java/de/brightbyte/wikiword/disambig/CoherenceDisambiguator.java
@@ -66,7 +66,7 @@
6767 //protected double popularityBias = 0.2; //FIXME: magic number. should "somehow" match popularityNormalizer and similarityNormalizer
6868 //protected double weightBias = 0.5; //FIXME: magic number. should "somehow" match popularityNormalizer
6969
70 - protected FeatureCache.Manager<LocalConcept, Integer> featureCacheManager;
 70+ protected FeatureFetcher<LocalConcept, Integer> featureFetcher;
7171
7272 protected Similarity<LabeledVector<Integer>> similarityMeasure;
7373 protected Measure<WikiWordConcept> popularityMeasure;
@@ -90,32 +90,28 @@
9191 protected Functor2.Double weightCombiner = ProductCombiner.instance;
9292 protected Functor.Double weightBooster = SquareBooster.instance;
9393
94 - public CoherenceDisambiguator(MeaningFetcher<LocalConcept> meaningFetcher, FeatureFetcher<LocalConcept, Integer> featureFetcher, int cacheDepth) {
95 - this(meaningFetcher, featureFetcher, cacheDepth, null, null);
 94+ public CoherenceDisambiguator(MeaningFetcher<LocalConcept> meaningFetcher, FeatureFetcher<LocalConcept, Integer> featureFetcher, int cacheCapacity) {
 95+ this(meaningFetcher, featureFetcher, cacheCapacity, null, null);
9696 }
9797
98 - public CoherenceDisambiguator(MeaningFetcher<LocalConcept> meaningFetcher, FeatureFetcher<LocalConcept, Integer> featureFetcher, int cacheDepth, Measure<WikiWordConcept> popularityMeasure, Similarity<LabeledVector<Integer>> sim) {
99 - this( new MeaningCache.Manager<LocalConcept>(meaningFetcher, cacheDepth),
100 - new FeatureCache.Manager<LocalConcept, Integer>(featureFetcher, cacheDepth),
101 - popularityMeasure, sim );
102 - }
103 -
104 - public CoherenceDisambiguator(MeaningCache.Manager<LocalConcept> meaningCacheManager, FeatureCache.Manager<LocalConcept, Integer> featureCacheManager, Measure<WikiWordConcept> popularityMeasure, Similarity<LabeledVector<Integer>> sim) {
105 - super(meaningCacheManager);
 98+ public CoherenceDisambiguator(MeaningFetcher<LocalConcept> meaningFetcher, FeatureFetcher<LocalConcept, Integer> featureFetcher, int cacheCapacity, Measure<WikiWordConcept> popularityMeasure, Similarity<LabeledVector<Integer>> sim) {
 99+ super(meaningFetcher, cacheCapacity);
106100
107101 if (popularityMeasure==null) popularityMeasure = WikiWordConcept.theCardinality;
108 - if (sim==null) sim = featureCacheManager.getFeaturesAreNormalized() ? ScalarVectorSimilarity.<Integer>getInstance() : CosineVectorSimilarity.<Integer>getInstance(); //if pre-normalized, use scalar to calc cosin
109 - if (featureCacheManager==null) throw new NullPointerException();
 102+ if (sim==null) sim = featureFetcher.getFeaturesAreNormalized() ? ScalarVectorSimilarity.<Integer>getInstance() : CosineVectorSimilarity.<Integer>getInstance(); //if pre-normalized, use scalar to calc cosin
 103+ if (featureFetcher==null) throw new NullPointerException();
110104
111 - this.featureCacheManager = featureCacheManager;
112 - this.popularityDisambiguator = new PopularityDisambiguator(meaningCacheManager, popularityMeasure);
 105+ if (cacheCapacity>0) featureFetcher = new CachingFeatureFetcher<LocalConcept, Integer>(featureFetcher, cacheCapacity);
113106
 107+ this.featureFetcher = featureFetcher;
 108+ this.popularityDisambiguator = new PopularityDisambiguator(getMeaningFetcher(), 0, popularityMeasure);
 109+
114110 this.setPopularityMeasure(popularityMeasure);
115111 this.setSimilarityMeasure(sim);
116112 }
117113
118 - public FeatureCache.Manager<LocalConcept, Integer> getFeatureCacheManager() {
119 - return featureCacheManager;
 114+ public FeatureFetcher<LocalConcept, Integer> getFeatureFetcher() {
 115+ return featureFetcher;
120116 }
121117
122118
@@ -172,7 +168,7 @@
173169 }
174170
175171 public void setFeatureFetcher(FeatureFetcher<LocalConcept, Integer> featureFetcher) {
176 - this.featureCacheManager = new FeatureCache.Manager<LocalConcept, Integer>(featureFetcher, 10); //FIXME: depth
 172+ this.featureFetcher = featureFetcher;
177173 }
178174
179175 public Similarity<LabeledVector<Integer>> getSimilarityMeasure() {
@@ -210,8 +206,6 @@
211207 }
212208
213209 protected FeatureFetcher<LocalConcept, Integer> getFeatureCache(Map<? extends TermReference, List<? extends LocalConcept>> meanings, Collection<? extends LocalConcept> context) throws PersistenceException {
214 - FeatureFetcher<LocalConcept, Integer> features = featureCacheManager.newCache();
215 -
216210 //NOTE: pre-fetch all features in one go
217211 List<LocalConcept> concepts = new ArrayList<LocalConcept>(meanings.size()*10);
218212 for (List<? extends LocalConcept> m: meanings.values()) {
@@ -219,9 +213,9 @@
220214 }
221215
222216 if (context!=null) concepts.addAll(context);
223 - features.getFeatures(concepts);
 217+ featureFetcher.getFeatures(concepts);
224218
225 - return features;
 219+ return featureFetcher;
226220 }
227221
228222 /* (non-Javadoc)
Index: trunk/WikiWord/WikiWord/src/main/java/de/brightbyte/wikiword/disambig/CachingFeatureFetcher.java
@@ -0,0 +1,65 @@
 2+package de.brightbyte.wikiword.disambig;
 3+
 4+import java.util.ArrayList;
 5+import java.util.Collection;
 6+import java.util.HashMap;
 7+import java.util.List;
 8+import java.util.Map;
 9+
 10+import de.brightbyte.data.MRUHashMap;
 11+import de.brightbyte.util.PersistenceException;
 12+import de.brightbyte.wikiword.model.ConceptFeatures;
 13+import de.brightbyte.wikiword.model.WikiWordConcept;
 14+
 15+public class CachingFeatureFetcher<C extends WikiWordConcept, K> implements FeatureFetcher<C, K> {
 16+
 17+ protected FeatureFetcher<C, K> fetcher;
 18+ protected MRUHashMap<Integer, ConceptFeatures<C, K>> cache;
 19+
 20+ public CachingFeatureFetcher(FeatureFetcher<C, K> fetcher, int capacity) {
 21+ this.fetcher = fetcher;
 22+ this.cache = new MRUHashMap<Integer, ConceptFeatures<C, K>>(capacity);
 23+ }
 24+
 25+ public boolean getFeaturesAreNormalized() {
 26+ return fetcher.getFeaturesAreNormalized();
 27+ }
 28+
 29+ public ConceptFeatures<C, K> getFeatures(C c)
 30+ throws PersistenceException {
 31+
 32+ ConceptFeatures<C, K> f = cache.get(c.getId());
 33+ if (f!=null) return f;
 34+
 35+ f = fetcher.getFeatures(c);
 36+ cache.put(c.getId(), f);
 37+
 38+ return f;
 39+ }
 40+
 41+ public Map<Integer, ConceptFeatures<C, K>> getFeatures(Collection<? extends C> concepts) throws PersistenceException {
 42+ Map<Integer, ConceptFeatures<C, K>> features = new HashMap<Integer, ConceptFeatures<C, K>> ();
 43+ List<C> todo = new ArrayList<C>(concepts.size());
 44+
 45+ for (C c: concepts) {
 46+ if (c==null) continue;
 47+
 48+ ConceptFeatures<C, K> f = cache.get(c.getId());
 49+ if (f!=null) {
 50+ features.put(c.getId(), f);
 51+ continue;
 52+ } else {
 53+ todo.add(c);
 54+ }
 55+ }
 56+
 57+ if (!todo.isEmpty()) {
 58+ Map<Integer, ConceptFeatures<C, K>> parentFeatures = fetcher.getFeatures(todo);
 59+ features.putAll(parentFeatures);
 60+ }
 61+
 62+ cache.putAll(features);
 63+ return features;
 64+ }
 65+
 66+}
Property changes on: trunk/WikiWord/WikiWord/src/main/java/de/brightbyte/wikiword/disambig/CachingFeatureFetcher.java
___________________________________________________________________
Name: svn:mergeinfo
167 +
Index: trunk/WikiWord/WikiWord/src/main/java/de/brightbyte/wikiword/disambig/AbstractDisambiguator.java
@@ -10,7 +10,6 @@
1111
1212 import de.brightbyte.io.Output;
1313 import de.brightbyte.util.PersistenceException;
14 -import de.brightbyte.wikiword.disambig.MeaningCache.Manager;
1514 import de.brightbyte.wikiword.model.PhraseNode;
1615 import de.brightbyte.wikiword.model.TermListNode;
1716 import de.brightbyte.wikiword.model.TermReference;
@@ -58,24 +57,22 @@
5958 }
6059 }
6160
62 - private MeaningCache.Manager<C> meaningCacheManager;
 61+ private MeaningFetcher<C> meaningFetcher;
6362
6463 private Output trace;
6564
6665 private Map<String, C> meaningOverrides;
6766
68 - public AbstractDisambiguator(MeaningFetcher<? extends C> meaningFetcher, int cacheDepth) {
69 - this(new MeaningCache.Manager<C>(meaningFetcher, cacheDepth));
 67+ public AbstractDisambiguator(MeaningFetcher<C> meaningFetcher, int cacheCapacity) {
 68+ if (meaningFetcher==null) throw new NullPointerException();
 69+
 70+ if (cacheCapacity>0) meaningFetcher = new CachingMeaningFetcher<C>(meaningFetcher, cacheCapacity);
 71+ this.meaningFetcher = meaningFetcher;
7072 }
7173
72 - public AbstractDisambiguator(MeaningCache.Manager<C> meaningCacheManager) {
73 - if (meaningCacheManager==null) throw new NullPointerException();
74 - this.meaningCacheManager = meaningCacheManager;
 74+ public MeaningFetcher<C> getMeaningFetcher() {
 75+ return meaningFetcher;
7576 }
76 -
77 - public MeaningCache.Manager<C> getMeaningCacheManager() {
78 - return meaningCacheManager;
79 - }
8077
8178 public void setMeaningOverrides(Map<String, C> overrideMap) {
8279 this.meaningOverrides = overrideMap;
@@ -169,8 +166,7 @@
170167 }
171168 }
172169
173 - MeaningCache<C> mcache = meaningCacheManager.newCache();
174 - Map<X, List<? extends C>> meanings = mcache.getMeanings(todo);
 170+ Map<X, List<? extends C>> meanings = meaningFetcher.getMeanings(todo);
175171
176172 if (meaningOverrides!=null && todo.size()!=terms.size()) {
177173 for (X t: terms) {
Index: trunk/WikiWord/WikiWord/src/main/java/de/brightbyte/wikiword/disambig/SlidingCoherenceDisambiguator.java
@@ -10,7 +10,6 @@
1111 import de.brightbyte.data.LabeledMatrix;
1212 import de.brightbyte.data.LabeledVector;
1313 import de.brightbyte.data.MapLabeledMatrix;
14 -import de.brightbyte.data.MapLabeledVector;
1514 import de.brightbyte.data.measure.Measure;
1615 import de.brightbyte.data.measure.Similarity;
1716 import de.brightbyte.util.PersistenceException;
@@ -25,18 +24,12 @@
2625 protected int window;
2726 protected int initialWindow;
2827
29 - public SlidingCoherenceDisambiguator(MeaningFetcher<LocalConcept> meaningFetcher, FeatureFetcher<LocalConcept, Integer> featureFetcher, int cacheDepth) {
30 - this(meaningFetcher, featureFetcher, cacheDepth, null, null, 5, 5);
 28+ public SlidingCoherenceDisambiguator(MeaningFetcher<LocalConcept> meaningFetcher, FeatureFetcher<LocalConcept, Integer> featureFetcher, int cacheCapacity) {
 29+ this(meaningFetcher, featureFetcher, cacheCapacity, null, null, 5, 5);
3130 }
3231
33 - public SlidingCoherenceDisambiguator(MeaningFetcher<LocalConcept> meaningFetcher, FeatureFetcher<LocalConcept, Integer> featureFetcher, int cacheDepth, Measure<WikiWordConcept> popularityMeasure, Similarity<LabeledVector<Integer>> sim, int window, int initialWindow) {
34 - this(new MeaningCache.Manager<LocalConcept>(meaningFetcher, cacheDepth),
35 - new FeatureCache.Manager<LocalConcept, Integer>(featureFetcher, cacheDepth),
36 - popularityMeasure, sim, window, initialWindow);
37 - }
38 -
39 - public SlidingCoherenceDisambiguator(MeaningCache.Manager<LocalConcept> meaningCacheManager, FeatureCache.Manager<LocalConcept, Integer> featureCacheManager, Measure<WikiWordConcept> popularityMeasure, Similarity<LabeledVector<Integer>> sim, int window, int initialWindow) {
40 - super(meaningCacheManager, featureCacheManager, popularityMeasure, sim);
 32+ public SlidingCoherenceDisambiguator(MeaningFetcher<LocalConcept> meaningFetcher, FeatureFetcher<LocalConcept, Integer> featureFetcher, int cacheCapacity, Measure<WikiWordConcept> popularityMeasure, Similarity<LabeledVector<Integer>> sim, int window, int initialWindow) {
 33+ super(meaningFetcher, featureFetcher, cacheCapacity, popularityMeasure, sim);
4134
4235 this.window = window;
4336 this.initialWindow = initialWindow;
Index: trunk/WikiWord/WikiWord/src/main/java/de/brightbyte/wikiword/disambig/PopularityDisambiguator.java
@@ -23,16 +23,12 @@
2424 protected Functor.Double weightBooster = SquareBooster.instance;
2525 protected Functor2.Double weigthCombiner = new ProductCombiner(); //NOTE: pop and weight are not in the same scale.
2626
27 - public PopularityDisambiguator(MeaningFetcher<LocalConcept> meaningFetcher, int cacheDepth) {
28 - this(meaningFetcher, cacheDepth, WikiWordConcept.theCardinality);
 27+ public PopularityDisambiguator(MeaningFetcher<LocalConcept> meaningFetcher, int cacheCapacity) {
 28+ this(meaningFetcher, cacheCapacity, WikiWordConcept.theCardinality);
2929 }
3030
31 - public PopularityDisambiguator(MeaningFetcher<LocalConcept> meaningFetcher, int cacheDepth, Measure<WikiWordConcept> popularityMeasure) {
32 - this(new MeaningCache.Manager<LocalConcept>(meaningFetcher, cacheDepth), popularityMeasure);
33 - }
34 -
35 - public PopularityDisambiguator(MeaningCache.Manager<LocalConcept> meaningCacheManager, Measure<WikiWordConcept> popularityMeasure) {
36 - super(meaningCacheManager);
 31+ public PopularityDisambiguator(MeaningFetcher<LocalConcept> meaningFetcher, int cacheCapacity, Measure<WikiWordConcept> popularityMeasure) {
 32+ super(meaningFetcher, cacheCapacity);
3733
3834 this.setPopularityMeasure(popularityMeasure);
3935 }
Index: trunk/WikiWord/WikiWord/src/main/java/de/brightbyte/wikiword/disambig/PropertyFetcher.java
@@ -0,0 +1,13 @@
 2+package de.brightbyte.wikiword.disambig;
 3+
 4+import java.util.Collection;
 5+import java.util.Map;
 6+
 7+import de.brightbyte.util.PersistenceException;
 8+import de.brightbyte.wikiword.model.ConceptProperties;
 9+import de.brightbyte.wikiword.model.WikiWordConcept;
 10+
 11+public interface PropertyFetcher<C extends WikiWordConcept> {
 12+ public ConceptProperties<C> getProperties(C c) throws PersistenceException;
 13+ public Map<Integer, ConceptProperties<C>> getProperties(Collection<? extends C> c) throws PersistenceException;
 14+}
Index: trunk/WikiWord/WikiWord/src/main/java/de/brightbyte/wikiword/disambig/CachingMeaningFetcher.java
@@ -0,0 +1,66 @@
 2+package de.brightbyte.wikiword.disambig;
 3+
 4+import java.util.ArrayList;
 5+import java.util.Collection;
 6+import java.util.Collections;
 7+import java.util.HashMap;
 8+import java.util.List;
 9+import java.util.Map;
 10+
 11+import de.brightbyte.data.MRUHashMap;
 12+import de.brightbyte.util.PersistenceException;
 13+import de.brightbyte.wikiword.model.TermReference;
 14+import de.brightbyte.wikiword.model.WikiWordConcept;
 15+
 16+public class CachingMeaningFetcher<C extends WikiWordConcept> implements MeaningFetcher<C> {
 17+
 18+ protected MeaningFetcher<? extends C> fetcher;
 19+ protected MRUHashMap<String, List<? extends C>> cache;
 20+
 21+ public CachingMeaningFetcher(MeaningFetcher<? extends C> fetcher, int capacity) {
 22+ this.fetcher = fetcher;
 23+ this.cache = new MRUHashMap<String, List<? extends C>>(capacity);
 24+ }
 25+
 26+ public List<? extends C> getMeanings(String term) throws PersistenceException {
 27+ List<? extends C> meanings = cache.get(term);
 28+
 29+ if (meanings==null) {
 30+ meanings = fetcher.getMeanings(term);
 31+ cache.put(term, meanings);
 32+ }
 33+
 34+ return meanings;
 35+ }
 36+
 37+
 38+ public <X extends TermReference> Map<X, List<? extends C>> getMeanings(Collection<X> terms) throws PersistenceException {
 39+ Map<X, List<? extends C>> meanings= new HashMap<X, List<? extends C>>();
 40+ List<X> todo = new ArrayList<X>(terms.size());
 41+
 42+ for (X t: terms) {
 43+ List<? extends C> m = cache.get(t.getTerm());
 44+ if (m!=null) {
 45+ meanings.put(t, m);
 46+ cache.put(t.getTerm(), m);
 47+ continue;
 48+ } else {
 49+ todo.add(t);
 50+ }
 51+ }
 52+
 53+ if (!todo.isEmpty()) {
 54+ Map<X, List<? extends C>> parentMeanings = (Map<X, List<? extends C>>)(Object)fetcher.getMeanings(todo); //XXX: ugly cast, generics are a pain
 55+ meanings.putAll(parentMeanings);
 56+
 57+ for (X t: todo) {
 58+ List<? extends C> m = parentMeanings.get(t);
 59+ if (m==null) m = Collections.emptyList();
 60+ cache.put(t.getTerm(), m);
 61+ }
 62+ }
 63+
 64+ return meanings;
 65+ }
 66+
 67+}
Property changes on: trunk/WikiWord/WikiWord/src/main/java/de/brightbyte/wikiword/disambig/CachingMeaningFetcher.java
___________________________________________________________________
Name: svn:mergeinfo
168 +

Status & tagging log