MCPcopy Create free account
hub / github.com/TechSpiritSS/Terminal-Portfolio / compareTwoStrings

Function compareTwoStrings

scripts/compare.js:9–40  ·  view source on GitHub ↗
(first, second)

Source from the content-addressed store, hash-verified

7import { commandsList } from "../script.js";
8
9function compareTwoStrings(first, second) {
10 first = first.replace(/\s+/g, '')
11 second = second.replace(/\s+/g, '')
12
13 if (first === second) return 1; // identical or empty
14 if (first.length < 2 || second.length < 2) return 0; // if either is a 0-letter or 1-letter string
15
16 let firstBigrams = new Map();
17 for (let i = 0; i < first.length - 1; i++) {
18 const bigram = first.substring(i, i + 2);
19 const count = firstBigrams.has(bigram)
20 ? firstBigrams.get(bigram) + 1
21 : 1;
22
23 firstBigrams.set(bigram, count);
24 };
25
26 let intersectionSize = 0;
27 for (let i = 0; i < second.length - 1; i++) {
28 const bigram = second.substring(i, i + 2);
29 const count = firstBigrams.has(bigram)
30 ? firstBigrams.get(bigram)
31 : 0;
32
33 if (count > 0) {
34 firstBigrams.set(bigram, count - 1);
35 intersectionSize++;
36 }
37 }
38
39 return (2.0 * intersectionSize) / (first.length + second.length - 2);
40}
41
42function levenshteinDistance(str1 = '', str2 = '') {
43 const track = Array(str2.length + 1).fill(null).map(() =>

Callers 1

suggestFurtherCommandFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected