Hunspell 拼写检查完全教程 / 第 02 章:安装与环境配置
第 02 章:安装与环境配置
2.1 安装概览
Hunspell 的安装分为两个部分:
- 引擎本身:
hunspell命令行工具 +libhunspell库 - 词典文件:对应语言的
.dic+.aff文件
大多数发行版的包管理器会自动处理依赖关系,但词典需要单独安装。
2.2 Linux 安装
2.2.1 Debian / Ubuntu
# 安装 Hunspell 引擎(通常作为依赖自动安装)
sudo apt update
sudo apt install hunspell libhunspell-dev
# 验证安装
hunspell --version
# hunspell 1.7.2
# 安装英语(美国)词典
sudo apt install hunspell-en-us
# 安装其他英语变体
sudo apt install hunspell-en-au # 澳大利亚
sudo apt install hunspell-en-gb # 英国
sudo apt install hunspell-en-ca # 加拿大
# 安装常用欧洲语言
sudo apt install hunspell-fr # 法语
sudo apt install hunspell-de-de # 德语
sudo apt install hunspell-es # 西班牙语
sudo apt install hunspell-pt-br # 巴西葡萄牙语
sudo apt install hunspell-it # 意大利语
sudo apt install hunspell-ru # 俄语
# 搜索所有可用的 hunspell 词典包
apt search hunspell- | grep -E "^hunspell-[a-z]"
# 查看已安装词典的位置
dpkg -L hunspell-en-us
# /usr/share/hunspell/en_US.aff
# /usr/share/hunspell/en_US.dic
# /usr/share/myspell/dicts/en_US.aff → symlink
# /usr/share/myspell/dicts/en_US.dic → symlink
2.2.2 Fedora / RHEL / CentOS
# 安装引擎
sudo dnf install hunspell
# 安装词典
sudo dnf install hunspell-en-US
sudo dnf install hunspell-de
sudo dnf install hunspell-fr
sudo dnf install hunspell-es
sudo dnf install hunspell-pt
# 开发库(用于 C/C++ 编程)
sudo dnf install hunspell-devel
# 搜索可用词典
dnf search hunspell
2.2.3 Arch Linux
# Hunspell 引擎(社区仓库)
sudo pacman -S hunspell
# 安装词典(AUR 仓库中的完整版更丰富)
sudo pacman -S hunspell-en_us
# 通过 AUR 安装更多词典(使用 yay)
yay -S hunspell-fr
yay -S hunspell-de
yay -S hunspell-es_es
yay -S hunspell-pt_br
yay -S hunspell-ru
# 查看已安装词典
pacman -Ql hunspell-en_us | grep -E "\.(dic|aff)$"
# hunspell-en_us /usr/share/hunspell/en_US.aff
# hunspell-en_us /usr/share/hunspell/en_US.dic
2.2.4 openSUSE
# 引擎
sudo zypper install hunspell
# 词典
sudo zypper install hunspell-dictionaries-en
sudo zypper install hunspell-dictionaries-de
sudo zypper install hunspell-dictionaries-fr
2.2.5 Alpine Linux(轻量级)
# 引擎(用于 Docker 镜像等轻量场景)
apk add hunspell
# 词典
apk add hunspell-en
2.3 macOS 安装
2.3.1 系统内置
macOS 10.6 (Snow Leopard) 起系统内置了 Hunspell 支持。系统词典位于:
# macOS 系统词典位置
ls /System/Library/Spelling/
# 或者(旧版本)
ls ~/Library/Spelling/
2.3.2 Homebrew 安装(推荐)
# 安装 Hunspell(用于命令行和编程)
brew install hunspell
# 验证
hunspell --version
# macOS 没有像 apt 那样的词典包管理
# 需要手动下载词典文件
2.3.3 手动安装词典
# 创建词典目录
mkdir -p ~/Library/Spelling
# 从 LibreOffice 词典项目下载英语词典
cd ~/Library/Spelling
curl -LO https://raw.githubusercontent.com/wooorm/dictionaries/main/dictionaries/en/index.aff
curl -LO https://raw.githubusercontent.com/wooorm/dictionaries/main/dictionaries/en/index.dic
# 重命名为标准命名
mv index.aff en_US.aff
mv index.dic en_US.dic
# 或者从 LibreOffice 扩展下载
# 访问:https://extensions.libreoffice.org/en/extensions/english-dictionaries
# 下载 .oxt 文件(本质是 zip),解压获取 .dic 和 .aff
2.3.4 配置环境变量
# 在 ~/.zshrc 或 ~/.bash_profile 中添加
export DICPATH="$HOME/Library/Spelling"
# 使生效
source ~/.zshrc
2.4 Windows 安装
2.4.1 MSYS2(推荐开发者使用)
# 在 MSYS2 终端中
pacman -S mingw-w64-x86_64-hunspell
pacman -S mingw-w64-x86_64-hunspell-en
# 词典位置
# C:\msys64\mingw64\share\hunspell\
2.4.2 Chocolatey
# 以管理员权限运行 PowerShell
choco install hunspell
# 安装后需要手动获取词典
2.4.3 Scoop
scoop install hunspell
2.4.4 手动安装
# 1. 从 GitHub Release 下载预编译版本
# https://github.com/hunspell/hunspell/releases
# 2. 解压到目录,例如 C:\hunspell
# 3. 添加到 PATH
$env:PATH += ";C:\hunspell\bin"
# 4. 设置词典路径
$env:DICPATH = "C:\hunspell\share\hunspell"
2.4.5 Windows 词典路径
Hunspell 在 Windows 上按以下顺序搜索词典:
DICPATH环境变量指定的目录HOME或USERPROFILE目录下的.hunspell_*- 注册表路径(如果由安装程序配置)
- 程序所在目录下的
share/hunspell/
2.5 从源码编译
2.5.1 获取源码
# 克隆仓库
git clone https://github.com/hunspell/hunspell.git
cd hunspell
# 查看最新版本
git tag | sort -V | tail -5
# v1.7.0
# v1.7.1
# v1.7.2
# v1.7.3
# v1.7.4
# 检出稳定版本
git checkout v1.7.2
2.5.2 编译安装(Linux/macOS)
# 安装构建依赖
# Debian/Ubuntu:
sudo apt install autoconf automake libtool autopoint gettext
# macOS:
brew install autoconf automake libtool gettext
# 生成构建配置
autoreconf -vfi
# 配置(指定安装前缀)
./configure --prefix=/usr/local
# 编译(使用所有 CPU 核心)
make -j$(nproc)
# 运行测试(可选)
make check
# 安装
sudo make install
# 更新动态链接库缓存(Linux)
sudo ldconfig
2.5.3 CMake 编译(替代方案)
mkdir build && cd build
cmake .. -DCMAKE_INSTALL_PREFIX=/usr/local
make -j$(nproc)
sudo make install
2.5.4 Windows 编译(MSYS2/MinGW)
# 在 MSYS2 MinGW 64-bit 终端中
pacman -S base-devel mingw-w64-x86_64-toolchain
cd hunspell
autoreconf -vfi
./configure --prefix=/mingw64
make -j$(nproc)
make install
2.5.5 编译选项参考
| 选项 | 说明 | 默认值 |
|---|---|---|
--prefix | 安装路径 | /usr/local |
--enable-shared | 构建共享库 | 是 |
--enable-static | 构建静态库 | 否 |
--with-readline | 启用 readline 支持 | 自动检测 |
--disable-nls | 禁用国际化消息 | 否 |
2.6 词典包详解
2.6.1 主要词典来源
| 来源 | 说明 | 网址 |
|---|---|---|
| LibreOffice Dictionaries | 最全面,社区维护 | extensions.libreoffice.org |
| Mozilla 系列 | Firefox/Thunderbird 词典 | addons.mozilla.org |
| SCOWL | 英语词表项目 | wordlist.aspell.net |
| wooorm/dictionaries | 统一格式的多语言词典(npm) | github.com/wooorm/dictionaries |
| Debian/Ubuntu 包 | 发行版官方维护 | packages.ubuntu.com |
2.6.2 常用语言词典安装一览
| 语言 | 代码 | Ubuntu 包名 | 词典大小 |
|---|---|---|---|
| 英语(美国) | en_US | hunspell-en-us | ~250K 词 |
| 英语(英国) | en_GB | hunspell-en-gb | ~250K 词 |
| 中文 | zh_CN | 手动安装 | 需分词 |
| 法语 | fr | hunspell-fr | ~400K 词 |
| 德语 | de_DE | hunspell-de-de | ~300K 词 |
| 西班牙语 | es | hunspell-es | ~500K 词 |
| 葡萄牙语(巴西) | pt_BR | hunspell-pt-br | ~350K 词 |
| 俄语 | ru | hunspell-ru | ~400K 词 |
| 日语 | ja | 手动安装 | 需特殊处理 |
| 阿拉伯语 | ar | hunspell-ar | ~50K 词根 |
2.6.3 词典文件安装位置
Hunspell 按以下优先级搜索词典文件:
1. 命令行 -d 参数指定的完整路径
2. DICPATH 环境变量
3. 用户目录 ~/.hunspell_<lang>
4. 系统路径:
├── /usr/share/hunspell/ ← Debian/Ubuntu
├── /usr/share/myspell/ ← symlink
├── /usr/share/myspell/dicts/ ← Fedora
└── /Library/Spelling/ ← macOS
# 查看 Hunspell 实际使用的词典搜索路径
hunspell -D
# 搜索路径:
# /home/user/.hunspell_en_US
# /usr/share/hunspell
# ...
# 可用词典:
# /usr/share/hunspell/en_US
2.6.4 手动安装词典文件
# 创建目录
sudo mkdir -p /usr/local/share/hunspell
# 下载词典(以法语为例)
cd /tmp
wget https://extensions.libreoffice.org/extensions/dictionnaires-francais/4.13/lo-oo-4.13.oxt
# .oxt 文件本质是 ZIP 格式
unzip lo-oo-4.13.oxt -d fr_dict
# 复制词典文件
sudo cp fr_dict/fr.aff /usr/local/share/hunspell/
sudo cp fr_dict/fr.dic /usr/local/share/hunspell/
# 创建符号链接到标准路径
sudo ln -s /usr/local/share/hunspell/fr.aff /usr/share/hunspell/
sudo ln -s /usr/local/share/hunspell/fr.dic /usr/share/hunspell/
# 验证
hunspell -D | grep fr
2.7 中文支持配置
2.7.1 中文拼写检查的特殊性
中文与拉丁语系语言有本质区别,Hunspell 原生设计针对单词级(word-level)检查。中文拼写检查需要:
- 分词预处理:将连续汉字切分为词
- 自定义词典:包含中文词语的词典
- 组合方案:通常需要配合 jieba 等分词工具
拉丁语系:The quik brown fox → 按空格分词 → 逐词检查
中文: 快速的棕色狐狸跳过了懒狗 → 无天然分隔 → 需要分词器
2.7.2 安装中文词典
# 方案一:使用 LibreOffice 中文词典
# 下载中文字典扩展
cd /tmp
wget https://extensions.libreoffice.org/extensions/chinese-dictionary-pack/4.0/zh_CN_dict-4.0.oxt
unzip zh_CN_dict-4.0.oxt -d zh_dict
# 安装
sudo cp zh_dict/zh_CN.aff /usr/share/hunspell/
sudo cp zh_dict/zh_CN.dic /usr/share/hunspell/
# 验证
echo "你好" | hunspell -d zh_CN -l
# 方案二:使用社区维护的中文词典
# GitHub 搜索 "hunspell chinese dictionary"
git clone https://github.com/nickyoungzzz/hunspell-zh_CN.git
sudo cp hunspell-zh_CN/zh_CN.* /usr/share/hunspell/
2.7.3 配合 jieba 分词
# 安装 jieba 分词(Python)
pip install jieba
# 创建分词 + 拼写检查脚本
cat > hunspell_zh_check.sh << 'EOF'
#!/bin/bash
# 中文拼写检查脚本:先分词,再用 Hunspell 检查
# 用法: ./hunspell_zh_check.sh <文件>
FILE="$1"
if [ ! -f "$FILE" ]; then
echo "用法: $0 <中文文本文件>"
exit 1
fi
# 使用 jieba 分词,用空格分隔后送入 hunspell
python3 -c "
import jieba
import sys
text = open('$FILE', encoding='utf-8').read()
words = jieba.cut(text)
print(' '.join(words))
" | hunspell -d zh_CN -l | sort -u
EOF
chmod +x hunspell_zh_check.sh
2.7.4 Python 中文拼写检查集成
#!/usr/bin/env python3
"""中文拼写检查工具:jieba + hunspell"""
import subprocess
import jieba
def check_chinese(text: str, dict_name: str = "zh_CN") -> list[dict]:
"""
对中文文本进行拼写检查。
返回格式:[{"word": "错词", "position": 42}, ...]
"""
# 分词
words = list(jieba.cut(text))
# 逐词检查
errors = []
pos = 0
for word in words:
# 跳过空白和标点
if not word.strip() or not any('\u4e00' <= c <= '\u9fff' for c in word):
pos += len(word)
continue
# 调用 hunspell 检查
result = subprocess.run(
["hunspell", "-d", dict_name, "-l"],
input=word, capture_output=True, text=True
)
if result.stdout.strip():
errors.append({"word": word, "position": pos})
pos += len(word)
return errors
# 使用示例
text = "这是一段包含错别子的文本,用于演试拼写检查功能。"
errors = check_chinese(text)
for err in errors:
print(f" 可能的错词: '{err['word']}' (位置: {err['position']})")
2.8 验证安装
2.8.1 基本验证
# 检查版本
hunspell --version
# hunspell 1.7.2
# 列出可用词典
hunspell -D
# 可用词典:
# /usr/share/hunspell/en_US
# /usr/share/hunspell/zh_CN
# 简单测试
echo "hello" | hunspell -d en_US -l
# 无输出 → 正确
echo "helo" | hunspell -d en_US -l
# helo → 拼写错误
2.8.2 检查建议功能
# -a 参数进入管道模式,可获取建议
echo "helo" | hunspell -d en_US -a
# @(#) International Ispell Version 3.2.06 (but really Hunspell 1.7.2)
# & helo 4 0: hello, Helo, helot, help
#
#
输出格式说明:
| 符号 | 含义 |
|---|---|
* | 单词正确 |
& | 有拼写错误,提供建议 |
# | 有拼写错误,无建议 |
+ | 词根正确,但词形变化错误 |
2.8.3 测试多语言
# 测试英语
echo "color" | hunspell -d en_US -l # 无输出(正确)
echo "colour" | hunspell -d en_US -l # colour(美式英语认为错误)
# 测试德语
echo "Schlüssel" | hunspell -d de_DE -l # 无输出(正确)
# 测试法语
echo "bonjour" | hunspell -d fr -l # 无输出(正确)
2.8.4 检查开发库
# 检查头文件是否存在(C/C++ 开发)
ls /usr/include/hunspell/
# hunspell.h hunvisapi.h hunspell.hxx w_*.hxx
# 检查库文件
pkg-config --libs --cflags hunspell
# -I/usr/include/hunspell -lhunspell-1.7
# 测试编译链接
cat > /tmp/test_hunspell.c << 'EOF'
#include <hunspell/hunspell.h>
#include <stdio.h>
int main() {
Hunhandle *handle = Hunspell_create("/usr/share/hunspell/en_US.aff",
"/usr/share/hunspell/en_US.dic");
if (!handle) {
printf("错误:无法加载词典\n");
return 1;
}
int result = Hunspell_spell(handle, "hello");
printf("'hello' 正确? %s\n", result ? "是" : "否");
result = Hunspell_spell(handle, "helo");
printf("'helo' 正确? %s\n", result ? "是" : "否");
Hunspell_free(handle);
return 0;
}
EOF
gcc /tmp/test_hunspell.c -o /tmp/test_hunspell $(pkg-config --cflags --libs hunspell)
/tmp/test_hunspell
# 'hello' 正确? 是
# 'helo' 正确? 否
2.9 常见安装问题
问题 1:找不到词典
hunspell: Can't open aff/orth for en_US.
解决方案:
# 1. 确认词典包已安装
dpkg -l | grep hunspell-en
# 2. 检查词典文件是否存在
ls -la /usr/share/hunspell/en_US.*
# 3. 检查 DICPATH 环境变量
echo $DICPATH
# 4. 使用完整路径指定
hunspell -d /usr/share/hunspell/en_US file.txt
问题 2:编码错误
hunspell: error: The affix file ... has wrong encoding.
解决方案:
# 确认 .aff 文件中的 SET 行与文件实际编码一致
head -5 /usr/share/hunspell/en_US.aff
# SET UTF-8
# 如果编码不匹配,使用 iconv 转换
iconv -f ISO-8859-1 -t UTF-8 old.aff > new.aff
iconv -f ISO-8859-1 -t UTF-8 old.dic > new.dic
问题 3:权限不足
# 词典文件权限应为 644
sudo chmod 644 /usr/share/hunspell/*.aff /usr/share/hunspell/*.dic
问题 4:多个版本冲突
# 查看实际使用的 hunspell 路径
which hunspell
# 可能存在 /usr/bin/hunspell 和 /usr/local/bin/hunspell
# 确认版本
/usr/bin/hunspell --version
/usr/local/bin/hunspell --version
# 使用 update-alternatives 管理(Debian)
sudo update-alternatives --config hunspell
2.10 本章小结
| 平台 | 安装命令 | 词典来源 |
|---|---|---|
| Ubuntu/Debian | apt install hunspell hunspell-en-us | 官方仓库 |
| Fedora | dnf install hunspell hunspell-en-US | 官方仓库 |
| Arch Linux | pacman -S hunspell hunspell-en_us | 官方仓库 + AUR |
| macOS | brew install hunspell + 手动下载词典 | LibreOffice 项目 |
| Windows | MSYS2/Chocolatey/Scoop + 手动下载词典 | LibreOffice 项目 |
| 源码编译 | autoreconf && ./configure && make | — |