Class: Uniword::Spellcheck::SpellcheckResult

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

Overview

Holds spell and grammar check results.

Responsibility: Collect and report spellcheck findings. Single Responsibility - only manages result storage and output.

Examples:

Build and query results

result = SpellcheckResult.new
result.add_misspelling(word: "teh", position: 5,
  suggestions: ["the", "tea"])
result.add_grammar_issue(message: "Double space", position: 12)
result.clean?  #=> false

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeSpellcheckResult

Initialize an empty result set.



22
23
24
25
# File 'lib/uniword/spellcheck/spellcheck_result.rb', line 22

def initialize
  @misspellings = []
  @grammar_issues = []
end

Instance Attribute Details

#grammar_issuesObject (readonly)

Returns the value of attribute grammar_issues.



19
20
21
# File 'lib/uniword/spellcheck/spellcheck_result.rb', line 19

def grammar_issues
  @grammar_issues
end

#misspellingsObject (readonly)

Returns the value of attribute misspellings.



19
20
21
# File 'lib/uniword/spellcheck/spellcheck_result.rb', line 19

def misspellings
  @misspellings
end

Instance Method Details

#add_grammar_issue(message:, position:, context: nil) ⇒ void

This method returns an undefined value.

Add a grammar issue.

Parameters:

  • message (String)

    Description of the issue

  • position (Integer)

    Character position in document text

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

    Surrounding text for context



47
48
49
50
51
52
53
# File 'lib/uniword/spellcheck/spellcheck_result.rb', line 47

def add_grammar_issue(message:, position:, context: nil)
  @grammar_issues << {
    message: message,
    position: position,
    context: context,
  }
end

#add_misspelling(word:, position:, suggestions: []) ⇒ void

This method returns an undefined value.

Add a misspelling.

Parameters:

  • word (String)

    The misspelled word

  • position (Integer)

    Character position in document text

  • suggestions (Array<String>) (defaults to: [])

    Suggested corrections



33
34
35
36
37
38
39
# File 'lib/uniword/spellcheck/spellcheck_result.rb', line 33

def add_misspelling(word:, position:, suggestions: [])
  @misspellings << {
    word: word,
    position: position,
    suggestions: suggestions,
  }
end

#clean?Boolean

Whether no issues were found.

Returns:

  • (Boolean)

    true if no misspellings or grammar issues



58
59
60
# File 'lib/uniword/spellcheck/spellcheck_result.rb', line 58

def clean?
  @misspellings.empty? && @grammar_issues.empty?
end

#issue_countInteger

Total number of issues found.

Returns:

  • (Integer)

    Combined count



65
66
67
# File 'lib/uniword/spellcheck/spellcheck_result.rb', line 65

def issue_count
  @misspellings.size + @grammar_issues.size
end

#to_json(*_args) ⇒ String

Serialize results to JSON.

Returns:

  • (String)

    JSON representation



72
73
74
75
76
77
78
79
# File 'lib/uniword/spellcheck/spellcheck_result.rb', line 72

def to_json(*_args)
  JSON.pretty_generate(
    misspellings: misspellings,
    grammar_issues: grammar_issues,
    issue_count: issue_count,
    clean: clean?,
  )
end