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