Nodejs String Levenshtein Distance levenshtein()

Here you can find the source of levenshtein()

Method Source Code

/**/*from w  w w .j  a v  a  2  s.  c  om*/
 *
 * The implementations of the Levenshtein algorithm.. 
 * This is implementation migrated from the modified Perl's one (see the wikibooks link)
 *
 * @usage
 * <code>
 * var s = 'google';
 * var t = 'googol';
 *
 * var result = s.levenshtein(t);
 * </code>
 *
 * @see   http://en.wikipedia.org/wiki/Levenshtein_distance
 * @see   http://ru.wikibooks.org/wiki/%D0%A0%D0%B0%D1%81%D1%81%D1%82%D0%BE%D1%8F%D0%BD%D0%B8%D0%B5_%D0%9B%D0%B5%D0%B2%D0%B5%D0%BD%D1%88%D1%82%D0%B5%D0%B9%D0%BD%D0%B0
 */
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;

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

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

   var buf = new Array(cuthalf * 2 - 1);

   for (var i = 0; i <= n; i++) {
      buf[i] = i;
   }

   var curr;
   var next;

   for (var i = 0; i < m; i++) {
      curr = i + 1;
      for (var j = 0; j < n; j++) {
         var cost = s.charAt(i) == t.charAt(j) ? 0 : 1;
         next = Math.min(
            buf[j + 1] + 1, 
            curr + 1, 
            buf[j] + cost);
         buf[j] = curr;
         curr = next;
      }
      buf[n] = next;
   }

   return next;
};

String.prototype.levenshtein.maxComparedLength = 150;

Related

  1. 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;
    ...
    
  2. levenshteinDistance(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)) {
    ...