Nodejs String Levenshtein Distance levenshteinDistance(c)

Here you can find the source of levenshteinDistance(c)

Method Source Code

/*! string.leveinstein-distance.js by Alexandre Mercier - https://github.com/amercier/string.metrics.js */

/**/*  w ww . j a  v  a 2  s.  c  o m*/
 * Determine the Levenshtein distance with another String.
 * 
 * The Levenshtein distance between two strings is defined as the minimum number
 * of edits needed to transform one string into the other, with the allowable
 * edit operations being insertion, deletion, or substitution of a single
 * character. See the [Wikipedia article][1] for more details.
 * 
 * [1]: http://en.wikipedia.org/wiki/Levenshtein_distance
 * 
 * @param  {String} c 
 * @return {Number} Returns the Levenshtein distance between this String and `c`
 */
String.prototype.levenshteinDistance = String.prototype.levenshteinDistance || function(c) {
   var s,
      l = (s = this.split("")).length,
      t = (c = c.split("")).length,
      i,
      j,
      m,
      n;
   if(!(l || t)) {
      return Math.max(l, t);
   }
   
   i = l + 1;
   for(var a = []; i; a[--i] = [i]) {
      ;
   }
   
   for(i = t + 1; a[0][--i] = i;) {
      ;
   }
   
   for(i = -1, m = s.length; ++i < m;) {
      for(j = -1, n = c.length; ++j < n;) {
         a[(i *= 1) + 1][(j *= 1) + 1] = Math.min(a[i][j + 1] + 1, a[i + 1][j] + 1, a[i][j] + (s[i] != c[j]));
      }
   }
   
   return a[l][t];
};

Related

  1. levenshtein()
    String.prototype.levenshtein = function()
      var s = this;
      var t = String(arguments[0] || '');
      var cuthalf = Number(arguments.callee.maxComparedLength) || t.length;
      s = s.substr(0, cuthalf);
      t = t.substr(0, cuthalf);
      var m = s.length;
      var n = t.length;
    ...
    
  2. levenshtein(str)
    String.prototype.levenshtein = function(str){
        if (! str ){
          return false;
        str = str.toLowerCase().replace(/[^a-z]/g, "");
          var t = this.toLowerCase().replace(/[^a-z]/g, "");
          var i;
            var j;
            var cost;
    ...