-
Notifications
You must be signed in to change notification settings - Fork 374
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
41 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,52 @@ | ||
var makeString = require('./helper/makeString'); | ||
|
||
/** | ||
* Based on the implementation here: https://github.com/hiddentao/fast-levenshtein | ||
*/ | ||
module.exports = function levenshtein(str1, str2) { | ||
'use strict'; | ||
str1 = makeString(str1); | ||
str2 = makeString(str2); | ||
|
||
if (str1 === str2) | ||
return 0; | ||
// Short cut cases | ||
if (str1 === str2) return 0; | ||
if (!str1 || !str2) return Math.max(str1.length, str2.length); | ||
|
||
var current = [], | ||
prev, value = 0; | ||
// two rows | ||
var prevRow = new Array(str2.length + 1); | ||
|
||
for (var i = 0; i <= str2.length; i++) { | ||
for (var j = 0; j <= str1.length; j++) { | ||
if (i && j) { | ||
if (str1.charAt(j - 1) === str2.charAt(i - 1)) | ||
value = prev; | ||
else | ||
value = Math.min(current[j], current[j - 1], prev) + 1; | ||
// initialise previous row | ||
for (var i = 0; i < prevRow.length; ++i) { | ||
prevRow[i] = i; | ||
} | ||
|
||
// calculate current row distance from previous row | ||
for (i = 0; i < str1.length; ++i) { | ||
var nextCol = i + 1; | ||
|
||
for (var j = 0; j < str2.length; ++j) { | ||
var curCol = nextCol; | ||
|
||
// substution | ||
nextCol = prevRow[j] + ( (str1.charAt(i) === str2.charAt(j)) ? 0 : 1 ); | ||
// insertion | ||
var tmp = curCol + 1; | ||
if (nextCol > tmp) { | ||
nextCol = tmp; | ||
} | ||
// deletion | ||
tmp = prevRow[j + 1] + 1; | ||
if (nextCol > tmp) { | ||
nextCol = tmp; | ||
} | ||
else | ||
value = i + j; | ||
|
||
prev = current[j]; | ||
current[j] = value; | ||
// copy current col value into previous (in preparation for next iteration) | ||
prevRow[j] = curCol; | ||
} | ||
|
||
// copy last col value into previous (in preparation for next iteration) | ||
prevRow[j] = nextCol; | ||
} | ||
|
||
return value; | ||
return nextCol; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters