Skip to content

Instantly share code, notes, and snippets.

@djm
Created April 28, 2011 09:38
Show Gist options
  • Save djm/4645a5f6591253bd9ed1 to your computer and use it in GitHub Desktop.
Save djm/4645a5f6591253bd9ed1 to your computer and use it in GitHub Desktop.
Reddit's Time Based Algorithm
def get_rating(objects):
"""
Pseduo-python for calculating a rating.
"""
upvotes = get_upvotes(object)
downvotes = get_downvotes(object)
vote_diff = upvotes - downvotes
abs_vote_diff = abs(vote_diff)
# y simply 1, 0 or -1 based upon whether the difference
# between votes was positive, equal or negative.
if vote_diff > 0:
y = 1
elif vote_diff == 0:
y = 0
else:
y = -1
# z is the maximal value of the absolute value of x and 1
if abs_vote_diff >= 1:
z = abs_vote_diff
else:
z = 1
# We need to compare a constant timestamp against the date
# of submission timestamp. Set this to your development time.
constant_timestamp = 1287662400
submission_timestamp = time.mktime(objects.date_added.timetuple())
age = submission_timestamp - constant_timestamp
# Create the log for weighting use later.
log_z = math.log10(z)
# The water down constant defines how long objects stay "new" for.
# With 45000 (12.5 hours), a 1-hour-old object would have to improve
# its vote differential 10x over the next 12.5 hours to maintain its
# rating to compensate for elapsed time. If you change the constant
# you will have to recalculate all ratings for all objects (with a
# management command).
water_down_constant = getattr(settings, 'WATER_DOWN_CONSTANT', 45000)
weighted_calc = (y*age) / water_down_constant
return log_z + weighted_calc
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment