Extract score calculation to a module
This commit is contained in:
@@ -102,24 +102,15 @@ class Debate < ActiveRecord::Base
|
|||||||
end
|
end
|
||||||
|
|
||||||
def calculate_hot_score
|
def calculate_hot_score
|
||||||
start = Time.new(2015, 6, 15)
|
self.hot_score = ScoreCalculator.hot_score(created_at,
|
||||||
comments_weight = 1.0/20 # 1 positive vote / x comments
|
cached_votes_total,
|
||||||
time_unit = 12.hours.to_f
|
cached_votes_up,
|
||||||
|
comments_count)
|
||||||
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
|
|
||||||
|
|
||||||
self.hot_score = (((order * sign) + (seconds/time_unit)) * 1000000).round
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def calculate_confidence_score
|
def calculate_confidence_score
|
||||||
return unless cached_votes_total > 0
|
self.confidence_score = ScoreCalculator.confidence_score(cached_votes_total,
|
||||||
self.confidence_score = cached_votes_score * (cached_votes_up / cached_votes_total.to_f) * 100
|
cached_votes_up)
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.search(terms)
|
def self.search(terms)
|
||||||
|
|||||||
30
lib/score_calculator.rb
Normal file
30
lib/score_calculator.rb
Normal file
@@ -0,0 +1,30 @@
|
|||||||
|
module ScoreCalculator
|
||||||
|
|
||||||
|
EPOC = Time.new(2015, 6, 15)
|
||||||
|
COMMENT_WEIGHT = 1.0/20 # 1 positive vote / x comments
|
||||||
|
TIME_UNIT = 12.hours.to_f
|
||||||
|
|
||||||
|
def self.hot_score(date, votes_total, votes_up, comments_count)
|
||||||
|
total = votes_total + COMMENT_WEIGHT * comments_count
|
||||||
|
ups = votes_up + COMMENT_WEIGHT * comments_count
|
||||||
|
downs = total - ups
|
||||||
|
score = ups - downs
|
||||||
|
order = Math.log([score.abs, 1].max, 10)
|
||||||
|
sign = (score <=> 0).to_f
|
||||||
|
seconds = ((date || Time.now) - EPOC).to_f
|
||||||
|
|
||||||
|
(((order * sign) + (seconds/TIME_UNIT)) * 10000000).round
|
||||||
|
end
|
||||||
|
|
||||||
|
def self.confidence_score(votes_total, votes_up)
|
||||||
|
return 0 unless votes_total > 0
|
||||||
|
|
||||||
|
votes_total = votes_total.to_f
|
||||||
|
votes_up = votes_up.to_f
|
||||||
|
votes_down = votes_total - votes_up
|
||||||
|
score = votes_up - votes_down
|
||||||
|
|
||||||
|
score * (votes_up / votes_total) * 100
|
||||||
|
end
|
||||||
|
|
||||||
|
end
|
||||||
@@ -244,16 +244,16 @@ describe Debate do
|
|||||||
debate = create(:debate, :with_confidence_score, cached_votes_up: 100, cached_votes_score: 100, cached_votes_total: 100)
|
debate = create(:debate, :with_confidence_score, cached_votes_up: 100, cached_votes_score: 100, cached_votes_total: 100)
|
||||||
expect(debate.confidence_score).to eq(10000)
|
expect(debate.confidence_score).to eq(10000)
|
||||||
|
|
||||||
debate = create(:debate, :with_confidence_score, cached_votes_up: 0, cached_votes_score: -100, cached_votes_total: 100)
|
debate = create(:debate, :with_confidence_score, cached_votes_up: 0, cached_votes_total: 100)
|
||||||
expect(debate.confidence_score).to eq(0)
|
expect(debate.confidence_score).to eq(0)
|
||||||
|
|
||||||
debate = create(:debate, :with_confidence_score, cached_votes_up: 50, cached_votes_score: 50, cached_votes_total: 100)
|
debate = create(:debate, :with_confidence_score, cached_votes_up: 75, cached_votes_total: 100)
|
||||||
expect(debate.confidence_score).to eq(2500)
|
expect(debate.confidence_score).to eq(3750)
|
||||||
|
|
||||||
debate = create(:debate, :with_confidence_score, cached_votes_up: 500, cached_votes_score: 500, cached_votes_total: 1000)
|
debate = create(:debate, :with_confidence_score, cached_votes_up: 750, cached_votes_total: 1000)
|
||||||
expect(debate.confidence_score).to eq(25000)
|
expect(debate.confidence_score).to eq(37500)
|
||||||
|
|
||||||
debate = create(:debate, :with_confidence_score, cached_votes_up: 10, cached_votes_score: -80, cached_votes_total: 100)
|
debate = create(:debate, :with_confidence_score, cached_votes_up: 10, cached_votes_total: 100)
|
||||||
expect(debate.confidence_score).to eq(-800)
|
expect(debate.confidence_score).to eq(-800)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user