Class: Kotoshu::Suggestions::Generator
- Inherits:
-
Object
- Object
- Kotoshu::Suggestions::Generator
- Defined in:
- lib/kotoshu/suggestions/generator.rb
Overview
Generator for spelling suggestions.
This class orchestrates multiple suggestion algorithms to generate comprehensive spelling suggestions.
Constant Summary collapse
- DEFAULT_ALGORITHMS =
Default suggestion algorithms.
[ Strategies::EditDistanceStrategy, Strategies::PhoneticStrategy, Strategies::KeyboardProximityStrategy, Strategies::NgramStrategy ].freeze
Class Attribute Summary collapse
-
.default_algorithms ⇒ Array<Class>
Get the default algorithms.
Instance Attribute Summary collapse
-
#dictionary ⇒ Object
readonly
The dictionary (any dictionary backend).
-
#strategy ⇒ Strategies::CompositeStrategy
readonly
The composite strategy.
Instance Method Summary collapse
-
#correct?(word) ⇒ Boolean
Check if a word is correct.
-
#generate(word, max_suggestions: nil) ⇒ SuggestionSet
(also: #suggest)
Generate suggestions for a word.
-
#incorrect?(word) ⇒ Boolean
(also: #misspelled?)
Check if a word is incorrect.
-
#initialize(dictionary, algorithms: nil, max_suggestions: 10, **config) ⇒ Generator
constructor
Create a new suggestion generator.
Constructor Details
#initialize(dictionary, algorithms: nil, max_suggestions: 10, **config) ⇒ Generator
Create a new suggestion generator.
38 39 40 41 42 43 44 |
# File 'lib/kotoshu/suggestions/generator.rb', line 38 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_algorithms ⇒ Array<Class>
Get the default algorithms.
107 108 109 |
# File 'lib/kotoshu/suggestions/generator.rb', line 107 def self.default_algorithms DEFAULT_ALGORITHMS.dup end |
Instance Attribute Details
#dictionary ⇒ Object (readonly)
Returns The dictionary (any dictionary backend).
27 28 29 |
# File 'lib/kotoshu/suggestions/generator.rb', line 27 def dictionary @dictionary end |
#strategy ⇒ Strategies::CompositeStrategy (readonly)
Returns The composite strategy.
30 31 32 |
# File 'lib/kotoshu/suggestions/generator.rb', line 30 def strategy @strategy end |
Instance Method Details
#correct?(word) ⇒ Boolean
Check if a word is correct.
86 87 88 89 90 |
# File 'lib/kotoshu/suggestions/generator.rb', line 86 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.
55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/kotoshu/suggestions/generator.rb', line 55 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.
96 97 98 |
# File 'lib/kotoshu/suggestions/generator.rb', line 96 def incorrect?(word) !correct?(word) end |