第三章: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章的單詞檢查,對我我的而言真的很無趣。期待後續的精彩腳本吧。
相關文章
相關標籤/搜索