forked from e621ng/e621ng
[Autocomplete] Remove unused files
This commit is contained in:
parent
a410d93576
commit
fa1fbcd72f
@ -1,115 +0,0 @@
|
||||
module TagAutocomplete
|
||||
extend self
|
||||
|
||||
PREFIX_BOUNDARIES = "(_/:;-"
|
||||
LIMIT = 10
|
||||
|
||||
class Result < Struct.new(:name, :post_count, :category, :antecedent_name, :source)
|
||||
include ActiveModel::Serializers::JSON
|
||||
include ActiveModel::Serializers::Xml
|
||||
|
||||
def attributes
|
||||
(members + [:weight]).map { |x| [x.to_s, send(x)] }.to_h
|
||||
end
|
||||
|
||||
def weight
|
||||
case source
|
||||
when :exact then 1.0
|
||||
when :prefix then 0.8
|
||||
when :alias then 0.2
|
||||
when :correct then 0.1
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def search(query)
|
||||
query = Tag.normalize_name(query)
|
||||
|
||||
candidates = count_sort(
|
||||
query,
|
||||
search_exact(query, 8) +
|
||||
search_prefix(query, 4) +
|
||||
search_correct(query, 2) +
|
||||
search_aliases(query, 3)
|
||||
)
|
||||
end
|
||||
|
||||
def count_sort(query, words)
|
||||
words.uniq(&:name).sort_by do |x|
|
||||
x.post_count * x.weight
|
||||
end.reverse.slice(0, LIMIT)
|
||||
end
|
||||
|
||||
def search_exact(query, n=4)
|
||||
Tag
|
||||
.where("name like ? escape e'\\\\'", query.to_escaped_for_sql_like + "%")
|
||||
.where("post_count > 0")
|
||||
.order("post_count desc")
|
||||
.limit(n)
|
||||
.pluck(:name, :post_count, :category)
|
||||
.map {|row| Result.new(*row, nil, :exact)}
|
||||
end
|
||||
|
||||
def search_correct(query, n=2)
|
||||
if query.size <= 3
|
||||
return []
|
||||
end
|
||||
|
||||
Tag
|
||||
.where("name % ?", query)
|
||||
.where("abs(length(name) - ?) <= 3", query.size)
|
||||
.where("name like ? escape E'\\\\'", query[0].to_escaped_for_sql_like + '%')
|
||||
.where("post_count > 0")
|
||||
.order(Arel.sql("similarity(name, #{Tag.connection.quote(query)}) DESC"))
|
||||
.limit(n)
|
||||
.pluck(:name, :post_count, :category)
|
||||
.map {|row| Result.new(*row, nil, :correct)}
|
||||
end
|
||||
|
||||
def search_prefix(query, n=3)
|
||||
if query.size >= 5
|
||||
return []
|
||||
end
|
||||
|
||||
if query.size <= 1
|
||||
return []
|
||||
end
|
||||
|
||||
if query =~ /[-_()]/
|
||||
return []
|
||||
end
|
||||
|
||||
if query.size >= 3
|
||||
min_post_count = 0
|
||||
else
|
||||
min_post_count = 5_000
|
||||
n += 2
|
||||
end
|
||||
|
||||
regexp = "([a-z0-9])[a-z0-9']*($|[^a-z0-9']+)"
|
||||
Tag
|
||||
.where('regexp_replace(name, ?, ?, ?) like ?', regexp, '\1', 'g', query.to_escaped_for_sql_like + '%')
|
||||
.where("post_count > ?", min_post_count)
|
||||
.where("post_count > 0")
|
||||
.order("post_count desc")
|
||||
.limit(n)
|
||||
.pluck(:name, :post_count, :category)
|
||||
.map {|row| Result.new(*row, nil, :prefix)}
|
||||
end
|
||||
|
||||
def search_aliases(query, n=10)
|
||||
wildcard_name = query + "*"
|
||||
TagAlias
|
||||
.select("tags.name, tags.post_count, tags.category, tag_aliases.antecedent_name")
|
||||
.joins("INNER JOIN tags ON tags.name = tag_aliases.consequent_name")
|
||||
.where("tag_aliases.antecedent_name LIKE ? ESCAPE E'\\\\'", wildcard_name.to_escaped_for_sql_like)
|
||||
.active
|
||||
.where("tags.name NOT LIKE ? ESCAPE E'\\\\'", wildcard_name.to_escaped_for_sql_like)
|
||||
.where("tag_aliases.post_count > 0")
|
||||
.order("tag_aliases.post_count desc")
|
||||
.limit(n)
|
||||
.pluck(:name, :post_count, :category, :antecedent_name)
|
||||
.map {|row| Result.new(*row, :alias)}
|
||||
end
|
||||
end
|
||||
|
@ -1,115 +0,0 @@
|
||||
require 'test_helper'
|
||||
|
||||
class TagAutocompleteTest < ActiveSupport::TestCase
|
||||
subject { TagAutocomplete }
|
||||
|
||||
context "#search" do
|
||||
should "be case insensitive" do
|
||||
create(:tag, name: "abcdef", post_count: 1)
|
||||
assert_equal(["abcdef"], subject.search("A").map(&:name))
|
||||
end
|
||||
|
||||
should "not return duplicates" do
|
||||
create(:tag, name: "red_eyes", post_count: 5001)
|
||||
assert_equal(%w[red_eyes], subject.search("re").map(&:name))
|
||||
end
|
||||
end
|
||||
|
||||
context "#search_exact" do
|
||||
setup do
|
||||
@tags = [
|
||||
create(:tag, name: "abcdef", post_count: 1),
|
||||
create(:tag, name: "abczzz", post_count: 2),
|
||||
create(:tag, name: "abcyyy", post_count: 0),
|
||||
create(:tag, name: "bbbbbb")
|
||||
]
|
||||
end
|
||||
|
||||
should "find the tags" do
|
||||
expected = [
|
||||
@tags[1],
|
||||
@tags[0]
|
||||
].map(&:name)
|
||||
assert_equal(expected, subject.search_exact("abc", 3).map(&:name))
|
||||
end
|
||||
end
|
||||
|
||||
context "#search_correct" do
|
||||
setup do
|
||||
CurrentUser.stubs(:id).returns(1)
|
||||
|
||||
@tags = [
|
||||
create(:tag, name: "abcde", post_count: 1),
|
||||
create(:tag, name: "abcdz", post_count: 2),
|
||||
|
||||
# one char mismatch
|
||||
create(:tag, name: "abcez", post_count: 2),
|
||||
|
||||
# too long
|
||||
create(:tag, name: "abcdefghijk", post_count: 2),
|
||||
|
||||
# wrong prefix
|
||||
create(:tag, name: "bbcdef", post_count: 2),
|
||||
|
||||
# zero post count
|
||||
create(:tag, name: "abcdy", post_count: 0),
|
||||
|
||||
# completely different
|
||||
create(:tag, name: "bbbbb")
|
||||
]
|
||||
end
|
||||
|
||||
should "find the tags" do
|
||||
expected = [
|
||||
@tags[0],
|
||||
@tags[1],
|
||||
@tags[2]
|
||||
].map(&:name)
|
||||
assert_equal(expected, subject.search_correct("abcd", 3).map(&:name))
|
||||
end
|
||||
end
|
||||
|
||||
context "#search_prefix" do
|
||||
setup do
|
||||
@tags = [
|
||||
create(:tag, name: "abcdef", post_count: 1),
|
||||
create(:tag, name: "alpha_beta_cat", post_count: 2),
|
||||
create(:tag, name: "alpha_beta_dat", post_count: 0),
|
||||
create(:tag, name: "alpha_beta_(cane)", post_count: 2),
|
||||
create(:tag, name: "alpha_beta/cane", post_count: 2)
|
||||
]
|
||||
end
|
||||
|
||||
should "find the tags" do
|
||||
expected = [
|
||||
@tags[1],
|
||||
@tags[3],
|
||||
@tags[4]
|
||||
].map(&:name)
|
||||
assert_equal(expected, subject.search_prefix("abc", 3).map(&:name))
|
||||
end
|
||||
end
|
||||
|
||||
context "#search_aliases" do
|
||||
setup do
|
||||
@user = create(:user)
|
||||
@tags = [
|
||||
create(:tag, name: "/abc", post_count: 0),
|
||||
create(:tag, name: "abcdef", post_count: 1),
|
||||
create(:tag, name: "zzzzzz", post_count: 1),
|
||||
]
|
||||
as_user do
|
||||
@aliases = [
|
||||
create(:tag_alias, antecedent_name: "/abc", consequent_name: "abcdef", status: "active", post_count: 1)
|
||||
]
|
||||
end
|
||||
end
|
||||
|
||||
should "find the tags" do
|
||||
results = subject.search_aliases("/abc", 3)
|
||||
assert_equal(1, results.size)
|
||||
assert_equal("abcdef", results[0].name)
|
||||
assert_equal("/abc", results[0].antecedent_name)
|
||||
end
|
||||
end
|
||||
end
|
Loading…
Reference in New Issue
Block a user