deno.land / std@0.224.0 / text / levenshtein_distance.ts

levenshtein_distance.ts
نووسراو ببینە
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.// This module is browser compatible.
/** * Calculates the Levenshtein distance between two strings. * * @example * ```ts * import { levenshteinDistance } from "https://deno.land/std@$STD_VERSION/text/levenshtein_distance.ts"; * levenshteinDistance("aa", "bb"); // 2 * ``` * @param str1 - The first string. * @param str2 - The second string. * @returns The Levenshtein distance between the two strings. */export function levenshteinDistance(str1: string, str2: string): number { if (str1.length > str2.length) { [str1, str2] = [str2, str1]; }
let distances: number[] = Array.from( { length: str1.length + 1 }, (_, i) => +i, ); for (let str2Index = 0; str2Index < str2.length; str2Index++) { const tempDistances: number[] = [str2Index + 1]; for (let str1Index = 0; str1Index < str1.length; str1Index++) { const char1 = str1[str1Index]; const char2 = str2[str2Index]; if (char1 === char2) { tempDistances.push(distances[str1Index]!); } else { tempDistances.push( 1 + Math.min( distances[str1Index]!, distances[str1Index + 1]!, tempDistances.at(-1)!, ), ); } } distances = tempDistances; } return distances.at(-1)!;}
std

Version Info

Tagged at
3 weeks ago