-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathTextSearchEngine.java
More file actions
349 lines (288 loc) · 10.9 KB
/
TextSearchEngine.java
File metadata and controls
349 lines (288 loc) · 10.9 KB
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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
package roj.text.pinyin;
import org.jetbrains.annotations.NotNull;
import roj.collect.ArrayList;
import roj.collect.IntList;
import roj.text.CharList;
import roj.text.TextUtil;
import roj.util.ArrayCache;
import roj.util.ArrayUtil;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
/**
* <a href="https://github.com/cjinhuo/text-search-engine">基于动态规划的模糊文本匹配</a>
* with some modifications
* @author Roj234
* @since 2025/07/20 21:14
*/
public class TextSearchEngine {
public record Boundary(int start, int end) implements Comparable<Boundary> {
public int getStart() {return start;}
public int getEnd() {return end;}
public String toString() {return "["+start+","+end+"]";}
public int compareTo(@NotNull Boundary o) {return Integer.compare(start, o.start);}
}
public record BoundaryMapping(
String pinyinString,
List<Boundary> boundary,
String originalString,
int[] originalIndices,
int originalLength,
Object attachment) {
}
public static BoundaryMapping extractBoundaryMapping(String source) {return extractBoundaryMapping(source, null);}
public static BoundaryMapping extractBoundaryMapping(String source, Object attachment) {
// input=a测试的
// output=[[a], [测, ce], [试, shi], [的, de, di]]
List<String[]> choices = JPinyin.getInstance().toChoices(source);
int index = 0;
List<Boundary> boundaries = new java.util.ArrayList<>();
boundaries.add(null); // 访问不到的
IntList originalIndices = new IntList();
CharList pinyinString = new CharList();
for (int i = 0; i < choices.size(); i++) {
String[] value = choices.get(i);
pinyinString.append(value[0]);
boundaries.add(new Boundary(i, index));
originalIndices.add(index);
index++;
for (int j = 1; j < value.length; j++) {
String pinyinItem = value[j];
pinyinString.append(pinyinItem);
int length = pinyinItem.length();
Boundary boundary = new Boundary(i, index);
for (int k = 0; k < length; k++)
boundaries.add(boundary);
index += length;
}
}
originalIndices.add(pinyinString.length());
return new BoundaryMapping(
pinyinString.toStringAndFree(),
boundaries,
source,
originalIndices.toArray(),
choices.size(),
attachment
);
}
public record SearchResult(int score, List<Boundary> hits, BoundaryMapping data) implements Comparable<SearchResult> {
private int hole() {
int offset = hits.get(0).getStart();
int hole = 0;
for (Boundary range : hits) {
int start = range.getStart();
int end = range.getEnd();
if (offset < start) {
hole += start - offset;
}
offset = Math.max(offset, end + 1);
}
hole += hits.get(hits.size()-1).getEnd() - offset;
return hole;
}
public int score() {
var base = score;
// 随便选的值,看起来效果尚可
base -= (hits.size() - 1) * 20;
base -= hole() * 5;
base -= hits.get(0).start * 2;
return base;
}
public int compareTo(@NotNull TextSearchEngine.SearchResult o) {return Integer.compare(o.score(), score());}
}
public SearchResult search(BoundaryMapping source, String target) {
int startIndex = source.originalString.indexOf(target);
if (startIndex != -1)
return new SearchResult(
// 随便选的值,看起来效果尚可
target.length() * 4,
Collections.singletonList(new Boundary(startIndex, startIndex + target.length() - 1)),
source
);
totalScores = 0;
var allHits = hitBoundaries;
allHits.clear();
for (String word : TextUtil.split(target, ' ')) {
if (word.isBlank()) continue;
hitAnyWord: {
for (Boundary range : getRestRanges(source.originalLength, allHits)) {
if (fuzzySearchDP(
source,
word,
range.getStart(),
range.getEnd()
)) break hitAnyWord;
}
allHits.clear();
return null;
}
}
allHits.sort(null);
return new SearchResult(totalScores, ArrayUtil.immutableCopyOf(allHits), source);
}
// 返回0-totalLength区间中未包含在ranges里的片段
private static List<Boundary> getRestRanges(int totalLength, List<Boundary> ranges) {
if (ranges.isEmpty()) return Collections.singletonList(new Boundary(0, totalLength));
ranges.sort(null);
List<Boundary> restRanges = new java.util.ArrayList<>();
int offset = 0;
for (Boundary range : ranges) {
int start = range.getStart();
int end = range.getEnd();
if (offset < start) {
restRanges.add(new Boundary(offset, start));
}
offset = Math.max(offset, end + 1);
}
if (offset < totalLength) {
restRanges.add(new Boundary(offset, totalLength));
}
return restRanges;
}
private int[] matchPositions = ArrayCache.INTS;
private final List<Boundary> hitBoundaries = new ArrayList<>();
private int totalScores;
private record DPTableItem(int matchedCharacters, int matchedLetters, int boundaryStart, int boundaryEnd) {
static final DPTableItem NULL = new DPTableItem(0, 0, -1, -1);
}
private record MatchPathItem(int start, int end, int matchedLetters) {}
// 基于动态规划的中英拼音混合模糊搜索
private boolean fuzzySearchDP(BoundaryMapping source, String target, int startIndex, int endIndex) {
// 获取原始索引数组和原始长度
int[] originalIndices = source.originalIndices;
int originalLength = source.originalLength;
// 计算拼音的截取范围
int pinyinStart = originalIndices[startIndex];
int pinyinEnd = originalIndices[endIndex];
String pinyinString = source.pinyinString.substring(pinyinStart, pinyinEnd);
List<Boundary> boundaries = source.boundary.subList(pinyinStart, Math.min(pinyinEnd+1, source.boundary.size()));
int targetLength = target.length();
int pinyinLength = boundaries.size()-1;
if (targetLength == 0 || pinyinLength < targetLength || originalLength == 0) return false;
// 第一个有效边界
int startBoundary = boundaries.get(1).getStart();
int endBoundary = boundaries.get(1).getEnd();
// 找到目标字符串在拼音字符串中的位置
int[] matchPositions = this.matchPositions;
if (matchPositions.length < targetLength) this.matchPositions = matchPositions = new int[targetLength];
// 如果未能完全匹配目标字符串,返回 null
// 注意这只是一个fail-fast检查,而不是验证,例如“湛zhanshen青”=zs青 可以通过这个检查
{
int j = -1;
for (int i = 0; i < targetLength; i++) {
j = pinyinString.indexOf(target.charAt(i), j+1);
if (j < 0 || j > pinyinLength) return false;
matchPositions[i] = j;
}
}
DPTableItem[] dpTable = new DPTableItem[pinyinLength+1];
Arrays.fill(dpTable, DPTableItem.NULL);
int[] dpScores = new int[pinyinLength+1];
MatchPathItem[][] dpMatchPath = new MatchPathItem[pinyinLength+1][targetLength];
// 动态规划
for (int matchIdx = 0; matchIdx < targetLength; matchIdx++) {
int matchedPinyinIndex = matchPositions[matchIdx];
DPTableItem prevDpTableItem = dpTable[matchedPinyinIndex];
int prevScore = dpScores[matchedPinyinIndex];
dpScores[matchedPinyinIndex] = 0;
dpTable[matchedPinyinIndex] = DPTableItem.NULL;
// 标记是否找到当前字符的有效匹配
var foundValidMatchForCurrentChar = false;
for (int j = matchedPinyinIndex + 1; j <= pinyinLength; j++) {
int tempScore = dpScores[j];
// 获取当前边界信息
Boundary currentBoundary = boundaries.get(j);
int boundaryStartOffset = currentBoundary.getStart() - startBoundary;
int boundaryEndOffset = currentBoundary.getEnd() - endBoundary;
// 检查是否是新词(新汉字开始)
boolean isNewWord = (j - 1) == (currentBoundary.getEnd() - endBoundary) &&
prevDpTableItem.boundaryStart != boundaryStartOffset;
// 检查是否是连续匹配
boolean isContinuation = false;
if (prevDpTableItem.matchedCharacters > 0) {
if (prevDpTableItem.boundaryEnd == boundaryEndOffset) {
// 检查前一个字符是否匹配
if (matchIdx > 0 && j >= 2) {
char prevChar = pinyinString.charAt(j - 2);
if (prevChar == target.charAt(matchIdx - 1)) {
isContinuation = true;
}
}
}
}
// 检查当前字符是否匹配
boolean isEqual = (j <= pinyinLength) &&
(pinyinString.charAt(j - 1) == target.charAt(matchIdx));
// 如果满足匹配条件,更新状态
if (isEqual && (isNewWord || isContinuation) && (matchIdx == 0 || prevScore > 0)) {
int newScore = prevScore + prevDpTableItem.matchedLetters * 2 + 1;
int newMatchedLetters = prevDpTableItem.matchedLetters + 1;
int newMatchedCharacters = prevDpTableItem.matchedCharacters + (isNewWord ? 1 : 0);
if (newScore >= dpScores[j]) {
dpScores[j] = newScore;
dpTable[j] = new DPTableItem(
newMatchedCharacters,
newMatchedLetters,
boundaryStartOffset,
boundaryEndOffset
);
boolean newMatched = newScore > tempScore;
if (newMatched) {
int originalStringIndex = currentBoundary.getStart() - startBoundary;
int startOriginal = originalStringIndex - prevDpTableItem.matchedCharacters + (isNewWord ? 0 : 1);
dpMatchPath[j][matchIdx] = new MatchPathItem(
startOriginal,
originalStringIndex,
newMatchedLetters
);
} else {
dpMatchPath[j][matchIdx] = dpMatchPath[j-1][matchIdx];
}
// 当前字符遍历一遍,如果都没有进入当前 if 分支说明没有匹配到,在外层即可 return
// issue: https://github.com/cjinhuo/text-search-engine/issues/21
foundValidMatchForCurrentChar = true;
continue;
}
}
// 如果不满足匹配条件,继承前一个状态
dpScores[j] = dpScores[j - 1];
dpMatchPath[j][matchIdx] = dpMatchPath[j - 1][matchIdx];
// 计算字符间隔
int gap = boundaries.get(j).getStart() - startBoundary - dpTable[j - 1].boundaryStart;
// 检查是否在同一个词内
boolean isSameWord = false;
if (j < pinyinLength) {
Boundary nextBoundary = boundaries.get(j + 1);
isSameWord = currentBoundary.getStart() == nextBoundary.getStart();
}
// 更新DP表
if (gap == 0 || (j < pinyinLength && gap == 1 && isSameWord)) {
dpTable[j] = dpTable[j - 1];
} else {
dpTable[j] = DPTableItem.NULL;
}
}
if (!foundValidMatchForCurrentChar) return false;
}
// 步骤4: 回溯查找匹配范围
if (dpMatchPath[pinyinLength][targetLength - 1] == null) return false;
int scores = 0;
int gIndex = pinyinLength;
int restMatched = targetLength - 1;
while (restMatched >= 0) {
MatchPathItem item = dpMatchPath[gIndex][restMatched];
if (item == null) break;
scores += dpScores[gIndex];
int start = item.start + startIndex;
int end = item.end + startIndex;
hitBoundaries.add(new Boundary(start, end)); // 逆序,不过最终要sort的
// 更新回溯索引
int pinyinPos = originalIndices[start] - pinyinStart;
gIndex = pinyinPos - 1;
restMatched -= item.matchedLetters;
}
this.totalScores += scores;
return true;
}
}