-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Diego Salazar
committed
Jan 25, 2017
1 parent
021380a
commit 234677c
Showing
4 changed files
with
39 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,41 +1,38 @@ | ||
class Recommender | ||
def initialize(subtopic_id, per = 4, page = 0) | ||
@subtopic_id, @per, @page = subtopic_id, per, page | ||
def initialize(subtopic_id) | ||
@subtopic_id = subtopic_id | ||
end | ||
|
||
def ask | ||
sub_topics = if get_recommendation | ||
get_recommendation.recommended_subtopic_ids | ||
def solve | ||
if (r = Recommendation.find_by_subtopic_id @subtopic_id) | ||
r.recommended_subtopic_ids | ||
else | ||
get_sub_topics.tap do |sub_topics| | ||
Recommendation.create({ | ||
subtopic_id: @subtopic_id, | ||
recommended_subtopic_ids: sub_topics.map { |s, l| [s, l.size] } | ||
}) | ||
end | ||
s = get_sub_topics | ||
binding.pry # debug | ||
create_recommendation s | ||
s | ||
end | ||
|
||
Kaminari.paginate_array(sub_topics).page(@page).per @per | ||
end | ||
|
||
def get_recommendation | ||
@recommendation ||= Recommendation.find_by_subtopic_id @subtopic_id | ||
end | ||
private | ||
|
||
def get_sub_topics | ||
sub_topics = {} | ||
|
||
users_also_listened.each do |user| | ||
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 | ||
end | ||
sub_topics.sort_by { |_, listens| listens.size }.reverse | ||
binding.pry # debug | ||
sub_topics | ||
end.sort_by { |_, listens| listens.size }.reverse | ||
end | ||
|
||
def users_also_listened | ||
User.who_listened_to(@subtopic_id) | ||
def create_recommendation(sub_topics) | ||
Recommendation.create({ | ||
subtopic_id: @subtopic_id, | ||
recommended_subtopic_ids: sub_topics.map { |sid, listens| [sid, listens.size] } | ||
}) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters