曲径通幽论坛

标题: aspell -- 交互式拼写检查工具 [打印本页]

作者: beyes    时间: 2012-3-26 14:05
标题: aspell -- 交互式拼写检查工具
在许多的用户文本编辑软件中经常会看到它们已经集成了单词拼写检查功能,如 Microsoft Office Word,以及各种电子邮件客户端等。

在 Linux 里,也有一个实用的,且基于命令行的拼写检查工具 -- aspell ,它亦能提供交互式的拼写检查,更改,保存等功能。

一般情况下,你所用的发行版也许没有默认安装该工具。如果使用的是 Debian 或者 Ubuntu 这样的发行版,那么安装命令为:
#apt-get install aspell
如果使用的是 Fedora 这样的发行版,可以:
# yum install aspell


下面是个测试文件,里面写了几个单词,有对的也有错的:
# cat tmp.txt
hello
linus
world
shell groad www net
unix and linux go spellings
下面使用 aspell 来检查该文件,运行命令如下:
# aspell check tmp.txt
这时会看到一个交互式界面如下:
[attach]273[/attach]
在上图中,程序对人命 linus 的拼写建议通过数字和字母的顺序列出来,比如你按下 1 键,那么相应的文本就会修改成 Linus ,修改所有的错误后,可以按下 x 键保存退出;如果按下 b 键则会中断修改操作。

在默认情况下,aspell 自带的检查辞典可能不够用,我们可以搜索安装更多,比如在 Debian 或 Ubuntu 下:
# apt-cache search aspell
aspell-de-alt - German dictionary for aspell (old spelling)
aspell-lt - aspell dictionary for Lithuanian (LT)
aspell-uk - Ukrainian dictionary for GNU Aspell
aspell-et - Estonian dictionary for Aspell
ilohamail - Light weight yet full featured multilingual web-based IMAP/POP3 client
aspell-gl-minimos - Aspell dictionary for Galician (minimos)
aspell-de - German dictionary for aspell
aspell-ca - Catalan dictionary for aspell
aspell-fo - The Faroese dictionary for aspell
aspell-bg - Bulgarian dictionary for aspell
... ...

下面是个测试脚本:
[Bash shell] 纯文本查看 复制代码
#!/bin/bash

word=$1

output=`echo \"$word\" | aspell list`

if [ -z $output ]; then
    echo $word is a dictionary word;
else
    echo $word is not a dictionary word;
fi

运行输出:
# ./aspell.sh groad
groad is not a dictionary word
# ./aspell.sh hello
hello is a dictionary word
在上面的脚本中,使用了 aspell 命令的 list 选项,该选项表示如果所检查到的单词如果在词典中,那么它不会输出任何内容,此时变量 output 为空,所以在 if 里测试时也正是使用了 -z 选项来判断 output 是否为空,若为空那么表示该单词在辞典中,否则不在词典中,此时只会简单的输出拼写错误的单词。




欢迎光临 曲径通幽论坛 (http://www.groad.net/bbs/) Powered by Discuz! X3.2