forked from farhan-dqz/JulieMwol
-
-
Notifications
You must be signed in to change notification settings - Fork 261
/
similarity.js
37 lines (30 loc) · 995 Bytes
/
similarity.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
/* WhatsAsena Duplicated - Artificial Intelligence Similarity
Codded by lyfee
function similarity(first, second) {
first = first.replace(/\s+/g, '')
second = second.replace(/\s+/g, '')
if (first === second) return 1; // identical or empty
if (first.length < 2 || second.length < 2) return 0; // if either is a 0-letter or 1-letter string
let firstBigrams = new Map();
for (let i = 0; i < first.length - 1; i++) {
const bigram = first.substring(i, i + 1);
const count = firstBigrams.has(bigram)
? firstBigrams.get(bigram) + 1
: 1;
firstBigrams.set(bigram, count);
}
let intersectionSize = 0;
for (let i = 0; i < second.length - 1; i++) {
const bigram = second.substring(i, i + 1);
const count = firstBigrams.has(bigram)
? firstBigrams.get(bigram)
: 0;
if (count > 0) {
firstBigrams.set(bigram, count - 1);
intersectionSize++;
}
}
return (2.0 * intersectionSize) / (first.length + second.length - 2);
}
module.exports = {similarity:similarity}
*/