-
-
Save symisc/29de31f8f954f54c63c044f3c4b656f8 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
////////////////////////////////// | |
// Reddit "hot" story algorithm // | |
////////////////////////////////// | |
function hot($ups, $downs, $date) | |
{ | |
if (is_string($date)) $date = strtotime($date); | |
$s = $ups - $downs; | |
$order = log10(max(abs($s), 1)); | |
if ($s > 0) | |
$sign = 1; | |
elseif ($s < 0) | |
$sign = -1; | |
else | |
$sign = 0; | |
$seconds = $date - 1134028003; | |
return round($sign * $order + $seconds / 45000, 7); | |
} | |
///////////////////////////////////// | |
// Reddit "Best" comment algorithm // | |
///////////////////////////////////// | |
function confidence($ups, $downs) | |
{ | |
$n = $ups + $downs; | |
if ($n == 0) return 0; | |
$z = 1.281551565545; // 80% confidence | |
$phat = $ups / $n; | |
$left = $phat + 1/(2*$n)*$z*$z; | |
$right = $z*sqrt($phat*(1-$phat)/$n + $z*$z/(4*$n*$n)); | |
$under = 1+1/$n*$z*$z; | |
return ($left - $right) / $under; | |
} | |
////////////////////////////////////////////// | |
// Hacker News front page ranking algorithm // | |
////////////////////////////////////////////// | |
function calculateScore($votes, $hourAge, $gravity = 1.8) | |
{ | |
return ($votes-1) / pow(($hourAge+2), $gravity); | |
} | |
////////////////////////////////////// | |
// Zite "Popular on Zite" algorithm // | |
////////////////////////////////////// | |
function popular($ups, $downs, $hourAge, $clicks) | |
{ | |
$averageRating = confidence($ups, $downs); | |
$interest = log10((1+$clicks) / (2*$hourAge/6.0)); | |
return $averageRating * $interest; | |
} | |
// @see http://stackoverflow.com/questions/11653545/hot-content-algorithm-score-with-time-decay | |
function decay($score, $weekNo) | |
{ | |
$score = 30; | |
$baseScore = log10(max($score,1)); | |
if ($weekNo > 1) { | |
$x = $weekNo - 1; | |
$baseScore = $baseScore * exp(-8*$x*$x); | |
} | |
return $baseScore; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment