class DidYouMean::TreeSpellChecker
spell checker for a dictionary that has a tree structure, see doc/tree_spell_checker_api.md
Attributes
augment[R]
dictionary[R]
dimensions[R]
separator[R]
Public Class Methods
new(dictionary:, separator: '/', augment: nil)
click to toggle source
# File did_you_mean/tree_spell_checker.rb, line 7 def initialize(dictionary:, separator: '/', augment: nil) @dictionary = dictionary @separator = separator @augment = augment @dimensions = parse_dimensions end
Public Instance Methods
correct(input)
click to toggle source
# File did_you_mean/tree_spell_checker.rb, line 14 def correct(input) plausibles = plausible_dimensions input return no_idea(input) if plausibles.empty? suggestions = find_suggestions input, plausibles return no_idea(input) if suggestions.empty? suggestions end
Private Instance Methods
find_ideas(paths, leaf)
click to toggle source
# File did_you_mean/tree_spell_checker.rb, line 41 def find_ideas(paths, leaf) paths.map do |path| names = find_leaves(path) ideas = CorrectElement.new.call names, leaf ideas_to_paths ideas, leaf, names, path end end
find_leaves(path)
click to toggle source
# File did_you_mean/tree_spell_checker.rb, line 55 def find_leaves(path) dictionary.map do |str| next unless str.include? "#{path}#{separator}" str.gsub("#{path}#{separator}", '') end.compact end
find_suggestions(input, plausibles)
click to toggle source
# File did_you_mean/tree_spell_checker.rb, line 28 def find_suggestions(input, plausibles) states = plausibles[0].product(*plausibles[1..-1]) paths = possible_paths states leaf = input.split(separator).last ideas = find_ideas(paths, leaf) ideas.compact.flatten end
ideas_to_paths(ideas, leaf, names, path)
click to toggle source
# File did_you_mean/tree_spell_checker.rb, line 49 def ideas_to_paths(ideas, leaf, names, path) return nil if ideas.empty? return [path + separator + leaf] if names.include? leaf ideas.map { |str| path + separator + str } end
no_idea(input)
click to toggle source
# File did_you_mean/tree_spell_checker.rb, line 36 def no_idea(input) return [] unless augment ::DidYouMean::SpellChecker.new(dictionary: dictionary).correct(input) end
parse_dimensions()
click to toggle source
# File did_you_mean/tree_spell_checker.rb, line 24 def parse_dimensions ParseDimensions.new(dictionary, separator).call end
plausible_dimensions(input)
click to toggle source
# File did_you_mean/tree_spell_checker.rb, line 68 def plausible_dimensions(input) elements = input.split(separator)[0..-2] elements.each_with_index.map do |element, i| next if dimensions[i].nil? CorrectElement.new.call dimensions[i], element end.compact end
possible_paths(states)
click to toggle source
# File did_you_mean/tree_spell_checker.rb, line 62 def possible_paths(states) states.map do |state| state.join separator end end