29
return text1 === text2 ? 1 : 0;
30
}
32
>
const nGramIdx = new Map<string, number>();
33
>
34
>
for (let i = 0; i <= text1.length - n; i++) {
35
>
const nGram = text1.substring(i, i + n);
36
>
const count = nGramIdx.get(nGram) || 0;
37
>
nGramIdx.set(nGram, count + 1);
38
>
}
39
>
40
>
for (let i = 0; i <= text2.length - n; i++) {
41
>
const nGram = text2.substring(i, i + n);
42
>
const count = nGramIdx.get(nGram) || 0;
43
>
nGramIdx.set(nGram, count - 1);
44
>
}
45
>
46
>
const totalNGramCount = text1.length - n + 1 + text2.length - n + 1;
47
>
48
>
let differentNGramCount = 0;
49
>
for (const count of nGramIdx.values()) {
50
>
differentNGramCount += Math.abs(count);
51
>
}
52
>
53
>
const equalNGramCount = totalNGramCount - differentNGramCount;
54
>
55
>
return equalNGramCount / totalNGramCount;
56
>
}
57
58
/**