[C#] C#通过编辑距离计算两个字符串的相似度 →→→→→进入此内容的聊天室

来自 , 2019-04-28, 写在 C#, 查看 108 次.
URL http://www.code666.cn/view/c80bcf42
  1. using System;
  2. using System.Text.RegularExpressions;
  3. using System.Threading.Tasks;
  4.  
  5. namespace Levenshtein
  6. {
  7.     /// <summary>
  8.     /// 分析完成事件委托
  9.     /// </summary>
  10.     /// <param name="sim">相似度</param>
  11.     public delegate void AnalyzerCompletedHander(double sim);
  12.  
  13.     /// <summary>
  14.     /// 文章相似度工具
  15.     /// </summary>
  16.     public class LevenshteinDistance:IDisposable
  17.     {
  18.         private string str1;
  19.         private string str2;
  20.         private int[,] index;
  21.         int k;
  22.         Task<double> task;
  23.  
  24.         /// <summary>
  25.         /// 分析完成事件
  26.         /// </summary>
  27.         public event AnalyzerCompletedHander AnalyzerCompleted;
  28.  
  29.         /// <summary>
  30.         /// 获取或设置文章1
  31.         /// </summary>
  32.         public string Str1
  33.         {
  34.             get { return str1; }
  35.             set
  36.             {
  37.                 str1 = Format(value);
  38.                 index = new int[str1.Length, str2.Length];
  39.             }
  40.         }
  41.  
  42.         /// <summary>
  43.         /// 获取或设置文章2
  44.         /// </summary>
  45.         public string Str2
  46.         {
  47.             get { return str2; }
  48.             set
  49.             {
  50.                 str2 = Format(value);
  51.                 index = new int[str1.Length, str2.Length];
  52.             }
  53.         }
  54.  
  55.         /// <summary>
  56.         /// 运算总次数
  57.         /// </summary>
  58.         public int TotalTimes
  59.         {
  60.             get { return str1.Length * str2.Length; }
  61.         }
  62.  
  63.         /// <summary>
  64.         /// 是否完成
  65.         /// </summary>
  66.         public bool IsCompleted
  67.         {
  68.             get { return task.IsCompleted; }
  69.         }
  70.  
  71.         /// <summary>
  72.         /// 实例化
  73.         /// </summary>
  74.         /// <param name="str1">文章1</param>
  75.         /// <param name="str2">文章2</param>
  76.         public LevenshteinDistance(string str1, string str2)
  77.         {
  78.             this.str1 = Format(str1);
  79.             this.str2 = Format(str2);
  80.             index = new int[str1.Length, str2.Length];
  81.         }
  82.  
  83.         public LevenshteinDistance()
  84.         {
  85.         }
  86.  
  87.         /// <summary>
  88.         /// 异步开始任务
  89.         /// </summary>
  90.         public void Start()
  91.         {
  92.             task = new Task<double>(Analyzer);
  93.             task.Start();
  94.             task.ContinueWith(o => Completed(o.Result));
  95.         }
  96.  
  97.         /// <summary>
  98.         /// 同步开始任务
  99.         /// </summary>
  100.         /// <returns>相似度</returns>
  101.         public double StartAyns()
  102.         {
  103.             task = new Task<double>(Analyzer);
  104.             task.Start();
  105.             task.Wait();
  106.             return task.Result;
  107.         }
  108.  
  109.         private void Completed(double s)
  110.         {
  111.             if (AnalyzerCompleted != null)
  112.             {
  113.                 AnalyzerCompleted(s);
  114.             }
  115.         }
  116.  
  117.         private double Analyzer()
  118.         {
  119.             if (str1.Length == 0 || str2.Length == 0)
  120.                 return 0;
  121.             for (int i = 0; i < str1.Length; i++)
  122.             {
  123.                 for (int j = 0; j < str2.Length; j++)
  124.                 {
  125.                     k = str1[i] == str2[j] ? 0 : 1;
  126.                     if (i == 0&&j==0)
  127.                     {
  128.                         continue;
  129.                     }
  130.                     else if (i == 0)
  131.                     {
  132.                         index[i, j] = k + index[i, j - 1];
  133.                         continue;
  134.                     }
  135.                     else if (j == 0)
  136.                     {
  137.                         index[i, j] = k + index[i - 1, j];
  138.                         continue;
  139.                     }
  140.                     int temp = Min(index[i, j - 1],
  141.                         index[i - 1, j],
  142.                         index[i - 1, j - 1]);
  143.                     index[i, j] = temp + k;
  144.                 }
  145.             }
  146.             float similarty = 1 - (float)index[str1.Length - 1, str2.Length - 1]
  147.                 / (str1.Length > str2.Length ? str1.Length : str2.Length);
  148.             return similarty;
  149.         }
  150.  
  151.         private string Format(string str)
  152.         {
  153.             str = Regex.Replace(str, @"[^a-zA-Z0-9\u4e00-\u9fa5\s]", "");
  154.             return str;
  155.         }
  156.  
  157.         private int Min(int a, int b, int c)
  158.         {
  159.             int temp = a < b ? a : b;
  160.             temp = temp < c ? temp : c;
  161.             return temp;
  162.         }
  163.  
  164.         public void Dispose()
  165.         {
  166.             task.Dispose();
  167.         }
  168.     }
  169. }
  170. //csharp/7291

回复 "C#通过编辑距离计算两个字符串的相似度"

这儿你可以回复上面这条便签

captcha