occurrence rule type counts how many times a specific pattern appears and enforces minimum or maximum thresholds. This is useful for enforcing style guidelines about frequency of certain elements.
How It Works
Theoccurrence rule searches for all instances of a token pattern and counts them. If the count exceeds max or falls below min, it triggers an alert.
Parameters
A regular expression pattern to search for and count.
The maximum number of times the token can appear. If the count exceeds this, an alert is triggered.
The minimum number of times the token must appear. If the count is below this, an alert is triggered.
Makes the pattern matching case-insensitive when set to
true.Examples
Character Count Limit
Limit heading length by counting characters:Sentence Length
Ensure sentences aren’t too long:Comma Count
Limit the number of commas per sentence for readability:Paragraph Length
Enforce minimum paragraph length:Minimum Word Count
Ensure documents aren’t too short:Use Cases
The
occurrence rule is ideal for:- Enforcing character limits on headings
- Limiting sentence complexity (word count)
- Ensuring minimum content length
- Counting specific punctuation marks
- Validating document structure requirements
Scope Considerations
The
occurrence rule is scope-dependent. The token count is calculated within each matched scope. For example, with scope: sentence, each sentence is evaluated independently.Technical Details
Internally, theoccurrence rule (internal/check/occurrence.go:52-114):
- Compiles the token pattern into a regex
- Finds all matches within the scoped text using
FindAllStringIndex - Counts the number of matches
- Compares the count against
maxorminthresholds - If violated, creates an alert at the first non-code match
- If
minis set and no matches are found, the alert is placed at line 1 - This makes the rule effectively document-scoped
core.IsCode() before using it as the alert location.
Zero Occurrence Handling
When a rule requires a minimum occurrence (min > 0) but finds zero matches, the alert is placed at the beginning of the document (line 1, column 1) since there’s no specific location to highlight.
Related Rule Types
- existence: Use when you only care about presence/absence, not frequency
- repetition: Use when you want to detect consecutive repeated words
- metric: Use for more complex calculations involving multiple variables