How to calculate the percentage difference (or similarity) between two strings?

Asked

Viewed 848 times

5

I’m using PHP in an application I’m developing, where I have a text editor.

In this text editor, with each change made, before saving, I create a record with the original data (i.e., how they are before the change is completed).

In addition, I now have a demand where I need to calculate the percentage of text changes, as well as find out which excerpt each user edited.

I think I’m halfway through knowing every snippet edited through that question: Compare php texts

But I need to know: Is there any way to calculate the percentage difference (or similarity) between two PHP texts?

How can I do that?

  • A doubt, you record this 'original' text somewhere? Because if you save in a column in a table, when edited, you would save in another column, then you could make the comparison between both texts.

  • No. I use the Laravel framework. In this, it is possible to create polymorphic relationships. So I do this using a table called registro_atividades, where save in format JSON all data in that table. That is, for each change, I create a record of that text in another table.

  • 1

    Much more or less related https://answall.com/q/1828/101

  • I have a PL/SQL function that calculates the similarity between two texts. Want to translate it?

  • Send to bro @Reginaldorigo

1 answer

1

Percentage of similarity

In PHP there is a function called similar_text, it serves to find the similarity between two strings. It follows the description of the language documentation about this function:

similar_text - Calculates the similarity between two strings

Knowing the purpose of the function, just add a third parameter to get the percentage of similarity.

similar_text('Hello World!', 'Hello World!', $percentage);
echo $percentage; // Resulta 100, que é a porcentagem.

Browser other questions tagged

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