辞書とプレイヤー名を長い語から順にロックする
このコミットが含まれているのは:
3ファイル変更
+68
-16
検索条件に一致するファイルはありません
@@ -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<String, String> keywordMap = new HashMap<String, String>();
|
||||
ArrayList<String> keywords = new ArrayList<String>();
|
||||
LinkedHashMap<String, String> keywords = new LinkedHashMap<String, String>();
|
||||
if ( LunaChat.getConfig().isJapanizeIgnorePlayerName() ) {
|
||||
keywords.addAll(LunaChat.getPlugin().getOnlinePlayerNames());
|
||||
for ( String name : LunaChat.getPlugin().getOnlinePlayerNames() ) {
|
||||
keywords.put(name, name);
|
||||
}
|
||||
}
|
||||
for ( Map.Entry<String, String> entry : LunaChat.getAPI().getAllDictionary().entrySet() ) {
|
||||
keywords.putIfAbsent(entry.getKey(), entry.getValue());
|
||||
}
|
||||
Map<String, String> 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));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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<String, String> keywordMap = new HashMap<String, String>();
|
||||
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<String> sortByLengthDescending(Collection<String> keywords) {
|
||||
List<String> 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]+") );
|
||||
|
||||
@@ -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<String, String> 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<String, String> 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());
|
||||
}
|
||||
}
|
||||
新しいイシューから参照
ユーザーをブロックする