2012-02-11 85 views
1

我正在使用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

問題是什麼? – rdlowrey 2012-02-11 00:57:53

+0

對不起,我編輯了代碼,使其更加清晰,但帖子失敗,我恢復到源代碼。 我編輯帖子......「但當然,項目2和3的評級應該都是3.0 ...」。所以我只想知道什麼是錯的:) (編輯) 和代碼的結果是在評論的底部 – Deji 2012-02-11 01:00:33

+0

對不起,我沒有看到jQuery,MySQL或AJAX在哪裏適合這個排名機制... – sarnold 2012-02-11 01:15:57

回答

1

這不是一個真正的PHP問題。錯誤在於算法。

不管怎麼說,更改爲:

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

應該工作。

+0

非常感謝! _THIS_COMMENT_NEEDS_TO_BE_LONGER_ – Deji 2012-02-11 11:28:12