Class: Uniword::Spellcheck::SpellChecker

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

Overview

Main orchestrator for spell and grammar checking.

Responsibility: Extract text from a document, delegate to HunspellAdapter for spell checking and GrammarChecker for grammar issues, then aggregate results into a SpellcheckResult.

Examples:

Check a document

checker = SpellChecker.new(language: "en_US")
doc = Uniword::DocumentFactory.from_file("report.docx")
result = checker.check(doc)
puts result.to_json

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(language: "en_US", dictionary: nil, spell_adapter: nil) ⇒ SpellChecker

Initialize the spell checker.

Parameters:

  • language (String) (defaults to: "en_US")

    Dictionary language (default: “en_US”)

  • dictionary (String, nil) (defaults to: nil)

    Custom dictionary file path (reserved for future use)

  • spell_adapter (HunspellAdapter, nil) (defaults to: nil)

    Override adapter (useful for testing)



26
27
28
29
30
31
32
33
# File 'lib/uniword/spellcheck/spell_checker.rb', line 26

def initialize(language: "en_US", dictionary: nil,
               spell_adapter: nil)
  @language = language
  @dictionary = dictionary
  @spell_adapter = spell_adapter ||
    HunspellAdapter.new(language: language)
  @grammar_checker = GrammarChecker.new
end

Instance Attribute Details

#grammar_checkerObject (readonly)

Returns the value of attribute grammar_checker.



17
18
19
# File 'lib/uniword/spellcheck/spell_checker.rb', line 17

def grammar_checker
  @grammar_checker
end

#languageObject (readonly)

Returns the value of attribute language.



17
18
19
# File 'lib/uniword/spellcheck/spell_checker.rb', line 17

def language
  @language
end

#spell_adapterObject (readonly)

Returns the value of attribute spell_adapter.



17
18
19
# File 'lib/uniword/spellcheck/spell_checker.rb', line 17

def spell_adapter
  @spell_adapter
end

Instance Method Details

#check(document) ⇒ SpellcheckResult

Run spell and grammar checks on a document.

Parameters:

  • document (DocumentRoot)

    The document to check

Returns:



39
40
41
42
43
44
45
46
47
# File 'lib/uniword/spellcheck/spell_checker.rb', line 39

def check(document)
  text = extract_text(document)
  result = SpellcheckResult.new

  check_spelling(text, result)
  check_grammar(text, result)

  result
end