Simple single letter spell checker in bash

Here’s a small bash script. It checks if the given word is in dictionary and if not, assuming that it contains only a single character error, gives suggestions.

#!/bin/bash
word=$1
grep -i "^$word$" /usr/share/dict/words > /dev/null
if [ $? -eq 0 ]; then
   echo "Word in dictionary";
else
    echo "Word not in dictionary. Suggestions:"
    l=${#word}
    for i in $(seq 1 $l);
    do
        regw=`echo $word | sed s/././$i`
        grep -i "^$regw$" /usr/share/dict/words
    done
fi

This was actually a question i had  in Tux of War, a shell scripting competition organized as a part of tathva at NIT Calicut. I got the sed expression from stackoveflow. It replaces just one letter of the word with ‘.’ in order in aloop, so that it would be a valid regex in grep to find the word suggestions.

I’m happy that my attempt works and I’m also sure there must be an efficient and easy method to do this! 😀

4 thoughts on “Simple single letter spell checker in bash

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s