public
Last active

Reddit Ranking Algorithms in PHP

  • Download Gist
Ranking.php
PHP
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
<?php
/**
* Ranking Library
* contains alogrithms for story ranking
* Basically a PHP Implementation of reddits algorithms
*
* @author Lucas Nolte <lnolte@i.biz>
* @since 0.1
* @package Polska Wiadomosc
* @subpackage Libraries
* @see https://github.com/reddit/reddit/blob/master/r2/r2/lib/db/_sorts.pyx
*/
class Ranking {
/**
* calculates the score for a link (upvotes - downvotes)
*
* @access private
* @since 0.1
* @param int $upvotes, int $downvotes
* @return int
*/
private function _score($upvotes = 0, $downvotes = 0) {
return $upvotes - $downvotes;
}
/**
* calculates the hotness of an article
*
* @access private
* @since 0.1
* @param int $upvotes, int $downvotes, int $posted
* @return float
*/
private function _hotness($upvotes = 0, $downvotes = 0, $posted = 0) {
$s = $this->_score($upvotes, $downvotes);
$order = log(max(abs($s), 1), 10);
if($s > 0) {
$sign = 1;
} elseif($s < 0) {
$sign = -1;
} else {
$sign = 0;
}
$seconds = $posted - 1134028003;
return round($order + (($sign * $seconds)/45000), 7);
}
/**
* confidence sort based on http://www.evanmiller.org/how-not-to-sort-by-average-rating.html
*
* @since 0.1
* @access private
* @param int $upvotes, int $downvotes
* @return double
* @see http://www.evanmiller.org/how-not-to-sort-by-average-rating.html
*/
private function _confidence($upvotes = 0, $downvotes = 0) {
$n = $upvotes + $downvotes;
if($n === 0) {
return 0;
}
$z = 1.281551565545; // 80% confidence
$p = floor($upvotes) / $n;
$left = $p + 1/(2*$n)*$z*$z;
$right = $z*sqrt($p*(1-$p)/$n + $z*$z/(4*$n*$n));
$under = 1+1/$n*$z*$z;
return ($left - $right) / $under;
}
/**
* calculates the controversy score for a link
*
* @since 0.1
* @param int $upvotes, int $downvotes
* @access public
* @return float
*/
public function controversy($upvotes = 0, $downvotes = 0) {
return ($upvotes + $downvotes) / max(abs($this->_score($upvotes, $downvotes)), 1);
}
/**
* public method to calculate a post's hotness
*
* @since 0.1
* @param int $upvotes, int $downvotes, int $posted
* @access public
* @return float
*/
public function hotness($upvotes, $downvotes, $posted) {
return $this->_hotness($upvotes, $downvotes, $posted);
}
/**
* public method to calculate a posts confidence
*
* @since 0.1
* @param int $upvotes, int $downvotes
* @access public
* @return double
*/
public function confidence($upvotes, $downvotes) {
return $this->_confidence($upvotes, $downvotes);
}
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.