1 | static int levenIC(S s, S t) { |
2 | // degenerate cases |
3 | if (eqic(s, t)) ret 0; |
4 | int ls = l(s), lt = l(t); |
5 | if (ls == 0) ret lt; |
6 | if (lt == 0) ret ls; |
7 | |
8 | // create two work vectors of integer distances |
9 | int[] v0 = new int[lt + 1]; |
10 | int[] v1 = new int[lt + 1]; |
11 | |
12 | // initialize v0 (the previous row of distances) |
13 | // this row is A[0][i]: edit distance for an empty s |
14 | // the distance is just the number of characters to delete from t |
15 | for (int i = 0; i < v0.length; i++) v0[i] = i; |
16 | |
17 | for (int i = 0; i < ls; i++) { |
18 | // calculate v1 (current row distances) from the previous row v0 |
19 | |
20 | // first element of v1 is A[i+1][0] |
21 | // edit distance is delete (i+1) chars from s to match empty t |
22 | v1[0] = i + 1; |
23 | |
24 | // use formula to fill in the rest of the row |
25 | for (int j = 0; j < lt; j++) { |
26 | int cost = equalsIgnoreCase(s.charAt(i), t.charAt(j)) ? 0 : 1; |
27 | v1[j + 1] = Math.min(Math.min( |
28 | v1[j] + 1, // delete |
29 | v0[j + 1] + 1), // insert |
30 | v0[j] + cost); // edit |
31 | } |
32 | |
33 | // swap arrays |
34 | int[] v = v1; v1 = v0; v0 = v; |
35 | } |
36 | |
37 | return v0[lt]; |
38 | } |
Began life as a copy of #1000388
download show line numbers debug dex old transpilations
Travelled to 9 computer(s): bhatertpkbcr, cfunsshuasjs, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tvejysmllsmz, vouqrxazstgt, whxojlpjdney, xrpafgyirdlv
No comments. add comment
Snippet ID: | #1022301 |
Snippet name: | levenIC - case-insensitive Levenshtein string distance function in JavaX |
Eternal ID of this version: | #1022301/9 |
Text MD5: | 73aaa9978fe7fcca6e71ac7168684f55 |
Author: | stefan |
Category: | javax |
Type: | JavaX fragment (include) |
Public (visible to everyone): | Yes |
Archived (hidden from active list): | No |
Created/modified: | 2020-04-19 12:19:46 |
Source code size: | 1170 bytes / 38 lines |
Pitched / IR pitched: | No / No |
Views / Downloads: | 331 / 381 |
Version history: | 8 change(s) |
Referenced in: | [show references] |