Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- [Microsoft.SqlServer.Server.SqlFunction]
- public static int? GetHammingDistance(SqlString s, SqlString t)
- {
- if (!s.IsNull && !t.IsNull)
- {
- if (s.ToString().Length != t.ToString().Length) return -1;
- int distance =
- s.ToString().ToCharArray()
- .Zip(t.ToString().ToCharArray(), (c1, c2) => new { c1, c2 })
- .Count(m => m.c1 != m.c2);
- return distance;
- }
- else return null;
- }
- [Microsoft.SqlServer.Server.SqlFunction]
- public static int? GetDamerauLevenshteinDistance(SqlString inS, SqlString inT)
- {
- if (!inS.IsNull && !inT.IsNull)
- {
- string s = inS.ToString();
- string t = inT.ToString();
- var bounds = new { Height = s.Length + 1, Width = t.Length + 1 };
- int[,] matrix = new int[bounds.Height, bounds.Width];
- for (int height = 0; height < bounds.Height; height++) { matrix[height, 0] = height; };
- for (int width = 0; width < bounds.Width; width++) { matrix[0, width] = width; };
- for (int height = 1; height < bounds.Height; height++)
- {
- for (int width = 1; width < bounds.Width; width++)
- {
- int cost = (s[height - 1] == t[width - 1]) ? 0 : 1;
- int insertion = matrix[height, width - 1] + 1;
- int deletion = matrix[height - 1, width] + 1;
- int substitution = matrix[height - 1, width - 1] + cost;
- int distance = Math.Min(insertion, Math.Min(deletion, substitution));
- if (height > 1 && width > 1 && s[height - 1] == t[width - 2] && s[height - 2] == t[width - 1])
- {
- distance = Math.Min(distance, matrix[height - 2, width - 2] + cost);
- }
- matrix[height, width] = distance;
- }
- }
- return matrix[bounds.Height - 1, bounds.Width - 1];
- }
- else return null;
- }
- public class Wynik
- {
- [SqlFunction(FillRowMethodName = "FillRow")]
- public static IEnumerable InitMethod(String logname)
- {
- return new EventLog(logname).Entries;
- }
- public static void FillRow(Object obj, out SqlDateTime timeWritten, out SqlChars message, out SqlChars category, out long instanceId)
- {
- EventLogEntry eventLogEntry = obj as EventLogEntry;
- timeWritten = new SqlDateTime(eventLogEntry.TimeWritten);
- message = new SqlChars(eventLogEntry.Message);
- category = new SqlChars(eventLogEntry.Category);
- instanceId = eventLogEntry.InstanceId;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement