Permalink
Browse files

Added a confidence sort order for comments

  • Loading branch information...
1 parent 34048b1 commit e38e6a0d4552c3b3efb3cb65a6312b2295a27f9e @johnsoft johnsoft committed Jun 27, 2012
Showing with 44 additions and 2 deletions.
  1. +35 −1 r2/r2/lib/db/sorts.py
  2. +4 −0 r2/r2/lib/db/thing.py
  3. +5 −1 r2/r2/lib/menus.py
View
36 r2/r2/lib/db/sorts.py
@@ -19,7 +19,7 @@
# All portions of the code written by CondeNet are Copyright (c) 2006-2008
# CondeNet, Inc. All Rights Reserved.
################################################################################
-from math import log
+from math import log, sqrt
from datetime import datetime, timedelta
from r2.config.databases import tz
@@ -46,3 +46,37 @@ def controversy(ups, downs):
"""The controversy sort."""
return float(ups + downs) / max(abs(score(ups, downs)), 1)
+
+def _confidence(ups, downs):
+ """The confidence sort.
+ http://www.evanmiller.org/how-not-to-sort-by-average-rating.html"""
+ n = ups + downs
+
+ if n == 0:
+ return 0
+
+ z = 1.281551565545 # 80% confidence
+ p = float(ups) / 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
+
+# precompute low values
+up_range = 400
+down_range = 100
+_confidences = []
+for _ups in xrange(up_range):
+ for _downs in xrange(down_range):
+ _confidences.append(_confidence(_ups, _downs))
+
+def confidence(ups, downs):
+ if ups + downs == 0:
+ return 0
+ elif ups < up_range and downs < down_range: # check if pair is precomputed
+ return _confidences[downs + ups * down_range]
+ else:
+ return _confidence(ups, downs)
+
View
4 r2/r2/lib/db/thing.py
@@ -440,6 +440,10 @@ def _score(self):
def _controversy(self):
return sorts.controversy(self._ups, self._downs)
+ @property
+ def _confidence(self):
+ return sorts.confidence(self._ups, self._downs)
+
@classmethod
def _build(cls, id, bases):
return cls(bases.ups, bases.downs, bases.date,
View
6 r2/r2/lib/menus.py
@@ -61,6 +61,7 @@ def __getattr__(self, attr):
more = _('More'),
relevance = _('Relevance'),
controversial = _('Controversial'),
+ confidence = _('Best'),
saved = _('Saved'),
recommended = _('Recommended'),
rising = _('Rising'),
@@ -435,10 +436,13 @@ def operator(self, sort):
return operators.desc('_score')
elif sort == 'controversial':
return operators.desc('_controversy')
+ elif sort == 'confidence':
+ return operators.desc('_confidence')
class CommentSortMenu(SortMenu):
"""Sort menu for comments pages"""
- options = ('hot', 'new', 'controversial', 'top', 'old')
+ default = 'confidence'
+ options = ('hot', 'new', 'controversial', 'top', 'old', 'confidence')
class SearchSortMenu(SortMenu):
"""Sort menu for search pages."""

0 comments on commit e38e6a0

Please sign in to comment.