Class: Kotoshu::Grammar::PatternMatchers::PhraseMatcher
- Inherits:
-
BaseMatcher
- Object
- BaseMatcher
- Kotoshu::Grammar::PatternMatchers::PhraseMatcher
- Defined in:
- lib/kotoshu/grammar/pattern_matchers/phrase_matcher.rb
Overview
Matcher for literal multi-word phrase confusions.
Catches phrases where every word is individually valid but the combination is a common error — typically phonetic confusions like "could of" (should be "could have"). The spelling checker passes these because each word is in the dictionary; only a phrase-level grammar check catches them.
The rule config declares the wrong phrase and its replacement:
conditions:
- type: phrase_check
wrong_phrase: "could of"
suggestion: "could have"
The matcher scans the token stream for consecutive tokens whose
downcased text matches wrong_phrase (split on whitespace)
and emits one error per match.
Instance Method Summary collapse
-
#match(tokens, rule) ⇒ Array<Hash>
Match tokens against the phrase-confusion pattern.
Methods inherited from BaseMatcher
Constructor Details
This class inherits a constructor from Kotoshu::Grammar::PatternMatchers::BaseMatcher
Instance Method Details
#match(tokens, rule) ⇒ Array<Hash>
Match tokens against the phrase-confusion pattern.
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/phrase_matcher.rb', line 30 def match(tokens, rule) wrong_phrase = phrase_condition&.dig('wrong_phrase') suggestion = phrase_condition&.dig('suggestion') return [] unless wrong_phrase && suggestion wrong_tokens = wrong_phrase.downcase.split return [] if wrong_tokens.empty? matches = [] tokens.each_with_index do |start_token, start_idx| next unless start_token[:token]&.downcase == wrong_tokens.first match_idx = find_match(tokens, start_idx, wrong_tokens) next unless match_idx matches << build_error(tokens, match_idx, wrong_tokens.length, wrong_phrase, suggestion, rule) end matches end |