Class: DidYouMean::TreeSpellChecker

Inherits:
Object
  • Object
show all
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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(dictionary:, separator: '/', augment: nil) ⇒ TreeSpellChecker

Returns a new instance of TreeSpellChecker.



9
10
11
12
13
# File 'lib/did_you_mean/tree_spell_checker.rb', line 9

def initialize(dictionary:, separator: '/', augment: nil)
  @dictionary = dictionary
  @separator = separator
  @augment = augment
end

Instance Attribute Details

#augmentObject (readonly)

Returns the value of attribute augment.



7
8
9
# File 'lib/did_you_mean/tree_spell_checker.rb', line 7

def augment
  @augment
end

#dictionaryObject (readonly)

Returns the value of attribute dictionary.



7
8
9
# File 'lib/did_you_mean/tree_spell_checker.rb', line 7

def dictionary
  @dictionary
end

#separatorObject (readonly)

Returns the value of attribute separator.



7
8
9
# File 'lib/did_you_mean/tree_spell_checker.rb', line 7

def separator
  @separator
end

Instance Method Details

#correct(input) ⇒ Object



15
16
17
18
19
20
21
22
23
# File 'lib/did_you_mean/tree_spell_checker.rb', line 15

def correct(input)
  plausibles = plausible_dimensions(input)
  return fall_back_to_normal_spell_check(input) if plausibles.empty?

  suggestions = find_suggestions(input, plausibles)
  return fall_back_to_normal_spell_check(input) if suggestions.empty?

  suggestions
end

#dictionary_without_leavesObject



25
26
27
# File 'lib/did_you_mean/tree_spell_checker.rb', line 25

def dictionary_without_leaves
  @dictionary_without_leaves ||= dictionary.map { |word| word.split(separator)[0..-2] }.uniq
end

#dimensionsObject



33
34
35
36
37
# File 'lib/did_you_mean/tree_spell_checker.rb', line 33

def dimensions
  @dimensions ||= tree_depth.times.map do |index|
                    dictionary_without_leaves.map { |element| element[index] }.compact.uniq
                  end
end

#find_leaves(path) ⇒ Object



39
40
41
42
43
44
45
# File 'lib/did_you_mean/tree_spell_checker.rb', line 39

def find_leaves(path)
  path_with_separator = "#{path}#{separator}"

  dictionary
    .select {|str| str.include?(path_with_separator) }
    .map {|str| str.gsub(path_with_separator, '') }
end

#plausible_dimensions(input) ⇒ Object



47
48
49
50
51
52
# File 'lib/did_you_mean/tree_spell_checker.rb', line 47

def plausible_dimensions(input)
  input.split(separator)[0..-2]
    .map
    .with_index { |element, index| correct_element(dimensions[index], element) if dimensions[index] }
    .compact
end

#possible_paths(states) ⇒ Object



54
55
56
# File 'lib/did_you_mean/tree_spell_checker.rb', line 54

def possible_paths(states)
  states.map { |state| state.join(separator) }
end

#tree_depthObject



29
30
31
# File 'lib/did_you_mean/tree_spell_checker.rb', line 29

def tree_depth
  @tree_depth ||= dictionary_without_leaves.max { |a, b| a.size <=> b.size }.size
end