Hi,
I really need some help implementing an algorithm into my app!
I am trying to work out the popularity of books digg style which users
can vote on. To do this I was think this algorithm might work for the
time being :
Popularity = [ (V1/A1) + (V2/A2) + … + (Vn/An) ] / Book entry Age
Vn is a vote, and An is the age of that vote (for example, in minutes)
and then finally divided by the total time the entry has been on the
website.
What I therefore need help with is the implementation. Firstly will
the code belong in the model or should I create a library and call
that? I need the code to loop through the books votes (self.votes) and
substitute it in the algorithm.
Here's where I got to:
def popularity
votes = Array.new
res = self.votes
res.each do |item|
vote = Hash.new
vote[:date] = item.created_at
votes << vote
end
return votes
end
I have no idea how I should move on from here? Any suggestions would
really be appreciated!
Thanks
Ollie
Hi,
I really need some help implementing an algorithm into my app!
I am trying to work out the popularity of books digg style which users
can vote on. To do this I was think this algorithm might work for the
time being :
Popularity = [ (V1/A1) + (V2/A2) + … + (Vn/An) ] / Book entry Age
Vn is a vote, and An is the age of that vote (for example, in minutes)
and then finally divided by the total time the entry has been on the
website.
What I therefore need help with is the implementation. Firstly will
the code belong in the model or should I create a library and call
that?
The code belongs in the Book model.
I need the code to loop through the books votes (self.votes) and
substitute it in the algorithm.
Here's where I got to:
def popularity
votes = Array.new
res = self.votes
res.each do |item|
vote = Hash.new
vote[:date] = item.created_at
votes << vote
end
return votes
end
I have no idea how I should move on from here? Any suggestions would
really be appreciated!
I wouldn't do any looping in Ruby at all. It's going to get awful slow when the next Harry Potter book comes out and a million people vote for it...
def popularity
votes.sum('vote_column / db_function_to_convert_time_to_minutes(minutes_column)').to_f / (entered_at.to_i/60.0)
end
That keeps it all in the database...
Haven't tested it, but something along those lines should work I would think.