Aspell 拼写检查完全教程 / 第3章 基本使用
第 3 章:基本使用
本章介绍 Aspell 最常用的三种使用模式——交互模式、管道模式和文件检查,并通过实例演示各项常用选项。
3.1 使用模式总览
| 模式 | 命令格式 | 用途 |
|---|---|---|
| 交互模式 | aspell check <file> | 逐词确认并修正,适合人工校对 |
| 管道模式 | echo "word" | aspell -a | 程序化检查,适合脚本集成 |
| 列表模式 | aspell list < <file> | 只列出拼写错误的单词,适合批量处理 |
| dump 模式 | aspell dump dicts | 查看词典、配置等内部信息 |
3.2 交互模式(Interactive Mode)
交互模式是 Aspell 最直观的使用方式,适用于人工逐词校对文档。
3.2.1 基本用法
# 交互式检查文件
aspell check document.txt
运行后 Aspell 会逐个高亮显示可疑单词,并显示建议列表:
document.txt
& teh 3 0: the, tea, tee
0: Skip 1: Replace 2: Accept
3: Accept all 4: Add 5: Add lowercase
6: Abort 7: Exit
3.2.2 交互命令详解
| 按键 | 命令 | 说明 |
|---|---|---|
0 或 Space | Skip | 跳过当前单词,保留原样 |
1 | Replace | 从建议列表中选择替换词 |
2 | Accept | 接受当前单词(仅本次出现) |
3 | Accept all | 接受当前单词(整个文件中所有出现) |
4 | Add | 将单词加入个人词典(区分大小写) |
5 | Add lowercase | 将单词小写形式加入个人词典 |
6 或 Ctrl+C | Abort | 中止检查,不保存修改 |
7 或 Ctrl+D | Exit | 退出并保存已做的修改 |
i | Insert | 在当前位置插入替换词 |
r | Replace manually | 手动输入替换词 |
l | Lookup | 在词典中查找 |
? | Help | 显示帮助信息 |
3.2.3 指定语言
# 使用英语(美式)检查
aspell check --lang=en_US document.txt
# 使用德语检查
aspell check --lang=de document.txt
# 使用英式英语检查
aspell check --lang=en_GB document.txt
3.2.4 过滤器模式
# 检查 HTML 文件(忽略标签)
aspell check --mode=html page.html
# 检查 TeX/LaTeX 文件
aspell check --mode=tex paper.tex
# 检查 Markdown 文件
aspell check --mode=markdown README.md
# 检查 Email 文件
aspell check --mode=email message.eml
# 检查 Nroff/Troff 文件
aspell check --mode=nroff manpage.1
3.2.5 常用交互选项
# 不创建备份文件
aspell check --dont-backup document.txt
# 指定备份文件后缀
aspell check --suffix=.bak document.txt
# 使用大小写敏感模式
aspell check --ignore-case=false document.txt
# 忽略大小写(默认)
aspell check --ignore-case document.txt
# 忽略重音符号
aspell check --ignore-accents document.txt
# 指定个人词典
aspell check --personal=my-dict.pws document.txt
# 指定额外词典
aspell check --extra-dicts=technical.pws document.txt
3.3 管道模式(Pipe Mode)
管道模式通过 aspell -a 命令进入,逐行读取输入并返回检查结果,适合脚本和程序集成。
3.3.1 基本用法
# 管道模式的基本交互
echo "teh" | aspell -a
输出格式:
@(#) International Ispell Version 3.1.20 (but really Aspell 0.60.8.1)
& teh 3 0: the, tea, tee
3.3.2 管道协议详解
管道模式的输出遵循 ispell 兼容协议,每行以特定字符开头:
| 前缀 | 含义 | 示例 |
|---|---|---|
* | 拼写正确 | * hello |
& | 找到近似匹配(建议) | & teh 3 0: the, tea, tee |
# | 未找到任何匹配 | # xyzzy 0 |
$ | 可能是词缀错误 | $ running 5 0: ruin, ... |
- | 连字符词的某部分 | - sub- |
+ | 连字符词(复合词) | + compound-word |
! | 未在词典中找到 | ! unknownword 0 |
| 空行原样返回 | `` |
& 行详细格式
& teh 3 0: the, tea, tee
│ │ │ │ │
│ │ │ │ └── 建议列表(逗号分隔)
│ │ │ └──── 原词在建议列表中的偏移(通常为 0)
│ │ └────── 建议数量
│ └───────── 原词
└──────────── & 表示有近似匹配
3.3.3 管道模式示例
# 检查多个单词
cat << 'EOF' | aspell -a
hello world
teh quik brown fox
EOF
# 输出:
# *
# *
# & teh 3 0: the, tea, tee
# & quik 5 0: quick, quirk, quit, quiz, quilt
# *
3.3.4 管道模式中指定选项
# 使用英式英语
echo "colour" | aspell -a -d en_GB
# 使用德语
echo "Hallo" | aspell -a -d de
# 指定建议模式
echo "teh" | aspell -a --sug-mode=ultra
# 指定编码
echo "café" | aspell -a --encoding=utf-8
# 指定过滤器
echo "<b>teh</b>" | aspell -a --mode=html
3.3.5 Shell 脚本解析管道输出
#!/bin/bash
# spell_check.sh — 从管道输出中提取拼写错误
WORDS="teh quik colour programing"
ERRORS=0
while IFS= read -r line; do
case "$line" in
\*) ;; # 正确单词,跳过
\&*|\#*|\!*)
WORD=$(echo "$line" | cut -d' ' -f2)
echo "拼写错误: $WORD"
if [[ "$line" == &* ]]; then
SUGGESTIONS=$(echo "$line" | cut -d':' -f2)
echo " 建议: $SUGGESTIONS"
fi
((ERRORS++))
;;
esac
done <<< "$(echo "$WORDS" | aspell -a)"
echo ""
echo "共发现 $ERRORS 个拼写错误"
运行示例:
./spell_check.sh
# 输出:
# 拼写错误: teh
# 建议: the, tea, tee
# 拼写错误: quik
# 建议: quick, quirk, quit, quiz, quilt
# 拼写错误: colour
# 建议: color, colours, coloured
# 拼写错误: programing
# 建议: programming, program ring, program-ring, program
#
# 共发现 4 个拼写错误
3.4 列表模式(List Mode)
列表模式只输出拼写错误的单词,不显示建议,非常适合批量处理。
3.4.1 基本用法
# 列出文件中所有拼写错误的单词
aspell list < document.txt
# 等价写法
cat document.txt | aspell list
# 从命令行参数(注意:不支持直接传文件名给 list)
aspell list < /path/to/file
3.4.2 列表模式选项
# 使用特定语言
aspell list < document.txt -d en_US
# 使用特定过滤器
aspell list --mode=html < page.html
# 忽略大小写敏感
aspell list --ignore-case < document.txt
# 指定个人词典
aspell list --personal=my-dict.pws < document.txt
# 忽略特定长度的单词(如只报告长度 > 3 的单词)
aspell list --ignore=3 < document.txt
# 不使用任何词典(只返回所有单词)
aspell list --dont-use-other-dicts < document.txt
3.4.3 批量检查多个文件
# 检查所有 Markdown 文件
find . -name "*.md" -exec aspell list --mode=markdown {} \;
# 只列出唯一的拼写错误单词
find . -name "*.md" -exec aspell list --mode=markdown {} \; | sort -u
# 按出现次数排序
find . -name "*.md" -exec aspell list --mode=markdown {} \; | sort | uniq -c | sort -rn
3.4.4 结合 comm 对比两份文档
# 找出 doc1.txt 中有但 doc2.txt 中没有的拼写错误
comm -23 \
<(aspell list < doc1.txt | sort -u) \
<(aspell list < doc2.txt | sort -u)
3.5 通用选项参考
3.5.1 语言与词典选项
| 选项 | 说明 | 默认值 |
|---|---|---|
-d, --lang=<lang> | 指定语言/词典 | en |
-p, --personal=<file> | 个人词典路径 | ~/.aspell.<lang>.pws |
--extra-dicts=<file> | 额外词典(可多个) | 无 |
--use-other-dicts | 使用其他可用词典 | true |
--master=<file> | 指定主词典文件 | 自动查找 |
# 使用美式英语 + 自定义词典
aspell check -d en_US --personal=project.pws document.txt
# 使用多个额外词典
aspell check --extra-dicts=tech.pws --extra-dicts=names.pws document.txt
3.5.2 行为选项
| 选项 | 说明 | 默认值 |
|---|---|---|
--ignore=<n> | 忽略长度 ≤ n 的单词 | 1 |
--ignore-case | 忽略大小写 | true |
--ignore-accents | 忽略重音符号 | false |
--dont-save | 不保存个人词典修改 | false |
--dont-backup | 不创建备份文件 | false |
--sug-mode=<mode> | 建议模式 | normal |
3.5.3 建议模式详解
| 模式 | 说明 | 速度 | 质量 |
|---|---|---|---|
ultra | 最快建议,使用简略算法 | ★★★★★ | ★★☆☆☆ |
fast | 快速建议 | ★★★★☆ | ★★★☆☆ |
normal | 平衡模式(默认) | ★★★☆☆ | ★★★★☆ |
bad-spellers | 慢速但最多建议,适合不确定的拼写 | ★★☆☆☆ | ★★★★★ |
# 使用 ultra 模式(最快)
echo "teh" | aspell -a --sug-mode=ultra
# 使用 bad-spellers 模式(最多建议)
echo "teh" | aspell -a --sug-mode=bad-spellers
3.6 输出控制
3.6.1 管道模式中禁用问候语
# -H / --dont-suggest 不显示问候行
echo "teh" | aspell -a -H
# 直接输出: & teh 3 0: the, tea, tee
3.6.2 管道模式中禁用建议
# -S / --dont-suggest-replacements 不显示建议
echo "teh" | aspell -a -S
# 输出: # teh 0
3.6.3 只显示错误(纯文本)
# 结合 grep 只提取错误行
echo "hello teh world" | aspell -a | grep -v '^\*$' | grep -v '^@'
# 输出: & teh 3 0: the, tea, tee
3.6.4 将结果重定向到文件
# 保存检查结果
echo "hello teh world" | aspell -a > check_result.txt
# 只保存错误单词
aspell list < document.txt > misspelled.txt
3.7 实际业务场景
场景 1:检查 Git commit message
#!/bin/bash
# check_commit_msg.sh — 检查最新 commit message 拼写
MSG=$(git log -1 --pretty=format:"%s")
ERRORS=$(echo "$MSG" | aspell list -d en_US)
if [ -n "$ERRORS" ]; then
echo "拼写错误 in commit message:"
echo "$ERRORS"
exit 1
else
echo "拼写检查通过"
exit 0
fi
场景 2:检查 README 中的拼写
#!/bin/bash
# check_readme.sh — 检查项目 README 拼写
FILE="README.md"
if [ ! -f "$FILE" ]; then
echo "未找到 $FILE"
exit 1
fi
echo "检查 $FILE ..."
ERRORS=$(aspell list --mode=markdown --personal=./.project-dict.pws < "$FILE" | sort -u)
if [ -n "$ERRORS" ]; then
echo "发现以下拼写错误:"
echo "$ERRORS"
echo ""
echo "提示:将正确的专有名词加入 .project-dict.pws"
exit 1
else
echo "拼写检查通过"
exit 0
fi
场景 3:带行号的错误报告
#!/bin/bash
# spell_with_lines.sh — 输出错误单词及其所在行号
FILE="$1"
if [ -z "$FILE" ]; then
echo "用法: $0 <file>"
exit 1
fi
# 获取所有拼写错误的单词(去重)
MISSPELLED=$(aspell list < "$FILE" | sort -u)
if [ -z "$MISSPELLED" ]; then
echo "拼写检查通过: $FILE"
exit 0
fi
echo "拼写错误: $FILE"
echo "=========="
while IFS= read -r word; do
# 使用 grep -n 找到行号
LINE_NUMS=$(grep -n -i -w "$word" "$FILE" | cut -d: -f1 | tr '\n' ',' | sed 's/,$//')
echo " \"$word\" (第 $LINE_NUMS 行)"
done <<< "$MISSPELLED"
运行示例:
./spell_with_lines.sh document.txt
# 输出:
# 拼写错误: document.txt
# ==========
# "teh" (第 3,15 行)
# "programing" (第 8 行)
# "recieve" (第 22 行)
场景 4:在管道中与其他工具配合
# 只检查 .md 文件中非代码块的内容
# (简单实现:排除以 ``` 开头的行之间的内容)
cat README.md | \
awk 'BEGIN{in_code=0} /^```/{in_code=!in_code; next} !in_code{print}' | \
aspell list --mode=markdown
# 检查 Markdown 文件中的标题
grep '^#' README.md | sed 's/^#* //' | aspell list
3.8 编码处理
3.8.1 指定输入编码
# 默认使用系统 locale 编码
locale charmap
# 明确指定 UTF-8
echo "café résumé" | aspell -a --encoding=utf-8
# 检查 Latin-1 编码文件
iconv -f latin1 -t utf-8 old_file.txt | aspell -a --encoding=utf-8
3.8.2 处理不同编码的文件
#!/bin/bash
# spell_check_encoding.sh — 自动检测编码并检查拼写
FILE="$1"
if [ ! -f "$FILE" ]; then
echo "文件不存在: $FILE"
exit 1
fi
# 检测文件编码
ENCODING=$(file -bi "$FILE" | sed 's/.*charset=//')
echo "检测到编码: $ENCODING"
# 转换为 UTF-8 后检查
if [ "$ENCODING" != "utf-8" ] && [ "$ENCODING" != "us-ascii" ]; then
echo "转换编码: $ENCODING -> utf-8"
iconv -f "$ENCODING" -t utf-8 "$FILE" | aspell list --encoding=utf-8
else
aspell list < "$FILE"
fi
3.9 与 Shell 环境集成
3.9.1 Shell 别名
# 在 ~/.bashrc 或 ~/.zshrc 中添加
alias spellcheck='aspell check --mode=markdown --personal=~/.aspell.pws'
alias spellme='aspell list --mode=markdown'
# 使用
spellcheck article.md
spellme < README.md
3.9.2 函数封装
# 快速检查函数(添加到 ~/.bashrc)
function spell() {
if [ -z "$1" ]; then
echo "用法: spell <file>"
return 1
fi
local errors
errors=$(aspell list --mode=markdown < "$1" | sort -u)
if [ -z "$errors" ]; then
echo "✓ 拼写检查通过: $1"
else
echo "✗ 拼写错误 ($1):"
echo "$errors" | sed 's/^/ /'
return 1
fi
}
# 使用
spell README.md
# 输出: ✓ 拼写检查通过: README.md
3.9.3 Tab 补全(Bash)
# ~/.bash_completion.d/aspell
_aspell_modes() {
local modes="email html markdown nroff tex texinfo url none"
COMPREPLY=($(compgen -W "$modes" -- "${COMP_WORDS[COMP_CWORD]}"))
}
complete -F _aspell_modes aspell
3.10 本章小结
| 要点 | 说明 |
|---|---|
| 交互模式 | aspell check file,逐词人工确认 |
| 管道模式 | echo "word" | aspell -a,程序化集成 |
| 列表模式 | aspell list < file,只输出错误单词 |
| 过滤器 | --mode=html/tex/markdown/email |
| 建议模式 | ultra / fast / normal / bad-spellers |
| 个人词典 | --personal=file.pws |
下一步
→ 第 4 章:词典管理 — 深入了解 Aspell 词典系统,学习管理个人词典和自定义词典。