Shan -4 years ago 86
C# Question

# How to compare two strings and find the percentage of similarity

The below code does the job, but takes lot of time. Am comparing the contents of two HTML files which I have saved as a string in MongoDB already. And the length of the string is around 30K+ and have around 250K+ records to compare. Thus the job is taking quite lot of time.

Is there any easier way or plugin to use and is quite fast too?

``````private int ComputeCost(string input1, string input2)
{
if (string.IsNullOrEmpty(input1))
return string.IsNullOrEmpty(input2) ? 0 : input2.Length;

if (string.IsNullOrEmpty(input2))
return string.IsNullOrEmpty(input1) ? 0 : input1.Length;

int input1Length = input1.Length;
int input2Length = input2.Length;

int[,] distance = new int[input1Length + 1, input2Length + 1];

for (int i = 0; i <= input1Length; distance[i, 0] = i++) ;
for (int j = 0; j <= input2Length; distance[0, j] = j++) ;

for (int i = 1; i <= input1Length; i++)
{
for (int j = 1; j <= input2Length; j++)
{
int cost = (input2[j - 1] == input1[i - 1]) ? 0 : 1;

distance[i, j] = Math.Min(
Math.Min(distance[i - 1, j] + 1, distance[i, j - 1] + 1),
distance[i - 1, j - 1] + cost);
}
}

return distance[input1Length, input2Length];
}
`````` Shan
{"ft_lang_label":"__label__en","ft_lang_prob":0.6714318,"math_prob":0.9986222,"size":1267,"snap":"2021-31-2021-39","text_gpt3_token_len":370,"char_repetition_ratio":0.17814727,"word_repetition_ratio":0.0,"special_character_ratio":0.33307025,"punctuation_ratio":0.20992367,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9960914,"pos_list":[0,1,2,3,4],"im_url_duplicate_count":[null,null,null,null,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2021-09-20T12:01:24Z\",\"WARC-Record-ID\":\"<urn:uuid:d34e245b-777e-4ec5-b464-0628f88cc147>\",\"Content-Length\":\"37000\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:be56d031-84bf-4717-8c71-e222407fcd73>\",\"WARC-Concurrent-To\":\"<urn:uuid:e883e3d7-5948-4a31-bf6a-8706c3609d1d>\",\"WARC-IP-Address\":\"72.52.219.43\",\"WARC-Target-URI\":\"https://codedump.io/share/Zj771Zp0XueD/1/how-to-compare-two-strings-and-find-the-percentage-of-similarity\",\"WARC-Payload-Digest\":\"sha1:6UMO4D4MQQLB7JVDRW47TUE6AOU2TMYU\",\"WARC-Block-Digest\":\"sha1:HZMMWHJQ5UD4XJ4LLSFJ5JQFJFHAG4YJ\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2021/CC-MAIN-2021-39/CC-MAIN-2021-39_segments_1631780057036.89_warc_CC-MAIN-20210920101029-20210920131029-00691.warc.gz\"}"}