How to get the percentage of similarity between strings?

Asked

Viewed 140 times

1

What is the best way to compare the level, or percentage of likeness between two strings using Javascript or Typescript?

Example:

string1 = "Este grupo é muito util para crescer profissionalmente e aprender coisas novas."

string2 = "Este grupo é muito útil para aprender coisas novas e úteis e crescer profissionalmente."

The function would return the percentage of similarity between string1 and string2, as for example: 58%

  • 1

    and how does it arrive at 58%? what have you tried to do?

  • you may need to use machine learning for this.

  • 1

    You came to see this question?

  • Search for Levenshtein or Soundex algorithm.

1 answer

0

From what I understand you are trying to get the percentage of equal words. I created this function.

She’s spinning on the Fiddle: https://jsfiddle.net/uybxw51z/1/

function semelhanca(s1, s2) {
  var arr1 = s1.split(" ");
  var arr2 = s2.split(" ");

  var arrMaior = [];
  var arrMenor = [];

  if(arr1.length > arr2.length) {
    arrMaior = arr1;
    arrMenor = arr2;
  } else {
    arrMaior = arr2;
    arrMenor = arr1;
  }

  var palavrasIguais = 0;

  for(var i = 0; i < arrMaior.length; i++) {
    if(arrMenor.includes(arrMaior[i]))  
        palavrasIguais++;
  }

  return  palavrasIguais / arrMaior.length * 100 ;
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.