Class: Kotoshu::Grammar::PatternMatchers::SentenceStartMatcher

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

Overview

Matcher for sentence-start capitalization rules.

English (and most Latin-script languages) capitalizes the first word of every sentence. This matcher walks the token stream tracking sentence boundaries and flags lowercase sentence starts.

A sentence starts:

- at the beginning of the stream, OR
- immediately after a token whose final character is a
sentence-ending punctuation mark (. ! ?).

The matcher only considers alphabetic tokens. Numbers, symbols, and whitespace don't reset the sentence-start flag.

Rule shape:

patterns:
- conditions:
    - type: sentence_start_check

Constant Summary collapse

SENTENCE_ENDERS =
%w[. ! ?].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) ⇒ Object



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

def match(tokens, rule)
  errors = []
  expecting_capital = true

  tokens.each do |token|
    word = token[:token]
    next if word.nil? || word.empty?

    if sentence_start_word?(word, expecting_capital)
      errors << build_error(token, rule)
    end

    # Any non-empty token resets the sentence-start expectation.
    # Only sentence-ending punctuation re-arms it (below).
    expecting_capital = false
    expecting_capital = true if ends_sentence?(word)
  end

  errors
end