@alordash/levenshtein
v2.1.4
Published
My realization of levenshtein algorithm.
Downloads
22
Readme
@alordash/levenshtein
$ npm i @alordash/levenshtein
Levenshtein algorithm
My realization of Levenshtein distance algorithm.
Usage
const { distance, closest } = require('@alordash/levenshtein')
// Print Levenshtein distance between "best" and "better"
console.log(distance("best", "better"));
//=> 3
// Select the most similar string to "money" from string list.
let value = closest("money", ["none", "some", "much"]);
// Print closest string
console.log(value.closest_string);
//=> "none"
// Print Levenshtein distance to closest string
console.log(value.distance);
//=> 2
Features
You can also create calculation object using distanceCalculation class:
const { distanceCalculation } = require("@alordash/levenshtein");
// Calculate Levenshtein distance between "entity" and "identity"
let object = new Levenshtein("entity", "identity");
// Print Levenshtein distance between "entity" and "identity"
console.log(object.valueOf(), object.distance);
//=> 2 2
// Change strings in object to "best" and "better" and calculate Levenshtein distance between them
object.strings = ["best", "better"];
// Print Levenshtein distance between "best" and "better"
console.log(object.distance);
//=> 3
// Print calculation matrix
console.log(object.toString());
//=>
// | | b | e | t | t | e | r
//—————+–––+–––+–––+–––+–––+–––+–––
// | 0 | 1 | 2 | 3 | 4 | 5 | 6
//—————+–––+–––+–––+–––+–––+–––+–––
// b | 1 | 0 | 1 | 2 | 3 | 4 | 5
//—————+–––+–––+–––+–––+–––+–––+–––
// e | 2 | 1 | 0 | 1 | 2 | 3 | 4
//—————+–––+–––+–––+–––+–––+–––+–––
// s | 3 | 2 | 1 | 1 | 2 | 3 | 4
//—————+–––+–––+–––+–––+–––+–––+–––
// t | 4 | 3 | 2 | 1 | 1 | 2 | 3
//—————+–––+–––+–––+–––+–––+–––+–––