|
| 1 | +// Licensed to Elasticsearch B.V under one or more agreements. |
| 2 | +// Elasticsearch B.V licenses this file to you under the Apache 2.0 License. |
| 3 | +// See the LICENSE file in the project root for more information |
| 4 | + |
| 5 | +namespace Elastic.Markdown.Suggestions; |
| 6 | + |
| 7 | +public class Suggestion(IReadOnlySet<string> candidates, string input) |
| 8 | +{ |
| 9 | + private IReadOnlyCollection<string> GetSuggestions() => |
| 10 | + candidates |
| 11 | + .Select(source => (source, Distance: LevenshteinDistance(input, source))) |
| 12 | + .OrderBy(suggestion => suggestion.Distance) |
| 13 | + .Where(suggestion => suggestion.Distance <= 2) |
| 14 | + .Select(suggestion => suggestion.source) |
| 15 | + .Take(3) |
| 16 | + .ToList(); |
| 17 | + |
| 18 | + public string GetSuggestionQuestion() |
| 19 | + { |
| 20 | + var suggestions = GetSuggestions(); |
| 21 | + if (suggestions.Count == 0) |
| 22 | + return string.Empty; |
| 23 | + |
| 24 | + return "Did you mean " + string.Join(", ", suggestions.SkipLast(1).Select(s => $"\"{s}\"")) + (suggestions.Count > 1 ? " or " : "") + (suggestions.LastOrDefault() != null ? $"\"{suggestions.LastOrDefault()}\"" : "") + "?"; |
| 25 | + } |
| 26 | + |
| 27 | + private static int LevenshteinDistance(string source, string target) |
| 28 | + { |
| 29 | + if (string.IsNullOrEmpty(target)) |
| 30 | + return int.MaxValue; |
| 31 | + |
| 32 | + var sourceLength = source.Length; |
| 33 | + var targetLength = target.Length; |
| 34 | + |
| 35 | + if (sourceLength == 0) |
| 36 | + return targetLength; |
| 37 | + |
| 38 | + if (targetLength == 0) |
| 39 | + return sourceLength; |
| 40 | + |
| 41 | + var distance = new int[sourceLength + 1, targetLength + 1]; |
| 42 | + |
| 43 | + for (var i = 0; i <= sourceLength; i++) |
| 44 | + distance[i, 0] = i; |
| 45 | + |
| 46 | + for (var j = 0; j <= targetLength; j++) |
| 47 | + distance[0, j] = j; |
| 48 | + |
| 49 | + for (var i = 1; i <= sourceLength; i++) |
| 50 | + { |
| 51 | + for (var j = 1; j <= targetLength; j++) |
| 52 | + { |
| 53 | + var cost = (source[i - 1] == target[j - 1]) ? 0 : 1; |
| 54 | + |
| 55 | + distance[i, j] = Math.Min( |
| 56 | + Math.Min( |
| 57 | + distance[i - 1, j] + 1, |
| 58 | + distance[i, j - 1] + 1), |
| 59 | + distance[i - 1, j - 1] + cost); |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + return distance[sourceLength, targetLength]; |
| 64 | + } |
| 65 | +} |
0 commit comments