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
31
32
33
34
35
# File 'lib/middleman-spellcheck/spellchecker.rb', line 23

def self.check(text, lang='en')
  # join then re-split the word list to get a consistent word count,
  # because sometimes there's a "" (blank) word in the array that gets lost,
  # which makes the maps not equal, leading to an off by one type issue, where
  # the reported mispelled word is actually a correct word.
  # see: https://github.com/minivan/middleman-spellcheck/issues/7
  words   = text.split(/[^A-Za-z’']+/).join(" ")
  results = query(words, lang).map do |query_result|
    correct?(query_result)
  end

  words.split(" ").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