Java Utililty Methods String Levenshtein Distance

List of utility methods to do String Levenshtein Distance

Description

The list of methods to do String Levenshtein Distance are organized into topic(s).

Method

int[][]levenshteinDistance(String wordForm, String lemma)
Computes the Levenshtein distance of two strings in a matrix.
int wordLength = wordForm.length();
int lemmaLength = lemma.length();
int cost;
int[][] distance = new int[wordLength + 1][lemmaLength + 1];
if (wordLength == 0) {
    return distance;
if (lemmaLength == 0) {
...
intlevenshteinDistance(String x, String y)
levenshtein Distance
int n = x.length(); 
int m = y.length(); 
if (n == 0)
    return m;
if (m == 0)
    return n;
int p[] = new int[n + 1]; 
int d[] = new int[n + 1]; 
...
intlevenshteinDistance(String[] a, String[] b)
Straight from Wikipedia
int[][] distance = new int[a.length + 1][b.length + 1];
for (int i = 0; i <= a.length; i++) {
    distance[i][0] = i;
for (int j = 1; j <= b.length; j++) {
    distance[0][j] = j;
for (int i = 1; i <= a.length; i++) {
...
doublelevenshteinDistanceRatio(CharSequence lhs, CharSequence rhs)
Returns string similarity as a ratio.
int maxDist = lhs.length();
if (rhs.length() > maxDist) {
    maxDist = rhs.length();
if (maxDist == 0) {
    return 1;
return (maxDist - Double.valueOf(levenshteinDistance(lhs, rhs))) / Double.valueOf(maxDist);
...
booleanlevenshteinEquals(double threshold, String dom1, String dom2)
This method implements the equality check between two strings using the Levenshtein Distance algorithm
double l = levenshteinDistance(dom1, dom2);
if (l >= ((dom1.length() + dom2.length()) * (1.0 - threshold))) {
    return false;
return true;
floatlevenshteinSimilarity(String string1, String string2)

Copyright (c) 2011, Regents of the University of Colorado
All rights reserved.
if (string1.length() == 0 && string2.length() == 0) {
    return 1.0f;
int editDistance = editDistance(string1, string2);
float similarity = (float) editDistance / (string1.length() + string2.length());
return 1 - Math.min(similarity, 1.0f);