|
1 | 1 | import type { Evalite } from "../types.js"; |
| 2 | +import levenshteinDistance from "js-levenshtein"; |
2 | 3 |
|
3 | 4 | /** |
4 | 5 | * Checks if your AI's output exactly matches the |
@@ -57,3 +58,43 @@ export async function contains(opts: Evalite.Scorers.ContainsOpts) { |
57 | 58 | score: opts.actual.includes(opts.expected) ? 1 : 0, |
58 | 59 | }; |
59 | 60 | } |
| 61 | + |
| 62 | +/** |
| 63 | + * Measures string similarity using Levenshtein distance |
| 64 | + * (edit distance), normalized to a 0-1 score. |
| 65 | + * |
| 66 | + * Returns a score from 0 to 1, where 1 means identical |
| 67 | + * strings and 0 means completely different. |
| 68 | + * |
| 69 | + * **When to use**: For fuzzy string matching when you |
| 70 | + * want to tolerate small typos, spelling variations, |
| 71 | + * or minor differences. Useful for testing outputs |
| 72 | + * that should be close but not necessarily exact. |
| 73 | + * |
| 74 | + * **When NOT to use**: When exact matches are required |
| 75 | + * (use exactMatch) or when you need semantic similarity |
| 76 | + * that understands meaning (use answerSimilarity). |
| 77 | + * |
| 78 | + * @param opts.actual - The actual output to check |
| 79 | + * @param opts.expected - The expected string to compare against |
| 80 | + */ |
| 81 | +export async function levenshtein(opts: Evalite.Scorers.LevenshteinOpts) { |
| 82 | + if (typeof opts.actual !== "string" || typeof opts.expected !== "string") { |
| 83 | + throw new Error("Both actual and expected must be strings"); |
| 84 | + } |
| 85 | + |
| 86 | + const maxLen = Math.max(opts.actual.length, opts.expected.length); |
| 87 | + |
| 88 | + let score = 1; |
| 89 | + if (maxLen > 0) { |
| 90 | + const distance = levenshteinDistance(opts.actual, opts.expected); |
| 91 | + score = 1 - distance / maxLen; |
| 92 | + } |
| 93 | + |
| 94 | + return { |
| 95 | + name: "Levenshtein", |
| 96 | + description: |
| 97 | + "Measures string similarity using edit distance (0 = different, 1 = identical).", |
| 98 | + score, |
| 99 | + }; |
| 100 | +} |
0 commit comments