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
package org.springblade.modules.words;
 
import org.springblade.modules.words.internals.BasePinyinMatch;
import org.springblade.modules.words.internals.PinyinDict;
import org.springblade.modules.words.internals.TwoTuple;
 
import java.util.ArrayList;
import java.util.List;
import java.util.function.Function;
 
public class PinyinMatch2<T> extends BasePinyinMatch {
    private final List<T> _list;
    private Function<T, String> _keywordsFunc;
    private Function<T, String> _pinyinFunc;
    private char _splitChar = ',';
 
    /**
     * 拼音匹配, 不支持[0x20000-0x2B81D]
     *
     * @param list
     */
    public PinyinMatch2(final List<T> list) {
        _list = list;
        _keywordsFunc = null;
        _pinyinFunc = null;
    }
 
    /**
     * 设置获取关键字的方法
     *
     * @param keywordsFunc
     */
    public void SetKeywordsFunc(final Function<T, String> keywordsFunc) {
        _keywordsFunc = keywordsFunc;
    }
 
    /**
     * 设置获取拼音的方法
     *
     * @param pinyinFunc
     */
    public void SetPinyinFunc(final Function<T, String> pinyinFunc) {
        _pinyinFunc = pinyinFunc;
    }
 
    /**
     * 设置拼音分隔符
     *
     * @param splitChar
     */
    public void SetPinyinSplitChar(final char splitChar) {
        _splitChar = splitChar;
    }
 
    /**
     * 查询
     *
     * @param keywords
     * @return
     * @throws Exception
     */
    public List<T> Find(String keywords) throws Exception {
        if (_keywordsFunc == null) {
            throw new Exception("请先使用SetKeywordsFunc方法。");
        }
        keywords = keywords.toUpperCase().trim();
        if (keywords == null || keywords.equals("")) {
            return null;
        }
        final List<T> result = new ArrayList<T>();
        final boolean hasPinyin = keywords.matches("^.*?[A-Z]+.*$");// Pattern.matches("[a-zA-Z]",key);
        if (hasPinyin == false) {
            for (final T item : _list) {
                final String keyword = _keywordsFunc.apply(item);
                if (keyword.contains(keywords)) {
                    result.add(item);
                }
            }
            return result;
        }
 
        final List<String> pykeys = SplitKeywords(keywords);
        int minLength = Integer.MAX_VALUE;
        final List<TwoTuple<String, String[]>> list = new ArrayList<TwoTuple<String, String[]>>();
        for (final String pykey : pykeys) {
            final String[] keys = pykey.split(((Character) (char) 0).toString());
            if (minLength > keys.length) {
                minLength = keys.length;
            }
            MergeKeywords(keys, 0, "", list);
        }
 
        final PinyinSearch search = new PinyinSearch();
        search.SetKeywords2(list);
        for (final T item : _list) {
            final String keyword = _keywordsFunc.apply(item);
            if (keyword.length() < minLength) {
                continue;
            }
            String fpy = "";
            String[] pylist;
            if (_pinyinFunc == null) {
                pylist = PinyinDict.GetPinyinList(keyword, 0);
            } else {
                pylist = _pinyinFunc.apply(item).split(((Character) _splitChar).toString());
            }
            for (int j = 0; j < pylist.length; j++) {
                pylist[j] = pylist[j].toUpperCase();
                fpy += pylist[j].charAt(0);
            }
            if (search.Find(fpy, keyword, pylist)) {
                result.add(item);
            }
        }
        return result;
    }
 
    /**
     * 查询,空格为通配符
     *
     * @param keywords
     * @return
     * @throws Exception
     */
    public List<T> FindWithSpace(String keywords) throws Exception {
        if (_keywordsFunc == null) {
            throw new Exception("请先使用SetKeywordsFunc方法。");
        }
        keywords = keywords.toUpperCase().trim();
        if (keywords == null || keywords.equals("")) {
            return null;
        }
        if (keywords.contains(" ") == false) {
            return Find(keywords);
        }
 
        final List<TwoTuple<String, String[]>> list = new ArrayList<TwoTuple<String, String[]>>();
        final List<Integer> indexs = new ArrayList<Integer>();
        int minLength = 0;
        int keysCount;
        {
 
            final String[] keys = keywords.split(" ");
            keysCount = keys.length;
            for (int i = 0; i < keys.length; i++) {
                final String key = keys[i];
                final List<String> pykeys = SplitKeywords(key);
                int min = Integer.MAX_VALUE;
                for (final String pykey : pykeys) {
                    final String[] keys2 = pykey.split(((Character) (char) 0).toString());
                    if (min > keys2.length) {
                        min = keys2.length;
                    }
                    MergeKeywords(keys2, 0, "", list, i, indexs);
                }
                minLength += min;
            }
        }
 
        final PinyinSearch search = new PinyinSearch();
        search.SetKeywords2(list);
        search.SetIndexs(indexs);
 
        final List<T> result = new ArrayList<T>();
        for (final T item : _list) {
            final String keyword = _keywordsFunc.apply(item);
            if (keyword.length() < minLength) {
                continue;
            }
            String fpy = "";
            String[] pylist;
            if (_pinyinFunc == null) {
                pylist = PinyinDict.GetPinyinList(keyword, 0);
            } else {
                pylist = _pinyinFunc.apply(item).split(((Character)_splitChar).toString());
            }
            for (int j = 0; j < pylist.length; j++) {
                pylist[j] = pylist[j].toUpperCase();
                fpy += pylist[j].charAt(0);
            }
            if (search.Find2(fpy, keyword, pylist, keysCount)) {
                result.add(item);
            }
        }
        return result;
    }
 
}