当前位置:网站首页>C # character similarity comparison general class
C # character similarity comparison general class
2022-07-04 05:25:00 【Brother Lei talks about programming】
This category is applicable to comparison 2 Similarity of characters , The code is as follows :

View Code
using System;
using System.Collections.Generic;
using System.Text;
public class StringCompute
{
#region Private variables
/// <summary>
/// character string 1
/// </summary>
private char[] _ArrChar1;
/// <summary>
/// character string 2
/// </summary>
private char[] _ArrChar2;
/// <summary>
/// The statistical results
/// </summary>
private Result _Result;
/// <summary>
/// Starting time
/// </summary>
private DateTime _BeginTime;
/// <summary>
/// End time
/// </summary>
private DateTime _EndTime;
/// <summary>
/// Count times
/// </summary>
private int _ComputeTimes;
/// <summary>
/// Algorithm matrix
/// </summary>
private int[,] _Matrix;
/// <summary>
/// Number of matrix columns
/// </summary>
private int _Column;
/// <summary>
/// Number of matrix lines
/// </summary>
private int _Row;
#endregion
#region attribute
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"> character string 1</param>
/// <param name="str2"> character 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()
{
// Starting time
_BeginTime = DateTime.Now;
// Initialize the first row and 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;
}
// Key steps , Calculate the current position value as the left +1、 above +1、 top left corner +intCost Minimum of
// Loop through to the end _Matrix[_Row - 1, _Column - 1] That 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 The number of moves is less than the length of the longest string 20% Calculate 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];
}
/// <summary>
/// Calculate similarity ( Do not record the comparison time )
/// </summary>
public void SpeedyCompute()
{
// Starting time
//_BeginTime = DateTime.Now;
// Initialize the first row and 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;
}
// Key steps , Calculate the current position value as the left +1、 above +1、 top left corner +intCost Minimum of
// Loop through to the end _Matrix[_Row - 1, _Column - 1] That 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 The number of moves is less than the length of the longest string 20% Calculate 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];
}
/// <summary>
/// Calculate similarity
/// </summary>
/// <param name="str1"> character string 1</param>
/// <param name="str2"> character string 2</param>
public void Compute(string str1, string str2)
{
this.StringComputeInit(str1, str2);
this.Compute();
}
/// <summary>
/// Calculate similarity
/// </summary>
/// <param name="str1"> character string 1</param>
/// <param name="str2"> character string 2</param>
public void SpeedyCompute(string str1, string str2)
{
this.StringComputeInit(str1, str2);
this.SpeedyCompute();
}
/// <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 smallest 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>
/// The result of the calculation is
/// </summary>
public struct Result
{
/// <summary>
/// Similarity degree
/// </summary>
public decimal Rate;
/// <summary>
/// Comparison times
/// </summary>
public string ComputeTimes;
/// <summary>
/// Use your time
/// </summary>
public string UseTime;
/// <summary>
/// differences
/// </summary>
public int Difference;
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
- 27.
- 28.
- 29.
- 30.
- 31.
- 32.
- 33.
- 34.
- 35.
- 36.
- 37.
- 38.
- 39.
- 40.
- 41.
- 42.
- 43.
- 44.
- 45.
- 46.
- 47.
- 48.
- 49.
- 50.
- 51.
- 52.
- 53.
- 54.
- 55.
- 56.
- 57.
- 58.
- 59.
- 60.
- 61.
- 62.
- 63.
- 64.
- 65.
- 66.
- 67.
- 68.
- 69.
- 70.
- 71.
- 72.
- 73.
- 74.
- 75.
- 76.
- 77.
- 78.
- 79.
- 80.
- 81.
- 82.
- 83.
- 84.
- 85.
- 86.
- 87.
- 88.
- 89.
- 90.
- 91.
- 92.
- 93.
- 94.
- 95.
- 96.
- 97.
- 98.
- 99.
- 100.
- 101.
- 102.
- 103.
- 104.
- 105.
- 106.
- 107.
- 108.
- 109.
- 110.
- 111.
- 112.
- 113.
- 114.
- 115.
- 116.
- 117.
- 118.
- 119.
- 120.
- 121.
- 122.
- 123.
- 124.
- 125.
- 126.
- 127.
- 128.
- 129.
- 130.
- 131.
- 132.
- 133.
- 134.
- 135.
- 136.
- 137.
- 138.
- 139.
- 140.
- 141.
- 142.
- 143.
- 144.
- 145.
- 146.
- 147.
- 148.
- 149.
- 150.
- 151.
- 152.
- 153.
- 154.
- 155.
- 156.
- 157.
- 158.
- 159.
- 160.
- 161.
- 162.
- 163.
- 164.
- 165.
- 166.
- 167.
- 168.
- 169.
- 170.
- 171.
- 172.
- 173.
- 174.
- 175.
- 176.
- 177.
- 178.
- 179.
- 180.
- 181.
- 182.
- 183.
- 184.
- 185.
- 186.
- 187.
- 188.
- 189.
- 190.
- 191.
- 192.
- 193.
- 194.
- 195.
- 196.
- 197.
- 198.
- 199.
- 200.
- 201.
- 202.
- 203.
- 204.
- 205.
- 206.
- 207.
- 208.
- 209.
- 210.
- 211.
- 212.
- 213.
- 214.
- 215.
- 216.
- 217.
- 218.
- 219.
- 220.
- 221.
- 222.
- 223.
- 224.
- 225.
- 226.
- 227.
- 228.
- 229.
- 230.
- 231.
- 232.
Calling method :
// Mode one
StringCompute stringcompute1 = new StringCompute();
stringcompute1.SpeedyCompute(" Contrast character one ", " Contrast character two "); // Calculate similarity , Do not record the comparison time
decimal rate = stringcompute1.ComputeResult.Rate; // What percentage of similarity , The exact matching similarity is 1
// Mode two
StringCompute stringcompute2 = new StringCompute();
stringcompute2.Compute(); // Calculate similarity , Record the comparison time
string usetime = stringcompute2.ComputeResult.UseTime; // Compare the usage time
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
Follow the QR code below , Subscribe to more .

边栏推荐
- Flink1.13 basic SQL syntax (II) join operation
- With the advent of the IP era, how can E-sports hotels take advantage of the "east wind" of games?
- 基于单片机的太阳能杀虫系统
- Signification des lettres du module optique et abréviation des paramètres Daquan
- A summary of the 8544 problem that SolidWorks Standard cannot obtain a license
- ETCD数据库源码分析——初始化总览
- 企业级日志分析系统ELK(如果事与愿违那一定另有安排)
- [matlab] general function of communication signal modulation - generation of narrow-band Gaussian white noise
- [paper summary] zero shot semantic segmentation
- Headache delayed double deletion
猜你喜欢

谷歌 Chrome 浏览器将支持选取文字翻译功能

2022g2 power station boiler stoker special operation certificate examination question bank and answers

基于单片机的太阳能杀虫系统

Simple g++ and GDB debugging

LM small programmable controller software (based on CoDeSys) note XXI: error 3703

光模塊字母含義及參數簡稱大全

Flutter ‘/usr/lib/libswiftCore. dylib‘ (no such file)

VB.net 简单的处理图片,黑白(类库——7)

Simulink与Arduino串口通信

Useful plug-ins for vscode
随机推荐
Automated testing selenium foundation -- webdriverapi
Two sides of the evening: tell me about the bloom filter and cuckoo filter? Application scenario? I'm confused..
Etcd database source code analysis - initialization overview
Easy change
Roles of rollup components
模拟小根堆
Flink1.13 basic SQL syntax (II) join operation
谷歌 Chrome 浏览器将支持选取文字翻译功能
JS string splicing
[matlab] matlab simulation - simulate the AM modulation process of the modulation system
Zhongke Panyun - 2022 Guangxi reverse analysis ideas
Build an Internet of things infrared temperature measuring punch in machine with esp32 / rush to work after the Spring Festival? Baa, no matter how hard you work, you must take your temperature first
[matlab] general function of communication signal modulation bandpass filter
Notepad++--显示相关的配置
2022g2 power station boiler stoker special operation certificate examination question bank and answers
Encryption and decryption
[matlab] general function of communication signal modulation inverse Fourier transform
PostgreSQL has officially surpassed mysql. Is this guy too strong!
Sécurité du réseau dans les écoles professionnelles secondaires - preuve de mémoire
TCP状态转换图