Cc implements string similarity comparison by editing distance algorithm

From , 5 Years ago, written in C#, viewed 75 times.
URL https://pastebin.vip/view/b7d35509
  1. public class LevenshteinDistance
  2.     {
  3.  
  4.         private static LevenshteinDistance _instance=null;
  5.         public static LevenshteinDistance Instance
  6.         {
  7.             get
  8.             {
  9.                 if (_instance == null)
  10.                 {
  11.                     return new LevenshteinDistance();
  12.                 }
  13.                 return _instance;
  14.             }
  15.         }
  16.    
  17.  
  18.         /// <summary>
  19.         /// 取最小的一位数
  20.         /// </summary>
  21.         /// <param name="first"></param>
  22.         /// <param name="second"></param>
  23.         /// <param name="third"></param>
  24.         /// <returns></returns>
  25.         public int LowerOfThree(int first, int second, int third)
  26.         {
  27.             int min = first;
  28.             if (second < min)
  29.                 min = second;
  30.             if (third < min)
  31.                 min = third;
  32.             return min;
  33.         }
  34.  
  35.         public int Levenshtein_Distance(string str1, string str2)
  36.         {
  37.             int[,] Matrix;
  38.             int n=str1.Length;
  39.             int m=str2.Length;
  40.  
  41.             int temp = 0;
  42.             char ch1;
  43.             char ch2;
  44.             int i = 0;
  45.             int j = 0;
  46.             if (n ==0)
  47.             {
  48.                 return m;
  49.             }
  50.             if (m == 0)
  51.             {
  52.  
  53.                 return n;
  54.             }
  55.             Matrix=new int[n+1,m+1];
  56.  
  57.             for (i = 0; i <= n; i++)
  58.             {
  59.                 //初始化第一列
  60.                 Matrix[i,0] = i;
  61.             }
  62.  
  63.             for (j = 0; j <= m; j++)
  64.             {
  65.                 //初始化第一行
  66.                 Matrix[0, j] = j;
  67.             }
  68.  
  69.             for (i = 1; i <= n; i++)
  70.             {
  71.                 ch1 = str1[i-1];
  72.                 for (j = 1; j <= m; j++)
  73.                 {
  74.                     ch2 = str2[j-1];
  75.                     if (ch1.Equals(ch2))
  76.                     {
  77.                         temp = 0;
  78.                     }
  79.                     else
  80.                     {
  81.                         temp = 1;
  82.                     }
  83.                     Matrix[i,j] = LowerOfThree(Matrix[i - 1,j] + 1, Matrix[i,j - 1] + 1, Matrix[i - 1,j - 1] + temp);
  84.  
  85.  
  86.                 }
  87.             }
  88.  
  89.             for (i = 0; i <= n; i++)
  90.             {
  91.                 for (j = 0; j <= m; j++)
  92.                 {
  93.                     Console.Write(" {0} ", Matrix[i, j]);
  94.                 }
  95.                 Console.WriteLine("");
  96.             }
  97.             return Matrix[n, m];
  98.  
  99.         }
  100.  
  101.         /// <summary>
  102.         /// 计算字符串相似度
  103.         /// </summary>
  104.         /// <param name="str1"></param>
  105.         /// <param name="str2"></param>
  106.         /// <returns></returns>
  107.         public decimal LevenshteinDistancePercent(string str1,string str2)
  108.         {
  109.             int maxLenth = str1.Length > str2.Length ? str1.Length : str2.Length;
  110.             int val = Levenshtein_Distance(str1, str2);
  111.             return 1 - (decimal)val / maxLenth;
  112.         }
  113.     }
  114.  
  115.     class Program
  116.     {
  117.  
  118.  
  119.         static void Main(string[] args)
  120.         {
  121.             string str1 = "你好蒂蒂";
  122.             string str2="你好蒂芬";
  123.             Console.WriteLine("字符串1 {0}", str1);
  124.  
  125.             Console.WriteLine("字符串2 {0}", str2);
  126.  
  127.             Console.WriteLine("相似度 {0} %", LevenshteinDistance.Instance.LevenshteinDistancePercent(str1, str2)*100);
  128.             Console.ReadLine();
  129.         }
  130.     }
  131. //csharp/7289

Reply to "Cc implements string similarity comparison by editing distance algorithm"

Here you can reply to the paste above

captcha

https://burned.cc - Burn After Reading Website