Revision 48805
Added by Eri Katsari almost 7 years ago
DistanceCalculator.java | ||
---|---|---|
2 | 2 |
|
3 | 3 |
/*Found in https://en.wikibooks.org/wiki/Algorithm_Implementation/Strings/Levenshtein_distance#Java*/ |
4 | 4 |
|
5 |
import org.datanucleus.util.StringUtils; |
|
6 |
import org.hsqldb.lib.StringUtil; |
|
7 |
|
|
8 | 5 |
public class DistanceCalculator { |
9 | 6 |
|
10 | 7 |
public static int getLevenshteinDistance(String s1, String s2) { |
11 |
if (StringUtils.isEmpty(s1) || StringUtils.isEmpty(s2)) {
|
|
8 |
if (s1 == null || s2 == null) {
|
|
12 | 9 |
return 0; |
13 | 10 |
} |
14 | 11 |
|
... | ... | |
59 | 56 |
if (sourceValue != null && targetValue != null) { |
60 | 57 |
sourceValue = sourceValue.replaceAll("[^A-Za-z0-9]", "").toLowerCase(); |
61 | 58 |
targetValue = targetValue.replaceAll("[^A-Za-z0-9]", "").toLowerCase(); |
62 |
int similarChars = 0;
|
|
59 |
double similarChars = 0.0;
|
|
63 | 60 |
int end = sourceValue.length() <= targetValue.length() ? sourceValue.length() : targetValue.length(); |
64 | 61 |
|
65 | 62 |
for (int i = 0; i < end; ++i) { |
66 | 63 |
if (sourceValue.charAt(i) == targetValue.charAt(i)) { |
67 |
++similarChars;
|
|
64 |
similarChars++;
|
|
68 | 65 |
} |
69 | 66 |
} |
70 |
return sourceValue.length() >= targetValue.length() ? (double) similarChars / (double) sourceValue.length() : (double) similarChars / (double) targetValue.length();
|
|
67 |
return sourceValue.length() >= targetValue.length() ? similarChars / (double) sourceValue.length() : similarChars / (double) targetValue.length();
|
|
71 | 68 |
} else { |
72 | 69 |
return 0.0D; |
73 | 70 |
} |
Also available in: Unified diff
Final update- fixed distance algs, added props un wf