Skip to content

guess-rater 2.0.0: fuzzy matching that just works

Choose a tag to compare

@Tfoucher5 Tfoucher5 released this 16 Oct 14:02
· 88 commits to master since this release
ee5f108

guess-rater 2.0.0: fuzzy matching that just works

A full rewrite. One smart score that forgives typos, accents, word order, missing spaces and numbers written in words, plus one function per job: check answers, search as you type, suggest "did you mean…?", spot a name in a message, clean up duplicates, match two lists.

npm install guess-rater@2
import { check, checkAll, search, closest, find, highlight, dedupe, join } from 'guess-rater'

check('bohemian rapsody', 'Bohemian Rhapsody').verdict // 'correct'
checkAll('bohemian rapsody de queen', { title: 'Bohemian Rhapsody', artist: 'Queen' }).found // ['title', 'artist']
closest('javascrpt', ['java', 'javascript', 'typescript'])?.item // 'javascript'

📖 Documentation: https://tfoucher5.github.io/guess-rater/

✨ New

  • check() grades a guess as correct, close ("almost!") or wrong, against one or several accepted answers.
  • checkAll() grades a guess that names several things at once, such as a title and an artist, in any order.
  • search(), closest(), dedupe(), group() work with strings or objects (keys, with optional weights: ['title', { name: 'artist', weight: 0.8 }]).
  • createIndex() prepares a list once, so each keystroke only scores the query.
  • find() finds fuzzy occurrences of a text inside a longer one, with their positions. highlight() splits a text into matching and non-matching pieces, ready to render.
  • join() pairs each item of a list with its best match in another; unique: true uses each one at most once.
  • Presets: music (drops feat., - Remastered 2011, (Live), leading articles), search (prefixes and partial matches), strict.
  • numbers (on by default): two, deux, II and 2 compare equal.
  • strip, replace and ignoreWords options to clean up text, all on whole words.
  • compare() shows the normalized texts and the score of each strategy, to tune your options.
  • Written in TypeScript: every option autocompletes, and keys only accepts properties that hold text.
  • ESM and CommonJS builds, zero dependencies, Node 18+ and every browser.

📊 Measured on real data

Test v2
Blind test, 26,230 Spotify tracks: right guesses accepted 97.6 % (v1: 70.1 %), 0 % of wrong songs
check() per guess ~25 µs
Search 26,230 songs, wanted song in top 10 82.3 % in ~40 ms per keystroke (Fuse.js: ~240 ms)
150,653 cities, one typo, no accents: right city in top 5 100 % in ~70 ms
20,000 English words, one typo: right word in top 3 98.8 % in ~6 ms

Reproduce with npm run bench:real.

⚠️ Breaking changes

  • New default algorithm smart (Damerau-Levenshtein, word order and missing spaces ignored): similar texts generally score higher than in v1. Re-check your thresholds.
  • Options are flat: no more normalize: { ... } block.
  • Each function always returns the same type (no more explain or return: 'entries' switches).
  • Removed: rate, getSimilarityScore, rankCandidates, findBestMatch, filterMatches, extract, normalizeString, getLevenshteinDistance, getLevenshteinScore, getJaroScore, getJaroWinklerScore, getTokenSortScore, getTokenSetScore, and the hybrid algorithm.
v1 v2
rate(a, b) / getSimilarityScore(a, b) similarity(a, b)
rate(a, b, { explain: true }) compare(a, b)
findBestMatch(q, list) closest(q, list)
rankCandidates(q, list) search(q, list, { threshold: 0 })
filterMatches(q, list) / extract(q, list, { limit }) search(q, list, { limit })
getLevenshteinDistance(a, b) levenshtein(a, b)

Full guide: https://tfoucher5.github.io/guess-rater/guide/migration

🐛 Fixes

  • replace no longer replaces text inside words (st never touches stairs).
  • Texts made only of punctuation no longer score 100 against each other.

Full changelog: #8