Skip to content

Instantly share code, notes, and snippets.

@cneud
Created April 18, 2014 16:02
Show Gist options
  • Save cneud/11051543 to your computer and use it in GitHub Desktop.
Save cneud/11051543 to your computer and use it in GitHub Desktop.
Beanshell for calculating Levenshtein distance of two input strings
import org.apache.commons.lang.StringUtils;
import java.text.DecimalFormat;
double ld = StringUtils.getLevenshteinDistance(text1, text2);
double avglen = ((double)text1.length()+(double)text2.length())/2.0;
double m = 1.0-(ld/avglen);
double normVal = (m<0)?0.0:m;
float f = (float) normVal * 100;
DecimalFormat s = new DecimalFormat("##.##");
normalized_levenshtein_distance = s.format(f);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment