Overview
Vale computes various metrics about your text files, including:- Word and sentence counts
- Readability scores (Flesch, Gunning Fog, etc.)
- Document structure (headings, paragraphs, lists)
- Complexity measures (syllables, polysyllabic words)
ls-metrics command or used to create custom readability rules.
Viewing Metrics
Use thels-metrics command to calculate metrics for any file:
Example Output
All metrics are computed from the document’s summary content, which excludes code blocks and other non-prose elements.
Available Metrics
- Text Counts
- Complexity
- Structure
Basic counting metrics:
These are the foundation for most readability formulas.
| Metric | Description |
|---|---|
characters | Total character count |
words | Total word count |
sentences | Number of sentences |
paragraphs | Number of paragraphs |
Metric Computation
Vale computes metrics using theComputeMetrics() method (see internal/core/file.go:152-179):
Metrics are computed only from the Summary buffer, which contains prose content. Code blocks, comments, and other non-prose elements are excluded.
The ls-metrics Command
Thels-metrics command provides programmatic access to metrics (see cmd/vale/command.go:141-169):
Usage Examples
- Single File
- CI/CD Integration
- Multiple Files
Calculate metrics for one document:Output:
Custom Metric Rules
Vale’smetric rule type lets you create custom readability rules using any computed metrics.
Creating a Metric Rule
Create a rule fileMyStyle/Readability.yml:
How Metric Rules Work
Themetric extension evaluates mathematical expressions (see internal/check/metric.go):
Formula Evaluation
Formula Evaluation
Vale uses the Tengo scripting language to evaluate formulas:All computed metrics are available as variables in your formula.
Condition Checking
Condition Checking
The condition is evaluated as a boolean expression:If the condition is true, Vale creates an alert showing the computed value.
Available Variables
Your formulas can use any of these metrics (seeinternal/check/metric.go:16-19):
words, sentences, syllables, paragraphs, etc.
Example Metric Rules
- Flesch Reading Ease
- Gunning Fog Index
- Average Sentence Length
- Document Length
Advanced Formulas
Metric rules support the full Tengo math library:math.sqrt(),math.pow(),math.abs()math.ceil(),math.floor(),math.round()math.log(),math.log10(),math.exp()- Standard operators:
+,-,*,/,%
Scope Considerations
Metric rules always usescope: summary:
Troubleshooting
Empty Metrics
Empty Metrics
If
ls-metrics returns {}, the file may contain only code blocks or comments.Check that your file has prose content that Vale can analyze.Division by Zero
Division by Zero
If a metric is missing (e.g., no sentences), Vale sets it to 0.0:Be careful with division in formulas. Add guards:
Formula Errors
Formula Errors
Vale reports the exact error if a formula fails:Check variable names and syntax.
Best Practices
Test Formulas
Use
ls-metrics to verify metrics exist before using them in formulas.Set Thresholds Carefully
Tune conditions based on your content type. Technical docs may need different targets than marketing copy.
Provide Context
Include links to explain readability scores in your rule messages.
Use Suggestions
Start with
level: suggestion for readability rules. They’re guidelines, not hard requirements.Related Topics
- Metric Rule Type - Full reference for metric rules
- Readability Rules - Pre-built readability checks
ls-metricsCommand - Command reference