diff --git a/index.html b/index.html new file mode 100644 index 0000000..3aeac5a --- /dev/null +++ b/index.html @@ -0,0 +1,49 @@ + + + + + +
+ String A:
+ String B:
+ +
+
+
+ Output: +
+
+
+
+ String A:
+ String B:
+ +
+
+
+ Output: +
+ + + + + + \ No newline at end of file diff --git a/src/editDistance.js b/src/editDistance.js index 36c7a01..655edeb 100644 --- a/src/editDistance.js +++ b/src/editDistance.js @@ -1,3 +1,58 @@ +// Note: The metric used here is Damerau–Levenshtein distance. +// This metric imposes a uniform cost for insertion, deletion, +// substitutions, and transposition, as these compose the most +// common human typing mistakes. var editDistance = function(str1, str2) { + if (str1 === str2) { + return 0; + } + if (!str1.length || !str2.length) { + return str1.length || str2.length; + } + + // Swap strings so can just focus on one + if (str2.length > str1.length) { + var temp = str1; + str1 = str2; + str2 = temp; + } + + var len1 = str1.length; + var len2 = str2.length; + + var prevPrevRow; + var prevRow = []; + + // Fill in previous row with empty subsequences. + for (var col = 0; col <= len2; col++) { + prevRow[col] = col; + } + var currRow = [1]; + + // Fake iterating through the whole matrix, + // but remembering only two rows at a time. + for (var row = 1; row <= len1; row++) { + for (var col = 1; col <= len2; col++) { + + // If two strings match at their final character, their LCS + // is the LCS of their prefixes, plus that final character. + if (str1[row-1] === str2[col-1]) { + currRow[col] = prevRow[col-1]; + + // Otherwise, their LCS is the best LCS that results from + // trimming either of their final characters, plus one. + } else { + var del = prevRow[col] + 1; + var ins = currRow[col-1] + 1; + var sub = prevRow[col-1] + 1; + currRow[col] = Math.min(del, ins, sub); + } + } + prevPrevRow = prevRow; + prevRow = currRow; + currRow = [row + 1]; + } + + return prevRow[len2]; }; \ No newline at end of file diff --git a/src/fuzzyStringMatch.js b/src/fuzzyStringMatch.js index e3fe293..2f26207 100644 --- a/src/fuzzyStringMatch.js +++ b/src/fuzzyStringMatch.js @@ -1,3 +1,41 @@ var fuzzyStringMatch = function(text, query) { + var qLen = query.length; + var tLen = text.length; + + var prevPrevRow; + var prevRow = []; + // Fill in previous row with empty subsequences. + for (var col = 0; col <= tLen; col++) { + prevRow[col] = 0; + } + var currRow = [1]; + + // Fake iterating through the whole matrix, + // but remembering only two rows at a time. + for (var row = 1; row <= qLen; row++) { + for (var col = 1; col <= tLen; col++) { + + // If two strings match at their final character, their LCS + // is the LCS of their prefixes, plus that final character. + if (query[row-1] === text[col-1]) { + currRow[col] = prevRow[col-1]; + + // Otherwise, their LCS is the best LCS that results from + // trimming either of their final characters. + } else { + var del = prevRow[col] + 1; + var ins = currRow[col-1] + 1; + var sub = prevRow[col-1] + 1; + currRow[col] = Math.min(del, ins, sub); + }; + } + prevPrevRow = prevRow; + prevRow = currRow; + currRow = [row + 1]; + } + + var min = Math.min.apply(null, prevRow); + console.log(min, qLen); + return (qLen-min)/qLen; };