minimum-edit-distance
The minimum edit distance is the integer number of character insertions, deletions, and substitutions required to transform one string into another.
Info
The minimum edit distance on its own is, albeit interesting, not terribly useful. However, the minimum edit matrix backtrace is both interesting and useful. This backtrace contains, not only all the information necessary to determine the minimum edit distance, but also the smallest amount of information required to transform the second string back into the first (see usage section).
install
$ npm install minimum-edit-distance
require
const minimumEditDistance = ;
Usage
let str1 = 'dhyfldnsgagfhc'; let str2 = 'dososjhbabadhfhshdsjds'; let difference = minimumEditDistance console; // 18 console; // [ 'sc', 'sh', 'sf', 'sg', 'sa', 'sg', '1', 'sn', 'd2', '1', 'sl', 'sf', 'sy', 'd', '1', 'd5', '1' ] /* * prefix key: * s = substitute * i = insertion * d = deletion # * # = skip num */ let stringOne = minimumEditDistance console; // true
Usage for Arrays
let array1 = 'cat' 'fees' 'hound' let array2 = 'cat' 'kite' 'undo' 'hound' let arrayDifference = minimumEditDistance console //2 console // [ '1', 'sfees', 'd', '1' ] console // [ 'cat', 'fees', 'hound' ]
API
/** * p1 - first string or array * p2 - second string or array * distance - integer minimum edit distance * backtrace - array of strings specifying edit operations */ { ... return {"distance": distance, "backtrace": backtrace} } /** * p2 - p2 used in diff * trace - backtrace from diff * p1 - equal to p1 used in diff */ { ... return p1 }
license
MIT