Skip to content

rate()

Computes the similarity between two strings and returns a score from 0 to 100.

Signature

ts
rate(left: string, right: string, options?: RateOptions): number
rate(left: string, right: string, options: RateOptions & { explain: true }): RateExplainResult

Basic usage

js
import { rate } from 'guess-rater'

rate('Molière', 'moliere')             // 100
rate('levenshtein', 'levenstein')      // ~88
rate('Saint-Nazaire', 'saint nazaire') // 100
rate('cat', 'dog')                     // low

Options

OptionDefaultDescription
algorithm'levenshtein'Matching strategy. → Algorithms
threshold80Used in match field when explain: true. → Thresholds
explainfalseReturn full breakdown object. → Explain mode
spaceInsensitivefalseAlso compare with spaces removed; keeps best score. → spaceInsensitive
normalizeNormalization pipeline. → Normalization
hybridCustom weights per sub-algorithm. → Hybrid
jaroWinkler{ prefixScale, maxPrefixLength }
tokenSort{ baseAlgorithm }
tokenSet{ baseAlgorithm }

Examples

Custom algorithm

js
rate('John Smith', 'Smith, John', { algorithm: 'tokenSort' }) // 100

Custom normalization

js
rate('J. Smith', 'John Smith', {
  normalize: { replacements: { 'j.': 'john' } }
}) // 100

spaceInsensitive

js
rate('stairwaytoheaven', 'stairway to heaven', {
  algorithm: 'levenshtein',
  spaceInsensitive: true
}) // 100

explain mode

js
const result = rate('hello', 'helo', { explain: true })
// {
//   score: 88, match: true, threshold: 80,
//   algorithm: 'levenshtein',
//   input: 'hello', target: 'helo',
//   normalizedLeft: 'hello', normalizedRight: 'helo',
//   details: { normalize: {...} }
// }

Alias

getSimilarityScore() is an alias for rate().

See also