Advertisement
Guest User

Levenshtein CLR

a guest
Aug 8th, 2017
242
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.33 KB | None | 0 0
  1. ///<summary>
  2. ///This will be the code file to hold any CLR funcitons that the apex team uses.
  3. ///</summmary>
  4.  
  5. /******Compiler Flags:******
  6. /fullpaths
  7. /platform:x64
  8. /nologo
  9. /target:library
  10. ****************************/
  11.  
  12. using System;
  13. using System.Data.SqlTypes;
  14.  
  15. public partial class UserDefinedFunctions
  16. {
  17. [Microsoft.SqlServer.Server.SqlFunction(IsDeterministic = true, IsPrecise = false)]
  18.     public static SqlInt32 Levenshtein(SqlString S1, SqlString S2)
  19.     {
  20.         if (S1.IsNull || S2.IsNull)
  21.             throw (new ArgumentNullException());
  22.  
  23.         int n = S1.Value.Length;
  24.         int m = S2.Value.Length;
  25.  
  26.         int[,] d = new int[n + 1, m + 1];
  27.         int cost = 0;
  28.  
  29.         if (n == 0)
  30.             return m;
  31.         if (m == 0)
  32.             return n;
  33.  
  34.         for (int i = 0; i <= n; i++)
  35.             d[i, 0] = i;
  36.  
  37.         for (int j = 0; j <= m; j++)
  38.             d[0, j] = j;
  39.  
  40.         for (int i = 1; i <= n; i++)
  41.         {
  42.             for (int j = 1; j <= m; j++)
  43.             {
  44.                 if (S1.Value[i-1] == S2.Value[j-1])
  45.                     cost = 0;
  46.                 else
  47.                     cost = 1;
  48.  
  49.                 d[i, j] = System.Math.Min(System.Math.Min(d[i - 1, j] + 1, d[i, j - 1] + 1), d[i - 1, j - 1] + cost);
  50.             }
  51.         }
  52.  
  53.         return d[n, m];
  54.     }
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement