🚀 Big News: Socket Acquires Coana to Bring Reachability Analysis to Every Appsec Team.Learn more
Socket
Book a DemoInstallSign in
Socket

fuzzy-matching

Package Overview
Dependencies
Maintainers
1
Versions
8
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

fuzzy-matching

Fuzzy string matcher

0.4.3
latest
Source
npm
Version published
Weekly downloads
4.3K
41.61%
Maintainers
1
Weekly downloads
 
Created
Source

fuzzy-matching

NPM version Build Status Dependencies Status devDependencies Status

Fuzzy string matcher

Installation

npm install fuzzy-matching

Code sample

var FuzzyMatching = require('fuzzy-matching');

var fm = new FuzzyMatching(['tough', 'thought', 'through', 'Café']);

// Finds words
console.log(fm.get('tough')); // --> { distance: 1, value: 'tough' }

// Finds words that are spelled wrong by looking at the closest ressembling word
console.log(fm.get('thouhgt')); // --> { distance: 0.7142857142857143, value: 'thought' }
// Beware when words in your dictionary are very close
console.log(fm.get('throught')); // --> { distance: 0.875, value: 'through' },
                                 // though you may have wanted to get 'thought'

// Case insensitive
console.log(fm.get('ThRouHg').value); // --> through

// Accent-proof
console.log(fm.get('cafe').value); // --> Café

// Add words after creation
console.log(fm.get('dinosaur')); // --> { distance: 0, value: null }
                                // because too remote to anything in the dictionary
fm.add('dinosaur');
console.log(fm.get('dinosaur')); // --> { distance: 1, value: 'dinosaur' }

// Want to limit to a certain degree of resemblance?
console.log(fm.get('touch', { maxChanges: 0 }).value); // --> null
console.log(fm.get('touch', { maxChanges: 1 }).value); // --> tough

Use case: quizzes or user inputs with certain expected answers

var FuzzyMatching = require('fuzzy-matching');

var possibleAnswers = ['Jupiter', 'Mercury', 'Venus', 'Earth'],
    fm = new FuzzyMatching(possibleAnswers),
    answer = 'Mercury';

console.log('Which planet is the closest to the Sun: ' + possibleAnswers.join(', ') + '?');

// Some user stuff...

var userAnswer = 'mercuyr';
var correctedAnswer = fm.get(userAnswer, { maxChanges: 2 }).value;
if (answer === correctedAnswer) {
    console.log('That\'s right!, it\'s ' + answer + '!');
} else {
    console.log('Sorry buddy, the answer was ' + answer + '.');
}

Keywords

fuzzy

FAQs

Package last updated on 04 May 2015

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts