Class: Kotoshu::Languages::Spanish::GrammarRules::InvertedPunctuationRule

Inherits:
Rule
  • Object
show all
Defined in:
lib/kotoshu/languages/es/language.rb

Overview

Rule: Inverted punctuation (¡, ¿)

Instance Attribute Summary

Attributes inherited from Rule

#description, #id, #name

Instance Method Summary collapse

Constructor Details

#initializeInvertedPunctuationRule

Returns a new instance of InvertedPunctuationRule.



265
266
267
# File 'lib/kotoshu/languages/es/language.rb', line 265

def initialize
  super('ES_INVERTED_PUNCTUATION', 'Inverted Punctuation', 'Spanish requires inverted punctuation marks (¡, ¿) at the start of exclamations/questions.')
end

Instance Method Details

#check(tokens) ⇒ Object



269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
# File 'lib/kotoshu/languages/es/language.rb', line 269

def check(tokens)
  errors = []
  tokens.each_with_index do |token, idx|
    word = token[:token]
    next if word.nil? || word.empty?

    # Check for standard ? or ! without corresponding inverted marks
    if word == '?' || word == '!'
      # Look backwards to see if there's an inverted mark
      found_inverted = false
      (0...idx).reverse_each do |j|
        check_token = tokens[j][:token]
        if (word == '?' && check_token == '¿') || (word == '!' && check_token == '¡')
          found_inverted = true
          break
        end
        # Stop checking if we hit another sentence-ending punctuation
        break if %w[. ? !].include?(check_token)
      end

      unless found_inverted
        errors << {
          rule_id: @id,
          position: token[:position],
          message: "Missing inverted punctuation mark: use '#{word == '?' ? '¿' : '¡'}' at the start",
          suggestion: word == '?' ? '¿...?' : '¡...!',
          context: word,
          suggestions: [word == '?' ? '¿...?' : '¡...!']
        }
      end
    end
  end
  errors
end