ruby on rails - Implement Post ranking algorithm with Likes and Time difference -
how can rank posts likes , time difference(how old post) in ruby on rails. there suitable algorithm task.
here code algorithm
@rank = 0.00 s = post.likers(user).count order = math.log10([s.abs, 1].max) if s > 0 sign = 1 elsif s < 0 sign = -1 else sign = 0 end td = (time.now - post.created_at) td2 = td.days * 86400 + td.seconds + ((1000000*(td.seconds)).to_f)/1000000 seconds = td2 - 1134028003 @rank = (sign * order + seconds / 45000).round(7) post.update_attributes(popularity: @rank) post.save!
how reddit ranking algorithms work. if posts can liked, is, if number of likes positive number can simplify equation removing y
.
update
ruby implementation of linked algo
def hot(ups, downs, date) s = ups - downs order = math::log([s.abs, 1].max, 10) sign = if s > 0 1 elsif s < 0 -1 else 0 end seconds = date.to_f - 1134028003 (sign * order + seconds / 45000).round(7) end
note doesn't use time.now
here. in implementation older post, smaller time difference (seconds
) is, makes sense treat reward (to add it).
in implementation, seconds
difference between , post creation time, should treat penalty, either subtracting seconds / 45000
or calculating td
post.created_at - time.now
, doesn't matter.
you don't need td2
.
also, if want use implementation time.now
, keep in mind have run script once in while update records in database.
Comments
Post a Comment