javascript - 我需要比较两个字符串(或数组)并返回它们的相似度百分比,而不管它们的顺序如何

标签 javascript arrays string

我目前正在尝试制作一个模块,您可以在其中学习古代语言的词汇,为此我需要一个工具来检查用户的答案是否与数据库中的答案匹配。

我想实现这个的方法(如果你有更有效的解决方案,请告诉我)是计算字符数(它们都是小写字符串或没有标点符号的数组)并检查它们的相似度百分比。

有什么办法可以做到这一点吗?

我试着用 .match() 做点什么但不幸的是,结果不太好。

// these are the variables

let p = 'The lazy dog jumps over the quick brown fox. It barked.';
p = p.toLowerCase();
p = p.replace(/\s/g, '');
p = p.replace('.', '');
p = p.replace('.', '');

let a = 'The quick brown fox jumps over the lazy dog. It barked.';
a = a.toLowerCase();
a = a.replace(/\s/g, '');
a = a.replace('.', '');
a = a.replace('.', '');

let c = 'The quick black ostrich jumps over the lazy dog. It barked.';
c = c.toLowerCase();
c = c.replace(/\s/g, '');
c = c.replace('.', '');
c = c.replace('.', '');

// this is what should happen: 

compare(p,a); // should return 100%
compare(p,c); // should return 72% (if my math is correct)

最佳答案

您可以对相同的字符进行计数,第一个字符递增,第二个字符递减,每次计数相加取绝对值作为总和。

然后返回相似度。

function compare(a, b) {
    var count = {}, delta;
    
    a = clean(a);
    b = clean(b);
    
    getCount(a, count, 1);
    getCount(b, count, -1);

    delta = Object.values(count).reduce((s, v) => s + Math.abs(v), 0);
    
    return (b.length - delta) / a.length;
}

function getCount(string, count = {}, inc = 1) {
    Array.from(string).forEach(c => count[c] = (count[c] || 0) + inc);
    return count;
}

const
    clean = s => s.toLowerCase().replace(/[\s.,]+/g, '');

var p = 'The lazy dog jumps over the quick brown fox. It barked.',
    a = 'The quick brown fox jumps over the lazy dog. It barked.',
    c = 'The quick black ostrich jumps over the lazy dog. It barked.';

console.log(compare(p, a));
console.log(compare(p, c));

关于javascript - 我需要比较两个字符串(或数组)并返回它们的相似度百分比,而不管它们的顺序如何,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55546514/

相关文章:

javascript - 在 JS 中定义一个变量是否比覆盖一个变量使用更多的资源?

javascript - 在 vue-chartjs 中循环遍历数组

javascript - 尝试在 Javascript 中连接二维数组

linux - 如何提取字符串开头的一部分?

Javascript:是否有用于引用对象中当前节点的关键字?

javascript - Uncaught ReferenceError : Firebase is not defined

javascript - React 函数的行为不符合预期

javascript - 在 AngularJS 中使用 ngCSV 之前操作数组

python - 长文本作为 Python 中的字符串

Java byte[] 到/从 String 转换