diff --git a/README.md b/README.md new file mode 100644 index 0000000..a453a49 --- /dev/null +++ b/README.md @@ -0,0 +1,91 @@ +# @hidden-finder/didyoumean + +A simple and lightweight matching input to a list of potential matches using the [Levenshtein distance](https://en.wikipedia.org/wiki/Levenshtein_distance) algorithm. + +## Install + +Install the dependency + +```shell +npm install @hidden-finder/didyoumean + +yarn add @hidden-finder/didyoumean +``` + +## Overview + +```ts +import { calculateDistance, similarity, didyoumean } from '@hidden-finder/didyoumean' +``` + +- [Calculate Distance](#calculateDistance) +- [Similarity](#similarity) +- [Didyoumean](#didyoumean) + +## API + +### calculateDistance + +The `calculateDistance` function calculates the Levenshtein distance between two input strings. The Levenshtein distance measures the similarity between two strings by determining the minimum number of single-character edits (insertions, deletions, or substitutions) needed to transform one string into the other. + +#### Parameters + +- `text` (string): The first input string. +- `pattern` (string): The second input string. + +#### Returns + +- (number): The Levenshtein distance between the two input strings. + +```ts +import { calculateDistance } from '@hidden-finder/didyoumean' + +const calculate = calculateDistance('hellow', 'hello') +const calculate2 = calculateDistance('hellow', 'world') +console.log(calculate, calculate2) // 1, 5 +``` + +### similarity + +The `similarity` function calculates the normalized Levenshtein similarity score between two input strings. This metric measures the similarity between two strings as a value between 0 and 1. A score of 0 indicates no similarity, while a score of 1 indicates identical strings. + +#### Parameters + +- `text` (string): The first input string. +- `pattern` (string): The second input string. + +#### Returns + +- (number): The Levenshtein similarity score between the two input strings, normalized to a value between 0 and 1. + +```ts +import { similarity } from '@hidden-finder/didyoumean' + +const similar = similarity('hellow', 'hello') +const similar2 = similarity('hellow', 'world') +console.log(similar, similar2) // 0.83, 0.16 +``` + +### didyoumean + +The `didyoumean` function is used to find the closest matching pattern from an array of patterns to a given input string. It does this by calculating the Levenshtein distance between the input string and each pattern in the provided array and returning the pattern with the smallest Levenshtein distance. + +#### Parameters + +- `string` (string): The input string for which you want to find the closest matching pattern. +- `patterns` (string[]): An array of patterns to compare against the input string. + +#### Returns + +- (string): The closest matching pattern from the provided array. + +```ts +import { didyoumean } from '@hidden-finder/didyoumean' + +const mean = didyoumean('hellow', ['hello', 'world']) +console.log(mean) // hello +``` + +## License + +[MIT License](LICENSE) diff --git a/src/index.ts b/src/index.ts index c3c1a42..ca5fc2f 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,5 +1,16 @@ import { calculateMyersDistanceShort, calculateMyersDistanceLong } from './calculate' +/** + * Calculates the Levenshtein distance between two strings. + * + * The Levenshtein distance is a measure of the similarity between two strings. + * It represents the minimum number of single-character edits (insertions, deletions, + * or substitutions) required to change one string into the other. + * + * @param {string} text - The first input string. + * @param {string} pattern - The second input string. + * @returns {number} The Levenshtein distance between the two input strings. + */ export const calculateDistance = (text: string, pattern: string): number => { if (text.length < pattern.length) [pattern, text] = [text, pattern] @@ -12,6 +23,18 @@ export const calculateDistance = (text: string, pattern: string): number => { return calculateMyersDistanceLong(text, pattern) } +/** + * Calculates the similarity between two strings using the normalized Levenshtein similarity metric. + * + * The Levenshtein similarity metric measures the similarity between two strings as a value between 0 and 1. + * It is calculated as (1 - (edit distance / longest length)), where the edit distance represents the minimum + * number of single-character edits (insertions, deletions, or substitutions) required to change one string into the other, + * and the longest length is the length of the longer of the two input strings. + * + * @param {string} text - The first input string. + * @param {string} pattern - The second input string. + * @returns {number} The Levenshtein similarity score between the two input strings (between 0 and 1). + */ export const similarity = (text: string, pattern: string): number => { if (!text || !pattern) return 0 if (text === pattern) return 1 @@ -22,6 +45,16 @@ export const similarity = (text: string, pattern: string): number => { return (longestLength - editDistance) / longestLength } +/** + * Finds the closest matching pattern from an array of patterns to a given string. + * + * This function calculates the Levenshtein distance between the input string and each pattern + * in the provided array and returns the pattern with the smallest Levenshtein distance. + * + * @param {string} string - The input string for which to find the closest matching pattern. + * @param {string[]} patterns - An array of patterns to compare against the input string. + * @returns {string} The closest matching pattern from the provided array. + */ export const didyoumean = (string: string, patterns: string[]): string => { let minDistance = Infinity let minIndex = 0