annstasi

Untitled

Jun 9th, 2021
260
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.61 KB | None | 0 0
  1. // Вставьте сюда финальное содержимое файла LevenshteinCalculator.cs
  2. using System;
  3. using System.Configuration;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6.  
  7. // Каждый документ — это список токенов. То есть List<string>.
  8. // Вместо этого будем использовать псевдоним DocumentTokens.
  9. // Это поможет избежать сложных конструкций:
  10. // вместо List<List<string» будет List<DocumentTokens>
  11. using DocumentTokens = System.Collections.Generic.List<string>;
  12. namespace Antiplagiarism
  13. {
  14. public class LevenshteinCalculator
  15. {
  16. public ComparisonResult Result(DocumentTokens first, DocumentTokens second)
  17. {
  18. // Код по аналогии из теста
  19. var opt = new double[first.Count + 1, second.Count + 1];
  20. opt[1, 1] = TokenDistanceCalculator.GetTokenDistance(first[0], second[0]);
  21. opt[0, 0] = 0;
  22.  
  23. for (var i = 1; i <= first.Count; ++i) opt[i, 0] = i;
  24. for (var i = 1; i <= second.Count; ++i) opt[0, i] = i;
  25.  
  26. for (var i = 1; i <= first.Count; ++i)
  27. for (var j = 1; j <= second.Count; ++j)
  28. {
  29. if (first[i - 1] == second[j - 1]) opt[i, j] = opt[i - 1, j - 1];
  30. else
  31. {
  32. opt[i, j] = Math.Min(Math.Min(1 + opt[i - 1, j], opt[i - 1, j - 1] + TokenDistanceCalculator.GetTokenDistance(first[i - 1], second[j - 1])),
  33. 1 + opt[i, j - 1]);
  34. }
  35. }
  36. return new ComparisonResult(first, second, opt[first.Count, second.Count]);
  37. //{
  38. // new ComparisonResult(
  39. // documents[0],
  40. // documents[1],
  41. // TokenDistanceCalculator.GetTokenDistance(documents[0][0], documents[1][0]))};
  42. //}
  43. }
  44. public List<ComparisonResult> CompareDocumentsPairwise(List<DocumentTokens> documents)
  45. {
  46. var r = new List<ComparisonResult>();
  47. var count = documents.Count;
  48. for (var i = 0; i < count; i++)
  49. {
  50. for (var j = i + 1; j < count; j++)
  51. {
  52. if (i == j) continue;
  53. r.Add(Result(documents[i], documents[j]));
  54. }
  55. }
  56. return r.OrderBy(x =>
  57. {
  58. return x.Distance;
  59. })
  60. .ToList();
  61. }
  62. }
  63. }
Advertisement
Add Comment
Please, Sign In to add comment