Class: Kotoshu::Suggestions::Generator

Inherits:
Object
  • Object
show all
Defined in:
lib/kotoshu/suggestions/generator.rb

Overview

Generator for spelling suggestions.

This class orchestrates multiple suggestion algorithms to generate comprehensive spelling suggestions.

Examples:

Using default algorithms

generator = Generator.new(dictionary)
suggestions = generator.generate("helo")

Using custom algorithms

custom_strategy = MyStrategy.new
generator = Generator.new(dictionary, algorithms: [custom_strategy])

Constant Summary collapse

DEFAULT_ALGORITHMS =

Default suggestion algorithms.

[
  Strategies::EditDistanceStrategy,
  Strategies::PhoneticStrategy,
  Strategies::KeyboardProximityStrategy,
  Strategies::NgramStrategy
].freeze

Class Attribute Summary collapse

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(dictionary, algorithms: nil, max_suggestions: 10, **config) ⇒ Generator

Create a new suggestion generator.

Parameters:

  • dictionary (Object)

    The dictionary instance

  • algorithms (Array<Class, Strategies::BaseStrategy>, nil) (defaults to: nil)

    Algorithm classes or instances

  • max_suggestions (Integer) (defaults to: 10)

    Maximum suggestions to return

  • config (Hash)

    Configuration options



48
49
50
51
52
53
54
# File 'lib/kotoshu/suggestions/generator.rb', line 48

def initialize(dictionary, algorithms: nil, max_suggestions: 10, **config)
  @dictionary = dictionary
  @max_suggestions = max_suggestions
  # Use default algorithms if none provided
  algorithms_to_use = algorithms || DEFAULT_ALGORITHMS
  @strategy = build_strategy(algorithms_to_use, config)
end

Class Attribute Details

.default_algorithmsArray<Class>

Get the default algorithms.

Examples:

Generator.default_algorithms

Returns:

  • (Array<Class>)

    Default algorithm classes



117
118
119
# File 'lib/kotoshu/suggestions/generator.rb', line 117

def self.default_algorithms
  DEFAULT_ALGORITHMS.dup
end

Instance Attribute Details

#dictionaryObject (readonly)

Returns The dictionary (any dictionary backend).

Returns:

  • (Object)

    The dictionary (any dictionary backend)



37
38
39
# File 'lib/kotoshu/suggestions/generator.rb', line 37

def dictionary
  @dictionary
end

#strategyStrategies::CompositeStrategy (readonly)

Returns The composite strategy.

Returns:



40
41
42
# File 'lib/kotoshu/suggestions/generator.rb', line 40

def strategy
  @strategy
end

Instance Method Details

#correct?(word) ⇒ Boolean

Check if a word is correct.

Examples:

generator.correct?("hello")  # => true
generator.correct?("helo")   # => false

Parameters:

  • word (String)

    The word to check

Returns:

  • (Boolean)

    True if the word is in the dictionary



96
97
98
99
100
# File 'lib/kotoshu/suggestions/generator.rb', line 96

def correct?(word)
  return false if word.nil? || word.empty?

  dictionary_lookup(word)
end

#generate(word, max_suggestions: nil) ⇒ SuggestionSet Also known as: suggest

Generate suggestions for a word.

Examples:

generator.generate("helo")
# => #<Kotoshu::Suggestions::SuggestionSet ...>

Parameters:

  • word (String)

    The misspelled word

  • max_suggestions (Integer) (defaults to: nil)

    Maximum suggestions (optional)

Returns:



65
66
67
68
69
70
71
72
73
74
75
# File 'lib/kotoshu/suggestions/generator.rb', line 65

def generate(word, max_suggestions: nil)
  return SuggestionSet.empty if word.nil? || word.empty?

  context = Context.new(
    word: word,
    dictionary: @dictionary,
    max_results: max_suggestions || @max_suggestions
  )

  @strategy.generate(context)
end

#incorrect?(word) ⇒ Boolean Also known as: misspelled?

Check if a word is incorrect.

Parameters:

  • word (String)

    The word to check

Returns:

  • (Boolean)

    True if the word is not in the dictionary



106
107
108
# File 'lib/kotoshu/suggestions/generator.rb', line 106

def incorrect?(word)
  !correct?(word)
end