Funzy Tech Musings on technology

21Jan/100

Percentage Comparison of 2 Strings

A recent project had me trying to determine a way to compare two strings and calculate their percentage difference. I was using Java and honestly figured there would be a built in function that would do just that. Unfortunately that was not the case.

After doing a good deal of research I discovered the Levenshtein Distance. Using the psuedocode on Wikipedia (and bits of code elsewhere) I was able to create a function that returns the percentage difference between the strings. I cannot take 100% credit for the script, but feel free to use it in any way you see fit.

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();
int m = t.length();

if (n == 0) {
return m;
}

else if (m == 0) {
return n;
}

int p[] = new int[n+1];
int d[] = new int[n+1];
int _d[];
int i;
int j;

char t_j;

int 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;

d[i] = Math.min(Math.min(d[i-1]+1, p[i]+1),  p[i-1]+cost);
}
_d = p;
p = d;
d = _d;
}

//Determine percentage difference
double levNum = (double)p[n];
double percent = (levNum/Math.max(s.length(),t.length()))*100;
int percentDiff = (int)percent;

return percentDiff;
}

   
Get Adobe Flash playerPlugin by wpburn.com wordpress themes