Skip to content

Instantly share code, notes, and snippets.

@symisc
Forked from xwlee/Sorting.php
Created July 17, 2017 00:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save symisc/29de31f8f954f54c63c044f3c4b656f8 to your computer and use it in GitHub Desktop.
Save symisc/29de31f8f954f54c63c044f3c4b656f8 to your computer and use it in GitHub Desktop.
<?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