-
Notifications
You must be signed in to change notification settings - Fork 634
/
Copy pathclosest_string.ts
64 lines (60 loc) · 2.03 KB
/
closest_string.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
// This module is browser compatible.
import { levenshteinDistance } from "./levenshtein_distance.ts";
// NOTE: this metric may change in future versions (e.g. better than levenshteinDistance)
const getWordDistance = levenshteinDistance;
/**
* The the most similar string from an array of strings.
*
* @example Usage
* ```ts
* import { closestString } from "@std/text/closest-string";
* import { assertEquals } from "@std/assert";
*
* const possibleWords = ["length", "size", "blah", "help"];
* const suggestion = closestString("hep", possibleWords);
*
* assertEquals(suggestion, "help");
* ```
*
* @param givenWord The string to measure distance against
* @param possibleWords The string-array that will be sorted
* @param options An options bag containing a `caseSensitive` flag indicating
* whether the distance should include case. Default is false.
* @returns A sorted copy of possibleWords
* @note
* the ordering of words may change with version-updates
* e.g. word-distance metric may change (improve)
* use a named-distance (e.g. levenshteinDistance) to
* guarantee a particular ordering
*/
export function closestString(
givenWord: string,
possibleWords: string[],
options?: {
caseSensitive?: boolean;
},
): string {
if (possibleWords.length === 0) {
throw new TypeError(
"When using closestString(), the possibleWords array must contain at least one word",
);
}
const { caseSensitive } = { ...options };
if (!caseSensitive) {
givenWord = givenWord.toLowerCase();
}
let nearestWord = possibleWords[0]!;
let closestStringDistance = Infinity;
for (const each of possibleWords) {
const distance = caseSensitive
? getWordDistance(givenWord, each)
: getWordDistance(givenWord, each.toLowerCase());
if (distance < closestStringDistance) {
nearestWord = each;
closestStringDistance = distance;
}
}
// this distance metric could be swapped/improved in the future
return nearestWord;
}