PHP reddit的排名算法 - 计算排名 [英] PHP Reddit Ranking Algorithm - Calculate Rank

查看:471
本文介绍了PHP reddit的排名算法 - 计算排名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我工作的一个讨论板,这是根据它的辣味/等级(如书签交易)列出的所有主题。于是我把reddits算法,开始尝试。我用这个例子:的http://blog.sodhanalibrary.com/2014/04/reddit-ranking-algorithm-implementation.html

I'm working on a discussion board, which is listing all topics according on it's hotness/rank (like reddit). So i took reddits algorithm and started trying. i used this example: http://blog.sodhanalibrary.com/2014/04/reddit-ranking-algorithm-implementation.html

function score($ups,$downs){
    return $ups - $downs;
}

function epoch_seconds($timestamp){
    $epoch = new DateTime("1970-01-01 00:00:00");
    $unix = new DateTime($timestamp);
    $td = $epoch->diff($unix);

    $days = $td->format('%a');
    $hours = $td->format('%h');
    $minutes = $td->format('%i');
    $seconds = $td->format('%s');
    $age = ($days * 86400) + ($hours * 3600) + ($minutes * 60) + $seconds;

    return $age;
}

function calculateRank($ups,$downs,$date){
    $s = score($ups,$downs);
    $order = log10(max(abs($s), 1), 10); 

    if($s > 0) {
        $sign = 1;
    } elseif($s < 0) {
        $sign = -1;
    } else {
        $sign = 0;
    }

    $seconds = epoch_seconds($date) - 1134028003;

    return round($order + (($sign * $seconds)/45000), 7);
}

例如:

echo calculateRank(1,0,"2015-02-14 12:00:00"); // = 6441.9377111

我不明白是什么,是一个事实,即如果分数(upvotes和downvotes之差)为0,则等级为0。这将意味着,与+ 1 / -1一个完全新的文章会排在佳境。

What i do not understand, is the fact, that if the score (the difference between upvotes and downvotes) is 0, then the rank is 0. This would mean, that a completely new article with +1/-1 would be ranked in nirvana.

echo calculateRank(1,1,"2015-02-14 12:00:00"); // = 0

此外,如果得分为负的等级为负。这意味着有一个+ 1 / -2一个完全新的条款将被排在更加遥远然后佳境。

Also, if the score is negative the rank is negative. Which means that a completely new article with a +1/-2 would be ranked even further away then nirvana.

echo calculateRank(1,2,"2015-02-14 12:00:00"); // = -6441.9377111

SELECT查询会是这个样子:

The Select Query would look something like this:

SELECT * FROM articles ORDER BY rank DESC 

根据结果我发现你,这将意味着,一个10岁的物品以积极的分数(例如:1 upvote / 0 downvotes),将排名较高,则每一篇文章,得分为0或负得分,无论是日期。这不可能是正确的,并混淆了我。

According to the results i showed you, that would mean that an 10 year old article with a positive score (eg: 1 upvote / 0 downvotes), would be ranked higher, then EVERY article with a score of 0 or a negative score, no matter of the date. That cannot be right and confuses me.

我所寻找的是类似的东西。我已经摆脱了零队伍由不容许得分为0的但是,负分。(如:0 upvotes / 2 downvotes)应降低其反转的成绩,而不是

What i'm looking for is something similar. I already got rid of zero-ranks by do not allowing a score of 0. However, negative scores (eg: 0 upvotes / 2 downvotes) should lower the score instead of inverting it.

任何帮助是非常AP preciated! 谢谢你。

Any help is highly appreciated! Thanks.

推荐答案

我调整了算法,以我的需求。 我想出了以下内容:

I adjusted the algorithm to my needs. I came up with the following:

if($score >= 0) {
    $sign = 1;
} elseif($score < 0) {
    $sign = -1;
}
return round( ($sign * $order) + ($seconds / 45000) , 7);

这样的文章有一个负分只会降低职级而不是反其道而行。 (处罚为-1例如分数不应该是:去天堂)

This way articles with a negative score will just lower the rank instead to invert it. (the punishment for a score of -1 for example shouldn't be: "go to nirvana!")

这篇关于PHP reddit的排名算法 - 计算排名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆