Created
February 8, 2017 11:52
-
-
Save maggiben/3c19b6d330426ffd3705588d71f5dd9a to your computer and use it in GitHub Desktop.
This file contains hidden or 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
/* | |
This script is free to use under the license below. | |
No obligation, but if you use this it I'd love to know, thanks! | |
Andrew Hedges, [email protected] | |
----------------------------------------------------------------------------- | |
The MIT License (MIT) | |
Copyright (c) 2016 Andrew Hedges | |
Permission is hereby granted, free of charge, to any person obtaining a copy | |
of this software and associated documentation files (the "Software"), to deal | |
in the Software without restriction, including without limitation the rights | |
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
copies of the Software, and to permit persons to whom the Software is | |
furnished to do so, subject to the following conditions: | |
The above copyright notice and this permission notice shall be included in all | |
copies or substantial portions of the Software. | |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
SOFTWARE. | |
----------------------------------------------------------------------------- | |
*/ | |
var levenshteinenator = (function () { | |
/** | |
* @param String a | |
* @param String b | |
* @return Array | |
*/ | |
function levenshteinenator(a, b) { | |
var cost; | |
var m = a.length; | |
var n = b.length; | |
// make sure a.length >= b.length to use O(min(n,m)) space, whatever that is | |
if (m < n) { | |
var c = a; a = b; b = c; | |
var o = m; m = n; n = o; | |
} | |
var r = []; r[0] = []; | |
for (var c = 0; c < n + 1; ++c) { | |
r[0][c] = c; | |
} | |
for (var i = 1; i < m + 1; ++i) { | |
r[i] = []; r[i][0] = i; | |
for ( var j = 1; j < n + 1; ++j ) { | |
cost = a.charAt( i - 1 ) === b.charAt( j - 1 ) ? 0 : 1; | |
r[i][j] = minimator( r[i-1][j] + 1, r[i][j-1] + 1, r[i-1][j-1] + cost ); | |
} | |
} | |
return r; | |
} | |
/** | |
* Return the smallest of the three numbers passed in | |
* @param Number x | |
* @param Number y | |
* @param Number z | |
* @return Number | |
*/ | |
function minimator(x, y, z) { | |
if (x <= y && x <= z) return x; | |
if (y <= x && y <= z) return y; | |
return z; | |
} | |
return levenshteinenator; | |
}()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment