https://github.com/alordash/levenshtein
My realization of Levenshtein distance algorithm.
https://github.com/alordash/levenshtein
Last synced: about 1 month ago
JSON representation
My realization of Levenshtein distance algorithm.
- Host: GitHub
- URL: https://github.com/alordash/levenshtein
- Owner: alordash
- License: mit
- Created: 2020-08-05T16:22:10.000Z (almost 5 years ago)
- Default Branch: master
- Last Pushed: 2021-07-02T11:00:23.000Z (almost 4 years ago)
- Last Synced: 2025-04-02T12:01:42.326Z (about 2 months ago)
- Language: JavaScript
- Homepage:
- Size: 113 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
README
# @alordash/levenshtein
```
$ npm i @alordash/levenshtein
```# Levenshtein algorithm
My realization of [Levenshtein distance](https://en.wikipedia.org/wiki/Levenshtein_distance) algorithm.
# Usage
```javascript
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:
```javascript
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
//—————+–––+–––+–––+–––+–––+–––+–––
```