Class: Uniword::Quality::ParagraphLengthRule

Inherits:
QualityRule
  • Object
show all
Defined in:
lib/uniword/quality/rules/paragraph_length_rule.rb

Overview

Checks paragraph word count for readability.

Responsibility: Validate paragraph length. Single Responsibility - only checks paragraph word count.

Validates:

  • Paragraphs don’t exceed maximum word count

  • Warns when paragraphs approach warning threshold

  • Promotes readability and scannability

Examples:

Configuration

paragraph_length:
  enabled: true
  max_words: 500
  warning_words: 400

Instance Attribute Summary

Attributes inherited from QualityRule

#config, #enabled

Instance Method Summary collapse

Methods inherited from QualityRule

#enabled?, #name

Constructor Details

#initialize(config = {}) ⇒ ParagraphLengthRule

Returns a new instance of ParagraphLengthRule.



21
22
23
24
25
# File 'lib/uniword/quality/rules/paragraph_length_rule.rb', line 21

def initialize(config = {})
  super
  @max_words = @config[:max_words] || 500
  @warning_words = @config[:warning_words] || 400
end

Instance Method Details

#check(document) ⇒ Array<QualityViolation>

Check document for paragraph length violations

Parameters:

  • document (Document)

    The document to check

Returns:



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/uniword/quality/rules/paragraph_length_rule.rb', line 31

def check(document)
  violations = []

  document.paragraphs.each_with_index do |para, index|
    word_count = count_words(para)

    # Skip empty paragraphs
    next if word_count.zero?

    if word_count > @max_words
      violations << create_violation(
        severity: :error,
        message: "Paragraph has #{word_count} words (maximum: #{@max_words}). " \
                 "Consider breaking into smaller paragraphs for readability.",
        location: "Paragraph #{index + 1}",
        element: para,
      )
    elsif word_count > @warning_words
      violations << create_violation(
        severity: :warning,
        message: "Paragraph has #{word_count} words (warning threshold: #{@warning_words}). " \
                 "Consider reviewing for clarity.",
        location: "Paragraph #{index + 1}",
        element: para,
      )
    end
  end

  violations
end