Class: Spellchecker

Inherits:
Object
  • Object
show all
Defined in:
lib/middleman-spellcheck/spellchecker.rb

Constant Summary collapse

@@aspell_path =
"aspell"

Class Method Summary collapse

Class Method Details

.aspell_pathObject



8
9
10
# File 'lib/middleman-spellcheck/spellchecker.rb', line 8

def self.aspell_path
  @@aspell_path
end

.aspell_path=(path) ⇒ Object



4
5
6
# File 'lib/middleman-spellcheck/spellchecker.rb', line 4

def self.aspell_path=(path)
  @@aspell_path = path
end

.check(text, lang = 'en') ⇒ Object



23
24
25
26
27
28
29
30
# File 'lib/middleman-spellcheck/spellchecker.rb', line 23

def self.check(text, lang='en')
  words   = text.split(/[^A-Za-z’']+/)
  results = query(words.join(" "), lang).map do |query_result|
    correct?(query_result)
  end

  words.zip(results).map {|word, correctness| { word: word, correct: correctness } }
end

.correct?(result_string) ⇒ Boolean

Returns:

  • (Boolean)


19
20
21
# File 'lib/middleman-spellcheck/spellchecker.rb', line 19

def self.correct?(result_string)
  result_string == "*"
end

.query(text, lang = 'en') ⇒ Object



12
13
14
15
16
17
# File 'lib/middleman-spellcheck/spellchecker.rb', line 12

def self.query(text, lang='en')
  result = `echo "#{text}" | #{@@aspell_path} -a -l #{lang}`
  raise 'Aspell command not found' unless result
  new_result = result.split("\n")
  new_result[1..-1] || []
end