Class: DidYouMean::TreeSpellChecker
Relationships & Source Files | |
Inherits: | Object |
Defined in: | lib/did_you_mean/tree_spell_checker.rb |
Overview
spell checker for a dictionary that has a tree structure, see doc/tree_spell_checker_api.md
Class Method Summary
Instance Attribute Summary
- #augment readonly
- #dictionary readonly
- #dimensions readonly
- #separator readonly
Instance Method Summary
- #correct(input)
- #find_ideas(paths, leaf) private
- #find_leaves(path) private
- #find_suggestions(input, plausibles) private
- #ideas_to_paths(ideas, leaf, names, path) private
- #no_idea(input) private
- #parse_dimensions private
- #plausible_dimensions(input) private
- #possible_paths(states) private
Constructor Details
.new(dictionary:, separator: '/', augment: nil) ⇒ TreeSpellChecker
# File 'lib/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
Instance Attribute Details
#augment (readonly)
[ GitHub ]# File 'lib/did_you_mean/tree_spell_checker.rb', line 5
attr_reader :dictionary, :dimensions, :separator, :augment
#dictionary (readonly)
[ GitHub ]# File 'lib/did_you_mean/tree_spell_checker.rb', line 5
attr_reader :dictionary, :dimensions, :separator, :augment
#dimensions (readonly)
[ GitHub ]# File 'lib/did_you_mean/tree_spell_checker.rb', line 5
attr_reader :dictionary, :dimensions, :separator, :augment
#separator (readonly)
[ GitHub ]# File 'lib/did_you_mean/tree_spell_checker.rb', line 5
attr_reader :dictionary, :dimensions, :separator, :augment
Instance Method Details
#correct(input)
[ GitHub ]# File 'lib/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
#find_ideas(paths, leaf) (private)
[ GitHub ]# File 'lib/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) (private)
[ GitHub ]# File 'lib/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) (private)
[ GitHub ]# File 'lib/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) (private)
[ GitHub ]#no_idea(input) (private)
[ GitHub ]# File 'lib/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 (private)
[ GitHub ]# File 'lib/did_you_mean/tree_spell_checker.rb', line 24
def parse_dimensions ParseDimensions.new(dictionary, separator).call end
#plausible_dimensions(input) (private)
[ GitHub ]# File 'lib/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) (private)
[ GitHub ]# File 'lib/did_you_mean/tree_spell_checker.rb', line 62
def possible_paths(states) states.map do |state| state.join separator end end