In Linux, the aspell utility is the primary utility for checking the spelling of text files. In this post, we learn how to use aspell to interactively spell check a file and customize the spell checker with a personal dictionary. When running aspell, the first argument (other than possible command-line switches) is interpreted as a command, telling aspell what to do. The following commands are supported by aspell.
|-c file, check file||Perform an interactive spell check on the file file.|
|-l, list||Print a list of misspelled words found in the standard in stream.|
|config||Dump the current aspell configuration to standard out.|
|dump master|personal|repl||Dump a copy of the master word list, personal word list, or personal replacement list, respectively.|
|create master|personal|repl||Create the master word list, personal word list, or personal replacement list, respectively, reading entries from standard in.|
|merge master|personal|repl||Merge entries read from standard in into the master word list, personal word list, or personal replacement list, respectively.|
The following table lists some of the more common command line switches that are used with the aspell command.
|-W –ignore=N||Ignore words less than N characters. (By default, only single letters are ignored.)|
|–ignore-case||Ignore case when performing word comparisons.|
|-p, –personal=filename||Use the word list filename for the personal word list.|
|-x, –dont-backup||Do not create a backup file when performing the spell check.|
Performing an Interactive Spell Check
The user prince has composed the following message, which he plans to email to the user elvis.
$ cat toelvis Hey Elvis! I heard that you were about to take the lab test for the string procesing workbook in Red Hat Academy. IIRC, its prety straightforward, if you’ve been keeping up with the exercises. LOL, Prince
Before sending the message, prince uses aspell -c to perform an interactive spell check.
$ aspell -c toelvis
Upon execution, the aspell command open an interactive session, highlighting the first recognized misspelled word.
Hey Elvis! I heard you were about to take the lab test for the string procesing workbook in Red Hat Academy. IIRC, its prety straightforward, if you’ve been keeping up with the exercises. LOL, Prince ===================================================================== 1) processing 6) preceding 2) precessing 7) professing 3) precising 8) promising 4) proceeding 9) proposing 5) prosing i) Ignore I) Ignore r) Replace R) Replace a) Add x) Exit ===================================================================== ?
At this point, prince has a “live” keyboard, meaning that single key presses will take effect without him needing to use the return key. He may choose from the following options.
Use Suggested Replacement
The aspell command will do its best to suggest replacements for the misspelled word from its library. If it has found a correct suggestion (as in this case, it has), that suggestion can be replaced by simply hitting the numeric key associated with it.
Ignore the Word
By pressing i, aspell will simply ignore the word this instance and move on. Pressing capital I will cause aspell to ignore all instances of the word in the current file.
Replace the Word
If aspell was not able to generate an appropriate suggestion, prince may use r to manually replace the word. When finished, aspell will pick up again, first rechecking the specified replacement. By using capital R, aspell will remember the replacement and automatically replace other instances of the misspelled word.
Add the Word to the Personal Dictionary
If prince would like aspell to learn a new word, so that it will not be flagged when checking future files, he may press a to add the word to his personal dictionary.
By pressing x, prince can immediately exit the interactive aspell section. Any spelling corrections already implemented will be saved.
As prince proceeds through the interactive session, aspell flags procesing, prety, IIRC, and LOL as misspelled. For the first two, prince accepts aspell’s suggestions for the correct spelling. The last two “words” are abbreviations that prince commonly uses in his emails, so he adds them to his personal dictionary. Unfortunately, because its is a legitimate word, aspell does not report prince’s misuse of it.
When finished, prince now has two files, the corrected version of toelvis, and an automatically generated backup of the original, toelvis.bak.
$ ls toelvis toelvis.bak
$ diff toelvis.bak toelvis 4c4 < processing workbook in Red Hat Academy. IIRC, its prety --- > processing workbook in Red Hat Academy. IIRC, its pretty
Performing a Non-interactive Spell Check
Using the -l command-line switch, the aspell command can be used to perform spell checks in a non-interactive batch mode. Used this way, aspell simple reads standard in, and writes to standard out every word it would flag as misspelled.
In the following, suppose prince performed a non-interactive spell check before he had run the aspell session interactively.
$ aspell -l < toelvis procesing IIRC prety LOL
The aspell utility lists the four words it would flag as misspelled. After the interactive spell check, prince performs a non-interactive spell check on his backup of the original file.
$ aspell -l < toelvis.bak procesing prety
Because the words IIRC and LOL were added to prince’s personal dictionary, they are no longer flagged as misspelled.
Managing the Personal Dictionary
By default, the aspell command uses two dictionaries when performing spell checks: the system wide master dictionary, and a user’s personal dictionary. When prince chooses to add a word, the word gets stored in his personal dictionary. He uses aspell’s ability to dump to view his personal dictionary.
$ aspell dump personal LOL IIRC
Likewise, he could dump the system’s master dictionary as well.
$ aspell dump master | wc -l 153675
$ aspell dump master | grep "^add.*ion$" addiction addition adduction
The aspell command can also automatically create a personal dictionary (if it doesn’t already exist), or merge into it (if it does) using words read from standard in. Suppose prince has a previous email message, in which he used many of his commonly used abbreviations. He would like to add all of the abbreviations found in that email to his personal dictionary. He first uses aspell -l to extract the words from the original message.
$ aspell -l < good_email.txt FWIW AFK RSN TTFN
After observing the results, he decides to add all of these words to his personal dictionary, using aspell merge personal. When he finishes, he again dumps his (expanded) personal dictionary.
$ aspell -l < good_email.txt | aspell merge personal
$ aspell dump personal TTFN AFK LOL RSN IIRC FWIW
What happens if prince tries to create the personal dictionary instead?
$ echo "foo" | aspell create personal Sorry I won’t overwrite "/home/prince/.aspell.english.pws"
In aspell’s unwillingness to clobber an already existing personal dictionary, we discover where it is stored: ~/.aspell.language.pwd.
Example - Adding Service Names to aspell’s Personal Dictionary
The user prince is commonly answering questions related to Linux’s networking services in his emails, and aspell consistently flags the conventional service names as misspelled words. He would like to add the service names found in the file /etc/services to his personal dictionary.
He first spell checks the /etc/services file non-interactively, and stores the results in ~/services.maybe.
$ aspell -l < /etc/services > services.maybe
Using the less pager to browse the file services.maybe, he finds many duplicate entries. He makes life easier for himself (and eventually aspell) by regenerating the list, removing duplicates.
$ aspell -l < /etc/services | sort | uniq > services.maybe
Browsing the file again, prince is satisfied that the list contains words he would rather not have flagged as misspelled. He adds the word list to his personal dictionary.
$ aspell merge personal < services.maybe
For confirmation, he again spell checks, non-interactively, the /etc/services file.
$ aspell -l < /etc/services $
As expected, no words were flagged as misspelled.