Skip to content

Instantly share code, notes, and snippets.

@twfarland
Created March 27, 2012 11:07
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save twfarland/2214978 to your computer and use it in GitHub Desktop.
Save twfarland/2214978 to your computer and use it in GitHub Desktop.
Reddit ranking algorithms in Racket
#lang racket
(require "connive.rkt")
; https://github.com/twfarland/connive
; Reddit ranking algorithms
; Story ranking (hot ranking)
; http://amix.dk/blog/post/19588
; Submission time greatly affects score
(:= epoch (current-seconds))
(:= (hot upvotes downvotes date-seconds)
(:= time (- date-seconds epoch))
(:= score (- upvotes downvotes))
(:= sign (?? (> score 0) 1
(> score 0) -1
0))
(:= order (?? (< score 1) 1
score))
(+ (log order) (/ (* sign time) 45000)))
; Comment ranking (confidence sort using Wilson score)
; http://www.evanmiller.org/how-not-to-sort-by-average-rating.html
; Submission time doesn't affect score
(:= (confidence upvotes downvotes)
(:= n (+ upvotes downvotes))
(?? (= n 0) 0
(let* ((z 1.0) ; 1.0 = 85%, 1.6 = 95% (sureness that comment will get to rank)
(z^2 (* z z))
(p (/ upvotes n)))
(real-part (/ (sqrt (- ; use minus for lower bound
(+ p (/ z^2 (* 2 n)))
(* z (/ (+ (* p (- 1 p)) (/ z^2 (* 4 n)) n)))))
(+ 1 (/ z^2 n)))))))
; generating test data
(struct story (id user time body up down comments) #:transparent)
(struct comment (id user body up down) #:transparent)
(:= users (vector "tim" "kat" "dave" "sally" "mitch" "walter" "george" "ingrid"))
(:= (random-user)
(vector-ref users (random (vector-length users))))
(:= (random-time)
(- epoch (random 1000)))
(:= stories
(for/list ((s 10))
(story s (random-user) (random-time) "" (random 300) (random 100)
(for/list ((c 10))
(comment c (random-user) "" (random 20) (random 20))))))
; applying ranking
(:= ((comparing f) x y)
(> (f x) (f y)))
(:= (story-heat story)
(hot (story-up story) (story-down story) (story-time story)))
(:= (comment-confidence comment)
(confidence (comment-up comment) (comment-down comment)))
(:= (rank-stories stories)
(sort stories (comparing story-heat)))
(:= (rank-comments comments)
(sort comments (comparing comment-confidence)))
; try it out
(rank-comments (story-comments (list-ref stories 0)))
(map (λ (s) (list (story-up s) (story-down s) (story-time s)))
(rank-stories stories))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment