reddit style voting with django

喜你入骨 提交于 2019-12-02 18:37:57

The reddit algorithm is based on the formula for calculating gravity. I found it from this website

Reddit Algorithm

let t = (t1 – epoch)

(where t1 is the time the post was made)

let x be the number of up votes minus the number of down votes.

Then,

let y be:
  • 1 if there are more up votes than down votes,
  • -1 If there are more down voets than up votes,
  • 0 if there are the same number.

Now Let

z = max({x,1})

And We Have

ranking = C log10(z) + yt1

Where C is a constant (C = 45000).

You may want to consider denormalizing your model slightly by adding a vote_score integer field.

Then all you have to do is override save() to calculate the score using your vote_difference() method.

That makes sorting much easier, and likely reduces the number of database calls you are making.

I know this is not a direct answer to your question. But taking a peek into reddit's code may be very helpful. It helped me when I had to implement a semi-inteligent image cropping algorithm similar to reddit's.

I released a voting application called qhonuskan-votes, you can check it from here: https://github.com/miratcan/qhonuskan-votes

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!