StringSimilarity by Joel Christner

<PackageReference Include="StringSimilarity" Version="1.0.2.1" />

.NET API 6,144 bytes

 StringSimilarity

public static class StringSimilarity
using System; using System.Collections.Generic; using System.Linq; namespace Similarity { public static class StringSimilarity { public static decimal Calculate(string str1, string str2) { if (string.IsNullOrEmpty(str1) && string.IsNullOrEmpty(str2)) return decimal.One; if (string.IsNullOrEmpty(str1) && !string.IsNullOrEmpty(str2)) return decimal.Zero; if (!string.IsNullOrEmpty(str1) && string.IsNullOrEmpty(str2)) return decimal.Zero; if (str1.Equals(str2)) return decimal.One; BaseString(str1); BaseString(str2); decimal d = decimal.One; if (str1.Length > str2.Length) d = decimal.Divide(str2.Length, str1.Length); if (str2.Length > str1.Length) d = decimal.Divide(str1.Length, str2.Length); char[] array = UniqueChars(str1); char[] array2 = UniqueChars(str2); int num = array.Length; int num2 = array2.Length; int num3 = 0; int num4 = 0; decimal d2 = default(decimal); if (num > num2) { num4 = num; for (int i = 0; i < array.Length; i++) { if (array2.Contains(array[i])) num3++; } } else { num4 = num2; for (int j = 0; j < array2.Length; j++) { if (array.Contains(array2[j])) num3++; } } if (num4 > 0) d2 = decimal.Divide(num3, num4); return d * d2; } private static string BaseString(string str) { if (string.IsNullOrEmpty(str)) return ""; string text = ""; for (int i = 0; i < str.Length; i++) { int num = str[i]; if ((num > 47 && num < 58) || (num > 64 && num < 91) || (num > 96 && num < 123)) text += Convert.ToString(str[i]).ToLower(); } return text; } private static int CharacterCount(string str) { if (string.IsNullOrEmpty(str)) return 0; int num = 0; foreach (int num2 in str) { if ((num2 > 47 && num2 < 58) || (num2 > 64 && num2 < 91) || (num2 > 96 && num2 < 123)) num++; } return num; } private static char[] UniqueChars(string str) { if (string.IsNullOrEmpty(str)) return new List<char>().ToArray(); return str.Distinct().ToArray(); } } }