damerau-levenshtein
Advanced tools
Comparing version 1.0.0 to 1.0.1
64
index.js
// TheSpanishInquisition | ||
// Cache the matrix. Note this implementation is limited to | ||
// strings of 64 char or less. This could be altered to update | ||
// dynamically, or a larger value could be used. | ||
// Cache the matrix. Note that if you not pass a limit this implementation will use a dynamically calculate one. | ||
// TODO: grok it. | ||
// The matrix was cached, as above comment states, but it was throwing unpredictable results sometimes. Either there was a bug or I was using it wrong. Now matrix is recreated on each call and results are correct. This probabily hits performance. | ||
module.exports = function(__this, that, limit) { | ||
module.exports = function(__this, that, limit) { | ||
var matrix = []; | ||
for (var i = 0; i < 64; i++) { | ||
var thisLength = __this.length, | ||
thatLength = that.length, | ||
matrix = []; | ||
// If the limit is not defined it will be calculate from this and that args. | ||
limit = (limit || ((thatLength > thisLength ? thatLength : thisLength)))+1; | ||
for (var i = 0; i < limit; i++) { | ||
matrix[i] = [i]; | ||
matrix[i].length = 64; | ||
matrix[i].length = limit; | ||
} | ||
for (var i = 0; i < 64; i++) { | ||
for (i = 0; i < limit; i++) { | ||
matrix[0][i] = i; | ||
} | ||
var thisLength = __this.length, | ||
thatLength = that.length; | ||
prepare = function (steps) { | ||
distance = {}; | ||
distance.steps = steps; | ||
distance.relative = steps / Math.max(thisLength, thatLength); | ||
distance.similarity = 1 - distance.relative; | ||
return distance; | ||
if (Math.abs(thisLength - thatLength) > (limit || 100)){ | ||
return prepare (limit || 100); | ||
} | ||
if (thisLength === 0){ | ||
return prepare (thatLength); | ||
} | ||
if (thatLength === 0){ | ||
return prepare (thisLength); | ||
} | ||
if (Math.abs(thisLength - thatLength) > (limit || 32)) return prepare (limit || 32); | ||
if (thisLength === 0) return prepare (thatLength); | ||
if (thatLength === 0) return prepare (thisLength); | ||
// Calculate matrix. | ||
@@ -49,3 +45,3 @@ var this_i, that_j, cost, min, t; | ||
// Calculate the minimum (much faster than Math.min(...)). | ||
min = matrix[i - 1][j ] + 1; // Deletion. | ||
min = matrix[i - 1][j ] + 1; // Deletion. | ||
if ((t = matrix[i ][j - 1] + 1 ) < min) min = t; // Insertion. | ||
@@ -55,8 +51,20 @@ if ((t = matrix[i - 1][j - 1] + cost) < min) min = t; // Substitution. | ||
// Update matrix. | ||
matrix[i][j] = (i > 1 && j > 1 && this_i === that[j-2] && this[i-2] === that_j && (t = matrix[i-2][j-2]+cost) < min) ? t : min; // Transposition. | ||
matrix[i][j] = (i > 1 && j > 1 && this_i === that[j-2] && __this[i-2] === that_j && (t = matrix[i-2][j-2]+cost) < min) ? t : min; // Transposition. | ||
} | ||
} | ||
return prepare (matrix[thisLength][thatLength]) | ||
return prepare (matrix[thisLength][thatLength]); | ||
}; | ||
/** | ||
* | ||
*/ | ||
function prepare(steps) { | ||
var distance = { | ||
steps: steps, | ||
relative: (steps / Math.max(thisLength, thatLength)) || 0 | ||
}; | ||
distance.similarity = (distance.relative !== 0) ?(1 - distance.relative) : 0; | ||
return distance; | ||
} | ||
}; |
{ | ||
"name": "damerau-levenshtein", | ||
"version": "1.0.0", | ||
"version": "1.0.1", | ||
"description": "Damerau - Levenshtein distance by The Spanish Inquisition + relative distance", | ||
@@ -18,3 +18,6 @@ "main": "index.js", | ||
"author": "The Spanish Inquisition", | ||
"license": "BSD-2-Clause" | ||
"license": "BSD-2-Clause", | ||
"devDependencies": { | ||
"mocha": "^3.0.2" | ||
} | ||
} |
6809
6
125
1