PHP贝叶斯评级系统(基于热门投票)

时间:2012-02-11 00:54:47

标签: php

我正在使用PHP的贝叶斯评级系统,但它只有一半有效。如果大多数其他成员给予高评价等等,那么低评级就会贬值。我花了很多时间来达到这一点,但现在我完全被难倒了。

在我开始设置数据库条目之前,这只是一个用于计算数学的测试脚本(我的数学不太好)。

<?php

// Item 1 votes
$ratings[0][1] = 10;
$ratings[0][2] = 4;
$ratings[0][3] = 1;
$ratings[0][4] = 72;
$ratings[0][5] = 853;       // z0mg, lots of people think this is 5 star material!

// Item 2 votes - it's 50:50, rating should be 3
$ratings[1][1] = 1000;
$ratings[1][2] = 1;
$ratings[1][3] = 1;
$ratings[1][4] = 1;
$ratings[1][5] = 1000;

// Item 3 votes - should also be 3
$ratings[2][1] = 1000;
$ratings[2][2] = 1000;
$ratings[2][3] = 1000;
$ratings[2][4] = 1000;
$ratings[2][5] = 1000;

// Item 4 votes - obviously the best thing ever
$ratings[3][1] = 0;
$ratings[3][2] = 0;
$ratings[3][3] = 0;
$ratings[3][4] = 0;
$ratings[3][5] = 99999999999;

foreach($ratings as $rating)
{
    $total_votes = $rating[1] + $rating[2] + $rating[3] + $rating[4] + $rating[5];

    $weight[1] = $rating[1] / $total_votes;
    $weight[2] = $rating[2] / $total_votes;
    $weight[3] = $rating[3] / $total_votes;
    $weight[4] = $rating[4] / $total_votes;
    $weight[5] = $rating[5] / $total_votes;

    // 1.0 == $weight[5] + $weight[4] + $weight[3] + $weight[2] + $weight[1];

    $yay = $rating[1] * $weight[1];
    $yay += $rating[2] * $weight[2];
    $yay += $rating[3] * $weight[3];
    $yay += $rating[4] * $weight[4];
    $yay += $rating[5] * $weight[5];

    echo ($yay / $total_votes) * 5;
    echo "\n";
}

/*
    RESULTS
    4.1472951561793
    2.4925205800884
    1
    5
*/

?>

但当然,第2项和第3项的评级都应该是3.0 ......

希望有人可以提供帮助。

1 个答案:

答案 0 :(得分:1)

这不是一个真正的php问题。错误在于算法。

无论如何,改为:

$yay = 1 * $weight[1];
$yay += 2 * $weight[2];
$yay += 3 * $weight[3];
$yay += 4 * $weight[4];
$yay += 5 * $weight[5];
echo $yay

应该工作。

相关问题