What is damerau-levenshtein?
The damerau-levenshtein npm package is used to calculate the Damerau-Levenshtein distance between two strings. This distance is a measure of similarity between two strings, considering the number of operations (insertions, deletions, substitutions, and transpositions) needed to transform one string into another. It is particularly useful in applications like spell checking, typo correction, and other forms of text analysis where slight variations in string input need to be quantified.
What are damerau-levenshtein's main functionalities?
Calculate Distance and Steps
This feature allows users to calculate the Damerau-Levenshtein distance between two strings and also provides detailed steps of the operations needed to transform the source string into the target string. This is useful for understanding the specific changes needed.
const damerau = require('damerau-levenshtein');
const result = damerau('hello', 'hallo');
console.log(result.distance); // Output: 2
console.log(result.steps); // Output: [ { operation: 'substitute', srcPos: 1, destPos: 1 }, { operation: 'substitute', srcPos: 4, destPos: 4 } ]
Other packages similar to damerau-levenshtein
fast-levenshtein
This package provides an efficient way to compute the Levenshtein distance. Unlike damerau-levenshtein, it does not account for transpositions, making it slightly less versatile for certain applications but faster for simple distance calculations.
levenshtein-edit-distance
Similar to fast-levenshtein, this package calculates the Levenshtein distance between two strings. It is optimized for performance but, like fast-levenshtein, does not handle transpositions, which can be crucial for more detailed text analysis compared to damerau-levenshtein.
It provides a function that takes two string arguments and returns a hash like this:
{
steps: 5,
relative: 0.7,
similarity: 0.3
}
Install
npm install damerau-levenshtein
Use with ES6 modules
import * as levenshtien from 'damerau-levenshtein';
const lev = levenshtien('hello world', 'Hello World!');
Please see tests for more insights.
Use with TypeScript
import * as levenshtien from 'damerau-levenshtein';
interface LevenshteinResponse {
steps: number;
relative: number;
similarity: number;
}
const lev: LevenshteinResponse = levenshtien('hello world', 'Hello World!');
console.log(lev.steps);
console.log(lev.foo);