Scored implements Serializable { @SuppressWarnings("unchecked") public static <T extends Scored> List<T> rmNormalizeScores(List<T> terms) { if(terms.isEmpty()) return terms; TDoubleArrayList values = new TDoubleArrayList(); for (T wt : terms) { values.add(wt.score); } double logSumExp = MathUtils.logSumExp(values.toArray()); ArrayList<T> newTerms = new ArrayList<T>(terms.size()); for(T wt : terms) { double posterior = wt.score - logSumExp; newTerms.add((T) wt.clone(Math.exp(posterior))); } return newTerms; } Scored(double score); abstract Scored clone(double score); @SuppressWarnings("unchecked") static List<T> rmNormalizeScores(List<T> terms); @SuppressWarnings("unchecked") static List<T> makeUniform(List<T> terms); @SuppressWarnings("unchecked") static List<T> maxMinNormalize(List<T> terms); static List<T> normalize(String method, List<T> input); public double score; static Comparator<? super Scored> byScore; }
@Test public void relevanceModelWeightingTest() { List<ScoredTerm> terms = Arrays.asList( new ScoredTerm("foo", -5.0), new ScoredTerm("bar", -6.0), new ScoredTerm("baz", -7.0) ); List<ScoredTerm> normalized = Scored.rmNormalizeScores(terms); assertEquals(0.665, normalized.get(0).score, 0.01); assertEquals(0.245, normalized.get(1).score, 0.01); assertEquals(0.090, normalized.get(2).score, 0.01); }