当前位置:网站首页>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 .
边栏推荐
- [matlab] general function of communication signal modulation - generation of narrow-band Gaussian white noise
- Zhongke panyun-d module analysis and scoring standard
- June 2022 summary
- Get the ID of the record just inserted from laravel
- flink1.13 sql基础语法(二)join操作
- Download kicad on Alibaba cloud image station
- Letter meaning and parameter abbreviation of optical module Daquan
- [matlab] matlab simulates digital baseband transmission system - digital baseband transmission system
- Simulated small root pile
- Simple g++ and GDB debugging
猜你喜欢
Unity is connected to the weather system
Simulink与Arduino串口通信
Simple g++ and GDB debugging
Headache delayed double deletion
[MySQL practice of massive data with high concurrency, high performance and high availability -8] - transaction isolation mechanism of InnoDB
全国职业院校技能大赛(中职组)网络安全竞赛试题—解析
中职组网络安全—内存取证
Electronic components mall and data manual download website summary
Useful plug-ins for vscode
How to use postman to realize simple interface Association [add, delete, modify and query]
随机推荐
[matlab] matlab simulation - low pass Gaussian white noise
JS string splicing
[matlab] matlab simulates digital baseband transmission system eye diagram of bipolar baseband signal (class I part response waveform)
LM小型可编程控制器软件(基于CoDeSys)笔记二十二:错误4268/4052
Enterprise level log analysis system elk (if things backfire, there must be other arrangements)
RSA加密应用常见缺陷的原理与实践
Void convolution, deformable convolution, deformable ROI pooling
中科磐云—2022广西逆向解析思路
flink1.13 sql基础语法(一)DDL、DML
Two sides of the evening: tell me about the bloom filter and cuckoo filter? Application scenario? I'm confused..
【QT】制作MyComboBox点击事件
Supplement the JS of a video website to decrypt the video
[matlab] matlab simulates digital bandpass transmission systems - QPSK and OQPSK systems
总线的基本概念
LM小型可编程控制器软件(基于CoDeSys)笔记二十一:错误3703
[matlab] matlab simulation - simulate the AM modulation process of the modulation system
How to build your own knowledge engine? Community open application
【兴趣阅读】Adversarial Filtering Modeling on Long-term User Behavior Sequences for Click-Through Rate Pre
19.Frambuffer应用编程
Fault analysis | mongodb 5.0 reports an error, and the legal instruction solves it