| 1 |
# frozen_string_literal: true |
| 2 |
|
| 3 |
module DidYouMean |
| 4 |
# spell checker for a dictionary that has a tree |
| 5 |
# structure, see doc/tree_spell_checker_api.md |
| 6 |
class TreeSpellChecker |
| 7 |
attr_reader :dictionary, :separator, :augment |
| 8 |
|
| 9 |
def initialize(dictionary:, separator: '/', augment: nil) |
| 10 |
@dictionary = dictionary |
| 11 |
@separator = separator |
| 12 |
@augment = augment |
| 13 |
end |
| 14 |
|
| 15 |
def correct(input) |
| 16 |
plausibles = plausible_dimensions(input) |
| 17 |
return fall_back_to_normal_spell_check(input) if plausibles.empty? |
| 18 |
|
| 19 |
suggestions = find_suggestions(input, plausibles) |
| 20 |
return fall_back_to_normal_spell_check(input) if suggestions.empty? |
| 21 |
|
| 22 |
suggestions |
| 23 |
end |
| 24 |
|
| 25 |
def dictionary_without_leaves |
| 26 |
@dictionary_without_leaves ||= dictionary.map { |word| word.split(separator)[0..-2] }.uniq |
| 27 |
end |
| 28 |
|
| 29 |
def tree_depth |
| 30 |
@tree_depth ||= dictionary_without_leaves.max { |a, b| a.size <=> b.size }.size |
| 31 |
end |
| 32 |
|
| 33 |
def dimensions |
| 34 |
@dimensions ||= tree_depth.times.map do |index| |
| 35 |
dictionary_without_leaves.map { |element| element[index] }.compact.uniq |
| 36 |
end |
| 37 |
end |
| 38 |
|
| 39 |
def find_leaves(path) |
| 40 |
path_with_separator = "#{path}#{separator}" |
| 41 |
|
| 42 |
dictionary |
| 43 |
.select {|str| str.include?(path_with_separator) } |
| 44 |
.map {|str| str.gsub(path_with_separator, '') } |
| 45 |
end |
| 46 |
|
| 47 |
def plausible_dimensions(input) |
| 48 |
input.split(separator)[0..-2] |
| 49 |
.map |
| 50 |
.with_index { |element, index| correct_element(dimensions[index], element) if dimensions[index] } |
| 51 |
.compact |
| 52 |
end |
| 53 |
|
| 54 |
def possible_paths(states) |
| 55 |
states.map { |state| state.join(separator) } |
| 56 |
end |
| 57 |
|
| 58 |
private |
| 59 |
|
| 60 |
def find_suggestions(input, plausibles) |
| 61 |
states = plausibles[0].product(*plausibles[1..-1]) |
| 62 |
paths = possible_paths(states) |
| 63 |
leaf = input.split(separator).last |
| 64 |
|
| 65 |
find_ideas(paths, leaf) |
| 66 |
end |
| 67 |
|
| 68 |
def fall_back_to_normal_spell_check(input) |
| 69 |
return [] unless augment |
| 70 |
|
| 71 |
::DidYouMean::SpellChecker.new(dictionary: dictionary).correct(input) |
| 72 |
end |
| 73 |
|
| 74 |
def find_ideas(paths, leaf) |
| 75 |
paths.flat_map do |path| |
| 76 |
names = find_leaves(path) |
| 77 |
ideas = correct_element(names, leaf) |
| 78 |
|
| 79 |
ideas_to_paths(ideas, leaf, names, path) |
| 80 |
end.compact |
| 81 |
end |
| 82 |
|
| 83 |
def ideas_to_paths(ideas, leaf, names, path) |
| 84 |
if ideas.empty? |
| 85 |
nil |
| 86 |
elsif names.include?(leaf) |
| 87 |
["#{path}#{separator}#{leaf}"] |
| 88 |
else |
| 89 |
ideas.map {|str| "#{path}#{separator}#{str}" } |
| 90 |
end |
| 91 |
end |
| 92 |
|
| 93 |
def correct_element(names, element) |
| 94 |
return names if names.size == 1 |
| 95 |
|
| 96 |
str = normalize(element) |
| 97 |
|
| 98 |
return [str] if names.include?(str) |
| 99 |
|
| 100 |
::DidYouMean::SpellChecker.new(dictionary: names).correct(str) |
| 101 |
end |
| 102 |
|
| 103 |
def normalize(str) |
| 104 |
str.downcase! |
| 105 |
str.tr!('@', ' ') if str.include?('@') |
| 106 |
str |
| 107 |
end |
| 108 |
end |
| 109 |
end |