- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我正在创建一个类似于 reddit 和 hacker news 的网站,它有一个链接和投票的数据库。我正在实现黑客新闻的流行度算法,在实际收集这些链接并显示它们之前,一切都进展顺利。算法很简单:
Y Combinator's Hacker News:Popularity = (p - 1) / (t + 2)^1.5`Votes divided by age factor.Where`p : votes (points) from users.t : time since submission in hours.p is subtracted by 1 to negate submitter's vote.Age factor is (time since submission in hours plus two) to the power of 1.5.factor is (time since submission in hours plus two) to the power of 1.5.
I asked a very similar question over yonder Complex ordering in Django but instead of contemplating my options I choose one and tried to make it work because that's how I did it with PHP/MySQL but I now know Django does things a lot differently.
My models look something (exactly) like this
class Link(models.Model):
category = models.ForeignKey(Category)
user = models.ForeignKey(User)
created = models.DateTimeField(auto_now_add = True)
modified = models.DateTimeField(auto_now = True)
fame = models.PositiveIntegerField(default = 1)
title = models.CharField(max_length = 256)
url = models.URLField(max_length = 2048)
def __unicode__(self):
return self.title
class Vote(models.Model):
link = models.ForeignKey(Link)
user = models.ForeignKey(User)
created = models.DateTimeField(auto_now_add = True)
modified = models.DateTimeField(auto_now = True)
karma_delta = models.SmallIntegerField()
def __unicode__(self):
return str(self.karma_delta)
我的观点:
def index(request):
popular_links = Link.objects.select_related().annotate(karma_total = Sum('vote__karma_delta'))
return render_to_response('links/index.html', {'links': popular_links})
根据我之前的问题,我正在尝试使用排序函数来实现算法。该问题的答案似乎认为我应该将算法放在选择和排序中。我将对这些结果进行分页,所以我认为我不能在不抓取所有内容的情况下在 python 中进行排序。关于如何有效地执行此操作的任何建议?
编辑
这还没有奏效,但我认为这是朝着正确方向迈出的一步:
from django.shortcuts import render_to_response
from linkett.apps.links.models import *
def index(request):
popular_links = Link.objects.select_related()
popular_links = popular_links.extra(
select = {
'karma_total': 'SUM(vote.karma_delta)',
'popularity': '(karma_total - 1) / POW(2, 1.5)',
},
order_by = ['-popularity']
)
return render_to_response('links/index.html', {'links': popular_links})
这个错误变成:
Caught an exception while rendering: column "karma_total" does not exist
LINE 1: SELECT ((karma_total - 1) / POW(2, 1.5)) AS "popularity", (S...
编辑 2
更好的错误?
TemplateSyntaxError: Caught an exception while rendering: missing FROM-clause entry for table "vote"
LINE 1: SELECT ((vote.karma_total - 1) / POW(2, 1.5)) AS "popularity...
我的 index.html 很简单:
{% block content %}{% for link in links %} karma-up {{ link.karma_total }} karma-down {{ link.title }} Posted by {{ link.user }} to {{ link.category }} at {{ link.created }} {% empty %} No Links{% endfor %}{% endblock content %}
EDIT 3So very close! Again, all these answers are great but I am concentrating on a particular one because I feel it works best for my situation.
from django.db.models import Sum
from django.shortcuts import render_to_response
from linkett.apps.links.models import *<p></p>
<p>def index(request):
popular_links = Link.objects.select_related().extra(
select = {
'popularity': '(SUM(links_vote.karma_delta) - 1) / POW(2, 1.5)',
},
tables = ['links_link', 'links_vote'],
order_by = ['-popularity'],
)
return render_to_response('links/test.html', {'links': popular_links})
</p>
运行此程序时,我遇到了一个错误,因为我没有按值分组。具体来说:
TemplateSyntaxError at /
Caught an exception while rendering: column "links_link.id" must appear in the GROUP BY clause or be used in an aggregate function
LINE 1: ...karma_delta) - 1) / POW(2, 1.5)) AS "popularity", "links_lin...
不确定为什么我的 links_link.id 不在我的分组依据中,但我不确定如何更改我的分组依据,django 通常会这样做。
最佳答案
在 Hacker News 上,只有 210 个最新故事和 210 个最受欢迎的故事被分页(7 页值(value) * 每页 30 个故事)。我的猜测是限制的原因(至少部分)是这个问题。
为什么不为最受欢迎的故事放弃所有花哨的 SQL 而只保留一个运行列表呢?一旦您建立了前 210 个故事的列表,您只需要担心在新投票进来时重新排序,因为相对顺序会随着时间的推移而保持不变。当有新的投票进来时,您只需要担心重新排序收到投票的故事。
如果获得投票的故事不在列表中,则计算该故事的得分,加上列表中 最不受欢迎的故事。如果获得投票的故事较低,那么您就完成了。如果更高,则计算倒数第二个最受欢迎(故事 209)的当前分数并再次比较。继续努力,直到找到得分更高的故事,然后将新投票的故事放在排名中紧靠该故事的下方。当然,除非它达到第一名。
这种方法的好处是它限制了您必须查看的故事集才能找出头条新闻列表。在绝对最坏的情况下,您必须计算 211 个故事的排名。因此它非常有效,除非您必须从现有数据集中建立列表 - 但假设您将列表缓存在某个地方,这只是一次性惩罚。
反对票是另一个问题,但我只能投票赞成票(无论如何,在我的业力水平上)。
关于python - 在 Django 中实现流行度算法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1965341/
我想知道是否有一个“标准”类(JDK、Guava、Apache *)可以帮助延迟初始化/计算模式? 我正在考虑这样的事情(现在让我们忽略同步): abstract class Lazy { T i
我要创建一个从 html 到某种格式的转换器。我正在考虑使用中间格式 XML(XSL-FO)。 我的问题:如果没有那么多应用程序渲染它,为什么 FO 格式很受欢迎? 最佳答案 我很清楚 CSS 和 X
我发现了 IDEF就在昨天,我立即想知道为什么它不比 UML 更流行。从documents来看,IDEF 至少与 UML 一样适用于软件建模,但与 UML 相比,它已经成熟和稳定了大约 30 年。 这
首先,对于版主来说,如果这不是发布此内容的正确位置,请不要生气:)。 我想知道是否有类似 TodoMVC 的东西适用于 Django、Rails、Symfony 等流行的 Web 框架。 如果没有这样
在我的应用程序中,我有一个 UIScrollView,其中包含 N 个可以向左或向右滑动的 ViewController 在每个 Controller 中,我希望能够推送新的 viewControll
我是一名优秀的程序员,十分优秀!