我正在尝试确定在Django项目中实施Wilson Score算法的最佳方法。该项目是典型论坛功能和reddit的upvotes / downvotes / trending页面的混搭。每个论坛都有主题,用户可以投票/降级主题。
我应该在数据库查询中对线程进行评分和排序吗?如果是,我将如何使用Django的数据库模型来做到这一点?
我当前的解决方案是获取论坛的主题,然后以python排序。
wilson_score.py
from datetime import datetime
from math import log
epoch = datetime(1970, 1, 1)
def epoch_seconds(date):
td = date.replace(tzinfo=None) - 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)
models.py
from . import wilson_score
class ForumThread(models.Model):
category = models.ForeignKey(ForumCategory, on_delete=models.CASCADE)
up_votes = models.IntegerField(default=0)
down_votes = models.IntegerField(default=0)
@staticmethod
def get_trending_threads(category):
return sorted(ForumThread.objects.filter(category=category), key=lambda x: wilson_score.hot(x.up_votes, x.down_votes, x.date_created), reverse=True)
views.py
threads = ForumThread.get_trending_threads(category)[offset:offset + 20]