From c6a0f8852016a27a964818587738184e789185fb Mon Sep 17 00:00:00 2001 From: kikito Date: Mon, 14 Sep 2015 16:29:01 +0200 Subject: [PATCH] implements a new hot scoring system --- app/models/debate.rb | 24 ++++++++++-------------- spec/models/debate_spec.rb | 8 ++++---- 2 files changed, 14 insertions(+), 18 deletions(-) diff --git a/app/models/debate.rb b/app/models/debate.rb index f1e9a621e..db3133877 100644 --- a/app/models/debate.rb +++ b/app/models/debate.rb @@ -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 diff --git a/spec/models/debate_spec.rb b/spec/models/debate_spec.rb index 377f933c8..f9af3c149 100644 --- a/spec/models/debate_spec.rb +++ b/spec/models/debate_spec.rb @@ -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 \ No newline at end of file +end