LoginSignup
3
2

More than 1 year has passed since last update.

Emacs上でtextlintを使って日本語の校正をするには

Last updated at Posted at 2022-12-30

手軽に日本語のチェックがしたい

商売柄、年末は大量の日本語を読んで手直しする羽目になるのだが、内容はともかく、誤字脱字やら抜き言葉、用語の揺れ程度は機械的にチェックしたいのである。Microsoft Wordのようなワープロにも若干の校正機能は付いているが貧弱だし、Just Right!のような専門ソフトウェアもあるが高価な上にプロプライエタリで、おまけにWindowsでしか使えなかったりもする。

textlintのインストール

そんなときに便利なのがtextlintだ。ルールを指定することでチェック内容を柔軟に変えることが出来、しかもクロスプラットフォーム対応なので、WindowsでもGNU/Linuxでも同じように使える。

textlintはJavaScriptで書かれているので、自身のインストールはnpmを使う。大体のGNU/Linuxディストロにはnpmが入っているだろうから apt-get install npm でいけるだろう。Windowsの場合はScoopを使って scoop install npm で入れるのがおすすめである。このへんは先日書いたEmacs設定の記事でも触れた通りだ。

なおあまり推奨はされていないようだが、 -g オプションを付けてグローバルにインストールしたほうがスタンドアロンのツールとして使う分には何かと便利なように思われる。その場合、Windowsの場合は不要だが、GNU/Linuxでは /usr/local 以下へインストールすることになるので、 sudo が必要になる。

> npm -g install textlint

さらに、プラグインや校正ルールをインストールする必要がある。どんなプラグインやルール・ファイルがあるかはCollection of textlint ruleに一覧がある。今回はたまたま日本語に絞っているが、英語などにも対応しているようだ。

texlint本体をグローバルでインストールした場合、これらも -g オプションをつけてインストールしなければならない。textlintは標準でMarkdownには対応しているので、Org形式やHTML形式に対応するプラグインを別途追加してある。雑な人間なので、textlint-jaの公式Githubレポジトリにあるルールはとりあえず全部インストールしてしまった。各ルールの説明も同じところにある。

> npm install -g textlint-ja/textlint-rule-morpheme-match \
 @textlint-ja/textlint-rule-no-dropping-i \
 @textlint-ja/textlint-rule-no-filler \
 @textlint-ja/textlint-rule-no-insert-dropping-sa \
 @textlint-ja/textlint-rule-no-insert-re \   
 @textlint-ja/textlint-rule-no-synonyms \
 sudachi-synonyms-dictionary \
 textlint-filter-rule-ja-named-entities \
 textlint-plugin-html \
 textlint-plugin-org \
 textlint-rule-ja-joyo-or-jinmeiyo-kanji \
 textlint-rule-ja-kyoiku-kanji \
 textlint-rule-ja-no-abusage \
 textlint-rule-ja-no-inappropriate-words \
 textlint-rule-ja-no-mixed-period \
 textlint-rule-ja-no-orthographic-variants \
 textlint-rule-ja-no-redundant-expression \
 textlint-rule-ja-no-successive-word \
 textlint-rule-ja-no-weak-phrase \
 textlint-rule-ja-unnatural-alphabet \
 textlint-rule-max-kanji-continuous-len \
 textlint-rule-max-ten \
 textlint-rule-no-double-negative-ja \
 textlint-rule-no-doubled-conjunction \
 textlint-rule-no-doubled-conjunctive-particle-ga \
 textlint-rule-no-doubled-joshi \
 textlint-rule-no-dropping-the-ra \
 textlint-rule-no-hankaku-kana \
 textlint-rule-no-mix-dearu-desumasu \
 textlint-rule-no-mixed-zenkaku-and-hankaku-alphabet \
 textlint-rule-no-nfd \
 textlint-rule-no-redundant-expression \
 textlint-rule-prefer-tari-tari \
 textlint-rule-preset-ja-spacing \
 textlint-rule-preset-ja-technical-writing \
 textlint-rule-preset-japanese

表記の統一をやるソフトウェアなのにルール名が案外統一されていないのはご愛敬といったところか。

さらに、ホームディレクトリにtextlintの設定ファイルを用意する必要がある。ファイル名は .textlintrc でもよいのだが、 .textlintrc.json と拡張子までつけておくとJSONとして扱ってくれるので編集が楽である。

{
    "plugins": ["org","html"],
    "filters": {
        "ja-named-entities": true
    },
    "rules": {
        "@textlint-ja/textlint-rule-no-dropping-i": true,
        "@textlint-ja/textlint-rule-no-filler": true,
        "@textlint-ja/textlint-rule-no-insert-dropping-sa": true,
        "@textlint-ja/textlint-rule-no-insert-re": true,
        "@textlint-ja/no-synonyms": true,
        "ja-joyo-or-jinmeiyo-kanji": true,
        "ja-kyoiku-kanji": false,
        "ja-no-abusage": true,
        "ja-no-inappropriate-words": true,
        "ja-no-mixed-period": true,
        "ja-no-orthographic-variants": true,
        "ja-no-redundant-expression": true,	
        "ja-no-successive-word": true,
        "ja-no-weak-phrase": true,
        "ja-unnatural-alphabet": true,
        "max-kanji-continuous-len": true,
        "max-ten": false,
        "no-double-negative-ja": true,
        "no-doubled-conjunctive-particle-ga": true,
        "no-doubled-joshi": true,
        "no-dropping-the-ra": true,
        "no-mix-dearu-desumasu": true,
        "no-mixed-zenkaku-and-hankaku-alphabet": true,
        "no-nfd": true,
        "prefer-tari-tari": true,
        "preset-ja-spacing": true,
        "preset-ja-technical-writing": false,
        "preset-japanese": true,
        "preset-jtf-style": true
    }
}

自分に必要ないルールは false を指定して無効にすれば良い。このあたりは若干の試行錯誤が必要だろう。

ここまででスタンドアロンのユーティリティとしてはすでに使えるので、

> textlint somejapanese.txt

などと実行してチェックしてみるとよい。

Emacsからの利用

EmacsからはFlycheckと組み合わせてバッファ内のリアルタイム校正が出来る。先日のEmacs設定の記事で使った init.el にはすでに必要な記述が入っているので、textlintをインストールすればすぐ有効になるはずだが、ようは flycheck-define-checker の部分で、これによりtextlintの出力を受け取ってEmacsのバッファで表示している。

;; checker for textlint
(flycheck-define-checker textlint
  "A linter for prose."
  :command ("textlint" "--format" "unix" source-inplace)
  :error-patterns
  ((warning line-start (file-name) ":" line ":" column ": "
            (id (one-or-more (not (any " "))))
            (message (one-or-more not-newline)
                     (zero-or-more "\n" (any " ") (one-or-more not-newline)))
            line-end))
  :modes (text-mode markdown-mode gfm-mode org-mode web-mode))
)

うまく行けばこんな風に表示される。

textlint.jpg


この記事は、個人ブログからの転載です。Emacs 上の Org-mode で書き、Org2blog で Wordpress に投稿したのと同じ原稿を、ox-qmd で変換してそのまま投稿しました。

3
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
2