public class Zen2HanUtil {
private static final Map<Character, String> zen2HanMap = new HashMap<>();
// https://so-zou.jp/web-app/text/fullwidth-halfwidth/
private static final String ZEN_CHARS = "0 1 2 3 4 5 6 7 8 9 "
+ "A B C D E F G H I J K L M N O P Q R S T U V W X Y Z "
+ "a b c d e f g h i j k l m n o p q r s t u v w x y z "
+ "Ai Ue Okakiku Kekosa Sashisuseso What is it?"
+ "Aiueokakikukekosashisusesotachitsutetonaninune nohahifuhehomamimumemoyayuyorarirurerowon"
+ "ゔ Giguge Gozajizuze Zodajizu de Dobabibu Bebo Papi Puppe Poa Iu ぉ ぉ"
+ "Vuga giggegozajizu zezo da zu de dobababi bu bebo papipeppo wa yeo yo"
+ "-"゜ ,. ・ """
+ "! ” # $ % & ’ ( ) * + , - . / : ; < = > ? @ [ \ ] ^ _ ` { | } ~ "
+ "⦅ ⦆ ¢ £ ¬  ̄ ¦ ¥ ₩ │ ← ↑ → ↓ ■ ○";
private static final String HAN_CHARS = "0 1 2 3 4 5 6 7 8 9 "
+ "A B C D E F G H I J K L M N O P Q R S T U V W X Y Z "
+ "a b c d e f g h i j k l m n o p q r s t u v w x y z "
+ "I'm sorry, I'm sorry, I'm sorry, I'm sorry, I'm sorry, I'm sorry, I'm sorry, I'm sorry."
+ "I'm sorry, I'm sorry, I'm sorry, I'm sorry, I'm sorry, I'm sorry, I'm sorry, I'm sorry."
+ "Vuga Guge Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go"
+ "Vuga Guge Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go"
+ "ー ゚, ‥ ・ """
+ "! \" # $ % & ' ( ) * + , - . / : ; < = > ? @ [ \\ ] ^ _ ` { | } ~ "
+ "⦅ ⦆ ¢ £ ¬ ¯ ¦ ¥ ₩ │ ← ↑ → ↓ ■ ○";
static {
String[] zenSplit = ZEN_CHARS.split(" ");
String[] hanSplit = HAN_CHARS.split(" ");
if (zenSplit.length != hanSplit.length) {
throw new RuntimeException("char count not match. zen=" + zenSplit.length + ", han=" + hanSplit.length);
}
for (int i = 0; i < zenSplit.length; i++) {
zen2HanMap.put(zenSplit[i].charAt(0), hanSplit[i]);
}
}
public static String conv(String s) {
StringBuilder sb = new StringBuilder();
s.chars().mapToObj(c -> (char) c).forEach(c -> {
if (c == ' ') {
sb.append(' ');
} else {
String han = zen2HanMap.get(c);
if (han == null) {
sb.append(c);
} else {
sb.append(han);
}
}
});
return sb.toString();
}
}
Recommended Posts