-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4fa3a85
commit 77afb0b
Showing
3 changed files
with
104 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
# 1.0.0 (2022-04-27) | ||
|
||
|
||
### Features | ||
|
||
* init ([4fa3a85](https://github.com/bent10/find-similar/commit/4fa3a85357746c226e22917be4bfbc6dcbdb3ab1)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
/** | ||
* Finds similar word(s) in a list of words. | ||
* | ||
* ## Install | ||
* | ||
* ```bash | ||
* npm i find-similar | ||
* ``` | ||
* | ||
* ## Usage | ||
* | ||
* This package is pure ESM, please read the | ||
* [esm-package](https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c). | ||
* | ||
* ```js | ||
* import { findSimilar, didYouMean } from 'find-similar' | ||
* | ||
* console.log(findSimilar('foos', ['bar', 'baz', 'foo'])) | ||
* // => ['foo'] | ||
* | ||
* // Suggests similar words | ||
* console.log(didYouMean('foos', ['bar', 'baz', 'foo'])) | ||
* // => 'Did you mean "foo"?' | ||
* ``` | ||
* | ||
* @module | ||
*/ | ||
export declare type Options = { | ||
/** | ||
* Maximum levenshtein distance threshold. | ||
* | ||
* @default 3 | ||
*/ | ||
maxScore?: number; | ||
/** | ||
* The similarity threshold, a number between 0 and 1. | ||
* | ||
* @default 0.5 | ||
*/ | ||
criteria?: number; | ||
/** | ||
* A string to prepend to the suggested word. | ||
* | ||
* @default '' | ||
*/ | ||
prefix?: string; | ||
}; | ||
/** | ||
* Finds similar word(s) in a list of words. | ||
* | ||
* @param word - The word to find similar words for. | ||
* @param candidates - An array of words to compare against. | ||
* @param options - Options for the function. | ||
* @returns An array of similar words. | ||
*/ | ||
export declare function findSimilar(word: string, candidates: string[], options?: Options): string[]; | ||
/** | ||
* Suggests similar words in a list of words. | ||
* | ||
* @param word - The word to find similar words for. | ||
* @param candidates - An array of words to compare against. | ||
* @param options - Options for the function. | ||
* @returns A string of suggested words. | ||
*/ | ||
export declare function didYouMean(word: string, candidates: string[], options?: Options): string; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import leven from "leven"; | ||
function findSimilar(word, candidates, options = {}) { | ||
let { maxScore = 3 } = options; | ||
const { criteria = 0.5, prefix = "" } = options; | ||
const matches = []; | ||
for (const candidate of candidates) { | ||
const length = Math.max(word.length, candidate.length); | ||
const score = leven(word, candidate); | ||
const similarity = (length - score) / length; | ||
if (similarity >= criteria && score <= maxScore) { | ||
if (score < maxScore) { | ||
maxScore = score; | ||
matches.length = 0; | ||
} | ||
matches.push(prefix + candidate); | ||
} | ||
} | ||
return matches; | ||
} | ||
function didYouMean(word, candidates, options = {}) { | ||
const matches = findSimilar(word, candidates, options); | ||
let message = "Did you mean "; | ||
if (matches.length > 0) { | ||
matches.length > 1 && (message += "one of "); | ||
message += `"${matches.join(", ")}"?`; | ||
return message; | ||
} | ||
return ""; | ||
} | ||
export { | ||
didYouMean, | ||
findSimilar | ||
}; |