[RUBY] I made a simple recommendation function.

Recommendation function Reasons to worry

――I often saw recommendations in the online shop. image.png

――I saw a recommended friend on SNS

→ Recommendations have been used recently → It's interesting, so I want to find out


Related articles


First make a movie recommendation

--A function that recommends movies that look interesting to users


Algorithm (1)

image.png


Algorithm (2)

image.png


Algorithm (3) -Scoring

--Highly related → High score --High score → Many recommendations


I wrote scoring in Rails.

    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

Display in Rails

https://github.com/phamthanhthuongbk/recommendation-rails

image.png


First, make a friend's recommendation.

--Make the same as the movie algorithm.


Result is

https://github.com/phamthanhthuongbk/recommendation-rails

image.png


Impressions

――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

I made a simple recommendation function.
[Ruby] I made a simple Ping client
[Rails] I made a draft function using enum
I made a chat app.
I made a simple calculation problem game in Java
I made a shopify app @java
I made a GUI with Swing
I made a matching app (Android app)
I made a simple MVC sample system using Spring Boot
I made a package.xml generation tool.
[Android] I made a pedometer app.
[Rails] I made a simple calendar mini app with customized specifications.
I made a Ruby container image and moved the Lambda function
I made a reply function for the Rails Tutorial extension (Part 1)
I made a simple search form with Spring Boot + GitHub Search API.
I made a reply function for the Rails Tutorial extension (Part 5):
[Rails withdrawal] Create a simple withdrawal function with rails
I made a risky die with Ruby
I made a plugin for IntelliJ IDEA
I made a rock-paper-scissors app with kotlin
I made a calculator app on Android
I made a new Java deployment tool
I made a rock-paper-scissors app with android
I made a bulletin board using Docker 1
I made StringUtils.isBlank
I made a reply function for Rails Tutorial extension (Part 2): Change model
I made a Diff tool for Java files
I made a primality test program in Java
04. I made a front end with SpringBoot + Thymeleaf
I made a mosaic art with Pokemon images
java I tried to break a simple block
I made a gender selection column with enum
I made a rock-paper-scissors game in Java (CLI)
I made a viewer app that displays a PDF
I made a Docker container to run Maven
I made a Ruby extension library in C
I made a LINE bot with Rails + heroku
I made a portfolio with Ruby On Rails
I made a simple graph library for smartphone apps [MP Android Chart Kai]
I made a Docker image of SDAPS for Japanese
A simple CRUD app made with Nuxt / Laravel (Docker)
I made a check tool for the release module
I made a method to ask for Premium Friday
[Ruby] I made a crawler with anemone and nokogiri.
I made a drawing chat "8bit paint chat" on WebAssembly
I made a Restful server and client in Spring.
I made a library that works like a Safari tab !!
I made a library for displaying tutorials on Android.
I made a Wrapper that calls KNP from Java
I made a function to register images with API in Spring Framework. Part 1 (API edition)
Rails Tutorial Extension: I created a follower notification function
I want to add a delete function to the comment function
I made a reply function for the Rails Tutorial extension (Part 4): A function that makes the user unique
I made a function to register images with API in Spring Framework. Part 2 (Client Edition)
I made a Japanese version of Rails / devise automatic email
I made a server side of an online card game ③
I made a development environment with rails6 + docker + postgreSQL + Materialize.
I want to implement a product information editing function ~ part1 ~
I made a plugin to execute jextract with Gradle task
[Beginner] I made a program to sell cakes in Java
I made a server side of an online card game ⑥