Class: Kotoshu::Grammar::PatternMatchers::VowelSoundMatcher

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

Overview

Matcher for a/an article usage rules.

This matcher checks if β€œa” or β€œan” is used correctly before vowel and consonant sounds.

Constant Summary collapse

VOWEL_SOUNDS =
%w[a e i o u].freeze

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 a/an pattern.

Parameters:

  • tokens (Array<Hash>)

    Array of token hashes

  • rule (Rule)

    The rule being checked

Returns:

  • (Array<Hash>)

    Array of error hashes



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/kotoshu/grammar/pattern_matchers/vowel_sound_matcher.rb', line 20

def match(tokens, rule)
  errors = []
  tokens.each_cons(2) do |prev_token, current_token|
    prev_word = prev_token[:token]&.downcase
    next unless %w[a an].include?(prev_word)
    next unless prev_token[:pos_tag] == 'DET' || prev_token[:pos_tag].nil?

    next_word = current_token[:token]
    next if next_word.nil? || next_word.empty?

    expected = article_for(next_word, rule)
    if prev_word != expected
      errors << build_error(prev_token, current_token, expected, rule)
    end
  end
  errors
end