Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Algorithm to compute similarity of two strings in javascript [closed]

Is there any text similarity algorithm in javascript? I want to compare too essays to determine how similar they are. I was thinking about edit distance, but I don't know how to translate it into percentage.

like image 758
DeviCabrera Avatar asked Dec 29 '25 15:12

DeviCabrera


2 Answers

Take a look at jsdifflib, a javascript implementation of python's SequenceMatcher. You can get the similar percentage: difflib.ratio(string1, string2) * 100. Here is the demo. Hope this is what you want.

like image 89
ChelsiWeaver Avatar answered Jan 01 '26 03:01

ChelsiWeaver


Yes there is!

In PHP, there's a convenient inbuilt string comparison function called levenshtein based on the (widely adopted) algorithm Levenshtein. In JavaScript, it isn't builtin, but it can be easily programmed, and has been.

Here is an implementation of the Levenshtein distance metric:

http://andrew.hedges.name/experiments/levenshtein/

like image 44
seanlevan Avatar answered Jan 01 '26 04:01

seanlevan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!