<!DOCTYPE html>
<html>
<head>
	<title>文章对比相似度</title>
	<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
	<div class="container">
		<h1>文章对比相似度</h1>
		<div class="input-group">
			<label for="text1">文本框1:</label>
			<textarea id="text1"></textarea>
			<div id="word-count1"></div>
		</div>
		<div class="input-group">
			<label for="text2">文本框2:</label>
			<textarea id="text2"></textarea>
			<div id="word-count2"></div>
		</div>
		<button id="btn">对比相似度</button>
		<div id="result"></div>
	</div>
<pre><code>&lt;script src=&quot;https://code.jquery.com/jquery-3.6.0.min.js&quot;&gt;&lt;/script&gt;
&lt;script type=&quot;text/javascript&quot;&gt;
$(function() {
$('#btn').click(function() {
	var text1 = $('#text1').val().replace(/\s+/g, '');
	var text2 = $('#text2').val().replace(/\s+/g, '');
	var similarity = getSimilarity(text1, text2);
	var result = '';
	var wordCount1 = text1.trim().split(/\s+/).length;
	var wordCount2 = text2.trim().split(/\s+/).length;
	if (similarity &lt; 0.3) {
		result = '相似度为:' + (similarity * 100).toFixed(2) + '%,判定为原创。';
	} else if (similarity &gt;= 0.3 &amp;&amp; similarity &lt;= 0.5) {
		result = '相似度为:' + (similarity * 100).toFixed(2) + '%,文章相似度高。';
	} else if (similarity &gt; 0.5 &amp;&amp; similarity &lt;= 0.8) {
		result = '相似度为:' + (similarity * 100).toFixed(2) + '%,相似度已经非常高。';
	} else {
		result = '相似度为:' + (similarity * 100).toFixed(2) + '%,纯属抄袭。';
	}
	$('#result').text(result);
	$('#word-count1').text('字数:' + wordCount1);
	$('#word-count2').text('字数:' + wordCount2);
});

function getSimilarity(text1, text2) {
	var len1 = text1.length;
	var len2 = text2.length;
	var maxlen = Math.max(len1, len2);
	var minlen = Math.min(len1, len2);
	var same = 0;
	for (var i = 0; i &lt; minlen; i++) {
		if (text1[i] == text2[i]) {
			same++;
		}
	}
	var similarity = same / maxlen;
	return similarity;
}
</code></pre>
<p>});
</script></p>
</body>
</html>
<p>代码中,<code>replace(/\s+/g, '')</code>表示用空字符替换所有的空格。</p>
JS代码实现文章对比相似度检测(去除空格和空字符)

原文地址: https://www.cveoy.top/t/topic/jrHZ 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录