Class: AlfonsoX::SpellChecker::Main

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

Overview

Main spellchecker class

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(paths, dictionaries) ⇒ Main

Construct a spellchecker object from the paths it must check and a dictionary



14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/alfonsox/spellchecker/main.rb', line 14

def initialize(paths, dictionaries)
  @paths = if paths.is_a?(String)
             [paths]
           else
             paths
           end
  @dictionaries = if dictionaries.is_a?(Array)
                    dictionaries
                  else
                    [dictionaries]
                  end
  @dictionaries = @dictionaries.concat([AlfonsoX::SpellChecker::Dictionary::Default.new])
end

Instance Attribute Details

#dictionariesObject (readonly)

Returns the value of attribute dictionaries.



11
12
13
# File 'lib/alfonsox/spellchecker/main.rb', line 11

def dictionaries
  @dictionaries
end

#pathsObject (readonly)

Returns the value of attribute paths.



11
12
13
# File 'lib/alfonsox/spellchecker/main.rb', line 11

def paths
  @paths
end

Class Method Details

.from_config(config_file_path) ⇒ AlfonsoX::SpellChecker::Dictionary::Hunspell, AlfonsoX::SpellChecker::Dictionary::Rubymine

Load from config

Parameters:

  • config_file_path (String)

    Config file path that will be loaded.

Returns:



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/alfonsox/spellchecker/main.rb', line 32

def self.from_config(config_file_path)
  config_file = YAML.load_file(config_file_path)
  dictionary_class_from_type = lambda do |config_dictionary_type|
    return AlfonsoX::SpellChecker::Dictionary::Hunspell if config_dictionary_type == 'hunspell'
    return AlfonsoX::SpellChecker::Dictionary::Rubymine if config_dictionary_type == 'rubymine'
    return AlfonsoX::SpellChecker::Dictionary::WordList if config_dictionary_type == 'word_list'
    return AlfonsoX::SpellChecker::Dictionary::WordListFile if config_dictionary_type == 'word_list_file'
    raise "Dictionary type #{config_dictionary_type} is not recognized"
  end

  dictionaries = config_file['Dictionaries'].map do |dictionary_config|
    _dictionary_local_name = dictionary_config[0]
    actual_dictionary_config = dictionary_config[1]
    dictionary_class = dictionary_class_from_type.call(actual_dictionary_config['type'])
    dictionary_class.from_config(actual_dictionary_config)
  end

  AlfonsoX::SpellChecker::Main.new(config_file['Paths'], dictionaries)
end

Instance Method Details

#check(applicable_files = nil) ⇒ Array<AlfonsoX::SpellChecker::Word>

Spellcheck some files.

Parameters:

  • applicable_files (Array<String>) (defaults to: nil)

    List of full file paths that will be spell-checked. Optional.

Returns:



70
71
72
73
74
75
76
77
78
79
# File 'lib/alfonsox/spellchecker/main.rb', line 70

def check(applicable_files = nil)
  return check_all unless applicable_files
  incorrect_words_by_file = {}
  applicable_files.each do |applicable_file_i|
    file_incorrect_words = check_file(applicable_file_i)
    next unless file_incorrect_words.length.positive?
    incorrect_words_by_file[applicable_file_i] = file_incorrect_words
  end
  incorrect_words_by_file
end

#check_allArray<AlfonsoX::SpellChecker::Word>

Spellcheck all the paths.

Returns:



54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/alfonsox/spellchecker/main.rb', line 54

def check_all
  incorrect_words_by_file = {}
  @paths.each do |path|
    rb_file_paths = Dir.glob(path).map { |expanded_path| ::File.realpath(expanded_path) }
    rb_file_paths.each do |rb_file|
      file_incorrect_words = check_file(rb_file)
      next unless file_incorrect_words.length.positive?
      incorrect_words_by_file[rb_file] = file_incorrect_words
    end
  end
  incorrect_words_by_file
end