如果你想搜索不精確匹配,我會盡量讓MyAppRecord
的地方
public class MyAppRecord {
private String record;
private int deviance;
}
的ArrayList
得到每個記錄要找到字符串的偏差:
public static int getLevenshteinDistance (String s, String t) {
if (s == null || t == null) {
throw new IllegalArgumentException("Strings must not be null");
}
int n = s.length(); // length of s
int m = t.length(); // length of t
if (n == 0) {
return m;
} else if (m == 0) {
return n;
}
int p[] = new int[n+1]; //'previous' cost array, horizontally
int d[] = new int[n+1]; // cost array, horizontally
int _d[]; //placeholder to assist in swapping p and d
// indexes into strings s and t
int i; // iterates through s
int j; // iterates through t
char t_j; // jth character of t
int cost; // cost
for (i = 0; i<=n; i++) {
p[i] = i;
}
for (j = 1; j<=m; j++) {
t_j = t.charAt(j-1);
d[0] = j;
for (i=1; i<=n; i++) {
cost = s.charAt(i-1)==t_j ? 0 : 1;
// minimum of cell to the left+1, to the top+1, diagonally left and up +cost
d[i] = Math.min(Math.min(d[i-1]+1, p[i]+1), p[i-1]+cost);
}
// copy current distance counts to 'previous row' distance counts
_d = p;
p = d;
d = _d;
}
// our last action in the above loop was to switch d and p, so p now
// actually has the most recent cost counts
return p[n];
}
}
將其保存到您的MyAppRecord
-object最後排序您ArrayList
其MyAppRecord
-obje的deviance
CTS。
請注意,這可能需要一些時間,具體取決於您的記錄集。並且注意,沒有辦法通過搜索狗來告訴她的狗狗或狗狗B在你的列表中的某個位置上。
閱讀Levensthein距離以瞭解它的工作原理。你可能會想到整理出可能需要長/短的字符串才能獲得可能達到閾值的距離。
也可以將「足夠好」的結果複製到不同的ArrayList
。
什麼是「最佳」結果?這是完美的搭配嗎? – sschrass
例如給定的字符串:「Fox今天結束等等..」和搜索字符串是「今天」,那麼它應該返回true,因爲字符串存在。 – Nirav
這個信息應該在問題中!你可能想閱讀有關正則表達式,模式和匹配器。 – sschrass