Skip to content

Commit

Permalink
refactor algo
Browse files Browse the repository at this point in the history
  • Loading branch information
Diego Salazar committed Jan 25, 2017
1 parent b4c7bcc commit 5324da2
Showing 1 changed file with 4 additions and 7 deletions.
11 changes: 4 additions & 7 deletions app/services/recommender.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,29 +7,26 @@ def solve
if (r = Recommendation.find_by_subtopic_id @subtopic_id)
r.recommended_subtopic_ids
else
s = get_sub_topics
binding.pry # debug
create_recommendation s
s
sub_topics = get_sub_topics
create_recommendation_for sub_topics
sub_topics
end
end

private

def get_sub_topics
binding.pry # debug
User.who_listened_to(@subtopic_id).inject({}) do |sub_topics, user|
user.listens.each do |listen|
next if listen.subtopic_id == @subtopic_id
sub_topics[listen.subtopic_id] ||= []
sub_topics[listen.subtopic_id] << listen
end
binding.pry # debug
sub_topics
end.sort_by { |_, listens| listens.size }.reverse
end

def create_recommendation(sub_topics)
def create_recommendation_for(sub_topics)
Recommendation.create({
subtopic_id: @subtopic_id,
recommended_subtopic_ids: sub_topics.map { |sid, listens| [sid, listens.size] }
Expand Down

0 comments on commit 5324da2

Please sign in to comment.