r78326 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r78325‎ | r78326 | r78327 >
Date:20:26, 13 December 2010
Author:daniel
Status:deferred
Tags:
Comment:
add well-known meaning override to Disambiguator interface; reworked generic type params for Disambiguator interface
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/CoherenceDisambiguator.java (modified) (history)
  • /trunk/WikiWord/WikiWord/src/main/java/de/brightbyte/wikiword/disambig/Disambiguator.java (modified) (history)
  • /trunk/WikiWord/WikiWord/src/main/java/de/brightbyte/wikiword/disambig/ForwardPopularityDisambiguator.java (modified) (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/SlidingCoherenceDisambiguator.java (modified) (history)
  • /trunk/WikiWord/WikiWord/src/main/java/de/brightbyte/wikiword/disambig/TermRelatedness.java (modified) (history)
  • /trunk/WikiWord/WikiWord/src/main/java/de/brightbyte/wikiword/query/QueryConsole.java (modified) (history)
  • /trunk/WikiWord/WikiWord/src/test/java/de/brightbyte/wikiword/disambig/CoherenceDisambiguatorTest.java (modified) (history)
  • /trunk/WikiWord/WikiWord/src/test/java/de/brightbyte/wikiword/disambig/PopularityDisambiguatorTest.java (modified) (history)
  • /trunk/WikiWord/WikiWord/src/test/java/de/brightbyte/wikiword/disambig/SlidingCoherenceDisambiguatorTest.java (modified) (history)

Diff [purge]

Index: trunk/WikiWord/WikiWord/src/test/java/de/brightbyte/wikiword/disambig/SlidingCoherenceDisambiguatorTest.java
@@ -25,7 +25,7 @@
2626
2727 List<Term> sequence = terms("UK", "London", "Underground", "Bank");
2828
29 - Disambiguation<Term, LocalConcept> result = disambiguator.disambiguate(sequence, null);
 29+ Disambiguation<Term, LocalConcept> result = disambiguator.disambiguate(sequence, null, null);
3030
3131 Map<? extends Term, ? extends LocalConcept> meanings = result.getMeanings();
3232
@@ -48,7 +48,7 @@
4949 disambiguator.setInitialWindow(1);
5050 disambiguator.setWindow(3);
5151
52 - Disambiguation<PhraseOccurance, LocalConcept> result = disambiguator.disambiguate(set.getRootNode(), null);
 52+ Disambiguation<PhraseOccurance, LocalConcept> result = disambiguator.disambiguate(set.getRootNode(), null, null);
5353
5454 List<? extends PhraseOccurance> sequence = result.getSequence();
5555 Map<? extends PhraseOccurance, ? extends LocalConcept> meanings = result.getMeanings();
@@ -70,7 +70,7 @@
7171 disambiguator.setInitialWindow(2);
7272 disambiguator.setWindow(3);
7373
74 - result = disambiguator.disambiguate(set.getRootNode(), null);
 74+ result = disambiguator.disambiguate(set.getRootNode(), null, null);
7575
7676 sequence = result.getSequence();
7777 meanings = result.getMeanings();
@@ -92,7 +92,7 @@
9393 disambiguator.setInitialWindow(3);
9494 disambiguator.setWindow(3);
9595
96 - result = disambiguator.disambiguate(set.getRootNode(), null);
 96+ result = disambiguator.disambiguate(set.getRootNode(), null, null);
9797
9898 sequence = result.getSequence();
9999 meanings = result.getMeanings();
Index: trunk/WikiWord/WikiWord/src/test/java/de/brightbyte/wikiword/disambig/PopularityDisambiguatorTest.java
@@ -13,7 +13,6 @@
1414 import de.brightbyte.wikiword.model.PhraseOccurance;
1515 import de.brightbyte.wikiword.model.PhraseOccuranceSet;
1616 import de.brightbyte.wikiword.model.TermListNode;
17 -import de.brightbyte.wikiword.model.TermReference;
1817
1918 public class PopularityDisambiguatorTest extends DisambiguatorTestBase {
2019
@@ -22,7 +21,7 @@
2322 }
2423
2524 public void testGetTermsForList() throws PersistenceException {
26 - PopularityDisambiguator<TermReference, LocalConcept> disambiguator = new PopularityDisambiguator<TermReference, LocalConcept>(meaningFetcher, 10);
 25+ PopularityDisambiguator<LocalConcept> disambiguator = new PopularityDisambiguator<LocalConcept>(meaningFetcher, 10);
2726
2827 Term uk = new Term("UK");
2928 Term london = new Term("London");
@@ -48,7 +47,7 @@
4948
5049 //FIXME: Test case for getHorizon
5150
52 - PopularityDisambiguator<TermReference, LocalConcept> disambiguator = new PopularityDisambiguator<TermReference, LocalConcept>(meaningFetcher, 10);
 51+ PopularityDisambiguator<LocalConcept> disambiguator = new PopularityDisambiguator<LocalConcept>(meaningFetcher, 10);
5352
5453 Collection<PhraseOccurance> terms = disambiguator.getTerms(set.getRootNode(), 0);
5554 assertTrue("empty term set", sameElements( getBankAndMonumentTerms(0), terms) );
@@ -61,7 +60,7 @@
6261 }
6362
6463 public void testGetMeaningsForList() throws PersistenceException {
65 - PopularityDisambiguator<TermReference, LocalConcept> disambiguator = new PopularityDisambiguator<TermReference, LocalConcept>(meaningFetcher, 10);
 64+ PopularityDisambiguator<LocalConcept> disambiguator = new PopularityDisambiguator<LocalConcept>(meaningFetcher, 10);
6665
6766 Term uk = new Term("UK");
6867 Term london = new Term("London");
@@ -72,7 +71,7 @@
7372 terms.add(london);
7473 terms.add(underground);
7574
76 - Map<Term, List<? extends LocalConcept>> res = disambiguator.getMeanings(terms);
 75+ Map<Term, List<? extends LocalConcept>> res = disambiguator.getMeanings(terms, null);
7776
7877 assertEquals(uk.getTerm(), meanings.get(uk.getTerm()), res.get(uk));
7978 assertEquals(london.getTerm(), meanings.get(london.getTerm()), res.get(london));
@@ -80,10 +79,10 @@
8180 }
8281
8382 public void testGetMeaningsForNode() throws PersistenceException {
84 - PopularityDisambiguator<TermReference, LocalConcept> disambiguator = new PopularityDisambiguator<TermReference, LocalConcept>(meaningFetcher, 10);
 83+ PopularityDisambiguator<LocalConcept> disambiguator = new PopularityDisambiguator<LocalConcept>(meaningFetcher, 10);
8584
8685 PhraseOccuranceSet set = getBankAndMonumentPhrases();
87 - Map<PhraseOccurance, List<? extends LocalConcept>> res = disambiguator.getMeanings(set.getRootNode());
 86+ Map<PhraseOccurance, List<? extends LocalConcept>> res = disambiguator.getMeanings(set.getRootNode(), null);
8887 List<PhraseOccurance> terms = getBankAndMonumentTerms(1000);
8988
9089 for (PhraseOccurance t: terms) {
@@ -95,7 +94,7 @@
9695 }
9796
9897 public void testGetSequences() throws PersistenceException {
99 - PopularityDisambiguator<TermReference, LocalConcept> disambiguator = new PopularityDisambiguator<TermReference, LocalConcept>(meaningFetcher, 10);
 98+ PopularityDisambiguator<LocalConcept> disambiguator = new PopularityDisambiguator<LocalConcept>(meaningFetcher, 10);
10099 PhraseOccuranceSet set = getBankAndMonumentPhrases();
101100
102101 Collection<List<PhraseOccurance>> res = disambiguator.getSequences(set.getRootNode(), 1);
@@ -109,7 +108,7 @@
110109 }
111110
112111 public void testGetSequences2() throws PersistenceException {
113 - PopularityDisambiguator<TermReference, LocalConcept> disambiguator = new PopularityDisambiguator<TermReference, LocalConcept>(meaningFetcher, 10);
 112+ PopularityDisambiguator<LocalConcept> disambiguator = new PopularityDisambiguator<LocalConcept>(meaningFetcher, 10);
114113 PhraseOccuranceSet set = getMargaretOfYorkPhrases();
115114
116115 Collection<List<PhraseOccurance>> res = disambiguator.getSequences(set.getRootNode(), 3);
@@ -143,14 +142,14 @@
144143 }
145144
146145 public void testDisambiguateTerms() throws PersistenceException {
147 - PopularityDisambiguator<TermReference, LocalConcept> disambiguator = new PopularityDisambiguator<TermReference, LocalConcept>(meaningFetcher, 10);
 146+ PopularityDisambiguator<LocalConcept> disambiguator = new PopularityDisambiguator<LocalConcept>(meaningFetcher, 10);
148147
149148 Term uk = new Term("UK");
150149 Term london = new Term("London");
151150 Term underground = new Term("Underground");
152151
153152 List<Term> sequence = Arrays.asList(new Term[] {uk, london, underground});
154 - Disambiguator.Disambiguation<Term, LocalConcept> result = disambiguator.disambiguate(sequence, null);
 153+ Disambiguator.Disambiguation<Term, LocalConcept> result = disambiguator.disambiguate(sequence, null, null);
155154
156155 assertEquals("sequence", sequence, result.getSequence());
157156
@@ -162,10 +161,10 @@
163162 public void testDisambiguateNode() throws PersistenceException {
164163 PhraseOccuranceSet set = getBankAndMonumentPhrases();
165164
166 - PopularityDisambiguator<TermReference, LocalConcept> disambiguator = new PopularityDisambiguator<TermReference, LocalConcept>(meaningFetcher, 10);
 165+ PopularityDisambiguator<LocalConcept> disambiguator = new PopularityDisambiguator<LocalConcept>(meaningFetcher, 10);
167166 disambiguator.setTrace(traceOutput);
168167
169 - Disambiguation<PhraseOccurance, LocalConcept> result = disambiguator.disambiguate(set.getRootNode(), null);
 168+ Disambiguation<PhraseOccurance, LocalConcept> result = disambiguator.disambiguate(set.getRootNode(), null, null);
170169
171170 List<? extends PhraseOccurance> sequence = result.getSequence();
172171 Map<? extends PhraseOccurance, ? extends LocalConcept> meanings = result.getMeanings();
Index: trunk/WikiWord/WikiWord/src/test/java/de/brightbyte/wikiword/disambig/CoherenceDisambiguatorTest.java
@@ -155,7 +155,7 @@
156156
157157 List<Term> sequence = terms("UK", "London", "Underground", "Bank");
158158
159 - Disambiguation<Term, LocalConcept> result = disambiguator.disambiguate(sequence, null);
 159+ Disambiguation<Term, LocalConcept> result = disambiguator.disambiguate(sequence, null, null);
160160
161161 Map<? extends Term, ? extends LocalConcept> meanings = result.getMeanings();
162162
@@ -176,7 +176,7 @@
177177 CoherenceDisambiguator disambiguator = new CoherenceDisambiguator(meaningFetcher, featureFetcher, 10);
178178 disambiguator.setTrace(traceOutput);
179179
180 - Disambiguation<PhraseOccurance, LocalConcept> result = disambiguator.disambiguate(set.getRootNode(), null);
 180+ Disambiguation<PhraseOccurance, LocalConcept> result = disambiguator.disambiguate(set.getRootNode(), null, null);
181181
182182 List<? extends PhraseOccurance> sequence = result.getSequence();
183183 Map<? extends PhraseOccurance, ? extends LocalConcept> meanings = result.getMeanings();
Index: trunk/WikiWord/WikiWord/src/main/java/de/brightbyte/wikiword/disambig/TermRelatedness.java
@@ -29,14 +29,14 @@
3030 }
3131
3232 protected Similarity<WikiWordConcept> relatedness;
33 - protected Disambiguator<TermReference, C> disambig;
 33+ protected Disambiguator<C> disambig;
3434 private Collection<C> context;
3535
36 - public TermRelatedness(Disambiguator<TermReference,C> disambig) {
 36+ public TermRelatedness(Disambiguator<C> disambig) {
3737 this(disambig, null, null);
3838 }
3939
40 - public TermRelatedness(Disambiguator<TermReference, C> disambig, Similarity<WikiWordConcept> relatedness, Collection<C> context) {
 40+ public TermRelatedness(Disambiguator<C> disambig, Similarity<WikiWordConcept> relatedness, Collection<C> context) {
4141 this.relatedness = relatedness;
4242 this.disambig = disambig;
4343 this.context = context;
@@ -51,7 +51,7 @@
5252
5353 public Relatedness relatedness(String a, String b) {
5454 try {
55 - Disambiguator.Disambiguation<Term, ? extends WikiWordConcept> r = disambig.<Term>disambiguate(Term.asTerms(a, b), context);
 55+ Disambiguator.Disambiguation<Term, ? extends WikiWordConcept> r = disambig.<Term>disambiguate(Term.asTerms(a, b), null, context);
5656 if (r==null || r.getMeanings().size()!=2) return null;
5757
5858 double d;
Index: trunk/WikiWord/WikiWord/src/main/java/de/brightbyte/wikiword/disambig/CoherenceDisambiguator.java
@@ -21,12 +21,13 @@
2222 import de.brightbyte.data.measure.Similarity;
2323 import de.brightbyte.util.PersistenceException;
2424 import de.brightbyte.util.SanityException;
 25+import de.brightbyte.wikiword.disambig.Disambiguator.Disambiguation;
2526 import de.brightbyte.wikiword.model.ConceptFeatures;
2627 import de.brightbyte.wikiword.model.PhraseNode;
2728 import de.brightbyte.wikiword.model.TermReference;
2829 import de.brightbyte.wikiword.model.WikiWordConcept;
2930
30 -public class CoherenceDisambiguator<T extends TermReference, C extends WikiWordConcept> extends AbstractDisambiguator<T, C> {
 31+public class CoherenceDisambiguator<C extends WikiWordConcept> extends AbstractDisambiguator<C> {
3132
3233 public static class CoherenceDisambiguation<T extends TermReference, C extends WikiWordConcept> extends Disambiguator.Disambiguation<T, C> {
3334 protected LabeledVector<Integer> centroid;
@@ -69,7 +70,7 @@
7071
7172 protected Similarity<LabeledVector<Integer>> similarityMeasure;
7273 protected Measure<? super C> popularityMeasure;
73 - protected PopularityDisambiguator<T, C> popularityDisambiguator;
 74+ protected PopularityDisambiguator<C> popularityDisambiguator;
7475 protected Comparator<? super C> popularityComparator;
7576
7677 private Functor.Double popularityNormalizer = new Functor.Double() { //NOTE: must map [0:inf] to [0:1] and grow monotonously
@@ -211,7 +212,7 @@
212213 this.maxMeanings = maxMeanings;
213214 }
214215
215 - protected FeatureFetcher<C, Integer> getFeatureCache(Map<? extends T, List<? extends C>> meanings, Collection<? extends C> context) throws PersistenceException {
 216+ protected <X extends TermReference>FeatureFetcher<C, Integer> getFeatureCache(Map<X, List<? extends C>> meanings, Collection<? extends C> context) throws PersistenceException {
216217 //NOTE: pre-fetch all features in one go
217218 List<C> concepts = new ArrayList<C>(meanings.size()*10);
218219 for (List<? extends C> m: meanings.values()) {
@@ -227,7 +228,7 @@
228229 /* (non-Javadoc)
229230 * @see de.brightbyte.wikiword.disambig.Disambiguator#disambiguate(java.util.List)
230231 */
231 - public <X extends T>CoherenceDisambiguation<X, C> disambiguate(PhraseNode<X> root, Map<X, List<? extends C>> meanings, Collection<? extends C> context) throws PersistenceException {
 232+ public <X extends TermReference>CoherenceDisambiguation<X, C> doDisambiguate(PhraseNode<X> root, Map<X, List<? extends C>> meanings, Collection<? extends C> context) throws PersistenceException {
232233 if (meanings.isEmpty()) return new CoherenceDisambiguation<X, C>(Collections.<X, C>emptyMap(), Collections.<X>emptyList(), Collections.<Integer, ConceptFeatures<C, Integer>>emptyMap(), ConceptFeatures.newIntFeaturVector(1), 0.0, "no terms or meanings");
233234
234235 LabeledMatrix<C, C> similarities = new MapLabeledMatrix<C, C>(true);
@@ -237,14 +238,14 @@
238239 if (context!=null) sz += context.size();
239240
240241 if (sz<2) {
241 - Disambiguation<X, C> r = popularityDisambiguator.disambiguate(root, meanings, context);
 242+ Disambiguation<X, C> r = popularityDisambiguator.doDisambiguate(root, meanings, context);
242243 return getScore(r.getInterpretation(), context, similarities, features);
243244 }
244245
245246 sz = meanings.size();
246247 if (context!=null) sz += context.size();
247248 if (sz <2) {
248 - Disambiguation<X, C> r = popularityDisambiguator.disambiguate(root, meanings, context);
 249+ Disambiguation<X, C> r = popularityDisambiguator.doDisambiguate(root, meanings, context);
249250 return getScore(r.getInterpretation(), context, similarities, features);
250251 }
251252
@@ -252,14 +253,14 @@
253254 return disambiguate(sequences, root, meanings, context);
254255 }
255256
256 - protected <X extends T>CoherenceDisambiguation<X, C> disambiguate(Collection<List<X>> sequences, PhraseNode<X> root, Map<X, List<? extends C>> meanings, Collection<? extends C> context) throws PersistenceException {
 257+ protected <X extends TermReference>CoherenceDisambiguation<X, C> disambiguate(Collection<List<X>> sequences, PhraseNode<X> root, Map<X, List<? extends C>> meanings, Collection<? extends C> context) throws PersistenceException {
257258 LabeledMatrix<C, C> similarities = new MapLabeledMatrix<C, C>(true);
258259 FeatureFetcher<C, Integer> features = getFeatureCache(meanings, context);
259260
260261 return disambiguate(sequences, root, meanings, context, similarities, features);
261262 }
262263
263 - private <X extends T>CoherenceDisambiguation<X, C> disambiguate(Collection<List<X>> sequences, PhraseNode<X> root, Map<X, List<? extends C>> meanings, Collection<? extends C> context, LabeledMatrix<C, C> similarities, FeatureFetcher<C, Integer> features) throws PersistenceException {
 264+ private <X extends TermReference>CoherenceDisambiguation<X, C> disambiguate(Collection<List<X>> sequences, PhraseNode<X> root, Map<X, List<? extends C>> meanings, Collection<? extends C> context, LabeledMatrix<C, C> similarities, FeatureFetcher<C, Integer> features) throws PersistenceException {
264265
265266 pruneMeaninglessSequences( sequences, meanings );
266267
@@ -270,16 +271,16 @@
271272 return getBestInterpretation(root, meanings, context, interpretations, similarities, features);
272273 }
273274
274 - protected <X extends T>Map<X, List<? extends C>> getMeanings(Collection<X> terms) throws PersistenceException {
275 - Map<X, List<? extends C>> meanings = super.getMeanings(terms);
 275+ protected <X extends TermReference>Map<X, List<? extends C>> getMeanings(Collection<X> terms, Map<X, C> known) throws PersistenceException {
 276+ Map<X, List<? extends C>> meanings = super.getMeanings(terms, known);
276277 pruneMeanings(meanings);
277278 return meanings;
278279 }
279280
280 - protected void pruneMeanings(Map<? extends T, List<? extends C>> meanings) {
 281+ protected <X extends TermReference>void pruneMeanings(Map<X, List<? extends C>> meanings) {
281282 Iterator<?> eit = meanings.entrySet().iterator();
282283 while (eit.hasNext()) {
283 - Entry<T, List<? extends C>> e = (Entry<T, List<? extends C>>) eit.next(); //XXX: ugly cast. got confused about generics. ugh.
 284+ Entry<X, List<? extends C>> e = (Entry<X, List<? extends C>>) eit.next(); //XXX: ugly cast. got confused about generics. ugh.
284285 List<? extends C> m = e.getValue();
285286 if (m==null) continue;
286287
@@ -314,7 +315,7 @@
315316 }
316317 }
317318
318 - protected <X extends T>CoherenceDisambiguation<X, C> getBestInterpretation(PhraseNode<X> root, Map<X, List<? extends C>> meanings,
 319+ protected <X extends TermReference>CoherenceDisambiguation<X, C> getBestInterpretation(PhraseNode<X> root, Map<X, List<? extends C>> meanings,
319320 Collection<? extends C> context, Collection<Disambiguator.Interpretation<X, C>> interpretations,
320321 LabeledMatrix<C, C> similarities, FeatureFetcher<C, Integer> features) throws PersistenceException {
321322
@@ -349,7 +350,7 @@
350351 if (best==null || bestScore<minScore || Double.isNaN(bestScore)) {
351352 trace("best score is not good enough ("+bestScore+"<"+minScore+"), using popularity disambiguator.");
352353
353 - Disambiguation<X, C> p = popularityDisambiguator.disambiguate(root, meanings, context);
 354+ Disambiguation<X, C> p = popularityDisambiguator.doDisambiguate(root, meanings, context);
354355 CoherenceDisambiguation<X, C> r = getScore(p.getInterpretation(), context, similarities, features);
355356
356357 trace("best of "+interpretations.size()+" interpretations by popularity: "+r);
@@ -362,7 +363,7 @@
363364 return best;
364365 }
365366
366 - public <X extends T>Collection<Disambiguator.Interpretation<X, C>> getInterpretations(Collection<List<X>> sequences, Map<X, List<? extends C>> meanings) {
 367+ public <X extends TermReference>Collection<Disambiguator.Interpretation<X, C>> getInterpretations(Collection<List<X>> sequences, Map<X, List<? extends C>> meanings) {
367368 List<Disambiguator.Interpretation<X, C>> interpretations = new ArrayList<Disambiguator.Interpretation<X, C>>();
368369 for (List<X> sq: sequences) {
369370 if (sq.isEmpty()) continue;
@@ -373,7 +374,7 @@
374375 return interpretations;
375376 }
376377
377 - public <X extends T>Collection<Disambiguator.Interpretation<X, C>> getSequenceInterpretations(List<X> sequence, Map<X, List<? extends C>> meanings) {
 378+ public <X extends TermReference>Collection<Disambiguator.Interpretation<X, C>> getSequenceInterpretations(List<X> sequence, Map<X, List<? extends C>> meanings) {
378379 if (sequence.size()==0) {
379380 return Collections.singletonList(new Disambiguator.Interpretation<X, C>(Collections.<X, C>emptyMap(), sequence));
380381 }
@@ -573,5 +574,5 @@
574575 public boolean exploresAllSequences() {
575576 return true;
576577 }
577 -
 578+
578579 }
Index: trunk/WikiWord/WikiWord/src/main/java/de/brightbyte/wikiword/disambig/ForwardPopularityDisambiguator.java
@@ -6,14 +6,13 @@
77 import java.util.List;
88 import java.util.Map;
99
10 -import de.brightbyte.data.Functors;
1110 import de.brightbyte.data.measure.Measure;
1211 import de.brightbyte.wikiword.model.PhraseNode;
1312 import de.brightbyte.wikiword.model.TermListNode;
1413 import de.brightbyte.wikiword.model.TermReference;
1514 import de.brightbyte.wikiword.model.WikiWordConcept;
1615
17 -public class ForwardPopularityDisambiguator<T extends TermReference, C extends WikiWordConcept> extends PopularityDisambiguator<T, C> {
 16+public class ForwardPopularityDisambiguator<C extends WikiWordConcept> extends PopularityDisambiguator<C> {
1817
1918 public ForwardPopularityDisambiguator(MeaningFetcher<? extends C> meaningFetcher,
2019 int cacheCapacity) {
@@ -26,13 +25,13 @@
2726 }
2827
2928 @Override
30 - public <X extends T> Disambiguator.Disambiguation<X, C> disambiguate(List<X> sequence, Map<X, List<? extends C>> meanings, Collection<? extends C> context) {
 29+ public <X extends TermReference> Disambiguator.Disambiguation<X, C> doDisambiguate(List<X> sequence, Map<X, List<? extends C>> meanings, Collection<? extends C> context) {
3130 PhraseNode<X> root = new TermListNode<X>( sequence, 0 );
32 - return disambiguate(root, meanings, context);
 31+ return doDisambiguate(root, meanings, context);
3332 }
3433
3534 @Override
36 - public <X extends T> Disambiguator.Disambiguation<X, C> disambiguate(PhraseNode<X> root, Map<X, List<? extends C>> meanings, Collection<? extends C> context) {
 35+ public <X extends TermReference> Disambiguator.Disambiguation<X, C> doDisambiguate(PhraseNode<X> root, Map<X, List<? extends C>> meanings, Collection<? extends C> context) {
3736 Map<X, C> disambig = new HashMap<X, C>();
3837 List<X> sequence = new ArrayList<X>();
3938
Index: trunk/WikiWord/WikiWord/src/main/java/de/brightbyte/wikiword/disambig/AbstractDisambiguator.java
@@ -5,7 +5,6 @@
66 import java.util.Collections;
77 import java.util.Iterator;
88 import java.util.List;
9 -import java.util.ListIterator;
109 import java.util.Map;
1110
1211 import de.brightbyte.io.Output;
@@ -15,7 +14,7 @@
1615 import de.brightbyte.wikiword.model.TermReference;
1716 import de.brightbyte.wikiword.model.WikiWordConcept;
1817
19 -public abstract class AbstractDisambiguator<T extends TermReference, C extends WikiWordConcept> implements Disambiguator<T, C> {
 18+public abstract class AbstractDisambiguator<C extends WikiWordConcept> implements Disambiguator<C> {
2019
2120 private MeaningFetcher<? extends C> meaningFetcher;
2221
@@ -38,13 +37,13 @@
3938 this.meaningOverrides = overrideMap;
4039 }
4140
42 - protected <X extends T>PhraseNode<X> getLastNode(PhraseNode<X> root, List<X> sequence) {
 41+ protected <X extends TermReference>PhraseNode<X> getLastNode(PhraseNode<X> root, List<X> sequence) {
4342 PhraseNode<X> n = findLastNode(root, sequence);
4443 if (n==null) throw new IllegalArgumentException("sequence does not match node structure: "+sequence);
4544 return n;
4645 }
4746
48 - private <X extends T>PhraseNode<X> findLastNode(PhraseNode<X> root, List<X> sequence) {
 47+ private <X extends TermReference>PhraseNode<X> findLastNode(PhraseNode<X> root, List<X> sequence) {
4948 if (root.getTermReference().getTerm().length()>0) {
5049 X t = sequence.get(0);
5150 if (!t.getTerm().equals(root.getTermReference().getTerm())) return null;
@@ -74,19 +73,19 @@
7574 return root;
7675 }
7776
78 - protected <X extends T>Collection<X> getTerms(PhraseNode<X> root, int depth) {
 77+ protected <X extends TermReference>Collection<X> getTerms(PhraseNode<X> root, int depth) {
7978 PhraseNode.TermSetBuilder<X> builder = new PhraseNode.TermSetBuilder<X>();
8079 builder.walk(root, 0, null, depth, Double.MAX_VALUE);
8180 return builder.getTerms();
8281 }
8382
84 - protected <X extends T>Collection<List<X>> getSequences(PhraseNode<X> root, int depth) {
 83+ protected <X extends TermReference>Collection<List<X>> getSequences(PhraseNode<X> root, int depth) {
8584 PhraseNode.SequenceSetBuilder<X> builder = new PhraseNode.SequenceSetBuilder<X>();
8685 builder.walk(root, 0, null, depth, Double.MAX_VALUE);
8786 return builder.getSequences();
8887 }
8988
90 - protected <X extends T>void pruneMeaninglessSequences(Collection<List<X>> sequences, Map<X, List<? extends C>> meanings) {
 89+ protected <X extends TermReference>void pruneMeaninglessSequences(Collection<List<X>> sequences, Map<X, List<? extends C>> meanings) {
9190 Iterator<List<X>> it = sequences.iterator();
9291 outer: while ( it.hasNext() ) {
9392 List<X> seq = it.next();
@@ -101,22 +100,27 @@
102101 }
103102 }
104103
105 - protected <X extends T>Map<X, List<? extends C>> getMeanings(PhraseNode<X> root) throws PersistenceException {
 104+ protected <X extends TermReference>Map<X, List<? extends C>> getMeanings(PhraseNode<X> root, Map<X, C> known) throws PersistenceException {
106105 Collection<X> terms = getTerms(root, Integer.MAX_VALUE);
107 - return getMeanings(terms);
 106+ return getMeanings(terms, known);
108107 }
109108
110 - protected <X extends T>Map<X, List<? extends C>> getMeanings(Collection<X> terms) throws PersistenceException {
 109+ protected <X extends TermReference>Map<X, List<? extends C>> getMeanings(Collection<X> terms, Map<X, C> known) throws PersistenceException {
111110 Collection<X> todo = terms;
112111
113 - if (meaningOverrides!=null) {
 112+ if (meaningOverrides!=null || known!=null) {
114113 todo = new ArrayList<X>();
 114+
115115 for (X t: terms) {
116 - if (!meaningOverrides.containsKey(t.getTerm())) todo.add(t);
 116+ if ( ( meaningOverrides==null || !meaningOverrides.containsKey(t.getTerm()) )
 117+ && ( known == null || !known.containsKey(t.getTerm()) ) ) {
 118+ todo.add(t);
 119+ }
117120 }
118121 }
119122
120 - Map<X, List<? extends C>> meanings = (Map<X, List<? extends C>>)(Object)meaningFetcher.getMeanings(todo); //FIXME: got confused by generics :(
 123+ //FIXME: got confused by generics :(
 124+ Map<X, List<? extends C>> meanings = (Map<X, List<? extends C>>)(Object)meaningFetcher.getMeanings(todo);
121125
122126 if (meaningOverrides!=null && todo.size()!=terms.size()) {
123127 for (X t: terms) {
@@ -125,22 +129,29 @@
126130 }
127131 }
128132
 133+ if (known!=null && todo.size()!=terms.size()) {
 134+ for (X t: terms) {
 135+ C c = known.get(t.getTerm());
 136+ if (c!=null) meanings.put(t, Collections.singletonList(c));
 137+ }
 138+ }
 139+
129140 return meanings;
130141 }
131142
132 - public <X extends T>Disambiguation<X, C> disambiguate(List<X> terms, Collection<? extends C> context) throws PersistenceException {
 143+ public <X extends TermReference>Disambiguation<X, C> disambiguate(List<X> terms, Map<X, C> known, Collection<? extends C> context) throws PersistenceException {
133144 PhraseNode<X> root = new TermListNode<X>(terms, 0);
134 - Map<X, List<? extends C>> meanings = getMeanings(terms);
135 - return disambiguate(root, meanings, context);
 145+ Map<X, List<? extends C>> meanings = getMeanings(terms, known);
 146+ return doDisambiguate(root, meanings, context);
136147 }
137148
138 - public <X extends T>Disambiguation<X, C> disambiguate(PhraseNode<X> root, Collection<? extends C> context) throws PersistenceException {
 149+ public <X extends TermReference>Disambiguation<X, C> disambiguate(PhraseNode<X> root, Map<X, C> known, Collection<? extends C> context) throws PersistenceException {
139150 Collection<X> terms = getTerms(root, Integer.MAX_VALUE);
140 - Map<X, List<? extends C>> meanings = getMeanings(terms);
141 - return disambiguate(root, meanings, context);
 151+ Map<X, List<? extends C>> meanings = getMeanings(terms, known);
 152+ return doDisambiguate(root, meanings, context);
142153 }
143 -
144 - public abstract <X extends T>Disambiguation<X, C> disambiguate(PhraseNode<X> root, Map<X, List<? extends C>> meanings, Collection<? extends C> context) throws PersistenceException;
 154+
 155+ public abstract <X extends TermReference>Disambiguation<X, C> doDisambiguate(PhraseNode<X> root, Map<X, List<? extends C>> meanings, Collection<? extends C> context) throws PersistenceException;
145156
146157 public Output getTrace() {
147158 return trace;
Index: trunk/WikiWord/WikiWord/src/main/java/de/brightbyte/wikiword/disambig/SlidingCoherenceDisambiguator.java
@@ -18,7 +18,7 @@
1919 import de.brightbyte.wikiword.model.TermReference;
2020 import de.brightbyte.wikiword.model.WikiWordConcept;
2121
22 -public class SlidingCoherenceDisambiguator<T extends TermReference, C extends WikiWordConcept> extends CoherenceDisambiguator<T, C> {
 22+public class SlidingCoherenceDisambiguator<C extends WikiWordConcept> extends CoherenceDisambiguator<C> {
2323
2424 protected int window;
2525 protected int initialWindow;
@@ -34,7 +34,7 @@
3535 this.initialWindow = initialWindow;
3636 }
3737
38 - public <X extends T>Disambiguation<X, C> evalStep(List<X> baseSequence, Map<X, C> interpretation, PhraseNode<X> node,
 38+ public <X extends TermReference>Disambiguation<X, C> evalStep(List<X> baseSequence, Map<X, C> interpretation, PhraseNode<X> node,
3939 Map<X, List<? extends C>> meanings, Collection<? extends C> context,
4040 LabeledMatrix<C, C> similarities, FeatureFetcher<C, Integer> features) throws PersistenceException {
4141 X term = node.getTermReference();
@@ -51,7 +51,7 @@
5252 Disambiguation<X, C> r ;
5353
5454 if (to-from < 2) {
55 - r = popularityDisambiguator.disambiguate(frame, meanings, context);
 55+ r = popularityDisambiguator.doDisambiguate(frame, meanings, context);
5656 } else {
5757 Collection<Disambiguator.Interpretation<X, C>> interpretations = getInterpretations(frame, interpretation, meanings);
5858 r = getBestInterpretation(node, meanings, context, interpretations, similarities, features);
@@ -63,7 +63,7 @@
6464 /* (non-Javadoc)
6565 * @see de.brightbyte.wikiword.disambig.Disambiguator#disambiguate(java.util.List)
6666 */
67 - public <X extends T>CoherenceDisambiguation<X, C> disambiguate(PhraseNode<X> root, Map<X, List<? extends C>> meanings, Collection<? extends C> context) throws PersistenceException {
 67+ public <X extends TermReference>CoherenceDisambiguation<X, C> doDisambiguate(PhraseNode<X> root, Map<X, List<? extends C>> meanings, Collection<? extends C> context) throws PersistenceException {
6868 if (meanings.isEmpty()) return new CoherenceDisambiguation<X, C>(Collections.<X, C>emptyMap(), Collections.<X>emptyList(), Collections.<Integer, ConceptFeatures<C, Integer>>emptyMap(), ConceptFeatures.newIntFeaturVector(1), 0.0, "no terms or meanings");
6969
7070 int sz = meanings.size();
@@ -75,7 +75,7 @@
7676 FeatureFetcher<C, Integer> features = getFeatureCache(meanings, context);
7777
7878 if (window < 2 || sz<2) {
79 - Disambiguation<X, C> r = popularityDisambiguator.disambiguate(root, meanings, context);
 79+ Disambiguation<X, C> r = popularityDisambiguator.doDisambiguate(root, meanings, context);
8080 return getScore(r.getInterpretation(), context, similarities, features);
8181 }
8282
@@ -83,7 +83,7 @@
8484 if (context!=null) sz += context.size();
8585
8686 if (sz<2) {
87 - Disambiguation<X, C> r = popularityDisambiguator.disambiguate(root, meanings, context);
 87+ Disambiguation<X, C> r = popularityDisambiguator.doDisambiguate(root, meanings, context);
8888 return getScore(r.getInterpretation(), context, similarities, features);
8989 }
9090
@@ -134,7 +134,7 @@
135135 return getScore(new Disambiguator.Interpretation<X, C>(disambig, sequence), context, similarities, features); //FIXME: this is unnecessarily expensive, we usually don't need the scores this calculates.
136136 }
137137
138 - protected <X extends T>Collection<Disambiguator.Interpretation<X, C>> getInterpretations(List<X> frame, Map<X, ? extends C> known, Map<? extends T, List<? extends C>> meanings) {
 138+ protected <X extends TermReference>Collection<Disambiguator.Interpretation<X, C>> getInterpretations(List<X> frame, Map<X, ? extends C> known, Map<X, List<? extends C>> meanings) {
139139 //strip out all terms with no known meaning
140140 if (meanings.keySet().size() != frame.size()) {
141141 List<X> t = new ArrayList<X>(frame.size());
Index: trunk/WikiWord/WikiWord/src/main/java/de/brightbyte/wikiword/disambig/PopularityDisambiguator.java
@@ -13,7 +13,7 @@
1414 import de.brightbyte.wikiword.model.TermReference;
1515 import de.brightbyte.wikiword.model.WikiWordConcept;
1616
17 -public class PopularityDisambiguator<T extends TermReference, C extends WikiWordConcept> extends AbstractDisambiguator<T, C> {
 17+public class PopularityDisambiguator<C extends WikiWordConcept> extends AbstractDisambiguator<C> {
1818
1919 protected Measure<? super C> popularityMeasure;
2020
@@ -58,18 +58,18 @@
5959 this.weigthCombiner = weigthCombiner;
6060 }
6161
62 - public <X extends T>Disambiguation<X, C> disambiguate(PhraseNode<X> root, Map<X, List<? extends C>> meanings, Collection<? extends C> context) {
 62+ public <X extends TermReference>Disambiguation<X, C> doDisambiguate(PhraseNode<X> root, Map<X, List<? extends C>> meanings, Collection<? extends C> context) {
6363 Collection<List<X>> sequences = getSequences(root, Integer.MAX_VALUE);
6464 return disambiguate(sequences, root, meanings, context);
6565 }
6666
67 - protected <X extends T>Disambiguation<X, C> disambiguate(Collection<List<X>> sequences, PhraseNode<X> root, Map<X, List<? extends C>> meanings, Collection<? extends C> context) {
 67+ protected <X extends TermReference>Disambiguation<X, C> disambiguate(Collection<List<X>> sequences, PhraseNode<X> root, Map<X, List<? extends C>> meanings, Collection<? extends C> context) {
6868 Disambiguation<X, C> best = null;
6969
7070 pruneMeaninglessSequences( sequences, meanings );
7171
7272 for (List<X> sequence: sequences) {
73 - Disambiguation<X, C> r = disambiguate(sequence, meanings, context);
 73+ Disambiguation<X, C> r = doDisambiguate(sequence, meanings, context);
7474 trace(r.toString());
7575 if (best == null || best.getScore() < r.getScore()) {
7676 best = r;
@@ -80,7 +80,7 @@
8181 return best;
8282 }
8383
84 - protected <X extends T> C getBestMeaning(X term, Map<X, List<? extends C>> meanings, Measure<? super C> measure) {
 84+ protected <X extends TermReference> C getBestMeaning(X term, Map<X, List<? extends C>> meanings, Measure<? super C> measure) {
8585 List<? extends C> m = meanings.get(term);
8686 if (m==null || m.size()==0) return null;
8787
@@ -99,7 +99,7 @@
100100 return c;
101101 }
102102
103 - public <X extends T>Disambiguation<X, C> disambiguate(List<X> sequence, Map<X, List<? extends C>> meanings, Collection<? extends C> context) {
 103+ public <X extends TermReference>Disambiguation<X, C> doDisambiguate(List<X> sequence, Map<X, List<? extends C>> meanings, Collection<? extends C> context) {
104104 if (sequence.isEmpty() || meanings.isEmpty()) return new Disambiguator.Disambiguation<X, C>(Collections.<X, C>emptyMap(), Collections.<X>emptyList(), 0.0, "no terms or meanings");
105105
106106 Map<X, C> disambig = new HashMap<X, C>();
Index: trunk/WikiWord/WikiWord/src/main/java/de/brightbyte/wikiword/disambig/Disambiguator.java
@@ -14,7 +14,7 @@
1515 import de.brightbyte.wikiword.model.TermReference;
1616 import de.brightbyte.wikiword.model.WikiWordConcept;
1717
18 -public interface Disambiguator<T extends TermReference, C extends WikiWordConcept> {
 18+public interface Disambiguator<C extends WikiWordConcept> {
1919
2020 public static class Interpretation<T extends TermReference, C extends WikiWordConcept> {
2121 private final Map<T, C> meanings;
@@ -166,8 +166,8 @@
167167
168168 public void setTrace(Output trace);
169169
170 - public <X extends T>Disambiguation<X, C> disambiguate(List<X> terms, Collection<? extends C> context) throws PersistenceException;
171 - public <X extends T>Disambiguation<X, C> disambiguate(PhraseNode<X> root, Collection<? extends C> context) throws PersistenceException;
 170+ public <X extends TermReference>Disambiguation<X, C> disambiguate(List<X> terms, Map<X, C> known, Collection<? extends C> context) throws PersistenceException;
 171+ public <X extends TermReference>Disambiguation<X, C> disambiguate(PhraseNode<X> root, Map<X, C> known, Collection<? extends C> context) throws PersistenceException;
172172
173173 public boolean exploresAllSequences();
174174
Index: trunk/WikiWord/WikiWord/src/main/java/de/brightbyte/wikiword/query/QueryConsole.java
@@ -560,7 +560,7 @@
561561 terms.add(t2);
562562
563563 CoherenceDisambiguation<Term, WikiWordConcept> r =
564 - (CoherenceDisambiguation<Term, WikiWordConcept>) getDisambiguator().disambiguate(terms, null);
 564+ (CoherenceDisambiguation<Term, WikiWordConcept>) getDisambiguator().disambiguate(terms, null, null);
565565
566566 WikiWordConcept concept1 = (WikiWordConcept)r.getMeanings().get(t1);
567567 WikiWordConcept concept2 = (WikiWordConcept)r.getMeanings().get(t2);
@@ -581,7 +581,7 @@
582582 }
583583
584584 public void showDisambiguation(PhraseNode<? extends TermReference> root, ConsoleOutput out) throws PersistenceException {
585 - Disambiguator.Disambiguation r = getDisambiguator().disambiguate(root, null);
 585+ Disambiguator.Disambiguation r = getDisambiguator().disambiguate(root, null, null);
586586 out.writeInterpretation(r.getMeanings());
587587 }
588588

Status & tagging log