繁体   English   中英

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

[英]I need to compare two strings (or arrays) and return their % of similarity regardless of their order

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

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

有没有办法做到这一点?

我试图用.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));

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM