-
Notifications
You must be signed in to change notification settings - Fork 13
/
hotscore.go
46 lines (38 loc) · 1.14 KB
/
hotscore.go
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
package brutalinks
import (
"math"
"time"
)
// represents the statistical confidence
// var StatisticalConfidence = 1.0 => ~69%, 1.96 => ~95% (default)
var StatisticalConfidence = 1.94
// represents how fast elapsed hours affect the order of an item
var HNGravity = 1.5
// wilson score interval sort
// http://www.evanmiller.org/how-not-to-sort-by-average-rating.html
func Wilson(ups, downs int64) float64 {
n := ups + downs
if n == 0 {
return 0
}
n1 := float64(n)
z := StatisticalConfidence
p := float64(ups / n)
zzfn := z * z / (4 * n1)
w := (p + 2.0*zzfn - z*math.Sqrt((zzfn/n1+p*(1.0-p))/n1)) / (1 + 4*zzfn)
return w
}
// hackernews' hot sort
// https://medium.com/hacking-and-gonzo/how-hacker-news-ranking-algorithm-works-1d9b0cf2c08d
func Hacker(votes int64, date time.Duration) float64 {
hoursAge := date.Hours()
return float64(votes-1) / math.Pow(hoursAge+2, HNGravity)
}
// reddit's hot sort
// http://amix.dk/blog/post/19588
func Reddit(ups, downs int64, date time.Duration) float64 {
decay := 45000.0
s := float64(ups - downs)
order := math.Log(math.Max(math.Abs(s), 1)) / math.Ln10
return order - date.Seconds()/float64(decay)
}