linwe
2024-08-09 8b7258c9427882bb1798f1502eaa35184c6e374e
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
package org.springblade.modules.words.internals;
 
import org.springblade.modules.words.WordsSearch;
import org.springblade.modules.words.WordsSearchResult;
 
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
 
public class BasePinyinMatch {
 
    public class PinyinSearch extends BaseSearch {
        String[][] _keywordPinyins;
        int[] _indexs;
 
        public void SetIndexs(final int[] indexs) {
            _indexs = indexs;
        }
        public void SetIndexs(List<Integer> indexs) {
            _indexs=new int[indexs.size()];
            for (int i = 0; i < indexs.size(); i++) {
                _indexs[i]=indexs.get(i);
            }
        }
 
        public void SetKeywords2(List<TwoTuple<String, String[]>> keywords) {
            _keywords = new String[keywords.size()];
            _keywordPinyins = new String[keywords.size()][];
            for (int i = 0; i < keywords.size(); i++) {
                _keywords[i] = keywords.get(i).Item1;
                _keywordPinyins[i] = keywords.get(i).Item2;
            }
            SetKeywords();
        }
 
        public boolean Find(final String text, final String hz, final String[] pinyins) {
            TrieNode2 ptr = null;
            for (int i = 0; i < text.length(); i++) {
                final Character t = text.charAt(i);
                TrieNode2 tn;
                if (ptr == null) {
                    tn = _first[t];
                } else {
                    if (ptr.HasKey(t) == false) {
                        tn = _first[t];
                    } else {
                        tn = ptr.GetValue(t);
                    }
                }
                if (tn != null) {
                    if (tn.End) {
                        for (final int result : tn.Results) {
                            final String keyword = _keywords[result];
                            final int start = i + 1 - keyword.length();
                            boolean isok = true;
                            final String[] keywordPinyins = _keywordPinyins[result];
 
                            for (int j = 0; j < keyword.length(); j++) {
                                final int idx = start + j;
                                final String py = keywordPinyins[j];
                                if (py.length() == 1 && py.charAt(0) >= 0x3400 && py.charAt(0) <= 0x9fd5) {
                                    if (hz.charAt(idx) != py.charAt(0)) {
                                        isok = false;
                                        break;
                                    }
                                } else {
                                    if (pinyins[idx].startsWith(py) == false) {
                                        isok = false;
                                        break;
                                    }
                                }
                            }
                            if (isok) {
                                return true;
                            }
                        }
                    }
                }
                ptr = tn;
            }
            return false;
        }
 
        public boolean Find2(final String text, final String hz, final String[] pinyins, final int keysCount) {
            int findCount = 0;
            int lastWordsIndex = -1;
            TrieNode2 ptr = null;
            for (int i = 0; i < text.length(); i++) {
                final Character t = text.charAt(i);
                TrieNode2 tn;
                if (ptr == null) {
                    tn = _first[t];
                } else {
                    if (ptr.HasKey(t) == false) {
                        tn = _first[t];
                    } else {
                        tn = ptr.GetValue(t);
                    }
                }
                if (tn != null) {
                    if (tn.End) {
                        for (final Integer result : tn.Results) {
                            final int index = _indexs[result];
                            if (index != findCount) {
                                continue;
                            }
 
                            final String keyword = _keywords[result];
                            final int start = i + 1 - keyword.length();
                            if (lastWordsIndex >= start) {
                                continue;
                            }
 
                            boolean isok = true;
                            final String[] keywordPinyins = _keywordPinyins[result];
 
                            for (int j = 0; j < keyword.length(); j++) {
                                final int idx = start + j;
                                final String py = keywordPinyins[j];
                                if (py.length() == 1 && py.charAt(0) >= 0x3400 && py.charAt(0) <= 0x9fd5) {
                                    if (hz.charAt(idx) != py.charAt(0)) {
                                        isok = false;
                                        break;
                                    }
                                } else {
                                    if (pinyins[idx].startsWith(py) == false) {
                                        isok = false;
                                        break;
                                    }
                                }
                            }
                            if (isok) {
                                findCount++;
                                lastWordsIndex = i;
                                if (findCount == keysCount) {
                                    return true;
                                }
                                break;
                            }
                        }
                    }
                }
                ptr = tn;
            }
            return false;
        }
 
    }
 
    protected void MergeKeywords(String[] keys, int id, String keyword, List<TwoTuple<String, String[]>> list)
            throws NumberFormatException, IOException {
        if (id >= keys.length) {
            TwoTuple<String, String[]> tuple = new TwoTuple<String, String[]>(keyword, keys);
            list.add(tuple);
            return;
        }
        String key = keys[id];
        if (key.charAt(0) >= 0x3400 && key.charAt(0) <= 0x9fd5) {
            List<String> all = PinyinDict.GetAllPinyin(key.charAt(0), 0);
            Set<Character> fpy = new HashSet<Character>();
            for (String item : all) {
                fpy.add(item.charAt(0));
            }
            for (Character item : fpy) {
                MergeKeywords(keys, id + 1, keyword + item, list);
            }
        } else {
            MergeKeywords(keys, id + 1, keyword + key.charAt(0), list);
        }
    }
 
    protected void MergeKeywords(String[] keys, int id, String keyword, List<TwoTuple<String, String[]>> list,
            int index, List<Integer> indexs) throws NumberFormatException, IOException {
        if (id >= keys.length) {
            TwoTuple<String, String[]> tuple = new TwoTuple<String, String[]>(keyword, keys);
            list.add(tuple);
            indexs.add(index);
            return;
        }
        String key = keys[id];
        if (key.charAt(0) >= 0x3400 && key.charAt(0) <= 0x9fd5) {
            List<String> all = PinyinDict.GetAllPinyin(key.charAt(0), 0);
            Set<Character> fpy = new HashSet<Character>();
            for (String item : all) {
                fpy.add(item.charAt(0));
            }
            for (Character item : fpy) {
                MergeKeywords(keys, id + 1, keyword + item, list, index, indexs);
            }
        } else {
            MergeKeywords(keys, id + 1, keyword + key.charAt(0), list, index, indexs);
        }
    }
 
    protected List<String> SplitKeywords(String key) throws NumberFormatException, IOException {
        InitPinyinSearch();
        List<TextNode> textNodes = new ArrayList<TextNode>();
        for (int i = 0; i <= key.length(); i++) {
            textNodes.add(new TextNode());
        }
        textNodes.get(textNodes.size() - 1).End = true;
 
        for (int i = 0; i < key.length(); i++) {
            TextLine line = new TextLine();
            line.Next = textNodes.get(i + 1);
            line.Words = ((Character) key.charAt(i)).toString();
            textNodes.get(i).Children.add(line);
        }
 
        List<WordsSearchResult> all = _wordsSearch.FindAll(key);
        for (WordsSearchResult searchResult : all) {
            TextLine line = new TextLine();
            line.Next = textNodes.get(searchResult.End + 1);
            line.Words = searchResult.Keyword;
            textNodes.get(searchResult.Start).Children.add(line);
        }
 
        List<String> list = new ArrayList<String>();
        BuildKsywords(textNodes.get(0), 0, "", list);
        return list;
    }
 
    private void BuildKsywords(TextNode textNode, int id, String keywords, List<String> list) {
        if (textNode.End) {
            String k = keywords.substring(1);
            if (list.contains(k) == false) {
                list.add(k);
            }
            return;
        }
        for (TextLine item : textNode.Children) {
            BuildKsywords(item.Next, id + 1, keywords + (char) 0 + item.Words, list);
        }
    }
 
    class TextNode {
        public boolean End;
        public List<TextLine> Children = new ArrayList<TextLine>();
    }
 
    class TextLine {
        public String Words;
        public TextNode Next;
    }
 
    private static WordsSearch _wordsSearch;
 
    private void InitPinyinSearch() throws NumberFormatException, IOException {
        if (_wordsSearch == null) {
            List<String> allPinyins = new ArrayList<String>();
            String[] pys = PinyinDict.getPyShow();
            for (int i = 1; i < pys.length; i += 2) {
                String py = pys[i].toUpperCase();
                for (int j = 1; j <= py.length(); j++) {
                    String key = py.substring(0, j);
                    if (allPinyins.contains(key) == false) {
                        allPinyins.add(key);
                    }
 
                }
            }
            WordsSearch wordsSearch = new WordsSearch();
            wordsSearch.SetKeywords(allPinyins);
            _wordsSearch = wordsSearch;
        }
    }
}