Advertisement
rubixcuber

Comparison

Jan 30th, 2021
34
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.35 KB | None | 0 0
  1. int ComputeLevenshteinDistance(string source, string target)
  2. {
  3. if ((source == null) || (target == null)) return 0;
  4. if ((source.Length == 0) || (target.Length == 0)) return 0;
  5. if (source == target) return source.Length;
  6.  
  7. int sourceWordCount = source.Length;
  8. int targetWordCount = target.Length;
  9.  
  10. // Step 1
  11. if (sourceWordCount == 0)
  12. return targetWordCount;
  13.  
  14. if (targetWordCount == 0)
  15. return sourceWordCount;
  16.  
  17. int[,] distance = new int[sourceWordCount + 1, targetWordCount + 1];
  18.  
  19. // Step 2
  20. for (int i = 0; i <= sourceWordCount; distance[i, 0] = i++) ;
  21. for (int j = 0; j <= targetWordCount; distance[0, j] = j++) ;
  22.  
  23. for (int i = 1; i <= sourceWordCount; i++)
  24. {
  25. for (int j = 1; j <= targetWordCount; j++)
  26. {
  27. // Step 3
  28. int cost = (target[j - 1] == source[i - 1]) ? 0 : 1;
  29.  
  30. // Step 4
  31. distance[i, j] = Math.Min(Math.Min(distance[i - 1, j] + 1, distance[i, j - 1] + 1), distance[i - 1, j - 1] + cost);
  32. }
  33. }
  34.  
  35. return distance[sourceWordCount, targetWordCount];
  36. }
  37.  
  38. double CalculateSimilarity(string source, string target)
  39. {
  40. if ((source == null) || (target == null)) return 0.0;
  41. if ((source.Length == 0) || (target.Length == 0)) return 0.0;
  42. if (source == target) return 1.0;
  43.  
  44. int stepsToSame = ComputeLevenshteinDistance(source, target);
  45. return (1.0 - ((double)stepsToSame / (double)Math.Max(source.Length, target.Length)));
  46. }
  47.  
  48. string FileToString(string path)
  49. {
  50. BinaryReader reader = new BinaryReader(File.Open(path, FileMode.Open));
  51. byte[] data = reader.ReadBytes((int)reader.BaseStream.Length);
  52. reader.Close();
  53. return System.Text.Encoding.UTF8.GetString(data, 0, data.Length);
  54. }
  55.  
  56. double CompareFiles(string path, string path2)
  57. {
  58. string data, data2;
  59.  
  60. data = FileToString(path);
  61. data2 = FileToString(path2);
  62.  
  63. return CalculateSimilarity(data, data2);
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement