Class: AlfonsoX::SpellChecker::Dictionary::DictionaryFinder

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

Overview

Finds the dictionary in this Gem

Constant Summary collapse

PATH =
"#{AlfonsoX::DICTIONARIES_PATH}/hunspell"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(language, path = nil) ⇒ DictionaryFinder



46
47
48
49
50
51
# File 'lib/alfonsox/spellchecker/dictionary/hunspell.rb', line 46

def initialize(language, path = nil)
  @language = language
  @path = path
  @aff_file_path = nil
  @dic_file_path = nil
end

Instance Attribute Details

#aff_file_pathObject (readonly)

Returns the value of attribute aff_file_path.



43
44
45
# File 'lib/alfonsox/spellchecker/dictionary/hunspell.rb', line 43

def aff_file_path
  @aff_file_path
end

#dic_file_pathObject (readonly)

Returns the value of attribute dic_file_path.



43
44
45
# File 'lib/alfonsox/spellchecker/dictionary/hunspell.rb', line 43

def dic_file_path
  @dic_file_path
end

Class Method Details

.find_from_language(language, path) ⇒ Array<String, nil>

Find language dictionary in a path



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/alfonsox/spellchecker/dictionary/hunspell.rb', line 66

def self.find_from_language(language, path)
  standard_language = language.split('_')[0]
  languages = [language, standard_language, "#{standard_language}_ANY}"]
  languages.each do |language_directory|
    next unless ::Dir.exist?("#{path}/#{language_directory}")
    languages.each do |language_file|
      file_common_path = "#{path}/#{language_directory}/#{language_file}"
      aff_file_path = "#{file_common_path}.aff"
      dic_file_path = "#{file_common_path}.dic"
      aff_file_exists = ::File.exist?(aff_file_path)
      dic_file_exists = ::File.exist?(dic_file_path)
      return aff_file_path, dic_file_path if aff_file_exists && dic_file_exists
    end
  end
  [nil, nil]
end

Instance Method Details

#findObject



53
54
55
56
57
58
59
60
# File 'lib/alfonsox/spellchecker/dictionary/hunspell.rb', line 53

def find
  paths = [@path, "#{AlfonsoX::DICTIONARIES_PATH}/hunspell"].compact
  paths.each do |path|
    @aff_file_path, @dic_file_path = DictionaryFinder.find_from_language(@language, path)
    return true if @aff_file_path && @dic_file_path
  end
  false
end