2024-01-01 21:11:32 +00:00
|
|
|
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
2024-04-10 02:43:44 +00:00
|
|
|
// This module is browser compatible.
|
2023-11-05 12:49:00 +00:00
|
|
|
import { levenshteinDistance } from "./levenshtein_distance.ts";
|
|
|
|
|
2024-07-16 19:19:21 +00:00
|
|
|
/** Options for {@linkcode closestString}. */
|
|
|
|
export interface ClosestStringOptions {
|
|
|
|
/**
|
|
|
|
* Whether the distance should include case.
|
|
|
|
*
|
|
|
|
* @default {false}
|
|
|
|
*/
|
|
|
|
caseSensitive?: boolean;
|
|
|
|
/**
|
|
|
|
* A custom comparison function to use for comparing strings.
|
|
|
|
*
|
|
|
|
* @param a The first string for comparison.
|
|
|
|
* @param b The second string for comparison.
|
|
|
|
* @returns The distance between the two strings.
|
|
|
|
* @default {levenshteinDistance}
|
|
|
|
*/
|
|
|
|
compareFn?: (a: string, b: string) => number;
|
|
|
|
}
|
2023-11-05 12:49:00 +00:00
|
|
|
|
|
|
|
/**
|
2024-07-12 04:57:32 +00:00
|
|
|
* Finds the most similar string from an array of strings.
|
|
|
|
*
|
2024-07-16 19:19:21 +00:00
|
|
|
* By default, calculates the distance between words using the
|
|
|
|
* {@link https://en.wikipedia.org/wiki/Levenshtein_distance | Levenshtein distance}.
|
2023-11-05 12:49:00 +00:00
|
|
|
*
|
2024-05-23 17:01:10 +00:00
|
|
|
* @example Usage
|
2023-11-05 12:49:00 +00:00
|
|
|
* ```ts
|
2024-04-29 02:57:30 +00:00
|
|
|
* import { closestString } from "@std/text/closest-string";
|
refactor(assert,async,bytes,cli,collections,crypto,csv,data-structures,datetime,dotenv,encoding,expect,fmt,front-matter,fs,html,http,ini,internal,io,json,jsonc,log,media-types,msgpack,net,path,semver,streams,testing,text,toml,ulid,url,uuid,webgpu,yaml): import from `@std/assert` (#5199)
* refactor: import from `@std/assert`
* update
2024-06-30 08:30:10 +00:00
|
|
|
* import { assertEquals } from "@std/assert";
|
2023-11-05 12:49:00 +00:00
|
|
|
*
|
2024-06-03 04:10:27 +00:00
|
|
|
* const possibleWords = ["length", "size", "blah", "help"];
|
|
|
|
* const suggestion = closestString("hep", possibleWords);
|
2023-11-05 12:49:00 +00:00
|
|
|
*
|
2024-06-03 04:10:27 +00:00
|
|
|
* assertEquals(suggestion, "help");
|
2023-11-05 12:49:00 +00:00
|
|
|
* ```
|
|
|
|
*
|
2024-05-23 17:01:10 +00:00
|
|
|
* @param givenWord The string to measure distance against
|
2024-07-12 04:57:32 +00:00
|
|
|
* @param possibleWords The string-array to pick the closest string from
|
2024-07-16 19:19:21 +00:00
|
|
|
* @param options The options for the comparison.
|
2024-07-12 04:57:32 +00:00
|
|
|
* @returns The closest string
|
2023-11-05 12:49:00 +00:00
|
|
|
*/
|
|
|
|
export function closestString(
|
|
|
|
givenWord: string,
|
2024-07-12 04:57:32 +00:00
|
|
|
possibleWords: ReadonlyArray<string>,
|
2024-07-16 19:19:21 +00:00
|
|
|
options?: ClosestStringOptions,
|
2023-11-05 12:49:00 +00:00
|
|
|
): string {
|
2024-06-05 22:02:17 +00:00
|
|
|
if (possibleWords.length === 0) {
|
|
|
|
throw new TypeError(
|
|
|
|
"When using closestString(), the possibleWords array must contain at least one word",
|
|
|
|
);
|
|
|
|
}
|
2024-07-16 19:19:21 +00:00
|
|
|
const { caseSensitive, compareFn = levenshteinDistance } = { ...options };
|
2023-11-05 12:49:00 +00:00
|
|
|
|
|
|
|
if (!caseSensitive) {
|
|
|
|
givenWord = givenWord.toLowerCase();
|
|
|
|
}
|
|
|
|
|
2024-01-10 20:33:57 +00:00
|
|
|
let nearestWord = possibleWords[0]!;
|
2023-12-12 10:34:42 +00:00
|
|
|
let closestStringDistance = Infinity;
|
2023-11-05 12:49:00 +00:00
|
|
|
for (const each of possibleWords) {
|
|
|
|
const distance = caseSensitive
|
2024-07-16 19:19:21 +00:00
|
|
|
? compareFn(givenWord, each)
|
|
|
|
: compareFn(givenWord, each.toLowerCase());
|
2023-11-05 12:49:00 +00:00
|
|
|
if (distance < closestStringDistance) {
|
|
|
|
nearestWord = each;
|
|
|
|
closestStringDistance = distance;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nearestWord;
|
|
|
|
}
|