Skip to content

Instantly share code, notes, and snippets.

@EdwardDrapkin
Created January 15, 2014 08:29
Show Gist options
  • Save EdwardDrapkin/8432691 to your computer and use it in GitHub Desktop.
Save EdwardDrapkin/8432691 to your computer and use it in GitHub Desktop.
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