Class: Kotoshu::Grammar::PatternMatchers::PossessiveContractionMatcher

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

Overview

Matcher for possessive-vs-contraction confusion rules.

Detects when a possessive form ("its", "your", "whose") is used where the corresponding contraction ("it's", "you're", "who's") is intended — or vice versa. The trigger is the following token's POS tag or word form.

Rule shape:

patterns:
- context:
    target_token: "its"
    trigger_when_followed_by:
      tags: [ADJ, ADV]
      words: [been, going, getting]
  conditions:
    - type: possessive_contraction_check

When the target token is followed by a token whose POS tag is in tags OR whose lowercased form is in words, emit an error with rule.suggestion as the replacement.

Examples:

"its cold"      → suggest "it's"  (cold is ADJ)
"your ready"    → suggest "you're"
"whose coming"  → suggest "who's"

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



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/kotoshu/grammar/pattern_matchers/possessive_contraction_matcher.rb', line 33

def match(tokens, rule)
  errors = []
  target = pattern_target_token
  triggers = pattern_triggers

  tokens.each_with_index do |token, idx|
    next unless matches_target?(token, target)

    next_token = tokens[idx + 1]
    next unless next_token
    next unless triggers_match?(next_token, triggers)

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