Class: Kotoshu::Components::SpellChecker Abstract

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

Overview

This class is abstract.

Subclasses must implement #check and #suggest

Base class for spell checkers.

Spell checkers validate words and provide suggestions for misspelled words. Different languages use different spell checking strategies:

  • Latin scripts: Dictionary lookup (Hunspell, Morfologik)

  • CJK: Confusion rule checking (no dictionary)

  • RTL: Dictionary lookup with bidirectional text handling

Examples:

Checking a word

checker = EnglishSpellChecker.new(aff_path: "en_US.aff", dic_path: "en_US.dic")
result = checker.check("hello")
# => { found: true, stem: "hello", flags: [] }

Getting suggestions

result = checker.check("helo")
# => { found: false, stem: nil, flags: [] }
suggestions = checker.suggest("helo")
# => [
#      { word: "hello", distance: 1, score: 0.9 },
#      { word: "help", distance: 2, score: 0.7 }
#    ]

Instance Method Summary collapse

Instance Method Details

#check(word) ⇒ Hash

This method is abstract.

Subclasses must implement

Check if a word is spelled correctly.

Returns a hash with:

  • :found (Boolean) - true if word is in dictionary

  • :stem (String, nil) - The stem/lemma if found

  • :flags (Array<String>) - Morphological flags

Parameters:

  • word (String)

    The word to check

Returns:

  • (Hash)

    Result with :found, :stem, :flags

Raises:

  • (NotImplementedError)

    if not implemented by subclass



40
41
42
# File 'lib/kotoshu/components/spell_checker.rb', line 40

def check(word)
  raise NotImplementedError, "#{self.class} must implement #check"
end

#correct?(word) ⇒ Boolean

Check if a word is spelled correctly.

Convenience method that returns a boolean.

Parameters:

  • word (String)

    The word to check

Returns:

  • (Boolean)

    true if word is correct



68
69
70
# File 'lib/kotoshu/components/spell_checker.rb', line 68

def correct?(word)
  check(word)[:found]
end

#suggest(word, max_suggestions: 10) ⇒ Array<Hash>

This method is abstract.

Subclasses must implement

Get spelling suggestions for a misspelled word.

Returns an array of suggestion hashes with:

  • :word (String) - The suggested word

  • :distance (Integer) - Edit distance from original word

  • :score (Float) - Confidence score (0-1, higher is better)

Suggestions are sorted by relevance (highest score first).

Parameters:

  • word (String)

    The misspelled word

  • max_suggestions (Integer) (defaults to: 10)

    Maximum number of suggestions to return

Returns:

  • (Array<Hash>)

    Array of suggestion hashes

Raises:

  • (NotImplementedError)

    if not implemented by subclass



58
59
60
# File 'lib/kotoshu/components/spell_checker.rb', line 58

def suggest(word, max_suggestions: 10)
  raise NotImplementedError, "#{self.class} must implement #suggest"
end