Find String Match Ratio In C#

Hello guys, Today I am gonna show you how can we match two strings and check how much those 2 strings actually matched by percentage ratio.

Prerequisite:

  • Basic knowledge of C#

To do this, you must take two steps:

  • Figuring out how many steps are needed to convert one string to another.
  • Calculate the degree of similarity between two words using the calculations given above.

The code is shown below and we are following these two steps.

For this create a simple console project in C#.

In the Program.cs file I’ve added the code of string match.

using System;
using System.Collections.Generic;

namespace StringMatch
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");

            string Source = "Synonyms";
            string Target = "Antonyms";

            double stringRatio = Compute(Source, Target);
            Console.WriteLine($"String Matched Percentage : {stringRatio*100}% ");
            Console.ReadKey();
        }


        private static double Compute(string source, string target)
        {
            if (string.IsNullOrEmpty(source) || string.IsNullOrEmpty(target)) return 0.0;

            if (source.Length < target.Length)
            {
                var temp = source;
                source = target;
                target = temp;
            }

            source = source.ToLower();
            target = target.ToLower();

            int sourceLength = source.Length;
            int targetLength = target.Length;

            if (source.Contains(target) || target.Contains(source)) return 1.0;

            double perc = 0;

            for (int i = 0; i < targetLength / 2; i++)
            {
                string first = target.Substring(i, 1);
                int matchCount = 0;

                List<int> list = AllIndexesOf(source, first);
                for (int j = 0; j < list.Count; j++)
                {
                    matchCount = 0;
                    for (int k = i; k < targetLength; k++)
                    {
                        if (list[j] + matchCount >= sourceLength) break;
                        if (source[list[j] + matchCount] != target[k]) break;
                        matchCount++;
                    }
                    perc = (double)matchCount / targetLength;
                    if (perc >= 0.70)
                    {
                        return perc;
                    }
                }
            }


            return perc;
        }

        public static List<int> AllIndexesOf(string str, string value)
        {
            List<int> indexes = new List<int>();
            for (int index = 0; ; index += value.Length)
            {
                index = str.IndexOf(value, index);
                if (index == -1)
                    return indexes;
                indexes.Add(index);
            }
        }
    }
}

Here is the output of the above code.

Thanks for reading this blog. Please like and comment if you find this helpful.

Submit a Comment

Your email address will not be published. Required fields are marked *

Subscribe

Select Categories