Recently I found this for computing Levenshtein distance in Java.
With Levenshtein you can calculate the differnece between two strings.
This is the Java code for the Levenshtein distance:
public class Levenshtein { private static int minimum(int a, int b, int c) { return Math.min(Math.min(a, b), c); } public static int computeLevenshteinDistance(String str1,String str2) { int[][] distance = new int[str1.length() + 1][str2.length() + 1]; for (int i = 0; i <= str1.length(); i++) distance[i][0] = i; for (int j = 1; j <= str2.length(); j++) distance[0][j] = j; for (int i = 1; i <= str1.length(); i++) for (int j = 1; j <= str2.length(); j++) distance[i][j] = minimum( distance[i - 1][j] + 1, distance[i][j - 1] + 1, distance[i - 1][j - 1] + ((str1.charAt(i - 1) == str2.charAt(j - 1)) ? 0 : 1)); return distance[str1.length()][str2.length()]; } }
In my case I want to have a relation between the levenshtein distance and the length of the strings which have been compared.
So I take the longest string and get the proportion of the distance to its length.
public static double computeDifferenceInPercent(String s1, String s2){ int levenshteinDistance = computeLevenshteinDistance(s1, s2); if (s1.length() > s2.length()) { return ((double)levenshteinDistance / (double)s1.length()); }else { return ((double)levenshteinDistance / (double)s2.length()); } }
But keep in mind, comparing two long strings is impractical…
Der Beitrag Using Levenshtein distance in Java erschien zuerst auf Duckout.