feat: Mod chinese search

This commit is contained in:
huanghongxun
2021-09-11 23:49:07 +08:00
parent 0df1b62c3e
commit 0032242da6
10 changed files with 4713 additions and 4533 deletions

View File

@@ -25,6 +25,7 @@ import java.util.Collection;
import java.util.LinkedList;
import java.util.List;
import java.util.StringTokenizer;
import java.util.regex.Pattern;
/**
*
@@ -243,4 +244,42 @@ public final class StringUtils {
}
return result.toString();
}
/**
* Class for computing the longest common subsequence between strings.
*/
public static class LongestCommonSubsequence {
// We reuse dynamic programming storage array here to reduce allocations.
private final int[][] f;
private final int maxLengthA;
private final int maxLengthB;
public LongestCommonSubsequence(int maxLengthA, int maxLengthB) {
this.maxLengthA = maxLengthA;
this.maxLengthB = maxLengthB;
f = new int[maxLengthA + 1][];
for (int i = 0; i <= maxLengthA; i++) {
f[i] = new int[maxLengthB + 1];
}
}
public int calc(CharSequence a, CharSequence b) {
if (a.length() > maxLengthA || b.length() > maxLengthB) {
throw new IllegalArgumentException("Too large length");
}
for (int i = 1; i <= a.length(); i++) {
for (int j = 1; j <= b.length(); j++) {
if (a.charAt(i - 1) == b.charAt(j - 1)) {
f[i][j] = 1 + f[i - 1][j - 1];
} else {
f[i][j] = Math.max(f[i - 1][j], f[i][j - 1]);
}
}
}
return f[a.length()][b.length()];
}
}
public static final Pattern CHINESE_PATTERN = Pattern.compile("[\\u4e00-\\u9fa5]");
}