js实现fuzzywuzzy
实现模糊字符串匹配(FuzzyWuzzy)的JavaScript方法
使用fuzzball.js库
fuzzball.js是Python库FuzzyWuzzy的JavaScript移植版本,支持Levenshtein距离、部分字符串匹配等算法。
安装方法:
npm install fuzzball
基础用法示例:
const fuzz = require('fuzzball');
const options = { scorer: fuzz.token_set_ratio };
console.log(fuzz.extract("hello", ["hey", "hell", "hola"], options));
纯JavaScript实现Levenshtein距离
以下是一个计算字符串相似度的基础实现:
function levenshteinDistance(a, b) {
const matrix = Array(b.length + 1).fill(null)
.map(() => Array(a.length + 1).fill(null));
for (let i = 0; i <= a.length; i++) matrix[0][i] = i;
for (let j = 0; j <= b.length; j++) matrix[j][0] = j;
for (let j = 1; j <= b.length; j++) {
for (let i = 1; i <= a.length; i++) {
const cost = a[i - 1] === b[j - 1] ? 0 : 1;
matrix[j][i] = Math.min(
matrix[j][i - 1] + 1,
matrix[j - 1][i] + 1,
matrix[j - 1][i - 1] + cost
);
}
}
return matrix[b.length][a.length];
}
function similarity(a, b) {
const distance = levenshteinDistance(a, b);
return 1 - distance / Math.max(a.length, b.length);
}
字符串预处理优化
进行匹配前对字符串进行标准化处理能提高准确率:
function normalizeString(str) {
return str.toLowerCase()
.replace(/[^\w\s]/g, '')
.replace(/\s+/g, ' ')
.trim();
}
const str1 = normalizeString("Hello-World!");
const str2 = normalizeString("hello world");
console.log(similarity(str1, str2)); // 将得到更高匹配度
基于n-gram的相似度计算
该方法通过比较字符串的子序列来评估相似度:
function getNGrams(str, n) {
const grams = [];
for (let i = 0; i <= str.length - n; i++) {
grams.push(str.substring(i, i + n));
}
return grams;
}
function nGramSimilarity(a, b, n = 2) {
const aGrams = getNGrams(a, n);
const bGrams = getNGrams(b, n);
const intersection = aGrams.filter(x => bGrams.includes(x));
return intersection.length / Math.max(aGrams.length, bGrams.length);
}
实际应用建议
对于生产环境,推荐使用经过优化的库如fuzzball.js。若需要轻量级解决方案,可组合使用Levenshtein距离和n-gram方法,配合字符串预处理。

性能敏感场景应考虑添加缓存机制,或限制最大比较字符串长度。对于大规模数据集,可先使用简单筛选(如首字母匹配)减少需要详细比较的条目数量。






