C # Implementation of string similarity comparison class

Source: Internet
Author: User
Tags datetime first row

This class is designed to compare the 2-character similarity with the following code:

?

Using System;

Using System.Collections.Generic;

Using System.Text;

public class Stringcompute

{

#region Private variables

///

String 1

///

Private char[] _arrchar1;

///

String 2

///

Private char[] _ARRCHAR2;

///

Statistical results

///

private result _result;

///

Start time

///

Private DateTime _begintime;

///

End time

///

Private DateTime _endtime;

///

Number of calculations

///

private int _computetimes;

///

Algorithm matrix

///

Private int[,] _matrix;

///

Number of matrix columns

///

private int _column;

///

Number of rows in a matrix

///

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

Implementation of #region algorithm

///

Initialization algorithm basic information

///

String 1

String 2

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];

}

///

Calculating similarity degree

///

public void Compute ()

{

Start time

_begintime = DateTime.Now;

Initializes the first row and the first column 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;

}

The key step is to calculate the current position value is left + 1, top + 1, upper left corner +intcost minimum value

Loop traversal 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 length of the string with the same number of moves less than the longest the same 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];

}

///

Calculates similarity (does not record comparison time)

///

public void Speedycompute ()

{

Start time

_begintime = DateTime.Now;

Initializes the first row and the first column 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;

}

The key step is to calculate the current position value is left + 1, top + 1, upper left corner +intcost minimum value

Loop traversal 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 length of the string with the same number of moves less than the longest the same 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];

}

///

Calculating similarity degree

///

String 1

String 2

public void Compute (String str1, String str2)

{

This. Stringcomputeinit (str1, str2);

This.compute ();

}

///

Calculating similarity degree

///

String 1

String 2

public void Speedycompute (String str1, String str2)

{

This. Stringcomputeinit (str1, str2);

This. Speedycompute ();

}

///

Initializes the first row and the first column of the matrix

///

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;

}

}

///

Take the minimum value from three numbers

///

///

///

///

///

private int Minimum (int Second, int third)

{

int intmin = i;

if (Second < intmin)

{

Intmin = Second;

}

if (third < intmin)

{

Intmin = third;

}

return intmin;

}

#endregion

}

///

Calculation results

///

public struct result

{

///

Similarity degree

///

public decimal Rate;

///

Number of comparisons

///

public string computetimes;

///

Use time

///

public string Usetime;

///

Difference

///

public int difference;

}

Call Method:

?

Way One

Stringcompute stringcompute1 = new Stringcompute ();

Stringcompute1. Speedycompute ("Contrast character one", "contrast word Fu"); Calculates the similarity, does not record the time of comparison

Decimal rate = stringcompute1.ComputeResult.Rate; Percent similarity, exact match similarity of 1

Mode two

Stringcompute stringcompute2 = new Stringcompute ();

Stringcompute2.compute (); Calculate the similarity, record the time of comparison

string usetime = Stringcompute2.ComputeResult.UseTime; Compare usage Time

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.