Class: Kotoshu::Languages::German::GrammarRules::NounCapitalizationRule

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

Overview

Rule: German noun capitalization.

Constant Summary collapse

NOUN_SUFFIXES =

Common German noun suffixes

%w[ung heit keit schaft tion ismus tum ling ner eur
able ibil ig igkeit lich sam los losung].freeze

Instance Attribute Summary

Attributes inherited from Rule

#description, #id, #name

Instance Method Summary collapse

Methods inherited from Rule

#applies?

Constructor Details

#initializeNounCapitalizationRule

Returns a new instance of NounCapitalizationRule.



346
347
348
# File 'lib/kotoshu/languages/de/language.rb', line 346

def initialize
  super('DE_NOUN_CAPITALIZATION', 'Noun Capitalization', 'German nouns must be capitalized.')
end

Instance Method Details

#check(tokens) ⇒ Object



350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
# File 'lib/kotoshu/languages/de/language.rb', line 350

def check(tokens)
  errors = []
  tokens.each_with_index do |token, idx|
    word = token[:token]
    next if word.nil? || word.empty?
    next if word == word.capitalize # Already capitalized
    next if word.length < 3 # Too short
    next unless word.match?(/^[a-zäöüß]+$/i) # Only letters

    # Check if it looks like a noun (has noun suffix or is in noun position)
    if word.end_with?(*NOUN_SUFFIXES)
      errors << {
        rule_id: @id,
        position: token[:position],
        message: "German nouns must be capitalized: '#{word}'",
        suggestion: word.capitalize,
        context: word,
        suggestions: [word.capitalize]
      }
    end

    # Check position: after determiners often indicates a noun
    if idx > 0
      prev_token = tokens[idx - 1][:token]&.downcase
      if %w[der die das ein eine einem einen einer eines].include?(prev_token)
        if word == word.downcase && word.length > 2
          errors << {
            rule_id: @id,
            position: token[:position],
            message: "German nouns must be capitalized after articles: '#{word}'",
            suggestion: word.capitalize,
            context: "#{prev_token} #{word}",
            suggestions: [word.capitalize]
          }
        end
      end
    end
  end
  errors
end