[英]Wilson Score in Rails + Add Random Number to Upvote in Postgres Select Statement
I am trying to add a randomized number to my Wilson Score select statement. 我想在我的Wilson Score选择语句中添加一个随机数。
My Post model has columns 'upvotes_count' and 'downvotes_count' that get incremented and decremented according to how many upvotes and downvotes they have (duh..). 我的帖子模型具有“ upvotes_count”和“ downvotes_count”列,该列根据其具有多少upvotes和downvotes来递增和递减(duh ..)。
Post.select("((upvotes_count + 1 + 1.9208) / (upvotes_count + downvotes_count + 2) - " +
"1.96 * SQRT(((upvotes_count + 1) * (downvotes_count + 1)) / (upvotes_count + downvotes_count + 2) + 0.9604) / " +
"(upvotes_count + downvotes_count + 2)) / (1 + 3.8416 / (upvotes_count + downvotes_count + 2)) " +
"AS ci_lower_bound, posts.*")
.order("ci_lower_bound DESC")
Note: In theory, Wilson Score doesn't allow for cases where both upvotes_count and downvotes_count equal 0, so I cheat and add upvote and downvote to each post record. 注意:从理论上讲,Wilson Score不允许upvotes_count和downvotes_count都等于0的情况,因此我作弊并将upvote和downvote添加到每个帖子记录中。 I know this is mathematical "blasphemy" so please don't hurt me math gods.
我知道这是数学上的“亵渎”,所以请不要伤害我数学之神。 Otherwise, I wouldn't get post records with zero upvotes and zero downvotes...and that sucks.
否则,我将不会获得零投票数和零投票数的过帐记录……这太糟了。
What would be ideal is to have a select statement that would add a different random number (Poisson preferably) to upvotes_count of each post to give an artificial bump to it's Wilson score so it would occasionally appear higher on the list. 理想的情况是具有一个select语句,该语句将为每个帖子的upvotes_count添加一个不同的随机数(最好是Poisson),以使其人为地提高其Wilson得分,从而使其偶尔出现在列表中更高的位置。
Question: How do I add a different random number to upvotes_count for each post selected in my select statement? 问题:如何为在select语句中选择的每个帖子添加不同的随机数到upvotes_count?
Something like this...but obviously it doesn't work... 像这样的东西...但显然不起作用...
Post.select("((upvotes_count + 1 + random + 1.9208) / (upvotes_count + downvotes_count + 2) - " +
"1.96 * SQRT(((upvotes_count + 1 + random) * (downvotes_count + 1)) / (upvotes_count + downvotes_count + 2) + 0.9604) / " +
"(upvotes_count + random + downvotes_count + 2)) / (1 + 3.8416 / (upvotes_count + random + downvotes_count + 2)) " +
"AS ci_lower_bound, posts.*")
This answer may be blasphemy too and I'm not sure I would ever do this, but here's how you could probably do what you're asking 这个答案也可能是亵渎神灵,我不确定我是否会这样做,但是这就是您可能会做的事情
# app/models/post.rb
class Post < ActiveRecord::Base
def self.custom_select_query
select("((upvotes_count + 1 + #{random(100)} + 1.9208) / (upvotes_count + downvotes_count + 2) - " +
"1.96 * SQRT(((upvotes_count + 1 + #{random(99)}) * (downvotes_count + 1)) / (upvotes_count + downvotes_count + 2) + 0.9604) / " +
"(upvotes_count + #{random(98)} + downvotes_count + 2)) / (1 + 3.8416 / (upvotes_count + #{random(97)} + downvotes_count + 2)) " +
"AS ci_lower_bound, posts.*")
end
private
def self.random(max)
SecureRandom.random_number(max)
end
end
Then call it Post.custom_select_query
然后将其
Post.custom_select_query
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.