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.
|
2024-06-11 07:34:14 +00:00
|
|
|
import {
|
|
|
|
compareSimilarity,
|
|
|
|
type CompareSimilarityOptions,
|
|
|
|
} from "./compare_similarity.ts";
|
|
|
|
|
|
|
|
/** Options for {@linkcode wordSimilaritySort}. */
|
|
|
|
export interface WordSimilaritySortOptions extends CompareSimilarityOptions {}
|
2023-11-05 12:49:00 +00:00
|
|
|
|
|
|
|
/**
|
2024-06-03 04:10:27 +00:00
|
|
|
* Sorts a string-array by similarity to a given string.
|
|
|
|
*
|
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}.
|
2024-07-12 04:57:32 +00:00
|
|
|
*
|
2024-06-03 04:10:27 +00:00
|
|
|
* @example Basic usage
|
|
|
|
*
|
2023-11-05 12:49:00 +00:00
|
|
|
* ```ts
|
2024-04-29 02:57:30 +00:00
|
|
|
* import { wordSimilaritySort } from "@std/text/word-similarity-sort";
|
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
|
|
|
*
|
|
|
|
* const possibleWords = ["length", "size", "blah", "help"];
|
2024-06-11 07:34:14 +00:00
|
|
|
* const suggestions = wordSimilaritySort("hep", possibleWords);
|
2023-11-05 12:49:00 +00:00
|
|
|
*
|
2024-06-11 07:34:14 +00:00
|
|
|
* assertEquals(suggestions, ["help", "size", "blah", "length"]);
|
2023-11-05 12:49:00 +00:00
|
|
|
* ```
|
|
|
|
*
|
2024-06-11 07:34:14 +00:00
|
|
|
* @example Case-sensitive sorting
|
|
|
|
*
|
|
|
|
* ```ts
|
|
|
|
* import { wordSimilaritySort } from "@std/text/word-similarity-sort";
|
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";
|
2024-06-11 07:34:14 +00:00
|
|
|
*
|
|
|
|
* const possibleWords = ["length", "Size", "blah", "HELP"];
|
|
|
|
* const suggestions = wordSimilaritySort("hep", possibleWords, { caseSensitive: true });
|
|
|
|
*
|
|
|
|
* assertEquals(suggestions, ["Size", "blah", "HELP", "length"]);
|
|
|
|
* ```
|
|
|
|
*
|
|
|
|
* @param givenWord The string to measure distance against.
|
2024-07-12 04:57:32 +00:00
|
|
|
* @param possibleWords The string-array that will be sorted. This array will
|
|
|
|
* not be mutated, but the sorted copy will be returned.
|
2024-06-11 07:34:14 +00:00
|
|
|
* @param options Options for the sort.
|
2024-07-12 04:57:32 +00:00
|
|
|
* @returns A sorted copy of `possibleWords`.
|
2023-11-05 12:49:00 +00:00
|
|
|
*/
|
|
|
|
export function wordSimilaritySort(
|
|
|
|
givenWord: string,
|
2024-07-12 04:57:32 +00:00
|
|
|
possibleWords: ReadonlyArray<string>,
|
2024-06-11 07:34:14 +00:00
|
|
|
options?: WordSimilaritySortOptions,
|
2023-11-05 12:49:00 +00:00
|
|
|
): string[] {
|
2024-06-11 07:34:14 +00:00
|
|
|
// This distance metric could be swapped/improved in the future
|
2024-07-12 04:57:32 +00:00
|
|
|
return possibleWords.toSorted(compareSimilarity(givenWord, options));
|
2023-11-05 12:49:00 +00:00
|
|
|
}
|