Skip to main content

Damerau-Levenshtein

GitHub license GitHub tag HitCount

Calculate the Damerau–Levenshtein distance between strings.

Usage

Call to “distance” functions outputs an integer, the calculated Damerau-Levenshtein distance between 2 strings given as parameters. If the result is 0, strings are identical. The higher the result, the less similar strings are.

import { distance} as damerau from "./mod.ts";

const firstString: string = "Hello";
const secondString: string = "Hello World";

const distance = damerau.distance(firstString, secondString);

console.log(
  `Damerau–Levenshtein distance between "${firstString}" and "${secondString}" is: ${distance} `,
);

There are 1 demo available. The first demo queries Wikipedia and print the first result

deno run .\demo.ts "Hello Wordl" "Hello world"

You can run the demo directly from the repository with the commands:

deno run https://raw.githubusercontent.com/el3um4s/Damerau-Levenshtein/master/demo.ts  "Hello Wordl" "Hello world"

I was inspirated by fabvalaaah’s repository damerau-levenshtein-js

API

  • function compareDistance(a: StringWithDistance, b: StringWithDistance): number : Compare distance between 2 words (format like StringWithDistance).
  • function distance(a: string, b: string) : Get the Damerau-Levenshtein distance between 2 strings
  • function distanceDamerau(string: string, compared: string): StringWithDistance : Return an object with string, compared string and distance beetween
  • function distanceList(target: string, list: Array): Array : Return an arry of StringWithDistance with the distance from the compared string
  • function minDistance(string: string, list: Array): number : Get the minimum Damerau-Levenshtein distance between a string and an array of strings
  • function sortByMinDistance(list: Array): Array : Return an arry of StringWithDistance sorted by min distance
  • function sortWordByMinDistance(target: string, list: Array): Array : Return an arry of StringWithDistance sorted by min distance
  • interface StringWithDistance : Interface for string, compared string and distance beetween