package com.github.mygreen.supercsv.cellprocessor.conversion; import java.util.Arrays; import java.util.Collection; import java.util.Collections; import java.util.HashMap; import java.util.HashSet; import java.util.Map; import java.util.Set; import com.github.mygreen.supercsv.util.Utils; /** * 日本語の全角・半角の文字を置換する。 * * @since 2.0 * @author T.TSUCHIE * */ public class JapaneseCharReplacer { private static final Map<CharCategory, String[][]> CHAR_MAPS; static { final Map<CharCategory, String[][]> map = new HashMap<>(); map.put(CharCategory.Number, new String[][]{ {"0", "0"}, {"1", "1"}, {"2", "2"}, {"3", "3"}, {"4", "4"}, {"5", "5"}, {"6", "6"}, {"7", "7"}, {"8", "8"}, {"9", "9"}, }); map.put(CharCategory.Alpha, new String[][]{ {"a", "a"}, {"b", "b"}, {"c", "c"}, {"d", "d"}, {"e", "e"}, {"f", "f"}, {"g", "g"}, {"h", "h"}, {"i", "i"}, {"j", "j"}, {"k", "k"}, {"l", "l"}, {"m", "m"}, {"n", "n"}, {"o", "o"}, {"p", "p"}, {"q", "q"}, {"r", "r"}, {"s", "s"}, {"t", "t"}, {"u", "u"}, {"v", "v"}, {"w", "w"}, {"x", "x"}, {"y", "y"}, {"z", "z"}, {"A", "A"}, {"B", "B"}, {"C", "C"}, {"D", "D"}, {"E", "E"}, {"F", "F"}, {"G", "G"}, {"H", "H"}, {"I", "I"}, {"J", "J"}, {"K", "K"}, {"L", "L"}, {"M", "M"}, {"N", "N"}, {"O", "O"}, {"P", "P"}, {"Q", "Q"}, {"R", "R"}, {"S", "S"}, {"T", "T"}, {"U", "U"}, {"V", "V"}, {"W", "W"}, {"X", "X"}, {"Y", "Y"}, {"Z", "Z"}, }); map.put(CharCategory.Space, new String[][]{ {" ", " "}, }); map.put(CharCategory.Symbol, new String[][]{ {"!", "!"}, {"\"", "”"}, {"#", "#"}, {"$", "$"}, {"%", "%"}, {"&", "&"}, {"'", "’"}, {"(", "("}, {")", ")"}, {"=", "="}, {"^", "^"}, {"~", "~"}, {"|", "|"}, {"\\", "¥"}, {"`", "‘"}, {"@", "@"}, {"[", "["}, {"]", "]"}, {"{", "{"}, {"}", "}"}, {"+", "+"}, {"-", "ー"}, {";", ";"}, {":", ":"}, {"*", "*"}, {"<", "<"}, {">", ">"}, {",", ","}, {".", "."}, {"?", "?"}, {"/", "/"}, {"_", "_"}, }); map.put(CharCategory.Katakana, new String[][] { {"ア", "ア"}, {"イ", "イ"}, {"ウ", "ウ"}, {"エ", "エ"}, {"オ", "オ"}, {"カ", "カ"}, {"キ", "キ"}, {"ク", "ク"}, {"ケ", "ケ"}, {"コ", "コ"}, {"サ", "サ"}, {"シ", "シ"}, {"ス", "ス"}, {"セ", "セ"}, {"ソ", "ソ"}, {"タ", "タ"}, {"チ", "チ"}, {"ツ", "ツ"}, {"テ", "テ"}, {"ト", "ト"}, {"ナ", "ナ"}, {"ニ", "ニ"}, {"ヌ", "ヌ"}, {"ネ", "ネ"}, {"ノ", "ノ"}, {"ハ", "ハ"}, {"ヒ", "ヒ"}, {"フ", "フ"}, {"ヘ", "ヘ"}, {"ホ", "ホ"}, {"マ", "マ"}, {"ミ", "ミ"}, {"ム", "ム"}, {"メ", "メ"}, {"モ", "モ"}, {"ヤ", "ヤ"}, {"ユ", "ユ"}, {"ヨ", "ヨ"}, {"ラ", "ラ"}, {"リ", "リ"}, {"ル", "ル"}, {"レ", "レ"}, {"ロ", "ロ"}, {"ワ", "ワ"}, {"ヲ", "ヲ"}, {"ン", "ン"}, {"ヴ", "ヴ"}, {"パ", "パ"}, {"ピ", "ピ"}, {"プ", "プ"}, {"ペ", "ペ"}, {"ポ", "ポ"}, {"バ", "バ"}, {"ビ", "ビ"}, {"ブ", "ブ"}, {"ベ", "ベ"}, {"ボ", "ボ"}, {"ァ", "ァ"}, {"ィ", "ィ"}, {"ゥ", "ゥ"}, {"ェ", "ェ"}, {"ォ", "ォ"}, {"ッ", "ッ"} }); CHAR_MAPS = Collections.unmodifiableMap(map); } /** 全角文字への置換処理 */ private final CharReplacer fullCharReplacer = new CharReplacer(); /** 半角文字への置換処理 */ private final CharReplacer halfCharReplacer = new CharReplacer(); public JapaneseCharReplacer(final Collection<CharCategory> categories) { Set<CharCategory> categorySet = new HashSet<>(categories); for(CharCategory category : categorySet) { final String[][] charMap = CHAR_MAPS.get(category); for(String[] map : charMap) { fullCharReplacer.register(map[0], map[1]); halfCharReplacer.register(map[1], map[0]); } } fullCharReplacer.ready(); halfCharReplacer.ready(); } public JapaneseCharReplacer(final CharCategory... categories) { this(Arrays.asList(categories)); } /** * 半角を全角に変換する。 * @param text 変換対象の文字列。 * @return 変換後の値。変換対象の値がnullまたは空文字の場合は、そのまま返します。 */ public String replaceToFullChar(final String text) { if(Utils.isEmpty(text)) { return text; } return fullCharReplacer.replace(text); } /** * 全角を半角に変換する。 * @param text 変換対象の文字列。 * @return 変換後の値。変換対象の値がnullまたは空文字の場合は、そのまま返します。 */ public String replaceToHalfChar(final String text) { if(Utils.isEmpty(text)) { return text; } return halfCharReplacer.replace(text); } }