C#實現的字串相似性對比類

來源:互聯網
上載者:User

C#實現的字串相似性對比類

   本類適用於比較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; // 對比使用時間

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.