9

Is there any way to calculate % match between 2 strings?

i have a situation where it is required to calculate matches between 2 strings if there is 85%

match then i will combine 2 tables, i have written the code for combining 2 tables

my sample strings are :

var str1 = 'i love javascript';
var str2 = 'i love javascripttt';

var matchPer = match(str1,str2); // result might be 80% , 85%, 90% ,95% etc
smci
  • 32,567
  • 20
  • 113
  • 146
Dilip G
  • 489
  • 2
  • 7
  • 15
  • 3
    What's your logic for calculating the percentage? – Robby Cornelissen Jan 10 '17 at 05:06
  • 5
    There's not just *a* way, there are *lots* of ways. Do you have any guideline for choosing one? What does "85% match" actually mean to you? – hobbs Jan 10 '17 at 07:23
  • 8
    `1 - levenshtein(str1, str2) / max(str1.length, str2.length)` seems like a reasonable metric to me, but you might want Damerau-Levenshtein (which considers the difference between "bacon" and "baocn" to be 1 instead of 2), or Hamming distance as in Ala Eddine JEBALI's answer, which is unforgiving of additions and deletions ("bananas" and "ananas" have a Levenshtein distance of 1 but a Hamming distance of 6. Are they 14% similar or 86% similar?) – hobbs Jan 10 '17 at 07:30
  • @hobbs Please post that as an answer – smci Jan 10 '17 at 09:51
  • 1
    More relevant info: https://en.wikipedia.org/wiki/String_metric – JollyJoker Jan 10 '17 at 09:58
  • You might want to have a look at https://github.com/joshaven/string_score – d.felber Jan 13 '17 at 08:23

1 Answers1

8

Something like this?

var str1 = 'i love javascript';
var str2 = 'i love javascripttt';

function match(str1, str2){
    var tmpValue = 0;
    var minLength = str1.length;
 if(str1.length > str2.length){
  var minLength = str2.length;
 } 
    var maxLength = str1.length;
 if(str1.length < str2.length){
  var maxLength = str2.length;
 }
    for(var i = 0; i < minLength; i++) {
        if(str1[i] == str2[i]) {
            tmpValue++;
        }
    }
    var weight = tmpValue / maxLength;
    return (weight * 100) + "%";
}

var matchPer = match(str1,str2);
console.log(matchPer); //outputs: 89.47%
console.log( match("aaaaa", "aaaaa") ); //outputs: 100%
console.log( match("aaaaa", "aXaaa") ); //outputs: 80%
console.log( match("aaaaa", "aXXaa") ); //outputs: 60%
console.log( match("aaaaa", "aXXXa") ); //outputs: 40%
console.log( match("aaaaa", "aXXXX") ); //outputs: 20%
Ala Eddine JEBALI
  • 7,033
  • 6
  • 46
  • 65