From b8a75d1674ee9387e23f2693e5dde6705c35ff63 Mon Sep 17 00:00:00 2001 From: routersys Date: Sat, 12 Sep 2026 09:39:18 +0900 Subject: [PATCH] =?UTF-8?q?=E8=BE=9E=E6=9B=B8=E3=81=A8=E3=83=97=E3=83=AC?= =?UTF-8?q?=E3=82=A4=E3=83=A4=E3=83=BC=E5=90=8D=E3=82=92=E9=95=B7=E3=81=84?= =?UTF-8?q?=E8=AA=9E=E3=81=8B=E3=82=89=E9=A0=86=E3=81=AB=E3=83=AD=E3=83=83?= =?UTF-8?q?=E3=82=AF=E3=81=99=E3=82=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../lc3/channel/JapanizeConvertTask.java | 26 +++++------ .../ucchyocean/lc3/japanize/Japanizer.java | 12 ++++- .../lc3/japanize/JapanizerTest.java | 46 +++++++++++++++++++ 3 files changed, 68 insertions(+), 16 deletions(-) create mode 100644 src/test/java/com/github/ucchyocean/lc3/japanize/JapanizerTest.java diff --git a/src/main/java/com/github/ucchyocean/lc3/channel/JapanizeConvertTask.java b/src/main/java/com/github/ucchyocean/lc3/channel/JapanizeConvertTask.java index 149180f..b710ff4 100644 --- a/src/main/java/com/github/ucchyocean/lc3/channel/JapanizeConvertTask.java +++ b/src/main/java/com/github/ucchyocean/lc3/channel/JapanizeConvertTask.java @@ -5,8 +5,8 @@ */ package com.github.ucchyocean.lc3.channel; -import java.util.ArrayList; import java.util.HashMap; +import java.util.LinkedHashMap; import java.util.Map; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -15,6 +15,7 @@ import com.github.ucchyocean.lc3.LunaChat; import com.github.ucchyocean.lc3.event.EventResult; import com.github.ucchyocean.lc3.japanize.IMEConverter; import com.github.ucchyocean.lc3.japanize.JapanizeType; +import com.github.ucchyocean.lc3.japanize.Japanizer; import com.github.ucchyocean.lc3.japanize.YukiKanaConverter; import com.github.ucchyocean.lc3.member.ChannelMember; import com.github.ucchyocean.lc3.util.Utility; @@ -63,12 +64,15 @@ public class JapanizeConvertTask { // 変換対象外のキーワード HashMap keywordMap = new HashMap(); - ArrayList keywords = new ArrayList(); + LinkedHashMap keywords = new LinkedHashMap(); if ( LunaChat.getConfig().isJapanizeIgnorePlayerName() ) { - keywords.addAll(LunaChat.getPlugin().getOnlinePlayerNames()); + for ( String name : LunaChat.getPlugin().getOnlinePlayerNames() ) { + keywords.put(name, name); + } + } + for ( Map.Entry entry : LunaChat.getAPI().getAllDictionary().entrySet() ) { + keywords.putIfAbsent(entry.getKey(), entry.getValue()); } - Map dictionary = - LunaChat.getAPI().getAllDictionary(); // カラーコード削除、URL削除 String deletedURL = Utility.stripColorCode(org.replaceAll(REGEX_URL, " ")); @@ -76,20 +80,12 @@ public class JapanizeConvertTask { // キーワードをロック int index = 0; String keywordLocked = deletedURL; - for ( String keyword : keywords ) { + for ( String keyword : Japanizer.sortByLengthDescending(keywords.keySet()) ) { if ( keywordLocked.contains(keyword) ) { index++; String key = "<" + makeMultibytesDigit(index) + ">"; keywordLocked = keywordLocked.replace(keyword, key); - keywordMap.put(key, keyword); - } - } - for ( String dickey : dictionary.keySet() ) { - if ( keywordLocked.contains(dickey) ) { - index++; - String key = "<" + makeMultibytesDigit(index) + ">"; - keywordLocked = keywordLocked.replace(dickey, key); - keywordMap.put(key, dictionary.get(dickey)); + keywordMap.put(key, keywords.get(keyword)); } } diff --git a/src/main/java/com/github/ucchyocean/lc3/japanize/Japanizer.java b/src/main/java/com/github/ucchyocean/lc3/japanize/Japanizer.java index 0eb5472..e490a86 100644 --- a/src/main/java/com/github/ucchyocean/lc3/japanize/Japanizer.java +++ b/src/main/java/com/github/ucchyocean/lc3/japanize/Japanizer.java @@ -5,7 +5,11 @@ */ package com.github.ucchyocean.lc3.japanize; +import java.util.ArrayList; +import java.util.Collection; +import java.util.Comparator; import java.util.HashMap; +import java.util.List; import java.util.Map; /** @@ -38,7 +42,7 @@ public class Japanizer { HashMap keywordMap = new HashMap(); int index = 0; String keywordLocked = deletedURL; - for ( String dickey : dictionary.keySet() ) { + for ( String dickey : sortByLengthDescending(dictionary.keySet()) ) { if ( keywordLocked.contains(dickey) ) { index++; String key = "<" + makeMultibytesDigit(index) + ">"; @@ -69,6 +73,12 @@ public class Japanizer { * @param org * @return */ + public static List sortByLengthDescending(Collection keywords) { + List sorted = new ArrayList<>(keywords); + sorted.sort(Comparator.comparingInt(String::length).reversed()); + return sorted; + } + private static boolean isNeedToJapanize(String org) { return ( org.getBytes().length == org.length() && !org.matches("[ \\uFF61-\\uFF9F]+") ); diff --git a/src/test/java/com/github/ucchyocean/lc3/japanize/JapanizerTest.java b/src/test/java/com/github/ucchyocean/lc3/japanize/JapanizerTest.java new file mode 100644 index 0000000..460b8d2 --- /dev/null +++ b/src/test/java/com/github/ucchyocean/lc3/japanize/JapanizerTest.java @@ -0,0 +1,46 @@ +/* + * @author ucchy + * @license LGPLv3 + * @copyright Copyright ucchy 2020 + */ +package com.github.ucchyocean.lc3.japanize; + +import java.util.LinkedHashMap; +import java.util.Map; + +import junit.framework.TestCase; + +/** + * + * @author ucchy + */ +public class JapanizerTest extends TestCase { + + public void testLongerKeywordIsLockedFirst() { + + Map dictionary = new LinkedHashMap<>(); + dictionary.put("subame", "スバメ"); + dictionary.put("oosubame", "オオスバメ"); + dictionary.put("koromori", "コロモリ"); + dictionary.put("kokoromori", "ココロモリ"); + dictionary.put("neiti", "ネイティ"); + dictionary.put("neitio", "ネイティオ"); + + assertEquals("スバメとオオスバメ", Japanizer.japanize("subametooosubame", JapanizeType.KANA, dictionary)); + assertEquals("オオスバメとスバメ", Japanizer.japanize("oosubametosubame", JapanizeType.KANA, dictionary)); + assertEquals("コロモリとココロモリ", Japanizer.japanize("koromoritokokoromori", JapanizeType.KANA, dictionary)); + assertEquals("ココロモリとコロモリ", Japanizer.japanize("kokoromoritokoromori", JapanizeType.KANA, dictionary)); + assertEquals("ネイティとネイティオ", Japanizer.japanize("neititoneitio", JapanizeType.KANA, dictionary)); + assertEquals("ネイティオとネイティ", Japanizer.japanize("neitiotoneiti", JapanizeType.KANA, dictionary)); + } + + public void testSortByLengthDescendingKeepsOrderOfSameLength() { + Map dictionary = new LinkedHashMap<>(); + dictionary.put("ab", ""); + dictionary.put("abcd", ""); + dictionary.put("cd", ""); + dictionary.put("abc", ""); + + assertEquals("[abcd, abc, ab, cd]", Japanizer.sortByLengthDescending(dictionary.keySet()).toString()); + } +}