Using System;
Using System.Collections.Generic;
Using System.Text;
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 Constructors
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 degree
</summary>
public void Compute ()
{
Start time
_begintime = DateTime.Now;
Initialize the first and first columns of the matrix
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 steps to calculate the current position value to the Left + 1, top + 1, upper left corner +intcost in the minimum value
Loop through to the last _matrix[_row-1, _column-1] is the distance of 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;
20% of the similarity rate is less than the longest string length of 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 (do not record comparison time)
//</summary>
public void Speedycompute ()
{
//Open Start time
//_begintime = DateTime.Now;
//Initialize the first row of the Matrix and the first column
this. Initmatrix ();
int intcost = 0;
for (int i = 1, i < _row; i++)
{
for (int j = 1; j < _column; J + +)
{
if (_arrchar1[i-1] = = _a Rrchar2[j-1])
{
Intcost = 0;
}
Else
{
Intcost = 1;
}
//Key step, calculate the current position value to the Left + 1, top + 1, upper left corner +intcost minimum
//loop through to the last _matrix[_row-1, _column-1] is the distance of 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 moves less than 20% of the longest string length the same question
int intlength = _row > _col Umn? _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 degree
</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 degree
</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 and first columns 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 from 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 degree
</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;
}
----------------------------------------------------------------------------------
Way One
Stringcompute stringcompute1 = new Stringcompute ();
Stringcompute1. Speedycompute ("Contrast character one", "Contrast Word II"); Calculate similarity, do not record comparison time
Decimal rate = stringcompute1.ComputeResult.Rate; Percent of similarity, exact match similarity of 1
Way Two
Stringcompute stringcompute2 = new Stringcompute ();
Stringcompute2.compute (); Calculate similarity, record comparison time
string usetime = Stringcompute2.ComputeResult.UseTime; Compare usage Time
Detection of text similarity