Class: Kotoshu::Grammar::PatternMatchers::PossessiveContextMatcher

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

Overview

Matcher for there/their/they’re confusion rules.

This matcher detects when “there” is used where “their” (possessive) is intended.

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) ⇒ Array<Hash>

Match tokens against the there/their pattern.

Parameters:

  • tokens (Array<Hash>)

    Array of token hashes

  • rule (Rule)

    The rule being checked

Returns:

  • (Array<Hash>)

    Array of error hashes



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/kotoshu/grammar/pattern_matchers/possessive_context_matcher.rb', line 18

def match(tokens, rule)
  errors = []
  exceptions = rule.exceptions || {}

  location_indicators = exceptions['location_indicators'] || {}
  location_verbs = location_indicators['verbs'] || []
  possessive_nouns = location_indicators['possessive_nouns'] || []

  tokens.each_with_index do |token, idx|
    word = token[:token]&.downcase
    next unless word == 'there'

    next_token = tokens[idx + 1]
    next unless next_token

    next_word = next_token[:token]&.downcase

    # Skip if followed by verb (location/existence context)
    next if location_verbs.include?(next_word)

    uses_their = false

    # Check POS tags first
    next_pos = next_token[:pos_tag]
    if next_pos && ['NOUN', 'NOUN_PROPER', 'ADJ'].include?(next_pos)
      uses_their = true
    # Fallback to word list
    elsif possessive_nouns.include?(next_word)
      uses_their = true
    end

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