v1
- by nigrimmist 2/15/202200
Setup HTML - click to add setup HTML
disable setup JavaScript
Setup JavaScript

//string-similarity library (https://github.com/aceakash/string-similarity/blob/master/src/index.js) 
function stringSimilarity___CompareTwoStrings(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 + 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']
delete caserun single casemove downdrag and drop case


ready



for(var i=0;i<cases.length ;i++)
	for(j=0;j<testArrToCompare.length;j++)
		stringSimilarity___CompareTwoStrings(cases[i], testArrToCompare[j])
delete caserun single casemove updrag and drop case


ready



for(var i=0;i<cases.length ;i++)
	for(j=0;j<testArrToCompare.length;j++)
		fromStackOverflow_Similarity(cases[i], testArrToCompare[j])
Test Case - click to add another test case
Teardown JS - click to add teardown JavaScript
Output (DOM) - click to monitor output (DOM) while test is running
RUN