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.



280
281
282
# File 'lib/kotoshu/languages/es/language.rb', line 280

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



284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
# File 'lib/kotoshu/languages/es/language.rb', line 284

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 ['?', '!'].include?(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