English | 日本語
A dictionary-based textlint rule that matches phrases using Intl.Segmenter word boundaries.
It supports language-specific matching, report-only rules, automatic fixes, and suggestions.
Embedded dictionary tests let you check both intended matches and similar phrases that should stay unchanged.
version: 1
language: ja
rules:
- patterns: 猫である。
expected: 猫です。
specs:
- from: 吾輩は猫である。
to: 吾輩は猫です。
- from: 吾輩は子猫である。
errors: 0
- patterns: を適応
message: 文脈に合う言葉を選んでください。
suggestions: [を適用, に対応]
- patterns: [絶対, 絶対に]
message: 断定の根拠を確認してください。
- language: en
patterns: comp
expected: componentThe design started with this comment on PRH Issue #133. See the dictionary design notes and js-primer migration and comparison for the reasoning and validation results (in Japanese).
Requires Node.js 22 or later with full ICU and textlint 15.3.0 or later. Install textlint and this rule in the project where you want to use them:
npm install --save-dev textlint textlint-rule-wordingThis package has not been published to npm yet. The command above will work after publication. To try the current code locally, see Development and verification at the end of this README.
Save the dictionary above as wording.yml, then reference it in .textlintrc.json.
Relative dictionary paths resolve from the configuration file's directory. Absolute paths and ~/ paths also work.
{
"rules": {
"wording": {
"rulePaths": ["./wording.yml"]
}
}
}npx textlint document.md
npx textlint --fix document.mdDictionaries can be YAML or JSON.
You can also pass rule objects through rules, or YAML/JSON strings through ruleContents.
Rules load in this order: files, dictionary strings, then inline rules. Explicit rule IDs must be unique.
| Format | Meaning |
|---|---|
patterns: 猫である。 |
A literal phrase whose start and end are word boundaries |
patterns: [サーバ, サーバー] |
Any of the alternatives |
patterns: { sequence: "猫|で|ある|。" } |
A sequence whose boundaries exactly match those from Intl.Segmenter |
patterns: { regexp: "ECMAScript([0-9]+)" } |
A regular expression, without word boundary constraints by default |
patterns: { literal: "猫", boundary: none } |
Explicit substring matching, including matches within a word |
Use plain strings for ordinary phrases, including English phrases such as a lot of.
Characters such as |, /, and . remain literal. A string like PRH's /pattern/i is not treated as a regular expression.
A patterns array lists alternatives; use sequence for a consecutive series of segments.
The format fixtures show each format alongside input, fixed output, and expected diagnostics.
Literal phrases require boundaries only at their start and end. Their internal segmentation is not fixed because context can change where segments break. Within one rule, overlapping matches are processed from left to right, preferring the longest match at the same position.
After installation, use the textlint-rule-wording command to inspect segmentation:
npx textlint-rule-wording segment '吾輩は猫である。' --language ja
# ["吾輩", "は", "猫", "で", "ある", "。"]
npx textlint-rule-wording segment 'This comp works.' --language en --detail--detail returns each segment, its UTF-16 start/end offsets, and isWordLike as JSON.
An explicit sequence can be a string or an array. The default separator is |; use separator to change it.
patterns:
sequence: "する|こと|が|可能|です"
message: 「$&」を簡潔な表現にできないか確認してください。Use an array when part of the sequence needs a regular expression or a capture:
patterns:
sequence:
- する
- こと
- が
- 可能
- regexp: だ|だが|です
capture: ending
message: 「$&」を簡潔な表現にできないか確認してください。Array elements can be strings, { regexp: "…", flags: "i", capture: ending }, or { any: true, capture: ending }.
A token regexp must match one entire segment. any matches any single segment, including whitespace or punctuation.
English spaces are segments too: use sequence: [a, " ", lot] for an explicit sequence.
Refer to captured text in expected, message, and suggestions using $<ending>, or $1, $2, etc. in capture order.
patterns:
regexp: ECMAScript([0-9]+)
expected: ECMAScript $1Write JavaScript regexp source without surrounding / delimiters.
Use flags for options such as i, m, and s. The engine adds u unless u or v is present.
Do not specify g or y; the rule searches for all matches itself. Zero-width matches cause an error.
Set boundary: word to require Intl.Segmenter boundaries around regexp matches.
Regexps operate on the original text. ignoreCase, normalize, and whitespace affect literal matching; for regexps, use flags such as i and patterns such as \s instead.
Capture numbering is independent for each alternative. Replacements support $1–$99, $<name>, $&, $$, and JavaScript's prefix/suffix substitutions.
Use except for exclusions. For example, patterns: { regexp: "事" } combined with except: { regexp: "記事" } excludes the 事 inside 記事.
This dictionary demonstrates alternatives and exceptions, both sequence forms, a custom separator, token captures, regular expressions, explicit substring matching, English spacing and case, canonical spelling, report-only rules, suggestions, and deletion.
Each rule includes a specs example. These examples are intentionally independent; use the rules relevant to your writing style.
version: 1
language: ja
rules:
- id: alternatives-with-exceptions
patterns: [フィルタ, フィルター]
expected: フィルター
except: フィルタリング
specs:
- from: フィルタとフィルタリング
to: フィルターとフィルタリング
- id: sequence-string
patterns:
sequence: "猫|で|ある|。"
expected: 猫です。
specs:
- from: 猫である。子猫である。
to: 猫です。子猫である。
- id: custom-separator
language: en
patterns:
sequence: "a/ /lot/ /of"
separator: "/"
expected: many
specs:
- from: a lot of items
to: many items
- id: sequence-regexp
patterns:
sequence: [する, こと, が, 可能, { regexp: "だ|です", capture: ending }]
message: "語尾「$<ending>」を含む表現を見直してください。"
specs:
- from: 省略することが可能です。
errors: 1
- id: any-segment
patterns:
sequence: [猫, { any: true, capture: particle }]
expected: 犬$<particle>
specs:
- from: 猫は眠る。
to: 犬は眠る。
- id: regexp-capture
language: en
patterns:
regexp: ECMAScript([0-9]+)
flags: i
boundary: word
expected: ECMAScript $1
specs:
- from: ecmascript2015 / XECMAScript2015
to: ECMAScript 2015 / XECMAScript2015
- id: explicit-substring
patterns: { literal: ネコ, boundary: none }
expected: 猫
specs:
- from: 子ネコ
to: 子猫
- id: english-phrase
language: en
patterns: a lot of
ignoreCase: true
whitespace: flexible
expected: many
specs:
- from: A LOT OF items
to: many items
- id: canonical-spelling
expected: jQuery
specs:
- from: JQUERYとjQuery
to: jQueryとjQuery
- id: report-only
patterns: [絶対, 絶対に]
message: 断定の根拠を確認してください。
specs:
- from: 絶対に成功する。
errors: 1
- id: suggestions-only
patterns: を適応
message: 文脈に合う言葉を選んでください。
suggestions:
- を適用
- text: に対応
message: 対応関係を表す場合はこちらを使います。
specs:
- from: ルールを適応する。
to: ルールを適応する。
errors: 1
suggestions: [を適用, に対応]
- id: delete-word
language: en
patterns: very
expected: ""
specs:
- from: very
to: ""The same dictionary is available as examples/formats.yml. Validate it with textlint-rule-wording check examples/formats.yml.
For the full Markdown input, fixed output, and diagnostic ranges for each format, see the format fixtures.
| Property | Behavior |
|---|---|
message |
Report a diagnostic without an automatic fix |
expected |
Replace the matched range; an empty string deletes it |
suggestions |
Offer replacements for the user to choose; --fix does not apply them |
These properties can be combined. The rule supplies a default diagnostic message when message is omitted.
Each suggestion is a string or { text: "replacement", message: "explanation" }.
Suggestions use textlint's Suggestion API; their presentation depends on the editor or formatter.
A match already equal to expected is not reported.
Overlapping matches from different rules are both reported. Fixes prefer the leftmost match, then the longest match at the same position, then dictionary order for identical ranges.
Overlapping fixes are not applied together, and identical diagnostics are deduplicated.
The engine's fix() and a single textlint execution each perform one pass. They do not automatically resolve conflicting rules.
| Option | Default | Purpose |
|---|---|---|
language |
ja |
A BCP 47 language tag, such as ja, en, en-US, or tr |
ignoreCase |
false |
Lowercase using the selected language before matching |
normalize |
NFC |
Unicode normalization for comparison: false, NFC, or NFKC |
whitespace |
exact |
flexible treats horizontal whitespace runs as equivalent, without crossing line breaks |
Options can be set at the textlint, dictionary, and individual rule levels; later levels override earlier ones. Language is not detected automatically. An unsupported language causes a loading error. Rules in the same dictionary can use different languages.
Omitting patterns and writing only expected: jQuery enables spelling normalization with defaults of ignoreCase: true and normalize: NFKC.
Explicit options take precedence. You can also use normalize: NFKC with ordinary patterns, but it equates a broad range of compatibility characters; use dictionary tests to verify the intended behavior.
Diagnostics and fixes use offsets in the original text even when normalization changes its length.
patterns: フィルタ
expected: フィルター
except: [フィルター, フィルタリング]except accepts the same formats as patterns. Any diagnostic overlapping an exception match is excluded.
In the tested ICU versions, フィルタリング splits into フィルタ and リング, so word boundaries alone do not prevent this false positive.
version: 1
language: ja
rules:
- id: comp
language: en
patterns: comp
expected: component
specs:
- from: This comp works.
to: This component works.
errors: 1
- from: This component works.
errors: 0Combine dictionaries in .textlintrc.json by listing their paths:
{
"rules": {
"wording": {
"rulePaths": ["./common.yml", "./terms.json", "./wording.yml"]
}
}
}Relative paths resolve from the textlint configuration file's directory. Each dictionary inherits textlint's defaults, then applies its own dictionary and rule options. Defaults do not carry over from one dictionary to another.
Dictionaries do not load other dictionaries. For YAML/JSON strings, list each dictionary in ruleContents instead.
Unknown properties, duplicate YAML keys, invalid regexps, and failing specs cause loading errors.
Each rule's specs run in isolation. to checks the fixed output, errors checks the diagnostic count, and suggestions checks replacement strings in diagnostic/candidate order.
Report-only and suggestion-only rules require an errors or suggestions assertion.
Test actual Markdown and interactions between multiple rules separately with textlint.
npx textlint-rule-wording check ./wording.yml
npx textlint-rule-wording --schema > wording.schema.jsonUse the JSON Schema for editor assistance.
textlint-rule-wording check validates segmentation, regexps, runtime language support, and dictionary specs.
textlint-rule-wording --help contains a writing workflow and examples covering all formats, exceptions, captures, suggestions, and specs.
When asking AI to write a dictionary, have it read this help and --schema first, then run check on the result.
segment --help and check --help show the same reference.
The rule checks Str nodes using the same default scope as PRH.
Links, block quotes, and emphasis are excluded by default. Headings, paragraphs, and strong text are included.
Code, images, and HTML comments are not checked as prose. Matches do not span separate Str nodes.
checkLink, checkBlockQuote, and checkEmphasis default to false; checkHeader and checkParagraph default to true.
With checkCodeComment: [js, javascript], fenced code blocks carrying those language labels are parsed as JavaScript, and only their comments are checked.
Unparseable code is skipped, or reported as a parsing error with debug: true.
Use textlint filter rules for inline suppression.
The matching engine is available as WordingEngine from textlint-rule-wording/engine.
It does not depend on the filesystem or PRH. For dictionary files, use loadRules from textlint-rule-wording/dictionary.
Use a bundler and a browser that supports Intl.Segmenter.
Matching an array of rule objects requires neither Node.js polyfills nor a JavaScript parser:
import { WordingEngine } from "textlint-rule-wording/engine";
const engine = new WordingEngine([{ language: "en", patterns: "comp", expected: "component" }]);
engine.find("comp component");
engine.fix("comp component"); // "component component"Import /browser explicitly for the textlint rule and YAML/JSON dictionary loading:
import wording, { loadRules, WordingEngine } from "textlint-rule-wording/browser";
const options = {
ruleContents: ["version: 1\nrules:\n - patterns: comp\n expected: component"]
};
const engine = new WordingEngine(loadRules(options));
engine.fix("comp"); // "component"
// Pass { ruleId: "wording", rule: wording, options } to textlint's configuration.Bundlers supporting the browser export condition also select the browser implementation for ordinary package imports.
The browser implementation accepts rules and ruleContents. Filesystem-based rulePaths causes an error.
For multiple dictionaries, pass each dictionary as a separate string in ruleContents.
When combining the rule with @textlint/kernel 15.8.0, the kernel's AST validator needs a browser replacement for node:assert.
The browser integration test aliases node:assert to assert in esbuild and injects process for that library.
This rule itself needs neither polyfill. JavaScript comment checks through checkCodeComment use @babel/parser in browsers as well.
Intl.Segmenter does not provide parts of speech, readings, or base forms of verbs. It cannot replace a morphological analyzer for those rules.
Segmentation depends on the runtime's ICU version. Run dictionary specs in the Node.js version you use.
Typos within words, inflected expressions, and compound words may need additional literal patterns, exceptions, or regexps.
The rule does not load PRH dictionaries directly. A migrated js-primer dictionary is included as an example.
Use Node.js 22 or later, pnpm 10.15.0, and Git 2.54 or later. Install dependencies and build in this repository:
pnpm install --frozen-lockfile
pnpm buildThe prepare script run by pnpm install includes the repository's .gitconfig, registering pnpm lint and pnpm format:check as pre-commit checks.
If your environment uses ignore-scripts=true, run pnpm prepare once.
See the Git hook documentation for configuration-based hooks.
To try the built rule, install the local package in a consumer project, then follow Usage:
npm install --save-dev textlint /path/to/textlint-rule-wordingInside this repository, invoke the CLI directly with node lib/cli.js.
The following commands run checks, migrate the dictionary, compare the corpus, and build a package:
pnpm test
pnpm lint
pnpm typecheck
pnpm format:check
pnpm migrate:js-primer
pnpm verify:js-primer /path/to/js-primer
pnpm pack --out /tmp/textlint-rule-wording.tgzTests are written in TypeScript and run with tsx. pnpm typecheck checks both implementation and tests; pnpm test includes that check.
Rule tests use textlint-tester and Mocha to verify diagnostics, ranges, fixes, and suggestions (pnpm test:rules).
Format fixtures run in the same suite; expected results are not updated automatically.
Engine, dictionary, CLI, and PRH comparison tests use node:test (pnpm test:unit).
Browser tests bundle the published entry points with esbuild and run matching and textlint fixes in a VM without Node.js globals.
Normal tests need neither network access nor a js-primer checkout.
verify:js-primer reads the supplied checkout and saves its comparison to docs/js-primer-comparison.json in this repository.
MIT © azu. See third-party materials for the sources and licenses of migrated dictionaries and test examples.