Class: AlfonsoX::SpellChecker::Dictionary::Hunspell

Inherits:
Object
  • Object
show all
Defined in:
lib/alfonsox/spellchecker/dictionary/hunspell.rb

Overview

Hunspell dictionary loader

Constant Summary collapse

DEFAULT_PATH =

Default hunspell dictionary path

"#{AlfonsoX::DICTIONARIES_PATH}/hunspell"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(language, path = nil) ⇒ Hunspell

Construct a hunspell dictionary object for this package



17
18
19
20
21
# File 'lib/alfonsox/spellchecker/dictionary/hunspell.rb', line 17

def initialize(language, path = nil)
  @language = language
  @path = path || DEFAULT_PATH
  initialize_spellchecker(path)
end

Instance Attribute Details

#languageObject (readonly)

All attributes are readable



14
15
16
# File 'lib/alfonsox/spellchecker/dictionary/hunspell.rb', line 14

def language
  @language
end

#pathObject (readonly)

All attributes are readable



14
15
16
# File 'lib/alfonsox/spellchecker/dictionary/hunspell.rb', line 14

def path
  @path
end

Class Method Details

.from_config(yml_config) ⇒ Object

Load a hunspell dictionary from configuration



24
25
26
# File 'lib/alfonsox/spellchecker/dictionary/hunspell.rb', line 24

def self.from_config(yml_config)
  new(yml_config['language'], yml_config.fetch('path') { DEFAULT_PATH })
end

Instance Method Details

#initialize_spellchecker(path) ⇒ Object

Initialize spellchecker attribute



34
35
36
37
38
# File 'lib/alfonsox/spellchecker/dictionary/hunspell.rb', line 34

def initialize_spellchecker(path)
  dictionary_finder = DictionaryFinder.new(@language, path)
  raise "'#{@language}' language Hunspell dictionary not found" unless dictionary_finder.find
  @spellchecker = ::Hunspell.new(dictionary_finder.aff_file_path, dictionary_finder.dic_file_path)
end

#word_present?(word) ⇒ Boolean

Inform if a word is present in this dictionary.

Returns:

  • (Boolean)


29
30
31
# File 'lib/alfonsox/spellchecker/dictionary/hunspell.rb', line 29

def word_present?(word)
  @spellchecker.spellcheck(word)
end