你好,我想问一些帮助我如何在Jquery中做到这一点
计算两个字符串之间匹配的单词数(按顺序),以便我可以生成准确性。
// Example
string1 = "The lazy fox jumps over the fence" // (7 words)
string2 = "The lazy dog jumps under a fence yesterday" // (8 words)
Output: 4
正确率为(4个正确单词/7个需要检查的单词)= 57%
有任何想法都将不胜感激
您可以使用filter
split
为单词并匹配相同的单词
function getWords(str) {
return str.split(" ").filter(Boolean);
}
function getMatchedWords(words1, words2) {
return words1.filter((word) => words2.includes(word));
}
const string1 = "The lazy fox jumps over the fence";
const string2 = "The lazy dog jumps under a fence yesterday";
const words1 = getWords(string1);
const words2 = getWords(string2);
const matchedWords = getMatchedWords(words1, words2);
const ratio = +((100 * matchedWords.length) / words1.length).toPrecision(2);
console.log(ratio);
从包含word, position
元组的字符串中生成集合,然后通过逻辑连接&
使它们相交。根据是否应该区分大小写,应用toUpperCase
操作之前。结果集包含所有字符串的匹配词。
通过这种方式,您可以测试任意数量的字符串是否相互匹配。
我在手机上,写代码不是这样的,抱歉。