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

compare_similarity.ts
View Documentation
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
// 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;
/** * Sort based on word similarity * * @example * ```ts * import { compareSimilarity } from "https://deno.land/std@$STD_VERSION/text/compare_similarity.ts"; * const words = ["hi", "hello", "help"]; * * // words most-similar to "hep" will be at the front * words.sort(compareSimilarity("hep")); * ``` * @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 compareSimilarity( givenWord: string, options?: { caseSensitive?: boolean },): (a: string, b: string) => number { const { caseSensitive } = { ...options }; if (caseSensitive) { return (a: string, b: string) => getWordDistance(givenWord, a) - getWordDistance(givenWord, b); } givenWord = givenWord.toLowerCase(); return (a: string, b: string) => getWordDistance(givenWord, a.toLowerCase()) - getWordDistance(givenWord, b.toLowerCase());}
std

Version Info

Tagged at
6 months ago