为了验证社区的优选帖子算法而写的测试类,实现了下 Comparable 接口,算是学习了。Reddit 这个算法是 D 大借鉴的,其中的道理完全看不懂,所谓的睁眼瞎就是这个样子了……
package org.b3log.symphony.util;
import org.testng.annotations.Test;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;
/**
* Created by Zephyr on 2017/2/18. */
public class redditScoreTest {
@Test
public void redditScore(){
List votes=new ArrayList();
Random random=new Random();
for(int i=0;i<100;i++){
int up=random.nextInt(100);
int down=random.nextInt(100);
double score=redditCommentScore(up,down);
votes.add(new vote(i,up,down,score));
}
Collections.sort(votes);
System.out.println(votes.toString());
}
private double redditCommentScore(final int ups, final int downs) {
final int n = ups + downs;
if (0 == n) {
return 0;
}
final double z = 1.281551565545; // 1.0: 85%, 1.6: 95%, 1.281551565545: 80%
final double p = (double) ups / n;
return (p + z * z / (2 * n) - z * Math.sqrt((p * (1 - p) + z * z / (4 * n)) / n)) / (1 + z * z / n);
}
}
class vote implements Comparable{
int id;
int up;
int down;
double score;
public vote(){
}
public vote(int id,int up,int down,double score){
this.id=id;
this.up=up;
this.down=down;
this.score=score;
}
public int getUp() {
return up;
}
public void setUp(int up) {
this.up = up;
}
public int getDown() {
return down;
}
public void setDown(int down) {
this.down = down;
}
public double getScore() {
return score;
}
public void setScore(double score) {
this.score = score;
}
@Override
public int compareTo(vote v) {
if(this.score>v.score){
return -1;
}else if(this.scorescore){
return 1;
}else{
return 0;
}
}
@Override
public String toString(){
return this.id+" : "+this.up+" , "+this.down+" , "+this.score+"\n";
}
}
欢迎来到这里!
我们正在构建一个小众社区,大家在这里相互信任,以平等 • 自由 • 奔放的价值观进行分享交流。最终,希望大家能够找到与自己志同道合的伙伴,共同成长。
注册 关于