Class: Kotoshu::Grammar::PatternMatchers::WordListMatcher

Inherits:
BaseMatcher
  • Object
show all
Defined in:
lib/kotoshu/grammar/pattern_matchers/word_list_matcher.rb

Overview

Matcher for word-list corrections.

Flags any token whose lowercase form is a key in the rule's corrections map. Useful for proper-noun capitalization (monday → Monday, english → English) where a single rule covers dozens of word pairs.

Rule shape:

patterns:
- conditions:
    - type: word_list_check
      corrections:
        monday: Monday
        tuesday: Tuesday
        english: English

The matcher only fires when the actual token text differs from the mapped correction (so "Monday" doesn't get flagged).

Instance Method Summary collapse

Methods inherited from BaseMatcher

#initialize

Constructor Details

This class inherits a constructor from Kotoshu::Grammar::PatternMatchers::BaseMatcher

Instance Method Details

#match(tokens, rule) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/kotoshu/grammar/pattern_matchers/word_list_matcher.rb', line 27

def match(tokens, rule)
  corrections = word_list_condition&.dig('corrections') || {}
  return [] if corrections.empty?

  errors = []
  tokens.each do |token|
    word = token[:token]
    next unless word

    correction = corrections[word.downcase]
    next unless correction
    next if word == correction # already correct

    errors << build_error(token, correction, rule)
  end
  errors
end