Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Вставьте сюда финальное содержимое файла LevenshteinCalculator.cs
- using System;
- using System.Configuration;
- using System.Collections.Generic;
- using System.Linq;
- // Каждый документ — это список токенов. То есть List<string>.
- // Вместо этого будем использовать псевдоним DocumentTokens.
- // Это поможет избежать сложных конструкций:
- // вместо List<List<string» будет List<DocumentTokens>
- using DocumentTokens = System.Collections.Generic.List<string>;
- namespace Antiplagiarism
- {
- public class LevenshteinCalculator
- {
- public ComparisonResult Result(DocumentTokens first, DocumentTokens second)
- {
- // Код по аналогии из теста
- var opt = new double[first.Count + 1, second.Count + 1];
- opt[1, 1] = TokenDistanceCalculator.GetTokenDistance(first[0], second[0]);
- opt[0, 0] = 0;
- for (var i = 1; i <= first.Count; ++i) opt[i, 0] = i;
- for (var i = 1; i <= second.Count; ++i) opt[0, i] = i;
- for (var i = 1; i <= first.Count; ++i)
- for (var j = 1; j <= second.Count; ++j)
- {
- if (first[i - 1] == second[j - 1]) opt[i, j] = opt[i - 1, j - 1];
- else
- {
- 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])),
- 1 + opt[i, j - 1]);
- }
- }
- return new ComparisonResult(first, second, opt[first.Count, second.Count]);
- //{
- // new ComparisonResult(
- // documents[0],
- // documents[1],
- // TokenDistanceCalculator.GetTokenDistance(documents[0][0], documents[1][0]))};
- //}
- }
- public List<ComparisonResult> CompareDocumentsPairwise(List<DocumentTokens> documents)
- {
- var r = new List<ComparisonResult>();
- var count = documents.Count;
- for (var i = 0; i < count; i++)
- {
- for (var j = i + 1; j < count; j++)
- {
- if (i == j) continue;
- r.Add(Result(documents[i], documents[j]));
- }
- }
- return r.OrderBy(x =>
- {
- return x.Distance;
- })
- .ToList();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment