function stringSimilarity___CompareTwoStrings(first, second) {
first = first.replace(/\s+/g, '')
second = second.replace(/\s+/g, '')
if (first === second) return 1;
if (first.length < 2 || second.length < 2) return 0;
let firstBigrams = new Map();
for (let i = 0; i < first.length - 1; i++) {
const bigram = first.substring(i, i + 2);
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 + 2);
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);
}
function fromStackOverflow_Similarity(s1, s2) {
var longer = s1;
var shorter = s2;
if (s1.length < s2.length) {
longer = s2;
shorter = s1;
}
var longerLength = longer.length;
if (longerLength == 0) {
return 1.0;
}
return (longerLength - editDistance(longer, shorter)) / parseFloat(longerLength);
}
function editDistance(s1, s2) {
s1 = s1.toLowerCase();
s2 = s2.toLowerCase();
var costs = new Array();
for (var i = 0; i <= s1.length; i++) {
var lastValue = i;
for (var j = 0; j <= s2.length; j++) {
if (i == 0)
costs[j] = j;
else {
if (j > 0) {
var newValue = costs[j - 1];
if (s1.charAt(i - 1) != s2.charAt(j - 1))
newValue = Math.min(Math.min(newValue, lastValue),
costs[j]) + 1;
costs[j - 1] = lastValue;
lastValue = newValue;
}
}
}
if (i > 0)
costs[s2.length] = lastValue;
}
return costs[s2.length];
}
const testArrToCompare = ['one', 'two', 'THREE', 'FiVe', 'Six', 'Simple sentence to match! One apple, free cars and one Big dog! Whoooooo', 'Second simple sentece about two friends in Belarus. Just some longer than previous ONE.']
const cases = ['one dog and five thousands apples!','one', 'TWO brothers', 'And another one value, not so small, but not too long']