Module: Spellchecker

Defined in:
lib/spellchecker.rb,
lib/spellchecker/utils.rb,
lib/spellchecker/version.rb,
lib/spellchecker/tokenizer.rb,
lib/spellchecker/detect_typo.rb,
lib/spellchecker/detect_ngram.rb,
lib/spellchecker/dictionaries.rb,
lib/spellchecker/tokenizer/list.rb,
lib/spellchecker/tokenizer/token.rb,
lib/spellchecker/detect_duplicate.rb,
lib/spellchecker/tokenizer/null_token.rb,
lib/spellchecker/dictionaries/ngram_list.rb,
lib/spellchecker/dictionaries/typos_list.rb,
lib/spellchecker/dictionaries/human_names.rb,
lib/spellchecker/dictionaries/us_toponyms.rb,
lib/spellchecker/dictionaries/company_names.rb,
lib/spellchecker/dictionaries/english_words.rb

Defined Under Namespace

Modules: DetectDuplicate, DetectNgram, DetectTypo, Dictionaries, MistakeTypes, Tokenizer, Utils Classes: Mistake

Constant Summary collapse

VERSION =
'0.1.5'

Class Method Summary collapse

Class Method Details

.apply_fixes(text, mistakes) ⇒ String

Parameters:

Returns:

  • (String)


60
61
62
63
64
65
# File 'lib/spellchecker.rb', line 60

def apply_fixes(text, mistakes)
  mistakes_hash = mistakes.map { |m| [m.text, m.correction] }.to_h
  regexp = Regexp.union(mistakes_hash.keys)

  text.gsub(regexp, mistakes_hash)
end

.check(text) ⇒ Array<Spellchecker::Mistake>

Parameters:

  • text (String)

Returns:



34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/spellchecker.rb', line 34

def check(text)
  tokens = Tokenizer.call(text)

  mistakes =
    tokens.each_with_object([]) do |token, acc|
      DetectDuplicate.call(token)&.then { |m| acc << m }
      DetectTypo.call(token)&.then { |m| acc << m }
      DetectNgram.call(token)&.then { |m| acc << m }
    end

  mistakes.each { |mistake| mistake.context = Utils.fetch_context(text, mistake.position) }

  mistakes
end

.correct(text) ⇒ String

Parameters:

  • text (String)

Returns:

  • (String)


51
52
53
54
55
# File 'lib/spellchecker.rb', line 51

def correct(text)
  mistakes = check(text)

  apply_fixes(text, mistakes)
end