Skip to content
Snippets Groups Projects
Commit 866a4538 authored by Mauricio Giacomini Girardello's avatar Mauricio Giacomini Girardello
Browse files

Merge ranking branch into Master

parents 570708f7 b3600a16
No related branches found
No related tags found
No related merge requests found
# Ranking docs
# Refactored
```ruby
items = []
items << Ranking::Item.new("ax",1,1,1,["otherStuffA","lala",'a'])
items << Ranking::Item.new("bx",2,2,2,[1,2,3,"teste","lblb",'b'])
items << Ranking::Item.new("cx",1,1,99,[3,2,1,"teste","lclc",'c'])
rater = Ranking::Rater.new(Ranking::Strategies::BasicRater.new(1000, 1, 100))
rater.sortByRate(items).each do |i|
p i.name
end
```
## Output
- ordered array of Items, starting from the most ranked to the least
\ No newline at end of file
# This class represents an item of ranking
class Ranking::Item
# name = name of object
# views = view count
# downloads = download count
# likes = like count
# info = just for convinience of sorting it together, won't be touched
attr_accessor :name, :views, :downloads, :likes, :info
def initialize(name, views = 0, downloads = 0, likes = 0, info = nil)
@name = name
@views = views
@downloads = downloads
@likes = likes
@info = info
end
end
\ No newline at end of file
# This class represents an item rated in ranking
# Have an association with a rank item
class Ranking::RatedItem
attr_accessor :item, :rate
def initialize(item, rate)
@item = item
@rate = rate
end
end
\ No newline at end of file
class Ranking::Rater
def initialize(rater_strategy)
@rater = rater_strategy
end
def sortByRate(items)
@rater.sortByRate items
end
end
\ No newline at end of file
class Ranking::Strategies::BasicRater < Ranking::Strategy
def sortByRate(items)
items.zip( items.size.downto(1) )
.collect { |item,reverseIndex| self.rateItem( item, reverseIndex ) }
.sort { |itemA,itemB| itemA.rate <=> itemB.rate }
.collect { |ri| ri.item }
.reverse # Best ranked comes first
end
private
def rateItem(item, inversePosition)
positionRating = inversePosition * @positionWeight
useRating = (item.downloads < item.views ? item.downloads : item.views) * @useWeight
likeRating = item.likes * @likeWeight
rate = positionRating + useRating + likeRating
build_rated_item item, rate
end
end
\ No newline at end of file
class Ranking::Strategy
def initialize(positionWeight = nil, useWeight = nil, likeWeight = nil)
@positionWeight = (positionWeight == nil ? 1000 : positionWeight)
@useWeight = (useWeight == nil ? 3 : useWeight)
@likeWeight = (likeWeight == nil ? 100 : likeWeight)
end
protected
def build_rated_item(item, rate)
Ranking::RatedItem.new(item, rate)
end
end
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment