C#实现的字符串相似度对比类
作者:junjie 时间:2023-08-08 20:35:10
本类适用于比较2个字符的相似度,代码如下:
using System;
using System.Collections.Generic;
using System.Text;
public class StringCompute
{
#region 私有变量
/// <summary>
/// 字符串1
/// </summary>
private char[] _ArrChar1;
/// <summary>
/// 字符串2
/// </summary>
private char[] _ArrChar2;
/// <summary>
/// 统计结果
/// </summary>
private Result _Result;
/// <summary>
/// 开始时间
/// </summary>
private DateTime _BeginTime;
/// <summary>
/// 结束时间
/// </summary>
private DateTime _EndTime;
/// <summary>
/// 计算次数
/// </summary>
private int _ComputeTimes;
/// <summary>
/// 算法矩阵
/// </summary>
private int[,] _Matrix;
/// <summary>
/// 矩阵列数
/// </summary>
private int _Column;
/// <summary>
/// 矩阵行数
/// </summary>
private int _Row;
#endregion
#region 属性
public Result ComputeResult
{
get { return _Result; }
}
#endregion
#region 构造函数
public StringCompute(string str1, string str2)
{
this.StringComputeInit(str1, str2);
}
public StringCompute()
{
}
#endregion
#region 算法实现
/// <summary>
/// 初始化算法基本信息
/// </summary>
/// <param name="str1">字符串1</param>
/// <param name="str2">字符串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>
/// 计算相似度
/// </summary>
public void Compute()
{
//开始时间
_BeginTime = DateTime.Now;
//初始化矩阵的第一行和第一列
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;
}
//关键步骤,计算当前位置值为左边+1、上面+1、左上角+intCost中的最小值
//循环遍历到最后_Matrix[_Row - 1, _Column - 1]即为两个字符串的距离
_Matrix[i, j] = this.Minimum(_Matrix[i - 1, j] + 1, _Matrix[i, j - 1] + 1, _Matrix[i - 1, j - 1] + intCost);
_ComputeTimes++;
}
}
//结束时间
_EndTime = DateTime.Now;
//相似率 移动次数小于最长的字符串长度的20%算同一题
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>
/// 计算相似度(不记录比较时间)
/// </summary>
public void SpeedyCompute()
{
//开始时间
//_BeginTime = DateTime.Now;
//初始化矩阵的第一行和第一列
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;
}
//关键步骤,计算当前位置值为左边+1、上面+1、左上角+intCost中的最小值
//循环遍历到最后_Matrix[_Row - 1, _Column - 1]即为两个字符串的距离
_Matrix[i, j] = this.Minimum(_Matrix[i - 1, j] + 1, _Matrix[i, j - 1] + 1, _Matrix[i - 1, j - 1] + intCost);
_ComputeTimes++;
}
}
//结束时间
//_EndTime = DateTime.Now;
//相似率 移动次数小于最长的字符串长度的20%算同一题
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>
/// 计算相似度
/// </summary>
/// <param name="str1">字符串1</param>
/// <param name="str2">字符串2</param>
public void Compute(string str1, string str2)
{
this.StringComputeInit(str1, str2);
this.Compute();
}
/// <summary>
/// 计算相似度
/// </summary>
/// <param name="str1">字符串1</param>
/// <param name="str2">字符串2</param>
public void SpeedyCompute(string str1, string str2)
{
this.StringComputeInit(str1, str2);
this.SpeedyCompute();
}
/// <summary>
/// 初始化矩阵的第一行和第一列
/// </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>
/// 取三个数中的最小值
/// </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>
/// 计算结果
/// </summary>
public struct Result
{
/// <summary>
/// 相似度
/// </summary>
public decimal Rate;
/// <summary>
/// 对比次数
/// </summary>
public string ComputeTimes;
/// <summary>
/// 使用时间
/// </summary>
public string UseTime;
/// <summary>
/// 差异
/// </summary>
public int Difference;
}
调用方法:
// 方式一
StringCompute stringcompute1 = new StringCompute();
stringcompute1.SpeedyCompute("对比字符一", "对比字符二"); // 计算相似度, 不记录比较时间
decimal rate = stringcompute1.ComputeResult.Rate; // 相似度百分之几,完全匹配相似度为1
// 方式二
StringCompute stringcompute2 = new StringCompute();
stringcompute2.Compute(); // 计算相似度, 记录比较时间
string usetime = stringcompute2.ComputeResult.UseTime; // 对比使用时间
标签:C#,字符串,相似度,对比
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Android编程之页面切换测试实例
2022-04-03 22:13:11
![](https://img.aspxhome.com/file/2023/5/139645_0s.png)
Java实现一个顺序表的完整代码
2023-09-21 01:00:59
![](https://img.aspxhome.com/file/2023/0/62500_0s.png)
Java使用原型模式展现每日生活应用案例详解
2023-03-08 04:27:08
![](https://img.aspxhome.com/file/2023/3/87373_0s.png)
聊聊Unity 自定义日志保存的问题
2021-11-28 15:38:55
![](https://img.aspxhome.com/file/2023/9/69199_0s.png)
Android 模仿QQ侧滑删除ListView功能示例
2023-10-27 21:03:43
java单机接口限流处理方案详解
2021-05-25 21:08:07
详解c# 中的DateTime
2023-05-15 01:48:58
![](https://img.aspxhome.com/file/2023/2/100192_0s.png)
Spring深入刨析声明式事务注解的源码
2023-10-23 09:41:48
![](https://img.aspxhome.com/file/2023/6/61026_0s.png)
java配置多个过滤器优先级以及几个常用过滤器操作
2023-12-17 01:52:10
WPF实现页面的切换的示例代码
2023-09-26 21:35:27
![](https://img.aspxhome.com/file/2023/0/102150_0s.jpg)
java和javascript中过滤掉img形式的字符串不显示图片的方法
2021-08-31 10:12:49
![](https://img.aspxhome.com/file/2023/8/65738_0s.png)
浅析c# 线程同步
2022-09-19 18:43:03
C#基于Socket套接字的网络通信封装
2023-11-08 16:42:18
![](https://img.aspxhome.com/file/2023/6/104346_0s.jpg)
javax.mail.SendFailedException: Sending failed问题原因
2021-08-07 20:00:57
Unity中EventTrigger的几种使用操作
2022-01-15 06:54:37
![](https://img.aspxhome.com/file/2023/6/88426_0s.jpg)
springboot 使用poi进行数据的导出过程详解
2022-12-01 07:23:31
Android UI效果之绘图篇(四)
2022-08-07 19:26:12
![](https://img.aspxhome.com/file/2023/2/126692_0s.png)
浅谈C#在网络波动时防重复提交的方法
2022-07-23 22:37:01
![](https://img.aspxhome.com/file/2023/3/92913_0s.png)
c#实现sqlserver事务处理示例
2022-03-28 19:39:50
Json读写本地文件实现代码
2023-10-10 06:03:21