尤川豪   ·  5年前
445 貼文  ·  275 留言
  分享   共 6,059 次點閱
按了喜歡:
共有 3 則留言
尤川豪   ·  5年前
445 貼文  ·  275 留言

非常棒的文章,開發論壇、討論區...等等需要替文章排序的功能時可以參考!

Reddit 文章的排名規則如下:

# Rewritten code from /r2/r2/lib/db/_sorts.pyx

from datetime import datetime, timedelta
from math import log

epoch = datetime(1970, 1, 1)

def epoch_seconds(date):
    td = date - epoch
    return td.days * 86400 + td.seconds + (float(td.microseconds) / 1000000)

def score(ups, downs):
    return ups - downs

def hot(ups, downs, date):
    s = score(ups, downs)
    order = log(max(abs(s), 1), 10)
    sign = 1 if s > 0 else -1 if s < 0 else 0
    seconds = epoch_seconds(date) - 1134028003
    return round(sign * order + seconds / 45000, 7)

非常簡潔有力的把發文時間、按讚數、倒讚數作為參數進去計算

而留言評論的排序公式則是:

# Rewritten code from /r2/r2/lib/db/_sorts.pyx
from math import sqrt

def _confidence(ups, downs):
    n = ups + downs

    if n == 0:
        return 0

    z = 1.281551565545
    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

def confidence(ups, downs):
    if ups + downs == 0:
        return 0
    else:
        return _confidence(ups, downs)

需要把文章新舊、按讚數等等綜合考量排序的時候,可以參考 Reddit 的作法

 
按了喜歡:
尤川豪   ·  5年前
445 貼文  ·  275 留言
 
按了喜歡:
HenryWu   ·  5年前
0 貼文  ·  1 留言

Thanks to share ~

 
按了喜歡:
您的留言
尤川豪
445 貼文  ·  275 留言

Devs.tw 是讓工程師寫筆記、網誌的平台。隨手紀錄、寫作,方便日後搜尋!

歡迎您一起加入寫作與分享的行列!

查看所有文章