linwe
2024-05-29 c10d6358b9f014375a13821465bc978d0c0da22e
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
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
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
package org.springblade.modules.words.internals;
 
import org.springblade.modules.words.WordsSearch;
import org.springblade.modules.words.WordsSearchResult;
 
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
public class Translate {
    private static WordsSearch s2tSearch;
    private static WordsSearch t2sSearch;
    private static WordsSearch t2twSearch;
    private static WordsSearch tw2tSearch;
    private static WordsSearch t2hkSearch;
    private static WordsSearch hk2tSearch;
 
    /**
     * 转繁体中文
     *
     * @param text
     * @param type 0、繁体中文,1、港澳繁体,2、台湾正体
     * @return
     * @throws Exception
     */
    public static String ToTraditionalChinese(String text, final int type) throws Exception {
        if (type > 2 || type < 0) {
            throw new Exception("type 不支持该类型");
        }
 
        final WordsSearch s2t = GetWordsSearch(true, 0);
        text = TransformationReplace(text, s2t);
        if (type > 0) {
            final WordsSearch t2 = GetWordsSearch(true, type);
            text = TransformationReplace(text, t2);
        }
        return text;
    }
 
    /**
     * 转简体中文
     *
     * @param text
     * @param srcType 0、繁体中文,1、港澳繁体,2、台湾正体
     * @return
     * @throws Exception
     */
    public static String ToSimplifiedChinese(String text, final int srcType) throws Exception {
        if (srcType > 2 || srcType < 0) {
            throw new Exception("srcType 不支持该类型");
        }
        if (srcType > 0) {
            final WordsSearch t2 = GetWordsSearch(false, srcType);
            text = TransformationReplace(text, t2);
        }
        final WordsSearch s2t = GetWordsSearch(false, 0);
        text = TransformationReplace(text, s2t);
        return text;
    }
 
    /**
     * 清理 简繁转换 缓存
     */
    public static void ClearTranslate() {
        s2tSearch = null;
        t2sSearch = null;
        t2twSearch = null;
        tw2tSearch = null;
        t2hkSearch = null;
        hk2tSearch = null;
    }
 
    /**
     *
     * @param text
     * @param wordsSearch
     * @return
     */
    private static String TransformationReplace(String text, WordsSearch wordsSearch) {
        List<WordsSearchResult> ts = wordsSearch.FindAll(text);
        StringBuilder sb = new StringBuilder();
        int index = 0;
        while (index < text.length()) {
            WordsSearchResult t = null;
            int end = -1;
            for (WordsSearchResult wordsSearchResult : ts) {
                if (wordsSearchResult.Start == index) {
                    if (end < wordsSearchResult.End) {
                        end = wordsSearchResult.End;
                        t = wordsSearchResult;
                    }
                }
            }
            if (t == null) {
                sb.append(text.charAt(index));
                index++;
            } else {
                sb.append(wordsSearch._others[t.Index]);
                index = t.End + 1;
            }
        }
        return sb.toString();
    }
 
    private final static Object lockObj = new Object();
    private static WordsSearch GetWordsSearch(Boolean s2t, int srcType) throws IOException {
        if (s2t) {
            if (srcType == 0) {
                if (s2tSearch == null) {
                    synchronized(lockObj){
                        if (s2tSearch == null) {
                            s2tSearch = BuildWordsSearch("s2t.dat", false);
                        }
                    }
                }
                return s2tSearch;
            } else if (srcType == 1) {
                if (t2hkSearch == null) {
                    synchronized(lockObj){
                        if (t2hkSearch == null) {
                            t2hkSearch = BuildWordsSearch("t2hk.dat", false);
                        }
                    }
                }
                return t2hkSearch;
            } else if (srcType == 2) {
                if (t2twSearch == null) {
                    synchronized(lockObj){
                        if (t2twSearch == null) {
                            t2twSearch = BuildWordsSearch("t2tw.dat", false);
                        }
                    }
                }
                return t2twSearch;
            }
        } else {
            if (srcType == 0) {
                if (t2sSearch == null) {
                    synchronized(lockObj){
                        if (t2sSearch == null) {
                            t2sSearch = BuildWordsSearch("t2s.dat", false);
                        }
                    }
                }
                return t2sSearch;
            } else if (srcType == 1) {
                if (hk2tSearch == null) {
                    synchronized(lockObj){
                        if (hk2tSearch == null) {
                            hk2tSearch = BuildWordsSearch("t2hk.dat", true);
                        }
                    }
                }
                return hk2tSearch;
            } else if (srcType == 2) {
                if (tw2tSearch == null) {
                    synchronized(lockObj){
                        if (tw2tSearch == null) {
                            tw2tSearch = BuildWordsSearch("t2tw.dat", true);
                        }
                    }
                }
                return tw2tSearch;
            }
        }
        return null;
    }
 
    private static WordsSearch BuildWordsSearch(String fileName, Boolean reverse) throws IOException {
        Map<String, String> dict = GetTransformationDict(fileName);
        List<String> Keys = new ArrayList<String>();
        List<String> Values = new ArrayList<String>();
        dict.forEach((k, v) -> {
            Keys.add(k);
            Values.add(v);
        });
        WordsSearch wordsSearch = new WordsSearch();
        if (reverse) {
            wordsSearch.SetKeywords(Values);
            String[] temp = new String[Keys.size()];
            wordsSearch._others = Keys.toArray(temp);
        } else {
            wordsSearch.SetKeywords(Keys);
            String[] temp = new String[Keys.size()];
            wordsSearch._others = Values.toArray(temp);
        }
        return wordsSearch;
    }
 
    static Map<String, String> GetTransformationDict(String fileName) throws IOException {
        String resourceName = fileName;
        InputStream u1 = WordsSearch.class.getClassLoader().getResourceAsStream(resourceName);
        BufferedReader br = new BufferedReader(new InputStreamReader(u1));
 
        String tStr = "";
        Map<String, String> dict = new HashMap<String, String>();
        while ((tStr = br.readLine()) != null) {
            String[] ss = tStr.split("\t");
            if (ss.length < 2) {
                continue;
            }
            dict.put(ss[0], ss[1]);
        }
        br.close();
        return dict;
    }
}