简体   繁体   English

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

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

I'm currently trying to make a module on which you can study vocabulary for ancient languages, and for that I need a tool to check wether or not the user's answer matches with the one in the database.我目前正在尝试制作一个模块,您可以在该模块上学习古代语言的词汇,为此我需要一个工具来检查用户的答案是否与数据库中的答案匹配。

The way I would want to implement this (if you have a more efficient solution, please let me know) is to count the characters (they will all be lower case strings or arrays without punctuation) and check their percent of similarity.我想要实现这个的方式(如果你有更有效的解决方案,请告诉我)是计算字符(它们都是小写字符串或没有标点符号的数组)并检查它们的相似性百分比。

Is there any way how to do this?有没有办法做到这一点?

I tried to do something with .match() but unfortunately that didn't work out too well.我试图用.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)

You could count the same characters, for the first with an incremeent and for the second by decremeenting each count add take the absolute value for the sum.您可以计算相同的字符,第一个带有增量,第二个通过递减每个计数相加取总和的绝对值。

Then return the similarity.然后返回相似度。

 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