Over simplistic fuzzy search library using Damerau-Levenshtein and optionally the keyboard layout to calculate distances.
FuzzyDist
is a simple tool for calculating the Damerau-Levenshtein distance between two strings. This class also supports optional keyboard-specific distance calculations, allowing for more nuanced distance measurements based on the layout of different keyboards.
npm install fuzzy-dist
To calculate the standard Damerau-Levenshtein distance between two strings, simply instantiate the FuzzyDist
class without specifying a keyboard layout.
import FuzzyDist from 'fuzzy-dist';
const fuzzyDist = new FuzzyDist();
const distance = fuzzyDist.calculateDistance('kitten', 'sitten');
console.log(distance); // Outputs: 1
To include keyboard distance in the Damerau-Levenshtein calculation, you can specify a keyboard layout when instantiating the FuzzyDist
class. This will add an additional layer of accuracy by taking into account the physical distance between keys on a specified keyboard.
import FuzzyDist from 'fuzzy-dist';
const fuzzyDistQwerty = new FuzzyDist(FuzzyDist.QwertyKeyboard);
const distance = fuzzyDistQwerty.calculateDistance('kitten', 'sitten');
console.log(distance); // Outputs: 2
const distanceWithKeyboard = fuzzyDistQwerty.calculateDistance('kitten', 'oitten');
console.log(distanceWithKeyboard); // Outputs: 1.4
const distanceWithKeyboard = fuzzyDistQwerty.calculateDistance('kitten', 'jitten');
console.log(distanceWithKeyboard); // Outputs: 1
The FuzzyDist
class also provides a method to find the closest match to a target string from an array of strings.
import FuzzyDist from './FuzzyDist';
const fuzzyDist = new FuzzyDist(FuzzyDist.QwertyKeyboard);
const target = 'hello';
const strings = ['hallo', 'hullo', 'hero', 'hell'];
const minDistance = fuzzyDist.getMinimumDistance(target, strings);
console.log(minDistance); // Outputs the minimum distance, e.g., 1
The FuzzyDist
class supports the following keyboard layouts:
- FuzzyDist.QwertyKeyboard: QWERTY keyboard layout
- FuzzyDist.QwertzKeyboard: QWERTZ keyboard layout (common in Germany)
- FuzzyDist.AzertyKeyboard: AZERTY keyboard layout (common in France)
- FuzzyDist.TurkishFKeyboard: Turkish F keyboard layout
- FuzzyDist.InScriptKeyboard: InScript keyboard layout (common in India)
constructor(keyboardId: number | null | undefined = null)
-
keyboardId: The identifier for the keyboard layout to use. If
null
, the standard Damerau-Levenshtein distance is calculated without considering keyboard layout.
Calculates the Damerau-Levenshtein distance between two strings, optionally considering keyboard layout if specified in the constructor.
Finds and returns the minimum Damerau-Levenshtein distance between a target string and an array of strings.
MIT © Remotal