Skip to content
This repository has been archived by the owner on Mar 2, 2024. It is now read-only.

Commit

Permalink
feat: added test suite + slight changes
Browse files Browse the repository at this point in the history
  • Loading branch information
gustaveWPM committed Mar 2, 2024
1 parent b79675c commit 313d565
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 17 deletions.
62 changes: 62 additions & 0 deletions damerauLevenshtein.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import { describe, expect, it } from "vitest";

import damerauLevenshtein from "./damerauLevenshtein";

// https://github.com/aldebaran/libport/blob/master/tests/libport/damerau-levenshtein-distance.cc

describe("damerauLevenshtein", () => {
const emptyString = "";

it("should pass, empty strings", () => {
const distance = damerauLevenshtein(emptyString, emptyString);
// eslint-disable-next-line @typescript-eslint/no-magic-numbers
expect(distance).toBe(0);
});

it("should pass, distance 0", () => {
const s = "cat";
const distance = damerauLevenshtein(s, s);
// eslint-disable-next-line @typescript-eslint/no-magic-numbers
expect(distance).toBe(0);
});

it("should pass, 100% distance", () => {
const s2 = "test";
const expectedDistance = s2.length;
const distance = damerauLevenshtein(emptyString, s2);
expect(distance).toBe(expectedDistance);
});

it("should pass, cat and dog", () => {
const distance = damerauLevenshtein("cat", "dog");
// eslint-disable-next-line @typescript-eslint/no-magic-numbers
expect(distance).toBe(3);
});

it("should pass, deletion", () => {
const distance = damerauLevenshtein("azertyuiop", "aeryuop");
// eslint-disable-next-line @typescript-eslint/no-magic-numbers
expect(distance).toBe(3);
});

it("should pass, insertion", () => {
const distance = damerauLevenshtein("aeryuop", "azertyuiop");
// eslint-disable-next-line @typescript-eslint/no-magic-numbers
expect(distance).toBe(3);
});

it("should pass, substitution", () => {
const distance = damerauLevenshtein(
"azertyuiopqsdfghjklmwxcvbn,",
"qwertyuiopasdfghjkl;zxcvbnm"
);
// eslint-disable-next-line @typescript-eslint/no-magic-numbers
expect(distance).toBe(6);
});

it("should pass, transposition", () => {
const distance = damerauLevenshtein("1234567890", "1324576809");
// eslint-disable-next-line @typescript-eslint/no-magic-numbers
expect(distance).toBe(3);
});
});
42 changes: 25 additions & 17 deletions damerauLevenshtein.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,37 @@
export function damerauLevenshtein(s1: string, s2: string): number {
const s1len = s1.length;
const s2len = s2.length;
const matrix: number[][] = Array.from({ length: s1len + 1 }, () => new Array(s2len + 1).fill(0));
/* eslint-disable @typescript-eslint/no-magic-numbers */
function damerauLevenshtein(s1: string, s2: string): number {
if (s1 === s2) return 0;

for (let i: number = 1; i <= s1len; i++) {
matrix[i][0] = i;
}
const [s1len, s2len] = [s1.length, s2.length];
const matrix: number[][] = Array.from({ length: s1len + 1 }, () =>
new Array(s2len + 1).fill(0)
);

for (let j: number = 1; j <= s2len; j++) {
matrix[0][j] = j;
}
for (let y = 1; y <= s1len; y++) matrix[y][0] = y;
for (let x = 1; x <= s2len; x++) matrix[0][x] = x;

for (let i: number = 1; i <= s1len; i++) {
for (let j: number = 1; j <= s2len; j++) {
if (s1[i - 1] === s2[j - 1]) {
matrix[i][j] = matrix[i - 1][j - 1];
for (let y = 1; y <= s1len; y++) {
for (let x = 1; x <= s2len; x++) {
if (s1[y - 1] === s2[x - 1]) {
matrix[y][x] = matrix[y - 1][x - 1];
} else {
matrix[i][j] = 1 + Math.min(matrix[i - 1][j], matrix[i][j - 1], matrix[i - 1][j - 1]);
if (i > 1 && j > 1 && s1[i - 1] === s2[j - 2] && s1[i - 2] === s2[j - 1]) {
matrix[i][j] = Math.min(matrix[i][j], matrix[i - 2][j - 2] + 1);
matrix[y][x] =
1 +
Math.min(matrix[y - 1][x], matrix[y][x - 1], matrix[y - 1][x - 1]);
if (
y > 1 &&
x > 1 &&
s1[y - 1] === s2[x - 2] &&
s1[y - 2] === s2[x - 1]
) {
matrix[y][x] = Math.min(matrix[y][x], matrix[y - 2][x - 2] + 1);
}
}
}
}

return matrix[s1len][s2len];
}
/* eslint-enable @typescript-eslint/no-magic-numbers */

export default damerauLevenshtein;

0 comments on commit 313d565

Please sign in to comment.