Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Edit Distance Based String Similarity Algorithm Implementation in JavaScript
var string = [];
string[0] = 'CALM 03/26 PURCHASE';
string[1] = 'CANVA* 02644-3020257 03/28 PURCHASE HTTPSCANVA.CO DE';
string[2] = 'VZWRLSS*PRPAY AUTOPAY 03/19 PURCHASE';
string[3] = 'WWW.NORDVPN.COM HTTPSWWW.NORD';
var findMe = 'CALM 02/26 PURCHASE INTERNET';
function calculateSimilarity(aString, bString) {
var matchingCharacters = 0;
var totalCharacters;
if (aString.length >= bString.length) {
totalCharacters = aString.length;
} else {
totalCharacters = bString.length
}
var shortestStringLength;
if (aString.length < bString.length) {
shortestStringLength = aString.length;
} else if (bString.length < aString.length) {
shortestStringLength = bString.length;
} else {
shortestStringLength = aString.length;
}
for (var i = 0; i < shortestStringLength; i++) {
if (aString[i] === bString[i]) {
matchingCharacters++;
}
}
var percentSimilar = matchingCharacters / totalCharacters;
return percentSimilar;
}
console.log(calculateSimilarity(findMe, string[0]).toString());
console.log(calculateSimilarity(findMe, string[1]).toString());
console.log(calculateSimilarity(findMe, string[2]).toString());
console.log(calculateSimilarity(findMe, string[3]).toString());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.