From 27a1a90cf3263a889a95cc532276bbb10d52c9b7 Mon Sep 17 00:00:00 2001 From: evazion Date: Mon, 2 Dec 2019 02:25:38 -0600 Subject: [PATCH] recommendations: allow filtering recs by tags. --- app/logical/recommender_service.rb | 15 ++++++++------- app/views/recommended_posts/index.html.erb | 1 + 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/app/logical/recommender_service.rb b/app/logical/recommender_service.rb index fe6798779..ab7b947d1 100644 --- a/app/logical/recommender_service.rb +++ b/app/logical/recommender_service.rb @@ -17,27 +17,28 @@ module RecommenderService enabled? && user.favorite_count > MIN_USER_FAVS end - def recommend_for_user(user, limit = 50) + def recommend_for_user(user, tags: nil, limit: 50) body, status = HttpartyCache.get("#{Danbooru.config.recommender_server}/recommend/#{user.id}", params: { limit: limit }, expiry: CACHE_LIFETIME) return [] if status != 200 - process_recs(body, uploader: user, favoriter: user) + process_recs(body, tags: tags, uploader: user, favoriter: user) end - def recommend_for_post(post, limit = 50) + def recommend_for_post(post, tags: nil, limit: 50) body, status = HttpartyCache.get("#{Danbooru.config.recommender_server}/similar/#{post.id}", params: { limit: limit }, expiry: CACHE_LIFETIME) return [] if status != 200 - process_recs(body, post: post) + process_recs(body, post: post, tags: tags) end - def process_recs(recs, uploader: nil, favoriter: nil) + def process_recs(recs, post: nil, uploader: nil, favoriter: nil, tags: nil) recs = JSON.parse(recs) posts = Post.where(id: recs.map(&:first)) posts = posts.where.not(id: post.id) if post posts = posts.where.not(uploader_id: uploader.id) if uploader posts = posts.where.not(id: favoriter.favorites.select(:post_id)) if favoriter + posts = posts.where(id: Post.tag_match(tags).reorder(nil).select(:id)) if tags.present? id_to_score = recs.to_h recs = posts.map { |post| { score: id_to_score[post.id], post: post } } @@ -57,10 +58,10 @@ module RecommenderService if user.present? raise User::PrivilegeError if user.hide_favorites? max_recommendations = params.fetch(:max_recommendations, user.favorite_count + 500).to_i.clamp(0, 50000) - recs = RecommenderService.recommend_for_user(user, max_recommendations) + recs = RecommenderService.recommend_for_user(user, tags: params[:post_tags_match], limit: max_recommendations) elsif post.present? max_recommendations = params.fetch(:max_recommendations, 100).to_i.clamp(0, 1000) - recs = RecommenderService.recommend_for_post(post, max_recommendations) + recs = RecommenderService.recommend_for_post(post, tags: params[:post_tags_match], limit: max_recommendations) else recs = [] end diff --git a/app/views/recommended_posts/index.html.erb b/app/views/recommended_posts/index.html.erb index 775602d5e..436771488 100644 --- a/app/views/recommended_posts/index.html.erb +++ b/app/views/recommended_posts/index.html.erb @@ -5,6 +5,7 @@ <%= search_form_for(recommended_posts_path) do |f| %> <%= f.input :user_name, label: "User", input_html: { value: params[:search][:user_name], "data-autocomplete": "user" } %> <%= f.input :post_id, label: "Post", input_html: { value: params[:search][:post_id] } %> + <%= f.input :post_tags_match, label: "Tags", input_html: { value: params[:search][:post_tags_match], "data-autocomplete": "tag-query" } %> <%= f.submit "Search" %> <% end %>