class Forspell::Speller
Constants
- HUNSPELL_DIRS
- RUBY_DICT
Attributes
dictionary[R]
Public Class Methods
new(main_dictionary, *custom_dictionaries, suggestions_size: 0)
click to toggle source
# File lib/forspell/speller.rb, line 13 def initialize(main_dictionary, *custom_dictionaries, suggestions_size: 0) @suggestions_size = suggestions_size FFI::Hunspell.directories = HUNSPELL_DIRS << File.dirname(main_dictionary) @dictionary = FFI::Hunspell.dict(File.basename(main_dictionary)) [RUBY_DICT, *custom_dictionaries].flat_map { |path| File.read(path).split("\n") } .compact .map { |line| line.gsub(/\s*\#.*$/, '') } .reject(&:empty?) .map { |line| line.split(/\s*:\s*/, 2) } .each do |word, example| example ? @dictionary.add_with_affix(word, example) : @dictionary.add(word) end rescue ArgumentError puts "Unable to find dictionary #{main_dictionary}" exit(2) end
Public Instance Methods
correct?(word)
click to toggle source
# File lib/forspell/speller.rb, line 31 def correct?(word) parts = word.split('-') if parts.size == 1 alterations = [word] alterations << word.capitalize unless word.capitalize == word alterations << word.upcase unless word.upcase == word alterations.any?{ |w| dictionary.check?(w) } else dictionary.check?(word) || parts.all? { |part| correct?(part) } end end
suggest(word)
click to toggle source
# File lib/forspell/speller.rb, line 44 def suggest(word) @suggestions_size.positive? ? dictionary.suggest(word).first(@suggestions_size) - [word, word.capitalize] : [] end