Class: SpellChecker

Inherits:
Object
  • Object
show all
Defined in:
lib/spell_checker.rb

Instance Method Summary collapse

Constructor Details

#initialize(model:) ⇒ SpellChecker

Returns a new instance of SpellChecker.



2
3
4
5
6
# File 'lib/spell_checker.rb', line 2

def initialize(model:)
  raise "No model given, cannot do spell checking" if model == nil
  @letters = ('a'..'z').to_a.join
  @model = model     
end

Instance Method Details

#correct_on_text(text) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/spell_checker.rb', line 8

def correct_on_text(text)
  # ++
  # should notice that correction happend right on the origin text
  # ++ 
  correct = {}
  text.scan(/\w+/).each do |word|
     correct[word] =  correct_on_word(word)
  end

  correct.each_pair do |key, val|
    text.gsub!(key, val)
  end
  text
end

#correct_on_word(word) ⇒ Object



23
24
25
26
27
28
# File 'lib/spell_checker.rb', line 23

def correct_on_word(word)
  return word if word.upcase == word
  return word if word[0].upcase == word[0] 
  (known([word]) or known(edits1(word)) or known_edits2(word) or
    [word]).max {|a,b| @model.tf[a] <=> @model.tf[b] }
end