Class: Uniword::Spellcheck::GrammarChecker

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

Overview

Simple rule-based grammar checker.

Responsibility: Detect common grammar issues in text using pattern-based rules (no external dependencies).

Follows the Open/Closed Principle: new rules can be added by creating new check methods and registering them in #rules.

Each rule returns an array of hashes with :message, :position, and :context keys.

Examples:

Check a sentence

checker = GrammarChecker.new
issues = checker.check("This is  a test.")
# => [{ message: "Double space detected", position: 8, context: ... }]

Instance Method Summary collapse

Instance Method Details

#check(text) ⇒ Array<Hash>

Check text for grammar issues using all registered rules.

Parameters:

  • text (String)

    The text to check

Returns:

  • (Array<Hash>)

    Array of grammar issues



25
26
27
28
29
# File 'lib/uniword/spellcheck/grammar_checker.rb', line 25

def check(text)
  return [] if text.nil? || text.strip.empty?

  rules.flat_map { |rule| rule.call(text) }
end