算法-根据帖子和评论的数量计算用户评分


Algorithm - Calculating user rating from number of posts and comments

我正在开发一个小型PHP脚本,用于计算发布平台中用户的比例。

这个比率应该使用我准备好的这两个变量来计算:

$user_comment_count=用户的总评论计数

$user_post_count=用户的文章总数。

为了保持稳定的比例,用户每篇文章需要2条评论。因此,如果用户发布了5篇文章和10条评论,那么用户的比例将是1.00。如果有5条帖子和15条评论,则为1.50。用户可以拥有的最低比率是0.00,并且不应该对最高比率设置限制。

如何使用这两个变量在PHP中进行计算?

最明显的解决方案:

$ratio = ($user_comment_count)/(2*$user_post_count);

思考更深入:

[1]好吧,你可能想同时奖励发帖和评论。因此,这一比例需要随着帖子数量和评论数量的增加而单调上升。因此,上述解决方案不能满足这一点。

[2] 你有点希望用户每条帖子至少有2条评论,否则用户将受到惩罚。

因此,新的解决方案是:

function base_score($user_post_count, $user_comment_count) {
    return $alpha*$user_post_count + $beta*$user_comment_count;
}
function score($user_post_count, $user_comment_count) {
    if (($user_comment_count >= 2*$user_post_count) || ($user_post_count = 0)) {
        return base_score($user_post_count, $user_comment_count);
    }else {
        $deficit = $user_comment_count / (2.0*$user_post_count);
        return base_score($user_post_count, $user_comment_count)*$deficit;
    }
}

因此,2*$user_post_count中缺少的$user_comment_count越多,实际得分就会越低。

CCD_ 8和CCD_。受制于:

0 <= alpha, beta
$ratio = $user_comment_count / max(1, $user_post_count * 2);

这也会起作用,尽管第一个解决方案可能更可读:

$ratio = $user_comment_count / ($user_post_count * 2 || 1);