1 package com.github.mygreen.supercsv.cellprocessor.conversion;
2
3 import java.util.Arrays;
4 import java.util.Collection;
5 import java.util.Collections;
6 import java.util.HashMap;
7 import java.util.HashSet;
8 import java.util.Map;
9 import java.util.Set;
10
11 import com.github.mygreen.supercsv.util.Utils;
12
13
14
15
16
17
18
19
20 public class JapaneseCharReplacer {
21
22 private static final Map<CharCategory, String[][]> CHAR_MAPS;
23 static {
24 final Map<CharCategory, String[][]> map = new HashMap<>();
25
26 map.put(CharCategory.Number, new String[][]{
27 {"0", "0"},
28 {"1", "1"},
29 {"2", "2"},
30 {"3", "3"},
31 {"4", "4"},
32 {"5", "5"},
33 {"6", "6"},
34 {"7", "7"},
35 {"8", "8"},
36 {"9", "9"},
37 });
38
39 map.put(CharCategory.Alpha, new String[][]{
40 {"a", "a"},
41 {"b", "b"},
42 {"c", "c"},
43 {"d", "d"},
44 {"e", "e"},
45 {"f", "f"},
46 {"g", "g"},
47 {"h", "h"},
48 {"i", "i"},
49 {"j", "j"},
50 {"k", "k"},
51 {"l", "l"},
52 {"m", "m"},
53 {"n", "n"},
54 {"o", "o"},
55 {"p", "p"},
56 {"q", "q"},
57 {"r", "r"},
58 {"s", "s"},
59 {"t", "t"},
60 {"u", "u"},
61 {"v", "v"},
62 {"w", "w"},
63 {"x", "x"},
64 {"y", "y"},
65 {"z", "z"},
66
67 {"A", "A"},
68 {"B", "B"},
69 {"C", "C"},
70 {"D", "D"},
71 {"E", "E"},
72 {"F", "F"},
73 {"G", "G"},
74 {"H", "H"},
75 {"I", "I"},
76 {"J", "J"},
77 {"K", "K"},
78 {"L", "L"},
79 {"M", "M"},
80 {"N", "N"},
81 {"O", "O"},
82 {"P", "P"},
83 {"Q", "Q"},
84 {"R", "R"},
85 {"S", "S"},
86 {"T", "T"},
87 {"U", "U"},
88 {"V", "V"},
89 {"W", "W"},
90 {"X", "X"},
91 {"Y", "Y"},
92 {"Z", "Z"},
93
94 });
95
96 map.put(CharCategory.Space, new String[][]{
97 {" ", " "},
98
99 });
100
101 map.put(CharCategory.Symbol, new String[][]{
102 {"!", "!"},
103 {"\"", "”"},
104 {"#", "#"},
105 {"$", "$"},
106 {"%", "%"},
107 {"&", "&"},
108 {"'", "’"},
109 {"(", "("},
110 {")", ")"},
111 {"=", "="},
112 {"^", "^"},
113 {"~", "~"},
114 {"|", "|"},
115 {"\\", "¥"},
116 {"`", "‘"},
117 {"@", "@"},
118 {"[", "["},
119 {"]", "]"},
120 {"{", "{"},
121 {"}", "}"},
122 {"+", "+"},
123 {"-", "ー"},
124 {";", ";"},
125 {":", ":"},
126 {"*", "*"},
127 {"<", "<"},
128 {">", ">"},
129 {",", ","},
130 {".", "."},
131 {"?", "?"},
132 {"/", "/"},
133 {"_", "_"},
134
135 });
136
137 map.put(CharCategory.Katakana, new String[][] {
138
139 {"ア", "ア"},
140 {"イ", "イ"},
141 {"ウ", "ウ"},
142 {"エ", "エ"},
143 {"オ", "オ"},
144
145 {"カ", "カ"},
146 {"キ", "キ"},
147 {"ク", "ク"},
148 {"ケ", "ケ"},
149 {"コ", "コ"},
150
151 {"サ", "サ"},
152 {"シ", "シ"},
153 {"ス", "ス"},
154 {"セ", "セ"},
155 {"ソ", "ソ"},
156
157 {"タ", "タ"},
158 {"チ", "チ"},
159 {"ツ", "ツ"},
160 {"テ", "テ"},
161 {"ト", "ト"},
162
163 {"ナ", "ナ"},
164 {"ニ", "ニ"},
165 {"ヌ", "ヌ"},
166 {"ネ", "ネ"},
167 {"ノ", "ノ"},
168
169 {"ハ", "ハ"},
170 {"ヒ", "ヒ"},
171 {"フ", "フ"},
172 {"ヘ", "ヘ"},
173 {"ホ", "ホ"},
174
175 {"マ", "マ"},
176 {"ミ", "ミ"},
177 {"ム", "ム"},
178 {"メ", "メ"},
179 {"モ", "モ"},
180
181 {"ヤ", "ヤ"},
182 {"ユ", "ユ"},
183 {"ヨ", "ヨ"},
184
185 {"ラ", "ラ"},
186 {"リ", "リ"},
187 {"ル", "ル"},
188 {"レ", "レ"},
189 {"ロ", "ロ"},
190
191 {"ワ", "ワ"},
192 {"ヲ", "ヲ"},
193
194 {"ン", "ン"},
195
196 {"ヴ", "ヴ"},
197
198 {"パ", "パ"},
199 {"ピ", "ピ"},
200 {"プ", "プ"},
201 {"ペ", "ペ"},
202 {"ポ", "ポ"},
203
204 {"バ", "バ"},
205 {"ビ", "ビ"},
206 {"ブ", "ブ"},
207 {"ベ", "ベ"},
208 {"ボ", "ボ"},
209
210 {"ァ", "ァ"},
211 {"ィ", "ィ"},
212 {"ゥ", "ゥ"},
213 {"ェ", "ェ"},
214 {"ォ", "ォ"},
215
216 {"ッ", "ッ"}
217
218 });
219
220 CHAR_MAPS = Collections.unmodifiableMap(map);
221
222 }
223
224
225 private final CharReplacersor/conversion/CharReplacer.html#CharReplacer">CharReplacer fullCharReplacer = new CharReplacer();
226
227
228 private final CharReplacersor/conversion/CharReplacer.html#CharReplacer">CharReplacer halfCharReplacer = new CharReplacer();
229
230 public JapaneseCharReplacer(final Collection<CharCategory> categories) {
231
232 Set<CharCategory> categorySet = new HashSet<>(categories);
233 for(CharCategory category : categorySet) {
234 final String[][] charMap = CHAR_MAPS.get(category);
235 for(String[] map : charMap) {
236 fullCharReplacer.register(map[0], map[1]);
237 halfCharReplacer.register(map[1], map[0]);
238 }
239 }
240
241 fullCharReplacer.ready();
242 halfCharReplacer.ready();
243
244 }
245
246 public JapaneseCharReplacer(final CharCategory... categories) {
247 this(Arrays.asList(categories));
248
249 }
250
251
252
253
254
255
256 public String replaceToFullChar(final String text) {
257 if(Utils.isEmpty(text)) {
258 return text;
259 }
260
261 return fullCharReplacer.replace(text);
262 }
263
264
265
266
267
268
269 public String replaceToHalfChar(final String text) {
270
271 if(Utils.isEmpty(text)) {
272 return text;
273 }
274
275 return halfCharReplacer.replace(text);
276
277 }
278
279
280 }