C#完成的字符串類似度比較類。本站提示廣大學習愛好者:(C#完成的字符串類似度比較類)文章只能為提供參考,不一定能成為您想要的結果。以下是C#完成的字符串類似度比較類正文
本類實用於比擬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; // 比較應用時光