Class: Kotoshu::Spellchecker
- Inherits:
-
Object
- Object
- Kotoshu::Spellchecker
- Defined in:
- lib/kotoshu/spellchecker.rb,
lib/kotoshu/fluent_checker.rb,
lib/kotoshu/spellchecker/parallel_checker.rb
Overview
Main spellchecker class.
This is the primary facade for spell checking operations, providing methods to check words, text, and files.
Defined Under Namespace
Classes: FluentChecker, ParallelChecker
Instance Attribute Summary collapse
-
#config ⇒ Configuration
readonly
The configuration.
-
#generator ⇒ Suggestions::Generator
readonly
The suggestion generator.
-
#resource_bundle ⇒ ResourceBundle?
readonly
The resource bundle if provided.
Instance Method Summary collapse
-
#check(text) ⇒ Models::Result::DocumentResult
Check text for spelling errors.
-
#check_directory(path, pattern: "*.txt") ⇒ Array<Models::Result::DocumentResult>
Check a directory for spelling errors.
-
#check_file(path) ⇒ Models::Result::DocumentResult
Check a file for spelling errors.
-
#check_word(word) ⇒ Models::Result::WordResult
Check a word and return a result object.
-
#correct?(word) ⇒ Boolean
Check if a word is spelled correctly.
-
#dictionary ⇒ Dictionary::Base
Get the dictionary being used.
-
#incorrect?(word) ⇒ Boolean
Check if a word is misspelled.
-
#initialize(dictionary: nil, config: nil, resource_bundle: nil, **kwargs) ⇒ Spellchecker
constructor
Create a new spellchecker.
-
#reload_dictionary ⇒ self
Reload the dictionary.
-
#suggest(word, max_suggestions: nil) ⇒ Suggestions::SuggestionSet
Get spelling suggestions for a word.
-
#tokenize(text) ⇒ Array<Array>
Tokenize text into words.
Constructor Details
#initialize(dictionary: nil, config: nil, resource_bundle: nil, **kwargs) ⇒ Spellchecker
Create a new spellchecker.
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/kotoshu/spellchecker.rb', line 53 def initialize(dictionary: nil, config: nil, resource_bundle: nil, **kwargs) @resource_bundle = resource_bundle if resource_bundle dictionary ||= resource_bundle.dictionary kwargs[:language] = resource_bundle.language unless kwargs.key?(:language) end if config.is_a?(Configuration) @config = config else settings = kwargs.dup settings[:dictionary_path] = dictionary.path if dictionary @config = Configuration.new(settings) end @config.dictionary = dictionary if dictionary dict = @config.dictionary max_suggestions = @config.max_suggestions @generator = Suggestions::Generator.new( dict, max_suggestions: max_suggestions, algorithms: @config.suggestion_algorithms ) end |
Instance Attribute Details
#config ⇒ Configuration (readonly)
Returns The configuration.
24 25 26 |
# File 'lib/kotoshu/spellchecker.rb', line 24 def config @config end |
#generator ⇒ Suggestions::Generator (readonly)
Returns The suggestion generator.
21 22 23 |
# File 'lib/kotoshu/spellchecker.rb', line 21 def generator @generator end |
#resource_bundle ⇒ ResourceBundle? (readonly)
Returns The resource bundle if provided.
27 28 29 |
# File 'lib/kotoshu/spellchecker.rb', line 27 def resource_bundle @resource_bundle end |
Instance Method Details
#check(text) ⇒ Models::Result::DocumentResult
Check text for spelling errors.
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 |
# File 'lib/kotoshu/spellchecker.rb', line 154 def check(text) return Models::Result::DocumentResult.success if text.nil? || text.empty? words = tokenize(text) errors = [] position = 0 words.each do |word_data| word, pos = word_data result = check_word(word) if result.incorrect? errors << Models::Result::WordResult.new( word: word, correct: false, suggestions: result.suggestions, position: pos ) end position = pos end Models::Result::DocumentResult.new( file: nil, errors: errors, word_count: words.size ) end |
#check_directory(path, pattern: "*.txt") ⇒ Array<Models::Result::DocumentResult>
Check a directory for spelling errors.
215 216 217 218 219 220 |
# File 'lib/kotoshu/spellchecker.rb', line 215 def check_directory(path, pattern: "*.txt") raise DictionaryNotFoundError, path unless File.exist?(path) && File.directory?(path) files = Dir.glob(File.join(path, pattern)) files.map { |file| check_file(file) } end |
#check_file(path) ⇒ Models::Result::DocumentResult
Check a file for spelling errors.
192 193 194 195 196 197 198 199 200 201 202 203 204 |
# File 'lib/kotoshu/spellchecker.rb', line 192 def check_file(path) raise DictionaryNotFoundError, path unless File.exist?(path) text = File.read(path, encoding: @config.encoding) result = check(text) # Create a new result with the file path Models::Result::DocumentResult.new( file: path, errors: result.errors, word_count: result.word_count ) end |
#check_word(word) ⇒ Models::Result::WordResult
Check a word and return a result object.
131 132 133 134 135 136 137 138 139 140 141 142 143 |
# File 'lib/kotoshu/spellchecker.rb', line 131 def check_word(word) if word.nil? || word.empty? return Models::Result::WordResult.new(word: "", correct: false, suggestions: Suggestions::SuggestionSet.empty) end if correct?(word) Models::Result::WordResult.correct(word) else suggestions = suggest(word) Models::Result::WordResult.incorrect(word, suggestions: suggestions) end end |
#correct?(word) ⇒ Boolean
Check if a word is spelled correctly.
89 90 91 92 93 |
# File 'lib/kotoshu/spellchecker.rb', line 89 def correct?(word) return false if word.nil? || word.empty? @generator.correct?(word) end |
#dictionary ⇒ Dictionary::Base
Get the dictionary being used.
258 259 260 |
# File 'lib/kotoshu/spellchecker.rb', line 258 def dictionary @generator.dictionary end |
#incorrect?(word) ⇒ Boolean
Check if a word is misspelled.
99 100 101 |
# File 'lib/kotoshu/spellchecker.rb', line 99 def incorrect?(word) !correct?(word) end |
#reload_dictionary ⇒ self
Reload the dictionary.
265 266 267 268 269 270 271 272 273 274 275 276 |
# File 'lib/kotoshu/spellchecker.rb', line 265 def reload_dictionary @config.reset_dictionary dict = @config.dictionary @generator = Suggestions::Generator.new( dict, max_suggestions: @config.max_suggestions, algorithms: @config.suggestion_algorithms ) self end |
#suggest(word, max_suggestions: nil) ⇒ Suggestions::SuggestionSet
Get spelling suggestions for a word.
112 113 114 115 116 |
# File 'lib/kotoshu/spellchecker.rb', line 112 def suggest(word, max_suggestions: nil) return Suggestions::SuggestionSet.empty if word.nil? || word.empty? @generator.generate(word, max_suggestions: max_suggestions) end |
#tokenize(text) ⇒ Array<Array>
Tokenize text into words.
230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 |
# File 'lib/kotoshu/spellchecker.rb', line 230 def tokenize(text) return [] if text.nil? || text.empty? words = [] position = 0 word_buffer = String.new word_start = 0 text.each_char.with_index do |char, i| if word_char?(char) word_buffer << char word_start = i if word_buffer.length == 1 position = i elsif !word_buffer.empty? words << [word_buffer.dup.freeze, word_start] word_buffer.clear end end # Don't forget the last word words << [word_buffer.dup.freeze, word_start] unless word_buffer.empty? words end |