Levenshtein Distance algorithm (edit Distance algorithm), levenshteindistance

Source: Internet
Author: User

Levenshtein Distance algorithm (edit Distance algorithm), levenshteindistance

Using System; using System. collections. generic;/** Author: bear child * Time: april 22, 2014 */namespace DataTool ool {// <summary> // similarity // the person of the bear child // April 22, 2014 /// </summary> public static class LevenshteinDistance {# region Levenshtein distance Algorithm (edit Distance algorithm) /// <summary> /// take the smallest of the three numbers. /// </summary> /// <param name = "first"> </param>/ // <param name = "second"> </param> /// <param name = "third"> </param> /// <returns> </ Returns> private static int LowerOfThree (int first, int second, int third) {int min = first; if (second <min) min = second; if (third <min) min = third; return min ;}/// <summary> /// according to Levenshtein Distance algorithm (edit Distance algorithm) calculate the similarity between two strings /// </summary> /// <param name = "text1"> </param> /// <param name = "text2"> </ param> // <returns> </returns> private static int Levenshtein_Distance (string text1, string te Xt2) {int [,] Matrix; int n = text1.Length; int m = text2.Length; int temp = 0; char temperature; char ch2; int I = 0; int j = 0; if (n = 0) {return m;} if (m = 0) {return n;} Matrix = new int [n + 1, m + 1]; for (I = 0; I <= n; I ++) {// initialize the first Matrix [I, 0] = I ;}for (j = 0; j <= m; j ++) {// initialize the first line of Matrix [0, j] = j;} for (I = 1; I <= n; I ++) {records = text1 [I-1]; for (j = 1; j <= m; j ++) {ch2 = te Xt2 [j-1]; if (ch1.Equals (ch2) {temp = 0;} else {temp = 1;} Matrix [I, j] = LowerOfThree (Matrix [I-1, j] + 1, Matrix [I, j-1] + 1, Matrix [I-1, j-1] + temp) ;}// for (I = 0; I <= n; I ++) // {// for (j = 0; j <= m; j ++) // {// Console. write ("{0}", Matrix [I, j]); //} // Console. writeLine (""); //} return Matrix [n, m];} // <summary> // according to Levenshtein Distance algorithm (edit Distance algorithm) calculate the similarity between two strings (Percentage) /// </Summary> /// <param name = "text1"> the first string </param> /// <param name = "text2"> the second string </param> // <returns> similarity (percentage) </returns> public static decimal LevenshteinDistancePercent (string text1, string text2) {if (string. isNullOrEmpty (text1) & string. isNullOrEmpty (text2) return 1; else if (string. isNullOrEmpty (text1) | string. isNullOrEmpty (text2) return 0; int maxLenth = text1.Length> text2.Lengt H? Text1.Length: text2.Length; int val = Levenshtein_Distance (text1, text2); return 1-(decimal) val/maxLenth ;}# endregion # region calculates the similarity (percentage) of the two strings) /// <summary> /// calculate the similarity (percentage) between two strings and compare the composition of each character. The similarity of the returned results is related to the character sequence, however, the sequence is not completely consistent. // </summary> /// <param name = "text1"> the first string </param> /// <param name = "text2 "> second string </param> // <returns> similarity (percentage) </returns> public static decimal SimilarByStringPercent (string text1, string text2) {if (string. isNullOrEmpty (text1) & string. isNullOrEmpty (text2) return 1; else if (string. isNullOrEmpty (text1) | string. isNullOrEmpty (text2) return 0; decimal returnValue = 0; int maxLength; int I, l; List <string> tb1 = new List <string> (); list <string> tb2 = new List <string> (); I = 0; l = 1; maxLength = text1.Length; if (text1.Length <text2.Length) maxLength = text2.Length; while (l <= text1.Length) {while (I <text1.Length-1) {if (I + l> text1.Length) break; tb1.Add (text1.Substring (I, l )); I ++;} I = 0; l ++;} I = 0; l = 1; while (l <= text2.Length) {while (I <text2.Length-1) {if (I + l> text2.Length) break; tb2.Add (text2.Substring (I, l); I ++;} I = 0; l ++ ;} foreach (string subStr in tb1) {decimal tempRe = 0; if (tb2.Contains (subStr) {tempRe = (decimal) subStr. length/maxLength; if (tempRe> returnValue) returnValue = tempRe; if (tempRe = 1) break ;}} return returnValue ;}# endregion }}

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.