String similarity comparison class implemented by C
- 2021-07-09 09:05:30
- OfStack
This class is suitable for comparing the similarity of two characters. The code is as follows:
using System;
using System.Collections.Generic;
using System.Text;
public class StringCompute
{
#region Private variable
/// <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 columns of matrix
/// </summary>
private int _Column;
/// <summary>
/// Number of rows of matrix
/// </summary>
private int _Row;
#endregion
#region Attribute
public Result ComputeResult
{
get { return _Result; }
}
#endregion
#region Constructor
public StringCompute(string str1, string str2)
{
this.StringComputeInit(str1, str2);
}
public StringCompute()
{
}
#endregion
#region Algorithm implementation
/// <summary>
/// Basic information of initialization algorithm
/// </summary>
/// <param name="str1"> String 1</param>
/// <param name="str2"> String 2</param>
private void StringComputeInit(string str1, string str2)
{
_ArrChar1 = str1.ToCharArray();
_ArrChar2 = str2.ToCharArray();
_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 = DateTime.Now;
// Initialize the first of the matrix 1 Row and number 1 Column
this.InitMatrix();
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 step: Calculate the current position value to the left +1 , above +1 Upper left corner +intCost The minimum value in
// Loop through to the end _Matrix[_Row - 1, _Column - 1] Is the distance between two strings
_Matrix[i, j] = this.Minimum(_Matrix[i - 1, j] + 1, _Matrix[i, j - 1] + 1, _Matrix[i - 1, j - 1] + intCost);
_ComputeTimes++;
}
}
// End time
_EndTime = DateTime.Now;
// Similarity rate That moves less than the longest string length 20% Calculate identical 1 Question
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 (do not record comparison time)
/// </summary>
public void SpeedyCompute()
{
// Start time
//_BeginTime = DateTime.Now;
// Initialize the first of the matrix 1 Row and number 1 Column
this.InitMatrix();
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 step: Calculate the current position value to the left +1 , above +1 Upper left corner +intCost The minimum value in
// Loop through to the end _Matrix[_Row - 1, _Column - 1] Is the distance between two strings
_Matrix[i, j] = this.Minimum(_Matrix[i - 1, j] + 1, _Matrix[i, j - 1] + 1, _Matrix[i - 1, j - 1] + intCost);
_ComputeTimes++;
}
}
// End time
//_EndTime = DateTime.Now;
// Similarity rate That moves less than the longest string length 20% Calculate identical 1 Question
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)
{
this.StringComputeInit(str1, str2);
this.Compute();
}
/// <summary>
/// Calculate similarity
/// </summary>
/// <param name="str1"> String 1</param>
/// <param name="str2"> String 2</param>
public void SpeedyCompute(string str1, string str2)
{
this.StringComputeInit(str1, str2);
this.SpeedyCompute();
}
/// <summary>
/// Initialize the first of the matrix 1 Row and number 1 Column
/// </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 3 The smallest value in the number
/// </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 result
/// </summary>
public struct Result
{
/// <summary>
/// Similarity
/// </summary>
public decimal Rate;
/// <summary>
/// Comparison times
/// </summary>
public string ComputeTimes;
/// <summary>
/// Usage time
/// </summary>
public string UseTime;
/// <summary>
/// Difference
/// </summary>
public int Difference;
}
Invoke the method:
// Mode 1
StringCompute stringcompute1 = new StringCompute();
stringcompute1.SpeedyCompute(" Contrast character 1", " Contrast character 2"); // Calculate similarity, Do not record comparison time
decimal rate = stringcompute1.ComputeResult.Rate; // What percentage of the similarity is, and the perfect matching similarity is 1
// Mode 2
StringCompute stringcompute2 = new StringCompute();
stringcompute2.Compute(); // Calculate similarity, Record comparison time
string usetime = stringcompute2.ComputeResult.UseTime; // Compare the use time