第三章:Creating Utilities--27.增长一个本地词典

    这是从博客园移过来的最后一篇,确实很麻烦呀。 git

   作完了上面的第2五、26个脚本后,咱们想要本身手动增长一个本地词典,这样就不用在每次遇到一个新的单词后,都会一遍遍的报错了。 shell

代码: this

#!/bin/sh
 
 # spelldict.sh -- 使用'aspell'特性以及一些过滤以便
 # 容许命令行拼写检查给定的输入文件
 
 # 不可避免的,你会发现,有些词汇是错误的,但你认为
 # 它们是正确的。简单的将它们保存在一个文件中,一次
 # 一行,而且肯定变量'okaywords'指向这个文件。
 
 okaywords="$HOME/okaywords"
 tempout="/tmp/spell.tmp.$$"
 spell="aspell"    # 根据须要修改
 
 trap "/bin/rm -f $tempout" EXIT
 
 if [ -z "$1" ]; then
     echo "Usage: spell file | URL" >&2
     exit 1
 elif [ ! -f $okaywords ]; then
     echo "No personal dictionary found. Create one and return this command." >&2
     echo "Your dictionary file: $okaywords" >&2
     exit 1
 fi
 
 for filename
 do
     $spell -a < $filename | \
     grep -v '@(#)' | sed "s/\'//g" | \
         awk '{if(length($0) > 15 && length($2) > 2) print $2}' | \
     grep -vif $okaywords | \
     grep '[[:lower:]]' | grep -v '[[:digit:]]' | sort -u | \
     sed 's/^/ /' > $tempout
 
     if [ -s $tempout ]; then
         sed 's/^/${filename}: /' $tempout
     fi
 done
 
 exit 0

运行脚本:
这个脚本须要在命令行上提供一个或多个文件名 spa

运行结果: 命令行

首先,一个空的我的字典,txt内容摘录自爱丽丝漫游记:
$ spelldict ragged.txt 
ragged.txt:    herrself 
ragged.txt:    teacups 
ragged.txt:    Gryphon 
ragged.txt:    clamour 

有两个词拼错了,因此准备用echo命令把它们加到okaywords文件中:

$ echo "Gryphon" >> ~/.okaywords 
$ echo "teacups" >> ~/.okaywords 

扩展了词典文件后的检查结果以下:

$ spelldict ragged.txt 
ragged.txt:    herrself 
ragged.txt:    clamour
ps: 这3章的单词检查,对我我的而言真的很无趣。期待后续的精彩脚本吧。
相关文章
相关标签/搜索