当前位置:网站首页>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 .

边栏推荐
- 全国职业院校技能大赛(中职组)网络安全竞赛试题—解析
- Just do it with your hands 7 - * project construction details 2 - hook configuration
- c语言经典指针和数组笔试题解析
- 19.Frambuffer应用编程
- [matlab] matlab simulation - low pass Gaussian white noise
- 模拟小根堆
- We believe that the development of consumer Internet will still be limited to the Internet industry itself
- [matlab] matlab simulation - simulate the AM modulation process of the modulation system
- Easy change
- How to build your own knowledge engine? Community open application
猜你喜欢

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

Simulink and Arduino serial port communication

A summary of the 8544 problem that SolidWorks Standard cannot obtain a license

PostgreSQL has officially surpassed mysql. Is this guy too strong!

企业级日志分析系统ELK(如果事与愿违那一定另有安排)

2022 question bank and answers for safety management personnel of hazardous chemical business units

Download kicad on Alibaba cloud image station

Fault analysis | mongodb 5.0 reports an error, and the legal instruction solves it

Integer type of C language

Unity is connected to the weather system
随机推荐
Trie number dictionary tree
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
LM小型可编程控制器软件(基于CoDeSys)笔记二十一:错误3703
FreeRTOS 中 RISC-V-Qemu-virt_GCC 的 锁机制 分析
We believe that the development of consumer Internet will still be limited to the Internet industry itself
Evolution of system architecture: differences and connections between SOA and microservice architecture
Notes on the paper "cross view transformers for real time map view semantic segmentation"
Nodejs learning document
Zhongke panyun-2022 Guangdong Trojan horse information acquisition and analysis
NTFS security permissions
全国职业院校技能大赛(中职组)网络安全竞赛试题—解析
Introduction To AMBA 简单理解
[matlab] matlab simulation modulation system - VSB system
Flink1.13 SQL basic syntax (I) DDL, DML
总线的基本概念
[high concurrency, high performance and high availability of massive data MySQL practice-7] - memory data drop disk
ping端口神器psping
JS string splicing enhancement
laravel 中获取刚刚插入的记录的id
Zhongke Panyun - module a infrastructure setting and safety reinforcement scoring standard