Repository navigation
guess-rater 2.0.0: fuzzy matching that just works
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@2import { 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 ascorrect,close("almost!") orwrong, 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: trueuses each one at most once.- Presets:
music(dropsfeat.,- Remastered 2011,(Live), leading articles),search(prefixes and partial matches),strict. numbers(on by default):two,deux,IIand2compare equal.strip,replaceandignoreWordsoptions 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
keysonly 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
explainorreturn: 'entries'switches). - Removed:
rate,getSimilarityScore,rankCandidates,findBestMatch,filterMatches,extract,normalizeString,getLevenshteinDistance,getLevenshteinScore,getJaroScore,getJaroWinklerScore,getTokenSortScore,getTokenSetScore, and thehybridalgorithm.
| 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
replaceno longer replaces text inside words (stnever touchesstairs).- Texts made only of punctuation no longer score 100 against each other.
Full changelog: #8