2.14拼写检查与词典操作

linux大多数发行版都含有一个词典文件,同时还有一个aspell的工具,其作用是进行拼写检查

1、目录/usr/share/dict/中包含了一些词典文件,“词典文件“就是包含了词典单词列表的文本文件,可以利用这个列表来检查某个单词是否为词典中的单词。

为了检查给定的单词是否属于词典中的单词,用下面的脚本:

#!/bin/bash

#文件名:checkword.sh

word=$1

grep "^$1$" /usr/share/dict/british-english -q

if [ $? -eq 0 ]; then

  echo $word is a dictionary word;

else

  echo $word is not a dictionary word;

fi

这个脚本的语法如下:

./checkword.sh ful

ful is not a dictionary word

./checkword.sh feel

ful is  a dictionary word

工作原理

在grep中,^标记着单词的开始,$标记着单词的结束。

-q禁止产生任何输出

我们也可以利用拼写检查命令aspell来核查某个单词时候再词典中

#!/bin/bash

#文件名:aspellcheck.sh

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 list命令产生输出文本,反之则不产生任何输出,-z用于确认$output是否为空。

列出文件中以特定单词起头的所有单词:

$look word filepath

或者

$grep "^word" filepath

在默认情况下,如果没有给出文件参数,look命令会使用默认词典(/usr/share/dict/words)并返回输出。

$look word

#像这样使用时,look命令以默认词典作为文件参数

原文地址:https://www.cnblogs.com/gary-guo/p/6194318.html