tmp - compare 2 str

working version compare two strings similarity

by slawe

JavaScript

function compareTwoStrings(first, second)
{
    first = first.replace(/\s+/g, '')
    second = second.replace(/\s+/g, '')

    // identical or empty
    if (first === second)
        return 100;
    // if either is a 0-letter or 1-letter string
    if (first.length < 2 || second.length < 2)
        return 0;

    let firstBigrams = new Map();

    for (let i = 0; i < first.length - 1; i++) {
        let bigram = first.substring(i, i + 2);
        let 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++) {
        let bigram = second.substring(i, i + 2);
        let 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)) * 100;
}

console.log(compareTwoStrings('Memeland ❤️ Memecoin', 'Memeland ❤️ Memecoin '));


function Compare(strA, strB) {
  for (var result = 0, i = strA.length; i--;) {
    if (typeof strB[i] == 'undefined' || strA[i] == strB[i]);
    else if (strA[i].toLowerCase() == strB[i].toLowerCase())
      result++;
    else
      result += 4;
  }
  return (1 - (result + 4 * Math.abs(strA.length - strB.length)) / (2 * (strA.length + strB.length))) * 100;
}

console.log(Compare('Memeland ❤️ Memecoin', 'Memeland ❤️ Memecoin '));


function stringSimilarity(str1, str2, substringLength = 2, caseSensitive = false) {
	if (!caseSensitive) {
		str1 = str1.toLowerCase();
		str2 = str2.toLowerCase();
	}

	if (str1.length < substringLength || str2.length < substringLength)
		return 0;

	const map = new Map();
	for (let i = 0; i < str1.length - (substringLength - 1); i++) {
		const substr1 = str1.substr(i, substringLength);
		map.set(substr1, map.has(substr1) ? map.get(substr1) + 1 : 1);
	}

	let match = 0;
	for (let j...