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.
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_algorithms ⇒ Array<Class>
Get the default algorithms.
117 118 119 |
# File 'lib/kotoshu/suggestions/generator.rb', line 117 def self.default_algorithms DEFAULT_ALGORITHMS.dup end |
Instance Attribute Details
#dictionary ⇒ Object (readonly)
Returns The dictionary (any dictionary backend).
37 38 39 |
# File 'lib/kotoshu/suggestions/generator.rb', line 37 def dictionary @dictionary end |
#strategy ⇒ Strategies::CompositeStrategy (readonly)
Returns The composite strategy.
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.
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.
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.
106 107 108 |
# File 'lib/kotoshu/suggestions/generator.rb', line 106 def incorrect?(word) !correct?(word) end |