SoFunction
Updated on 2025-03-07

String similarity comparison class implemented by C#

This class is suitable for comparing the similarity of 2 characters, the code is as follows:

using System;
using ;
using ;

public class StringCompute
{
  #region Private variables  /// <summary>
  /// String 1  /// </summary>
  private char[] _ArrChar1;
  /// <summary>
  /// String 2  /// </summary>
  private char[] _ArrChar2;
  /// <summary>
  /// Statistical results  /// </summary>
  private Result _Result;
  /// <summary>
  /// Start time  /// </summary>
  private DateTime _BeginTime;
  /// <summary>
  /// End time  /// </summary>
  private DateTime _EndTime;
  /// <summary>
  /// Number of calculations  /// </summary>
  private int _ComputeTimes;
  /// <summary>
  /// Algorithm matrix  /// </summary>
  private int[,] _Matrix;
  /// <summary>
  /// Number of matrix columns  /// </summary>
  private int _Column;
  /// <summary>
  /// Number of matrix rows  /// </summary>
  private int _Row;
  #endregion
  #region properties  public Result ComputeResult
  {
    get { return _Result; }
  }
  #endregion
  #region constructor  public StringCompute(string str1, string str2)
  {
    (str1, str2);
  }
  public StringCompute()
  {
  }
  #endregion
  #region algorithm implementation  /// <summary>
  /// Basic information of the initialization algorithm  /// </summary>
  /// <param name="str1">String 1</param>  /// <param name="str2">String 2</param>  private void StringComputeInit(string str1, string str2)
  {
    _ArrChar1 = ();
    _ArrChar2 = ();
    _Result = new Result();
    _ComputeTimes = 0;
    _Row = _ArrChar1.Length + 1;
    _Column = _ArrChar2.Length + 1;
    _Matrix = new int[_Row, _Column];
  }
  /// &lt;summary&gt;
  /// Calculate similarity  /// &lt;/summary&gt;
  public void Compute()
  {
    //Start time    _BeginTime = ;
    //Initialize the first row and first column of the matrix    ();
    int intCost = 0;
    for (int i = 1; i &lt; _Row; i++)
    {
      for (int j = 1; j &lt; _Column; j++)
      {
        if (_ArrChar1[i - 1] == _ArrChar2[j - 1])
        {
          intCost = 0;
        }
        else
        {
          intCost = 1;
        }
        //Key steps, calculate the current position value as the minimum value in the left +1, the upper left corner + intCost        //Loop traversal to the end _Matrix[_Row - 1, _Column - 1] is the distance between two strings        _Matrix[i, j] = (_Matrix[i - 1, j] + 1, _Matrix[i, j - 1] + 1, _Matrix[i - 1, j - 1] + intCost);
        _ComputeTimes++;
      }
    }
    //End time    _EndTime = ;
    //Similarity rate: The number of movements is less than 20% of the longest string length. It is considered the same problem.    int intLength = _Row &gt; _Column ? _Row : _Column;

    _Result.Rate = (1 - (decimal)_Matrix[_Row - 1, _Column - 1] / intLength);
    _Result.UseTime = (_EndTime - _BeginTime).ToString();
    _Result.ComputeTimes = _ComputeTimes.ToString();
    _Result.Difference = _Matrix[_Row - 1, _Column - 1];
  }


  /// &lt;summary&gt;
  /// Calculate similarity (no comparison time recorded)  /// &lt;/summary&gt;
  public void SpeedyCompute()
  {
    //Start time    //_BeginTime = ;
    //Initialize the first row and first column of the matrix    ();
    int intCost = 0;
    for (int i = 1; i &lt; _Row; i++)
    {
      for (int j = 1; j &lt; _Column; j++)
      {
        if (_ArrChar1[i - 1] == _ArrChar2[j - 1])
        {
          intCost = 0;
        }
        else
        {
          intCost = 1;
        }
        //Key steps, calculate the current position value as the minimum value in the left +1, the upper left corner + intCost        //Loop traversal to the end _Matrix[_Row - 1, _Column - 1] is the distance between two strings        _Matrix[i, j] = (_Matrix[i - 1, j] + 1, _Matrix[i, j - 1] + 1, _Matrix[i - 1, j - 1] + intCost);
        _ComputeTimes++;
      }
    }
    //End time    //_EndTime = ;
    //Similarity rate: The number of movements is less than 20% of the longest string length. It is considered the same problem.    int intLength = _Row &gt; _Column ? _Row : _Column;

    _Result.Rate = (1 - (decimal)_Matrix[_Row - 1, _Column - 1] / intLength);
    // _Result.UseTime = (_EndTime - _BeginTime).ToString();
    _Result.ComputeTimes = _ComputeTimes.ToString();
    _Result.Difference = _Matrix[_Row - 1, _Column - 1];
  }
  /// &lt;summary&gt;
  /// Calculate similarity  /// &lt;/summary&gt;
  /// <param name="str1">String 1</param>  /// <param name="str2">String 2</param>  public void Compute(string str1, string str2)
  {
    (str1, str2);
    ();
  }

  /// &lt;summary&gt;
  /// Calculate similarity  /// &lt;/summary&gt;
  /// <param name="str1">String 1</param>  /// <param name="str2">String 2</param>  public void SpeedyCompute(string str1, string str2)
  {
    (str1, str2);
    ();
  }
  /// &lt;summary&gt;
  /// Initialize the first row and first column of the matrix  /// &lt;/summary&gt;
  private void InitMatrix()
  {
    for (int i = 0; i &lt; _Column; i++)
    {
      _Matrix[0, i] = i;
    }
    for (int i = 0; i &lt; _Row; i++)
    {
      _Matrix[i, 0] = i;
    }
  }
  /// &lt;summary&gt;
  /// Take the minimum value of the three numbers  /// &lt;/summary&gt;
  /// &lt;param name="First"&gt;&lt;/param&gt;
  /// &lt;param name="Second"&gt;&lt;/param&gt;
  /// &lt;param name="Third"&gt;&lt;/param&gt;
  /// &lt;returns&gt;&lt;/returns&gt;
  private int Minimum(int First, int Second, int Third)
  {
    int intMin = First;
    if (Second &lt; intMin)
    {
      intMin = Second;
    }
    if (Third &lt; intMin)
    {
      intMin = Third;
    }
    return intMin;
  }
  #endregion
}
/// &lt;summary&gt;
/// Calculation results/// &lt;/summary&gt;
public struct Result
{
  /// &lt;summary&gt;
  /// Similarity  /// &lt;/summary&gt;
  public decimal Rate;
  /// &lt;summary&gt;
  /// Number of comparisons  /// &lt;/summary&gt;
  public string ComputeTimes;
  /// &lt;summary&gt;
  /// Use time  /// &lt;/summary&gt;
  public string UseTime;
  /// &lt;summary&gt;
  /// Difference  /// &lt;/summary&gt;
  public int Difference;
}

Calling method:

// Method 1StringCompute stringcompute1 = new StringCompute();
("Contrast Character One", "Contrast Character Two");  // Calculate the similarity, do not record the comparison timedecimal rate = ;     // What percentage of similarity is exactly matched by 1
// Method 2StringCompute stringcompute2 = new StringCompute();
();                 // Calculate the similarity and record the comparison timestring usetime = ;   // Compare usage time