CFLib.org – Common Function Library Project

levDistance(s, t)

Last updated March 15, 2004
Download UDF

author

Nicholas Zographos                                Nicholas Zographos

Version: 1 | Requires: ColdFusion 5 | Library: StrLib

 
Rated 0 time(s). Average Rating: 0

Description:
Computes the Levenshtein distance between two strings which is defined as the number of replacements, insertions or deletions necessary to transform the first string into the second. Ported from the Java version at http://www.merriampark.com/ld.htm

Return Values:
Returns a number.

Example:

<cfoutput>#levDistance("first string", "second string")#</cfoutput>
<!--- should output 6 --->

Parameters:

Name Description Required
s First string. Yes
t Second string. Yes

Full UDF Source:

<cfscript>
/**
* Computes the Levenshtein distance between two strings.
*
* @param s      First string. (Required)
* @param t      Second string. (Required)
* @return Returns a number.
* @author Nicholas Zographos (nicholas@nezen.net)
* @version 1, March 15, 2004
*/

function levDistance(s,t) {
    var d = ArrayNew(2);
    var i = 1;
    var j = 1;
    var s_i = "A";
    var t_j = "A";
    var cost = 0;
    
    var n = len(s)+1;
    var m = len(t)+1;
    
    d[n][m]=0;
    
    if (n is 1) {
        return m;
    }
    
    if (m is 1) {
        return n;
    }
    
     for (i = 1; i lte n; i=i+1) {
d[i][1] = i-1;
}

for (j = 1; j lte m; j=j+1) {
d[1][j] = j-1;
}
    
    for (i = 2; i lte n; i=i+1) {
s_i = Mid(s,i-1,1);

     for (j = 2; j lte m; j=j+1) {
    t_j = Mid(t,j-1,1);

        if (s_i is t_j) {
cost = 0;
}
else {
cost = 1;
}
        d[i][j] = min(d[i-1][j]+1, d[i][j-1]+1);
        d[i][j] = min(d[i][j], d[i-1][j-1] + cost);
}
}
    
return d[n][m];
}
</cfscript>

Search CFLib.org


Latest Additions

Shawn Porter Shawn Porter added
DeMoronize
2 hour(s) ago

Chris Carey Chris Carey added
readPropertiesFi...
1 day(s) ago

Randy Johnson Randy Johnson added
lastDayofWeek
3 day(s) ago

Frank Marion Frank Marion added
sitemapPing
7 day(s) ago

Top Rated

James Sleeman                                     QuickSort
Rated 5.0, 3 time(s)

Barney Boisvert indentXml
Rated 5.0, 3 time(s)

Nathan Dintenfass                                 queryColumnsToSt...
Rated 5.0, 3 time(s)

Kevin Pepperman generateSsccAsn
Rated 5.0, 3 time(s)

Created by Raymond Camden / Design by Justin Johnson