move vote similarity code into danbooru, add listing for super voters

This commit is contained in:
r888888888
2016-09-19 16:43:15 -07:00
parent 6241a50c31
commit bf2246f895
7 changed files with 101 additions and 11 deletions

View File

@@ -0,0 +1,8 @@
class SuperVotersController < ApplicationController
before_filter :member_only
def index
@super_voters = SuperVoter.all
end
end

View File

@@ -0,0 +1,58 @@
require "set"
class PostVoteSimilarity
class Element
attr_reader :user_id, :score
def initialize(user_id, score)
@user_id = user_id
@score = score
end
def <=>(rhs)
score <=> rhs.score
end
end
attr_reader :user_id
def initialize(user_id)
@user_id = user_id
end
# returns user ids with strong negative correlation
def calculate_negative(limit = 10)
posts0 = PostVote.negative_post_ids(user_id)
set = SortedSet.new
PostVote.positive_user_ids.each do |uid|
posts1 = PostVote.positive_post_ids(uid)
set.add(Element.new(uid, calculate_with_cosine(posts0, posts1)))
end
set.first(limit)
end
# returns user ids with strong positive correlation
def calculate_positive(limit = 10)
posts0 = PostVote.positive_post_ids(user_id)
set = SortedSet.new
PostVote.positive_user_ids.each do |uid|
posts1 = PostVote.positive_post_ids(uid)
set.add(Element.new(uid, calculate_with_cosine(posts0, posts1)))
end
set.first(limit)
end
def calculate_with_cosine(posts0, posts1)
a = (posts0 & posts1).size
div = Math.sqrt(posts0.size * posts1.size)
if div == 0
0
else
a / div
end
end
end

View File

@@ -12,6 +12,18 @@ class PostVote < ActiveRecord::Base
where("created_at < ?", 30.days.ago).delete_all
end
def self.positive_user_ids
select_values_sql("select user_id from post_votes where score > 0 group by user_id having count(*) > 100")
end
def self.negative_post_ids(user_id)
select_values_sql("select post_id from post_votes where score < 0 and user_id = ?", user_id)
end
def self.positive_post_ids(user_id)
select_values_sql("select post_id from post_votes where score > 0 and user_id = ?", user_id)
end
def score=(x)
if x == "up"
Post.where(:id => post_id).update_all("score = score + #{magnitude}, up_score = up_score + #{magnitude}")

View File

@@ -13,18 +13,11 @@ class SuperVoter < ActiveRecord::Base
def self.init!
prune!
report = Reports::UserSimilarity.new(User.admins.first.id)
report.prime("post_vote_similarity")
report = PostVoteSimilarity.new(User.admins.first.id)
report.result.scan(/\S+/).in_groups_of(2).each do |user_id, score|
unless where("user_id = ?", user_id.to_i).exists?
create(:user_id => user_id)
end
end
User.admins.each do |user|
unless where("user_id = ?", user.id).exists?
create(:user_id => user.id)
report.calculate_positive.each do |element|
unless where("user_id = ?", element.user_id).exists?
create(:user_id => element.user_id)
end
end
end

View File

@@ -109,6 +109,7 @@
<li><%= link_to("Signup", new_user_path) %></li>
<li><%= link_to("Terms of Service", terms_of_service_path) %></li>
<li><%= link_to("Upgrade Information", new_user_upgrade_path) %></li>
<li><%= link_to("Super Voters", super_voters_path)</li>
</ul>
<ul>
<li><h1>Admin</h1></li>

View File

@@ -0,0 +1,17 @@
<div id="c-super-voters">
<div id="a-index">
<h1>Super Voters</h1>
<p>The following users will increment or decrement a post's score by <%= SuperVoter::MAGNITUDE %> when they vote. This list is regenerated once a week.</p>
<ul>
<% @super_voters.each do |super_voter| %>
<li><%= link_to_if CurrentUser.user.is_janitor?, super_voter.user.name, posts_path(tags: "upvote:#{super_voter.user.name}") %></li>
<% end %>
</ul>
</div>
</div>
<% content_for(:page_title) do %>
Super Voters - <%= Danbooru.config.app_name %>
<% end %>

View File

@@ -243,6 +243,7 @@ Rails.application.routes.draw do
end
end
resource :source, :only => [:show]
resources :super_voters, :only => [:index]
resources :tags do
resource :correction, :only => [:new, :create, :show], :controller => "tag_corrections"
collection do