Fix #3266: Normalize fullwidth Unicode characters in translated tags.

This commit is contained in:
evazion
2017-08-12 17:13:52 -05:00
parent ee38d82ba0
commit 9b031a9c20
3 changed files with 18 additions and 4 deletions

View File

@@ -87,7 +87,7 @@ module Sources
# Given a tag from the source site, should return an array of corresponding Danbooru tags.
def translate_tag(untranslated_tag)
translated_tags = Tag.where(name: WikiPage.active.other_names_equal([untranslated_tag]).uniq.select(:title))
translated_tags = Tag.where(name: WikiPage.active.other_names_equal(untranslated_tag).uniq.select(:title))
if translated_tags.empty?
normalized_name = TagAlias.to_aliased([Tag.normalize_name(untranslated_tag)])

View File

@@ -45,8 +45,8 @@ class WikiPage < ApplicationRecord
end
end
def other_names_equal(names)
query_sql = names.map(&:to_escaped_for_tsquery).join(" | ")
def other_names_equal(name)
query_sql = name.unicode_normalize(:nfkc).to_escaped_for_tsquery
where("other_names_index @@ to_tsquery('danbooru', E?)", query_sql)
end
@@ -55,7 +55,7 @@ class WikiPage < ApplicationRecord
subquery = WikiPage.from("unnest(string_to_array(other_names, ' ')) AS other_name").where("other_name ILIKE ?", name.to_escaped_for_sql_like)
where(id: subquery)
else
other_names_equal([name])
other_names_equal(name)
end
end

View File

@@ -4,6 +4,9 @@ module Sources
class NijieTest < ActiveSupport::TestCase
context "The source site for a nijie page" do
setup do
CurrentUser.user = FactoryGirl.create(:user)
CurrentUser.ip_addr = "127.0.0.1"
@site = Sources::Site.new("http://nijie.info/view.php?id=213043")
@site.get
end
@@ -24,6 +27,17 @@ module Sources
assert_equal([["眼鏡", "http://nijie.info/search.php?word=%E7%9C%BC%E9%8F%A1"], ["リトルウィッチアカデミア", "http://nijie.info/search.php?word=%E3%83%AA%E3%83%88%E3%83%AB%E3%82%A6%E3%82%A3%E3%83%83%E3%83%81%E3%82%A2%E3%82%AB%E3%83%87%E3%83%9F%E3%82%A2"], ["アーシュラ先生", "http://nijie.info/search.php?word=%E3%82%A2%E3%83%BC%E3%82%B7%E3%83%A5%E3%83%A9%E5%85%88%E7%94%9F"]], @site.tags)
end
should "normalize characters in tags" do
FactoryGirl.create(:tag, :name => "kaga")
FactoryGirl.create(:wiki_page, :title => "kaga", :other_names => "加賀(艦これ)")
@site = Sources::Site.new("http://nijie.info/view.php?id=208316")
@site.get
assert_includes(@site.tags.map(&:first), "加賀(艦これ)")
assert_includes(@site.translated_tags.map(&:first), "kaga")
end
should "get the commentary" do
title = "ジャージの下は"
desc = "「リトルウィッチアカデミア」から無自覚サキュバスぶりを発揮するアーシュラ先生です"