Skip to content

Instantly share code, notes, and snippets.

@twfarland
Created March 27, 2012 11:07

Revisions

  1. twfarland revised this gist Mar 27, 2012. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions reddit-ranking.rkt
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,6 @@
    #lang racket
    (require "lite.rkt")
    ; https://raw.github.com/gist/2214946/7d16522c4feebae1745b7608801a5e5d4294b0c1/lite.rkt
    (require "connive.rkt")
    ; https://github.com/twfarland/connive

    ; Reddit ranking algorithms

  2. twfarland revised this gist Mar 27, 2012. 1 changed file with 6 additions and 3 deletions.
    9 changes: 6 additions & 3 deletions reddit-ranking.rkt
    Original file line number Diff line number Diff line change
    @@ -45,11 +45,14 @@
    (struct story (id user time body up down comments) #:transparent)
    (struct comment (id user body up down) #:transparent)

    (:= (random-time) (- epoch (random 1000)))
    (:= users (vector "tim" "kat" "dave" "sally" "mitch" "walter" "george" "ingrid"))

    (:= (random-user)
    (:= users (vector "tim" "kat" "dave" "sally" "mitch" "walter" "george" "ingrid"))
    (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)
    @@ -78,4 +81,4 @@
    (rank-comments (story-comments (list-ref stories 0)))

    (map (λ (s) (list (story-up s) (story-down s) (story-time s)))
    (rank-stories stories))
    (rank-stories stories))
  3. twfarland created this gist Mar 27, 2012.
    81 changes: 81 additions & 0 deletions reddit-ranking.rkt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,81 @@
    #lang racket
    (require "lite.rkt")
    ; https://raw.github.com/gist/2214946/7d16522c4feebae1745b7608801a5e5d4294b0c1/lite.rkt

    ; 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)

    (:= (random-time) (- epoch (random 1000)))
    (:= (random-user)
    (:= users (vector "tim" "kat" "dave" "sally" "mitch" "walter" "george" "ingrid"))
    (vector-ref users (random (vector-length users))))

    (:= 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))