Created
January 15, 2014 08:29
-
-
Save EdwardDrapkin/8432691 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private static <E> Queue<WItem<E>> _select(Map<E, Double> docWeights, int numResults, Random rand) { | |
PriorityQueue<WItem<E>> resultQueue = new PriorityQueue<WItem<E>>(numResults, new WComparator<E>()); | |
for(Entry<E, Double> item : docWeights.entrySet()) { | |
WItem<E> w = new WItem<E>(); | |
w.setItem(item.getKey()); | |
double score = item.getValue(); | |
double rando = rand.nextDouble(); | |
w.setWeight(Math.pow(rando, 1/score)); | |
if(resultQueue.size() < numResults) { | |
resultQueue.add(w); | |
} else { | |
WItem<E> peek = resultQueue.peek(); | |
if(peek.getWeight() < w.getWeight()) { | |
resultQueue.poll(); | |
resultQueue.add(w); | |
} | |
} | |
} | |
return resultQueue; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment