Class: AlfonsoX::SpellChecker::File

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

Overview

Each one of the source code files we want to spell-check

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file_path, dictionaries) ⇒ File

Initialize spellcheck on a file with some dictionaries

Parameters:



16
17
18
19
20
# File 'lib/alfonsox/spellchecker/file.rb', line 16

def initialize(file_path, dictionaries)
  @file_path = file_path
  @dictionaries = dictionaries
  @incorrect_words = []
end

Instance Attribute Details

#incorrect_wordsObject (readonly)

Returns the value of attribute incorrect_words.



10
11
12
# File 'lib/alfonsox/spellchecker/file.rb', line 10

def incorrect_words
  @incorrect_words
end

Class Method Details

.word_splitter(word_line) ⇒ Array<String>

Tokenize a line, i.e. split words of a line.

Parameters:

  • word_line (String)

    A line of a text.

Returns:

  • (Array<String>)

    Words of the word_line.



34
35
36
# File 'lib/alfonsox/spellchecker/file.rb', line 34

def self.word_splitter(word_line)
  word_line.split(/[_\-\s\d]+|(?<=[a-z])(?=[A-Z])|(?<=[A-Z])(?=[A-Z][a-z])|\W+/).map(&:downcase)
end

Instance Method Details

#spellcheckObject

Perform the spellcheck on the file



23
24
25
26
27
28
29
# File 'lib/alfonsox/spellchecker/file.rb', line 23

def spellcheck
  lines = ::File.open(@file_path).readlines
  lines.each_with_index do |line, line_index|
    check_line(line, line_index + 1)
  end
  @incorrect_words
end