我正在开发一个小的 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
越多,实际得分下降的幅度就越大。
$alpha
和$beta
分别是帖子数和评论数的重要性因子。受制于:
0 <= alpha, beta
关于php - 算法 - 根据帖子和评论的数量计算用户评分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16744930/