Merge pull request #486 from AyuntamientoMadrid/new-hot-score

implements a new hot scoring system
This commit is contained in:
Enrique García
2015-09-14 17:03:47 +02:00
2 changed files with 14 additions and 18 deletions

View File

@@ -113,23 +113,19 @@ class Debate < ActiveRecord::Base
end
def calculate_hot_score
z = 1.96 # Normal distribution with a confidence of 0.95
time_unit = 1.0 * 12.hours
start = Time.new(2015, 6, 15)
start = Time.new(2015, 6, 15)
comments_weight = 1.0/20 # 1 positive vote / x comments
time_unit = 12.hours.to_f
weighted_score = 0
total = cached_votes_total + comments_weight * comments_count
ups = cached_votes_up + comments_weight * comments_count
downs = total - ups
score = ups - downs
order = Math.log([score.abs, 1].max, 10)
sign = (score <=> 0).to_f
seconds = ((created_at || Time.now) - start).to_f
n = cached_votes_total + comments_weight * comments_count
if n > 0 then
pos = cached_votes_up + comments_weight * comments_count
phat = 1.0 * pos / n
weighted_score = (phat + z*z/(2*n) - z * Math.sqrt((phat*(1-phat)+z*z/(4*n))/n))/(1+z*z/n)
end
age_in_units = 1.0 * ((created_at || Time.now) - start) / time_unit
self.hot_score = ((age_in_units**2 + weighted_score)*1000).round
self.hot_score = (((order * sign) + (seconds/time_unit)) * 1000000).round
end
def calculate_confidence_score

View File

@@ -191,7 +191,7 @@ describe Debate do
end
it "increases for debates with more comments" do
more_comments = create(:debate, :with_hot_score, created_at: now, comments_count: 10)
more_comments = create(:debate, :with_hot_score, created_at: now, comments_count: 25)
less_comments = create(:debate, :with_hot_score, created_at: now, comments_count: 1)
expect(more_comments.hot_score).to be > less_comments.hot_score
end
@@ -232,8 +232,8 @@ describe Debate do
it "increases with comments" do
previous = debate.hot_score
Comment.create(user: create(:user), commentable: debate, body: 'foo')
expect(previous).to be < debate.hot_score
25.times{ Comment.create(user: create(:user), commentable: debate, body: 'foobarbaz') }
expect(previous).to be < debate.reload.hot_score
end
end
end
@@ -396,4 +396,4 @@ describe Debate do
end
end
end