欢迎来到山村网

C#实现的字符串相似度对比类

2019-03-02 14:32:56浏览:693 来源:山村网   
核心摘要:  本类适用于比较2个字符的相似度,代码如下:  ?using System;  using System.Collections.Generic;  using System.Tex

  本类适用于比较2个字符的相似度,代码如下:

  ?

using System;

  using System.Collections.Generic;

  using System.Text;

  public class StringCompute

  {

  #region 私有变量

  ///

  /// 字符串1

  ///

  private char[] _ArrChar1;

  ///

  /// 字符串2

  ///

  private char[] _ArrChar2;

  ///

  /// 统计结果

  ///

  private Result _Result;

  ///

  /// 开始时间

  ///

  private DateTime _BeginTime;

  ///

  /// 结束时间

  ///

  private DateTime _EndTime;

  ///

  /// 计算次数

  ///

  private int _ComputeTimes;

  ///

  /// 算法矩阵

  ///

  private int[,] _Matrix;

  ///

  /// 矩阵列数

  ///

  private int _Column;

  ///

  /// 矩阵行数

  ///

  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 算法实现

  ///

  /// 初始化算法基本信息

  ///

  /// 字符串1

  /// 字符串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];

  }

  ///

  /// 计算相似度

  ///

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

  }

  ///

  /// 计算相似度(不记录比较时间)

  ///

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

  }

  ///

  /// 计算相似度

  ///

  /// 字符串1

  /// 字符串2

  public void Compute(string str1, string str2)

  {

  this.StringComputeInit(str1, str2);

  this.Compute();

  }

  ///

  /// 计算相似度

  ///

  /// 字符串1

  /// 字符串2

  public void SpeedyCompute(string str1, string str2)

  {

  this.StringComputeInit(str1, str2);

  this.SpeedyCompute();

  }

  ///

  /// 初始化矩阵的第一行和第一列

  ///

  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;

  }

  }

  ///

  /// 取三个数中的最小值

  ///

  ///

  ///

  ///

  ///

  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

  }

  ///

  /// 计算结果

  ///

  public struct Result

  {

  ///

  /// 相似度

  ///

  public decimal Rate;

  ///

  /// 对比次数

  ///

  public string ComputeTimes;

  ///

  /// 使用时间

  ///

  public string UseTime;

  ///

  /// 差异

  ///

  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#、.Net中把字符串(String)格式转换为DateTime类型的三种方法

上一篇:

做VB工程时不可注册为ActiveX控件的问题

  • 信息二维码

    手机看新闻

  • 分享到
打赏
免责声明
• 
本文仅代表作者个人观点,本站未对其内容进行核实,请读者仅做参考,如若文中涉及有违公德、触犯法律的内容,一经发现,立即删除,作者需自行承担相应责任。涉及到版权或其他问题,请及时联系我们 xfptx@outlook.com