――I often saw recommendations in the online shop.
――I saw a recommended friend on SNS
→ Recommendations have been used recently → It's interesting, so I want to find out
--A function that recommends movies that look interesting to users
--Highly related → High score --High score → Many recommendations
def recommend_movies # recommend movies to a user
# find all other users, equivalent to .where(‘id != ?’, self.id)
other_users = self.class.all.where.not(id: self.id)
# instantiate a new hash, set default value for any keys to 0
recommended = Hash.new(0)
# for each user of all other users
other_users.each do |user|
# find the movies this user and another user both liked
common_movies = user.movies & self.movies
# calculate the weight (recommendation rating)
weight = common_movies.size.to_f / user.movies.size
# add the extra movies the other user liked
common_movie_ids = common_movies.pluck(:id)
user.movies.each do |movie|
next if common_movie_ids.include? movie.id
# put the movie along with the cumulative weight into hash
recommended[movie] += weight
end
end
# sort by weight in descending order
sorted_recommended = recommended.sort_by { |key, value| value }.reverse
end
https://github.com/phamthanhthuongbk/recommendation-rails
--Make the same as the movie algorithm.
https://github.com/phamthanhthuongbk/recommendation-rails
――The algorithm is easy to understand, and I saw the effect. ――I have an image of the recommended movement. ――For scoring, you have to look at all the DBs, so how about a big system?
Recommended Posts